-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathapp.js
95 lines (79 loc) · 2.87 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Copyright (c) 2013, Bernhard Posselt <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING file.
*/
/* jshint unused: false */
var app = angular.module('Notes', ['restangular', 'ngRoute']).
config(function($provide, $routeProvider, RestangularProvider, $httpProvider,
$windowProvider) {
'use strict';
// Always send the CSRF token by default
$httpProvider.defaults.headers.common.requesttoken = requestToken;
// you have to use $provide inside the config method to provide a globally
// shared and injectable object
$provide.value('Constants', {
saveInterval: 5*1000 // miliseconds
});
// define your routes that that load templates into the ng-view
$routeProvider.when('/notes/:noteId', {
templateUrl: 'note.html',
controller: 'NoteController',
resolve: {
// $routeParams does not work inside resolve so use $route
// note is the name of the argument that will be injected into the
// controller
/* @ngInject */
note: function ($route, $q, is, Restangular) {
var deferred = $q.defer();
var noteId = $route.current.params.noteId;
is.loading = true;
Restangular.one('notes', noteId).get().then(function (note) {
is.loading = false;
deferred.resolve(note);
}, function (response) {
is.loading = false;
// Token expired
if (response.status == 412) {
OC.reload();
}
else {
deferred.reject();
}
});
return deferred.promise;
}
}
}).otherwise({
redirectTo: '/'
});
var baseUrl = OC.generateUrl('/apps/notes');
RestangularProvider.setBaseUrl(baseUrl);
}).run(function ($rootScope, $location, NotesModel) {
'use strict';
$('link[rel="shortcut icon"]').attr(
'href',
OC.filePath('notes', 'img', 'favicon.png')
);
// handle route errors
$rootScope.$on('$routeChangeError', function () {
var notes = NotesModel.getAll();
// route change error should redirect to the latest note if possible
if (notes.length > 0) {
var sorted = notes.sort(function (a, b) {
if(a.modified > b.modified) {
return 1;
} else if(a.modified < b.modified) {
return -1;
} else {
return 0;
}
});
var note = notes[sorted.length-1];
$location.path('/notes/' + note.id);
} else {
$location.path('/');
}
});
});