app.js
4.25 KB
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
/**
* acuefuel - Responsive Admin Theme
*
*/
(function () {
angular.module('acuefuel', [
'ngCookies', // angular-cookies
'ngResource', //angular resouce
'ngAnimate', // angular animate
'ui.router', // Routing
'ui.bootstrap', // Bootstrap
'ngFileUpload', // File Upload
'angular-loading-bar' //angular loading bar
])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
$httpProvider.interceptors.push('myCSRF');
$httpProvider.interceptors.push('httpRequestInterceptor');
}])
.factory('httpRequestInterceptor', ['$q', '$rootScope', '$location', function($q, $rootScope, $location) {
return {
request: function($config) {
return $config;
},
responseError: function(rejection) {
if (rejection.status === 401) {
if($location.path() != "/login"){
localStorage.clear();
window.location.reload();
}
}
return $q.reject(rejection);
}
}
}])
// .run(function($rootScope, $window, $state, $location, LoginService) {
// $rootScope.$on('$stateChangeStart', function(event, next, nextParams, fromState) {
// var loginStatus = localStorage.getItem("loginStatus");
// if (loginStatus == null) {
// if (next.url == "/login" || next.url == "/signup" || next.url == "/forgot-password") {
// // if route already going to #login, no redirect needed
// } else {
// // another route, we should redirect now
// $window.location.href = '#/login';
// event.preventDefault();
// }
// } else {
// if (next.name == 'login') {
// event.preventDefault();
// if ($state.current.name.length == 0) {
// $state.go('app.upload');
// } else {
// $state.go($state.current, {}, {reload: true});
// }
// }
// }
// });
// })
.provider('myCSRF',[function(){
var headerName = 'X-CSRFToken';
var cookieName = 'csrftoken';
var allowedMethods = ['GET'];
this.setHeaderName = function(n) {
headerName = n;
}
this.setCookieName = function(n) {
cookieName = n;
}
this.setAllowedMethods = function(n) {
allowedMethods = n;
}
this.$get = ['$cookies', function($cookies){
return {
'request': function(config) {
if(allowedMethods.indexOf(config.method) === -1) {
// do something on success
config.headers[headerName] = $cookies[cookieName];
}
return config;
}
}
}];
}])
.directive('icheck', ['$timeout', '$parse', function($timeout, $parse) {
return {
restrict: 'A',
require: '?ngModel',
link: function(scope, element, attr, ngModel) {
$timeout(function() {
var value = attr.value;
function update(checked) {
if(attr.type==='radio') {
ngModel.$setViewValue(value);
} else {
ngModel.$setViewValue(checked);
}
}
$(element).iCheck({
checkboxClass: attr.checkboxClass || 'icheckbox_square-green',
radioClass: attr.radioClass || 'iradio_square-green'
}).on('ifChanged', function(e) {
scope.$apply(function() {
update(e.target.checked);
});
});
scope.$watch(attr.ngChecked, function(checked) {
if(typeof checked === 'undefined') checked = !!ngModel.$viewValue;
update(checked)
}, true);
scope.$watch(attr.ngModel, function(model) {
$(element).iCheck('update');
}, true);
})
}
}
}])
})();