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