f33a4398f56ff48d9a09e3aef85fbd62beadef98
[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('uiFilterForm', function() {
133 return {
134 restrict: 'E',
135 scope: {
136 searchFilter: '=',
137 visibilityFilter: '='
138 },
139 replace: true,
140 templateUrl: '/directives/ui-filter-form.html'
141 };
142 })
143
144 .directive('uiFilterField', function() {
145 return {
146 restrict: 'E',
147 scope: {
148 property: '='
149 },
150 replace: true,
151 templateUrl: '/directives/ui-filter-field.html'
152 };
153 })
154
155 .directive('uiFilterGroupVis', function() {
156 return {
157 restrict: 'E',
158 scope: {
159 property: '='
160 },
161 replace: true,
162 templateUrl: '/directives/ui-filter-group-vis.html'
163 };
164 })
165
166 .directive('uiFilterButtonVis', function() {
167 return {
168 restrict: 'E',
169 scope: {
170 property: '=',
171 classesOn: '=',
172 classesOff: '='
173 },
174 replace: true,
175 templateUrl: '/directives/ui-filter-button-vis.html',
176 link: function ($scope, element, attrs) {
177 $scope.toggle = function() {
178 $scope.property = !$scope.property;
179 }
180 }
181 };
182 })
183 })();