-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
143 lines (115 loc) · 4 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
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
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'toaster', 'ngSanitize', 'mgcrea.ngStrap']);
app.config(['$locationProvider', '$routeProvider', '$httpProvider', function ($locationProvider, $routeProvider, $httpProvider) {
var modulesPath = 'modules';
$routeProvider
.when('/', {
templateUrl: modulesPath + '/site/views/main.html'
})
.when('/login', {
templateUrl: modulesPath + '/site/views/login.html',
controller: 'SiteLogin'
})
.when('/post/published', {
templateUrl: modulesPath + '/post/views/index.html',
controller: 'PostIndex',
resolve: {
status: function () {
return 2;
}
}
})
.when('/post/draft', {
templateUrl: modulesPath + '/post/views/index.html',
controller: 'PostIndex',
resolve: {
status: function () {
return 1;
}
}
})
.when('/post/create', {
templateUrl: modulesPath + '/post/views/form.html',
controller: 'PostCreate'
})
.when('/post/:id/edit', {
templateUrl: modulesPath + '/post/views/form.html',
controller: 'PostEdit'
})
.when('/post/:id/delete', {
templateUrl: modulesPath + '/post/views/delete.html',
controller: 'PostDelete'
})
.when('/post/:id', {
templateUrl: modulesPath + '/post/views/view.html',
controller: 'PostView'
})
.when('/404', {
templateUrl: '404.html'
})
.otherwise({redirectTo: '/404'})
;
$locationProvider.html5Mode(true).hashPrefix('!');
$httpProvider.interceptors.push('authInterceptor');
}]);
app.factory('authInterceptor', function ($q, $window) {
return {
request: function (config) {
if ($window.sessionStorage._auth && config.url.substring(0, 4) == 'http') {
config.params = {'access-token': $window.sessionStorage._auth};
}
return config;
},
responseError: function (rejection) {
if (rejection.status === 401) {
$window.setTimeout(function () {
$window.location = '/#!/login';
}, 1000);
}
return $q.reject(rejection);
}
};
});
app.value('app-version', '0.0.3');
// Need set url REST Api in controller!
app.service('rest', function ($http, $location, $routeParams) {
return {
baseUrl: 'https://yii2-rest-githubjeka.c9.io/rest/web/',
path: undefined,
models: function () {
return $http.get(this.baseUrl + this.path + location.search);
},
model: function () {
if ($routeParams.expand != null) {
return $http.get(this.baseUrl + this.path + "/" + $routeParams.id + '?expand=' + $routeParams.expand);
}
return $http.get(this.baseUrl + this.path + "/" + $routeParams.id);
},
get: function () {
return $http.get(this.baseUrl + this.path);
},
postModel: function (model) {
return $http.post(this.baseUrl + this.path, model);
},
putModel: function (model) {
return $http.put(this.baseUrl + this.path + "/" + $routeParams.id, model);
},
deleteModel: function () {
return $http.delete(this.baseUrl + this.path);
}
};
});
app
.directive('login', ['$http', function ($http) {
return {
transclude: true,
link: function (scope, element, attrs) {
scope.isGuest = window.sessionStorage._auth == undefined;
},
template: '<a href="login" ng-if="isGuest">Login</a>'
}
}])
.filter('checkmark', function () {
return function (input) {
return input ? '\u2713' : '\u2718';
};
});