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