eb4f5f921fe657c40cafbfb4960c63726629e7a6
[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', '$routeParams', '$scope', '$sce', function(Model, Metrics, $routeParams, $scope, $sce) {
22 $scope.entityId = $routeParams.id;
23
24 this.loadEntityLinearization = function() {
25 Model.loadEntityLinearization($routeParams.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($routeParams.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($routeParams.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($routeParams.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($routeParams.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($routeParams.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', function() {
162 return {
163 restrict: 'E',
164 scope: {
165 mentity: '=',
166 defaultTab: '@'
167 },
168 replace: true,
169 templateUrl: '/directives/entity/card.html',
170 link: function ($scope, element, attrs) {
171 $scope.currentTab = $scope.defaultTab ? $scope.defaultTab : 'signature';
172 }
173 };
174 })
175
176 .directive('entityList', function() {
177 return {
178 restrict: 'E',
179 scope: {
180 listEntities: '=',
181 listId: '@',
182 listTitle: '@',
183 listObjectFilter: '=',
184 },
185 templateUrl: '/directives/entity/list.html',
186 link: function ($scope, element, attrs) {
187 $scope.showFilters = false;
188 if(!$scope.listObjectFilter) {
189 $scope.listObjectFilter = {};
190 }
191 if(!$scope.visibilityFilter) {
192 $scope.visibilityFilter = {
193 public: true,
194 protected: true,
195 private: false
196 };
197 }
198 $scope.toggleFilters = function() {
199 $scope.showFilters = !$scope.showFilters;
200 };
201 }
202 };
203 })
204
205 .directive('entityLinearization', function() {
206 return {
207 restrict: 'E',
208 scope: {
209 listEntities: '=',
210 listTitle: '@',
211 listFocus: '='
212 },
213 templateUrl: '/directives/entity/linearization.html'
214 };
215 })
216
217 .directive('entityDef', ['Model', function(Model, Code) {
218 return {
219 restrict: 'E',
220 scope: {
221 definition: '=',
222 focus: '='
223 },
224 templateUrl: '/directives/entity/defcard.html',
225 link: function ($scope, element, attrs) {
226 $scope.codeId = 'code_' + $scope.definition.full_name.replace(/[^a-zA-Z0-9]/g, '_');
227
228 $scope.isActive = function() {
229 return $scope.focus.full_name == $scope.definition.full_name;
230 }
231
232 $scope.loadCardCode = function() {
233 if(!$scope.code) {
234 Model.loadEntityCode($scope.definition.full_name,
235 function(data) {
236 $scope.code = data;
237 setTimeout(function() { // smooth collapse
238 $('#' + $scope.codeId).collapse('show')
239 }, 1);
240 }, function(err) {
241 $scope.code = err;
242 });
243 } else {
244 if($('#' + $scope.codeId).hasClass('in')) {
245 $('#' + $scope.codeId).collapse('hide');
246 } else {
247 $('#' + $scope.codeId).collapse('show');
248 }
249 }
250 };
251 }
252 };
253 }])
254
255 .directive('entityRating', ['Feedback', function(Feedback, Code) {
256 return {
257 restrict: 'E',
258 scope: {
259 mentity: '='
260 },
261 templateUrl: '/directives/entity/stars.html',
262 link: function ($scope, element, attrs) {
263 $scope.postStar = function(rating) {
264 Feedback.postEntityStar($scope.mentity.full_name, rating,
265 function(data) {
266 $scope.ratings = data;
267 }, function(err) {
268 $scope.err = err;
269 });
270 }
271
272 Feedback.loadEntityStars($scope.mentity.full_name,
273 function(data) {
274 $scope.ratings = data;
275 }, function(err) {
276 $scope.err = err;
277 });
278 }
279 };
280 }])
281 })();