/
application.js
153 lines (135 loc) · 6.28 KB
/
application.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";
angular.module('smlBootstrap', ['smlBootstrap.services', 'smlBootstrap.filters', 'smlBootstrap.controllers', 'smlBootstrap.directives', 'ngSanitize', 'ajaxthrobber'])
.config(function ($routeProvider) {
$routeProvider.
when('/', {controller: 'EntriesController', templateUrl: 'partials/main.html'}).
when("/entry/:entryId", {controller: 'EntryEditController', templateUrl: "partials/entry.html"}).
when("/login", {controller: 'LoginController', templateUrl: "partials/login.html"}).
when("/register", {controller: 'RegisterController', templateUrl: "partials/register.html"}).
when("/error404", {controller: 'EntriesController', templateUrl: "partials/errorpages/error404.html"}).
when("/error500", {controller: 'EntriesController', templateUrl: "partials/errorpages/error500.html"}).
when("/error", {controller: 'EntriesController', templateUrl: "partials/errorpages/error500.html"}).
when("/recover-lost-password", {controller: 'PasswordRecoveryController', templateUrl: "partials/recover-lost-password.html"}).
when("/password-reset", {controller: "PasswordRecoveryController", templateUrl: "partials/password-reset.html"}).
when("/profile", {controller: "ProfileController", templateUrl: "partials/secured/profile.html"}).
otherwise({redirectTo: '/error404'});
})
.config(['$httpProvider', function ($httpProvider) {
var interceptor = ['$q', '$location', 'FlashService', '$injector', function ($q, $location, FlashService, $injector) {
function success(response) {
return response;
}
function error(response) {
if (response.status === 401) { // user is not logged in
var UserSessionService = $injector.get('UserSessionService'); // uses $injector to avoid circular dependency
if (UserSessionService.isLogged()) {
UserSessionService.logout(); // Http session expired / logged out - logout on Angular layer
FlashService.set('Your session timed out. Please login again.');
$location.path("/login");
}
} else if (response.status === 403) {
console.log(response.data);
// do nothing, user is trying to modify data without privileges
} else if (response.status === 404) {
$location.path("/error404");
} else if (response.status === 500) {
$location.path("/error500");
} else {
$location.path("/error");
}
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
};
}];
$httpProvider.responseInterceptors.push(interceptor);
}])
.run(function ($rootScope, UserSessionService) {
UserSessionService.validate();
})
.run(function ($rootScope, $timeout, FlashService) {
$rootScope.$on("$routeChangeSuccess", function () {
var message = FlashService.get();
if (angular.isDefined(message)) {
showInfoMessage(message);
}
});
})
.run(function ($rootScope, UserSessionService, $location) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
if (!UserSessionService.isLogged() && next.templateUrl.indexOf("/secured/") > -1) {
$location.path("/login");
} else if (UserSessionService.isLogged() && next.templateUrl === "partials/login.html") {
$location.path("/");
}
});
});
angular.module('smlBootstrap.filters', []).
filter('newlines', function () {
return function (text) {
return text.replace(/\n/g, '<br/>');
};
});
angular.module("ajaxthrobber", [])
.config(["$httpProvider", function ($httpProvider) {
var stopAjaxInterceptor;
stopAjaxInterceptor = ["$rootScope", "$q", function ($rootScope, $q) {
var error, stopAjax, success, wasPageBlocked;
stopAjax = function (responseConfig) {
if (wasPageBlocked(responseConfig)) {
return $rootScope.inProgressAjaxCount--;
}
};
wasPageBlocked = function (responseConfig) {
var nonBlocking;
if (typeof responseConfig === "object" && typeof responseConfig.headers === "object") {
nonBlocking = responseConfig.headers.dontBlockPageOnAjax;
}
if (nonBlocking) {
return !nonBlocking;
} else {
return true;
}
};
success = function (response) {
stopAjax(response.config);
return response;
};
error = function (response) {
stopAjax(response.config);
return $q.reject(response);
};
return function (promise) {
return promise.then(success, error);
};
}];
return $httpProvider.responseInterceptors.push(stopAjaxInterceptor);
}])
.run(["$rootScope", "$http", function ($rootScope, $http) {
var startAjaxTransformer;
startAjaxTransformer = function (data, headersGetter) {
if (!headersGetter().dontBlockPageOnAjax) {
$rootScope.inProgressAjaxCount++;
}
return data;
};
$rootScope.inProgressAjaxCount = 0;
return $http.defaults.transformRequest.push(startAjaxTransformer);
}])
.directive("ajaxthrobber", function () {
return {
restrict: "E",
link: function (scope, element, attrs) {
return scope.$watch("inProgressAjaxCount", function (count) {
if (count === 1) {
return $.blockUI({
message: element
});
} else if (count === 0) {
return $.unblockUI();
}
});
}
};
});