Merge: Added contributing guidelines and link from readme
[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', '$window', function(Model, $routeParams, $scope, $window) {
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() {
54 $window.location.href = $scope.results[$scope.activeItem].web_url;
55 $scope.reset();
56 }
57
58 $scope.selectEscape = function() {
59 $scope.reset();
60 }
61
62 $scope.search = function() {
63 if(!$scope.query) {
64 $scope.reset();
65 return;
66 }
67 Model.search($scope.query, 10,
68 function(data) {
69 $scope.reset();
70 $scope.results = data;
71 }, function(err) {
72 $scope.reset();
73 $scope.error = err;
74 });
75 }
76
77 $scope.reset();
78 }])
79
80 .directive('uiFilters', function() {
81 return {
82 restrict: 'E',
83 scope: {
84 property: '=',
85 classesOn: '=',
86 classesOff: '='
87 },
88 replace: true,
89 templateUrl: '/directives/ui-filter-button-vis.html',
90 link: function ($scope, element, attrs) {
91 $scope.toggle = function() {
92 $scope.property = !$scope.property;
93 }
94 }
95 };
96 })
97
98 .filter('visibility', function() {
99 return function(input, visibilityFilter) {
100 var res = [];
101 input.forEach(function(entry) {
102 if(visibilityFilter.public == false && entry.visibility == "public") {
103 return;
104 }
105 if(visibilityFilter.protected == false && entry.visibility == "protected") {
106 return;
107 }
108 if(visibilityFilter.private == false && entry.visibility == "private") {
109 return;
110 }
111 res.push(entry);
112 });
113 return res;
114 };
115 })
116
117 .directive('uiFilterForm', function() {
118 return {
119 restrict: 'E',
120 scope: {
121 searchFilter: '=',
122 visibilityFilter: '='
123 },
124 replace: true,
125 templateUrl: '/directives/ui-filter-form.html'
126 };
127 })
128
129 .directive('uiFilterField', function() {
130 return {
131 restrict: 'E',
132 scope: {
133 property: '='
134 },
135 replace: true,
136 templateUrl: '/directives/ui-filter-field.html'
137 };
138 })
139
140 .directive('uiFilterGroupVis', function() {
141 return {
142 restrict: 'E',
143 scope: {
144 property: '='
145 },
146 replace: true,
147 templateUrl: '/directives/ui-filter-group-vis.html'
148 };
149 })
150
151 .directive('uiFilterButtonVis', function() {
152 return {
153 restrict: 'E',
154 scope: {
155 property: '=',
156 classesOn: '=',
157 classesOff: '='
158 },
159 replace: true,
160 templateUrl: '/directives/ui-filter-button-vis.html',
161 link: function ($scope, element, attrs) {
162 $scope.toggle = function() {
163 $scope.property = !$scope.property;
164 }
165 }
166 };
167 })
168 })();