e9f91d27485273af6f935c500d44de39663b6151
[nit.git] / share / nitweb / javascripts / ui.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('ui', [ 'model' ])
20
21 .controller('SearchCtrl', ['Model', '$routeParams', '$scope', '$location', function(Model, $routeParams, $scope, $location) {
22 $scope.query = '';
23
24 $scope.reset = function() {
25 $scope.activeItem = 0;
26 $scope.results = [];
27 }
28
29 $scope.update = function(e) {
30 if(e.keyCode == 38) {
31 $scope.selectUp();
32 } else if(e.keyCode == 40) {
33 $scope.selectDown();
34 } else if(e.keyCode == 27) {
35 $scope.selectEscape();
36 } else if(e.keyCode == 13) {
37 $scope.selectEnter();
38 }
39 }
40
41 $scope.selectUp = function() {
42 if($scope.activeItem > 0) {
43 $scope.activeItem -= 1;
44 }
45 }
46
47 $scope.selectDown = function() {
48 if($scope.activeItem < $scope.results.length - 1) {
49 $scope.activeItem += 1;
50 }
51 }
52
53 $scope.selectEnter = function(e) {
54 $location.url($scope.results[$scope.activeItem].web_url);
55 $scope.reset();
56 }
57
58 $scope.selectEscape = function() {
59 $scope.reset();
60 }
61
62 $scope.setActive = function(index) {
63 $scope.activeItem = index;
64 }
65
66 $scope.search = function() {
67 if(!$scope.query) {
68 $scope.reset();
69 return;
70 }
71 Model.search($scope.query, 10,
72 function(data) {
73 $scope.reset();
74 $scope.results = data;
75 }, function(err) {
76 $scope.reset();
77 $scope.error = err;
78 });
79 }
80
81 $scope.reset();
82 }])
83
84 .directive('searchCard', function() {
85 return {
86 restrict: 'E',
87 scope: {
88 mentity: '='
89 },
90 replace: true,
91 templateUrl: '/directives/search/card.html'
92 };
93 })
94
95 .directive('uiFilters', function() {
96 return {
97 restrict: 'E',
98 scope: {
99 property: '=',
100 classesOn: '=',
101 classesOff: '='
102 },
103 replace: true,
104 templateUrl: '/directives/ui-filter-button-vis.html',
105 link: function ($scope, element, attrs) {
106 $scope.toggle = function() {
107 $scope.property = !$scope.property;
108 }
109 }
110 };
111 })
112
113 .filter('visibility', function() {
114 return function(input, visibilityFilter) {
115 var res = [];
116 input.forEach(function(entry) {
117 if(visibilityFilter.public == false && entry.visibility == "public") {
118 return;
119 }
120 if(visibilityFilter.protected == false && entry.visibility == "protected") {
121 return;
122 }
123 if(visibilityFilter.private == false && entry.visibility == "private") {
124 return;
125 }
126 res.push(entry);
127 });
128 return res;
129 };
130 })
131
132 .directive('uiSummary', function($rootScope, $location, $anchorScroll) {
133 return {
134 restrict: 'E',
135 scope: {
136 target: '@'
137 },
138 replace: true,
139 templateUrl: '/directives/ui-summary.html',
140 link: function ($scope, element, attrs) {
141 $scope.goTo = function(entity) {
142 $location.hash(entity.id);
143 }
144
145 $scope.textToId = function(text) {
146 return text.replace(/ /g, '-').replace(/[^A-Za-z_-]/g, '');
147 }
148
149 $rootScope.reloadSummary = function() {
150 var h = angular.element(document.querySelectorAll(
151 $scope.target + ' h1, ' +
152 $scope.target + ' h2, ' +
153 $scope.target + ' h3, ' +
154 $scope.target + ' h4, ' +
155 $scope.target + ' h5, ' +
156 $scope.target + ' h6 '));
157
158 $scope.headings = [];
159 angular.forEach(h, function(heading) {
160 var head = angular.element(heading);
161 if(!head.is(':visible')) { return ; }
162 var text = head.text().trim();
163 var id = $scope.textToId(text);
164 if(!head.attr('id')) {
165 head.attr('id', id);
166 } else {
167 id = head.attr('id');
168 }
169 $scope.headings.push({
170 id: id,
171 text: text,
172 level: parseInt(head[0].nodeName[1])
173 });
174 });
175 $anchorScroll();
176 }
177
178 $scope.$watch('target', function() {
179 setTimeout(function() {
180 $rootScope.reloadSummary();
181 }, 100);
182 });
183 }
184 };
185 })
186
187 .directive('uiFilterForm', function() {
188 return {
189 restrict: 'E',
190 scope: {
191 searchFilter: '=',
192 visibilityFilter: '='
193 },
194 replace: true,
195 templateUrl: '/directives/ui-filter-form.html'
196 };
197 })
198
199 .directive('uiFilterField', function() {
200 return {
201 restrict: 'E',
202 scope: {
203 property: '='
204 },
205 replace: true,
206 templateUrl: '/directives/ui-filter-field.html'
207 };
208 })
209
210 .directive('uiFilterGroupVis', function() {
211 return {
212 restrict: 'E',
213 scope: {
214 property: '='
215 },
216 replace: true,
217 templateUrl: '/directives/ui-filter-group-vis.html'
218 };
219 })
220
221 .directive('uiFilterButtonVis', function() {
222 return {
223 restrict: 'E',
224 scope: {
225 property: '=',
226 classesOn: '=',
227 classesOff: '='
228 },
229 replace: true,
230 templateUrl: '/directives/ui-filter-button-vis.html',
231 link: function ($scope, element, attrs) {
232 $scope.toggle = function() {
233 $scope.property = !$scope.property;
234 }
235 }
236 };
237 })
238 })();