Merge: modelize_class: Remove an unnecessary pre-condition
[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 mentity: '='
112 },
113 templateUrl: '/directives/entity/namespace.html'
114 };
115 })
116
117 .directive('entityTag', function() {
118 return {
119 restrict: 'E',
120 scope: {
121 mentity: '='
122 },
123 replace: true,
124 templateUrl: '/directives/entity/tag.html'
125 };
126 })
127
128 .directive('entityLocation', function() {
129 return {
130 restrict: 'E',
131 scope: {
132 mentity: '='
133 },
134 templateUrl: '/directives/entity/location.html'
135 };
136 })
137
138 .directive('entityCard', function() {
139 return {
140 restrict: 'E',
141 scope: {
142 mentity: '='
143 },
144 replace: true,
145 templateUrl: '/directives/entity/card.html'
146 };
147 })
148
149 .directive('entityList', function() {
150 return {
151 restrict: 'E',
152 scope: {
153 listEntities: '=',
154 listId: '@',
155 listTitle: '@',
156 listObjectFilter: '=',
157 },
158 templateUrl: '/directives/entity/list.html',
159 link: function ($scope, element, attrs) {
160 $scope.showFilters = false;
161 if(!$scope.listObjectFilter) {
162 $scope.listObjectFilter = {};
163 }
164 if(!$scope.visibilityFilter) {
165 $scope.visibilityFilter = {
166 public: true,
167 protected: true,
168 private: false
169 };
170 }
171 $scope.toggleFilters = function() {
172 $scope.showFilters = !$scope.showFilters;
173 };
174 }
175 };
176 })
177
178 .directive('entityLinearization', function() {
179 return {
180 restrict: 'E',
181 scope: {
182 listEntities: '=',
183 listTitle: '@',
184 listFocus: '='
185 },
186 templateUrl: '/directives/entity/linearization.html'
187 };
188 })
189
190 .directive('entityDef', ['Model', function(Model, Code) {
191 return {
192 restrict: 'E',
193 scope: {
194 definition: '=',
195 focus: '='
196 },
197 templateUrl: '/directives/entity/defcard.html',
198 link: function ($scope, element, attrs) {
199 $scope.codeId = 'code_' + $scope.definition.full_name.replace(/[^a-zA-Z0-9]/g, '_');
200 $scope.loadCardCode = function() {
201 if(!$scope.code) {
202 Model.loadEntityCode($scope.definition.full_name,
203 function(data) {
204 $scope.code = data;
205 setTimeout(function() { // smooth collapse
206 $('#' + $scope.codeId).collapse('show')
207 }, 1);
208 }, function(err) {
209 $scope.code = err;
210 });
211 } else {
212 if($('#' + $scope.codeId).hasClass('in')) {
213 $('#' + $scope.codeId).collapse('hide');
214 } else {
215 $('#' + $scope.codeId).collapse('show');
216 }
217 }
218 };
219 }
220 };
221 }])
222
223 .directive('entityRating', ['Feedback', function(Feedback, Code) {
224 return {
225 restrict: 'E',
226 scope: {
227 mentity: '='
228 },
229 templateUrl: '/directives/entity/stars.html',
230 link: function ($scope, element, attrs) {
231 $scope.postStar = function(rating) {
232 Feedback.postEntityStar($scope.mentity.full_name, rating,
233 function(data) {
234 $scope.ratings = data;
235 }, function(err) {
236 $scope.err = err;
237 });
238 }
239
240 Feedback.loadEntityStars($scope.mentity.full_name,
241 function(data) {
242 $scope.ratings = data;
243 }, function(err) {
244 $scope.err = err;
245 });
246 }
247 };
248 }])
249 })();