nitweb: move entity state configuration to entity module
[nit.git] / share / nitweb / javascripts / entities.js
1 /*
2 * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 (function() {
18 angular
19 .module('entities', ['ngSanitize', 'ui'])
20
21 .config(function($stateProvider, $locationProvider) {
22 $stateProvider
23 .state('doc', {
24 url: '/doc/:id',
25 templateUrl: 'views/doc.html',
26 controller: 'EntityCtrl',
27 controllerAs: 'entityCtrl',
28 })
29 })
30
31 .factory('Model', [ '$http', function($http) {
32 return {
33
34 loadEntity: function(id, cb, cbErr) {
35 $http.get('/api/entity/' + id)
36 .success(cb)
37 .error(cbErr);
38 },
39
40 loadEntityLinearization: function(id, cb, cbErr) {
41 $http.get('/api/linearization/' + id)
42 .success(cb)
43 .error(cbErr);
44 },
45
46 loadEntityDefs: function(id, cb, cbErr) {
47 $http.get('/api/defs/' + id)
48 .success(cb)
49 .error(cbErr);
50 },
51
52 loadEntityCode: function(id, cb, cbErr) {
53 $http.get('/api/code/' + id)
54 .success(cb)
55 .error(cbErr);
56 },
57
58 loadEntityGraph: function(id, cb, cbErr) {
59 $http.get('/api/graph/inheritance/' + id + '?cdepth=3')
60 .success(cb)
61 .error(cbErr);
62 },
63
64 search: function(q, n, cb, cbErr) {
65 $http.get('/api/search?q=' + q + '&n=' + n)
66 .success(cb)
67 .error(cbErr);
68 }
69 };
70 }])
71
72 .controller('EntityCtrl', ['Model', 'Metrics', 'Feedback', '$stateParams', '$scope', '$sce', function(Model, Metrics, Feedback, $stateParams, $scope, $sce) {
73 $scope.entityId = $stateParams.id;
74
75 this.loadEntityLinearization = function() {
76 Model.loadEntityLinearization($stateParams.id,
77 function(data) {
78 $scope.linearization = data;
79 }, function(err) {
80 $scope.error = err;
81 });
82 };
83
84 this.loadEntityDefs = function() {
85 Model.loadEntityDefs($stateParams.id,
86 function(data) {
87 $scope.defs = data;
88 }, function(err) {
89 $scope.error = err;
90 });
91 };
92
93 this.loadEntityCode = function() {
94 Model.loadEntityCode($stateParams.id,
95 function(data) {
96 $scope.code = data;
97 }, function(err) {
98 $scope.code = err;
99 });
100 };
101
102 this.loadEntityGraph = function(e) {
103 Model.loadEntityGraph($stateParams.id,
104 function(data) {
105 $scope.graph = $sce.trustAsHtml(data);
106 }, function(err) {
107 $scope.error = err;
108 });
109 };
110
111 this.loadStructuralMetrics = function() {
112 Metrics.loadStructuralMetrics($stateParams.id,
113 function(data) {
114 $scope.metrics = data;
115 }, function(message, status) {
116 $scope.error = {message: message, status: status};
117 });
118 };
119
120 Model.loadEntity($stateParams.id,
121 function(data) {
122 $scope.mentity = data;
123 }, function(message, status) {
124 $scope.error = {message: message, status: status};
125 });
126 }])
127
128 .directive('entityLink', function() {
129 return {
130 restrict: 'E',
131 scope: {
132 mentity: '='
133 },
134 templateUrl: '/directives/entity/link.html'
135 };
136 })
137
138 .directive('entityDoc', function() {
139 return {
140 restrict: 'E',
141 scope: {
142 mentity: '='
143 },
144 templateUrl: '/directives/entity/doc.html'
145 };
146 })
147
148 .directive('entitySignature', function() {
149 return {
150 restrict: 'E',
151 scope: {
152 mentity: '='
153 },
154 templateUrl: '/directives/entity/signature.html'
155 };
156 })
157
158 .directive('entityNamespace', function() {
159 return {
160 restrict: 'E',
161 scope: {
162 namespace: '='
163 },
164 templateUrl: '/directives/entity/namespace.html',
165 link: function ($scope, element, attrs) {
166 $scope.isObject = function(obj) {
167 return typeof obj === 'object';
168 };
169 $scope.isArray = function(obj) {
170 return Array.isArray(obj);
171 };
172 $scope.isString = function(obj) {
173 return typeof obj === 'string';
174 };
175 }
176 };
177 })
178
179 .directive('entityTag', function() {
180 return {
181 restrict: 'E',
182 scope: {
183 mentity: '='
184 },
185 replace: true,
186 templateUrl: '/directives/entity/tag.html'
187 };
188 })
189
190 .directive('entityLocation', function() {
191 return {
192 restrict: 'E',
193 scope: {
194 mentity: '='
195 },
196 templateUrl: '/directives/entity/location.html'
197 };
198 })
199
200 .directive('entityGraph', function() {
201 return {
202 restrict: 'E',
203 scope: {
204 mentity: '=',
205 graph: '='
206 },
207 replace: true,
208 templateUrl: '/directives/entity/graph.html'
209 };
210 })
211
212 .directive('entityCard', ['Feedback', function(Feedback) {
213 return {
214 restrict: 'E',
215 scope: {
216 mentity: '=',
217 defaultTab: '@',
218 noSynopsis: '='
219 },
220 replace: true,
221 templateUrl: '/directives/entity/card.html',
222 link: function ($scope, element, attrs) {
223 $scope.currentTab = $scope.defaultTab ? $scope.defaultTab : 'signature';
224
225 $scope.loadEntityStars = function() {
226 Feedback.loadEntityStars($scope.mentity.full_name,
227 function(data) {
228 $scope.ratings = data;
229 }, function(message, status) {
230 $scope.error = {message: message, status: status};
231 });
232 };
233 }
234 };
235 }])
236
237 .directive('entityList', function() {
238 return {
239 restrict: 'E',
240 scope: {
241 listEntities: '=',
242 listId: '@',
243 listTitle: '@',
244 listObjectFilter: '=',
245 },
246 templateUrl: '/directives/entity/list.html',
247 link: function ($scope, element, attrs) {
248 $scope.showFilters = false;
249 if(!$scope.listObjectFilter) {
250 $scope.listObjectFilter = {};
251 }
252 if(!$scope.visibilityFilter) {
253 $scope.visibilityFilter = {
254 public: true,
255 protected: true,
256 private: false
257 };
258 }
259 $scope.toggleFilters = function() {
260 $scope.showFilters = !$scope.showFilters;
261 };
262 }
263 };
264 })
265
266 .directive('entityLinearization', function() {
267 return {
268 restrict: 'E',
269 scope: {
270 listEntities: '=',
271 listTitle: '@',
272 listFocus: '='
273 },
274 templateUrl: '/directives/entity/linearization.html'
275 };
276 })
277
278 .directive('entityDef', ['Model', function(Model, Code) {
279 return {
280 restrict: 'E',
281 scope: {
282 definition: '=',
283 focus: '='
284 },
285 templateUrl: '/directives/entity/defcard.html',
286 link: function ($scope, element, attrs) {
287 $scope.codeId = 'code_' + $scope.definition.full_name.replace(/[^a-zA-Z0-9]/g, '_');
288
289 $scope.isActive = function() {
290 return $scope.focus.full_name == $scope.definition.full_name;
291 }
292
293 $scope.loadCardCode = function() {
294 if(!$scope.code) {
295 Model.loadEntityCode($scope.definition.full_name,
296 function(data) {
297 $scope.code = data;
298 setTimeout(function() { // smooth collapse
299 $('#' + $scope.codeId).collapse('show')
300 }, 1);
301 }, function(err) {
302 $scope.code = err;
303 });
304 } else {
305 if($('#' + $scope.codeId).hasClass('in')) {
306 $('#' + $scope.codeId).collapse('hide');
307 } else {
308 $('#' + $scope.codeId).collapse('show');
309 }
310 }
311 };
312
313 if($scope.isActive()) $scope.loadCardCode();
314 }
315 };
316 }])
317
318 .controller('StarsCtrl', ['Feedback', '$scope', function(Feedback, $scope) {
319 $ctrl = this;
320
321 this.postStar = function(rating) {
322 Feedback.postEntityStarDimension($scope.mentity.full_name,
323 $scope.dimension, rating,
324 function(data) {
325 $scope.mean = data.mean;
326 $scope.list = data.ratings;
327 $scope.user = data.user;
328 $ctrl.loadEntityStars($scope);
329 }, function(err) {
330 $scope.err = err;
331 });
332 }
333
334 this.loadEntityStars = function($scope) {
335 Feedback.loadEntityStars($scope.mentity.full_name,
336 function(data) {
337 $scope.ratings = data;
338 }, function(message, status) {
339 $scope.error = {message: message, status: status};
340 });
341 };
342 }])
343
344 .directive('entityRating', ['Feedback', function(Feedback) {
345 return {
346 restrict: 'E',
347 scope: {
348 mentity: '=',
349 ratings: '='
350 },
351 controller: 'StarsCtrl',
352 controllerAs: 'ratingsCtrl',
353 templateUrl: '/directives/entity/rating.html'
354 };
355 }])
356
357 .directive('entityStars', ['Feedback', function(Feedback) {
358 return {
359 restrict: 'E',
360 scope: {
361 mentity: '=',
362 dimension: '@',
363 mean: '=',
364 list: '=',
365 user: '=',
366 refresh: '=',
367 ratings: '='
368 },
369 controller: 'StarsCtrl',
370 controllerAs: 'starsCtrl',
371 templateUrl: '/directives/entity/stars.html'
372 };
373 }])
374 })();