2 * Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 .config(function($stateProvider
, $locationProvider
) {
27 controller
: 'SearchCtrl',
29 templateUrl
: 'views/search.html',
31 entities
: function(Model
, $q
, $stateParams
, $state
) {
33 var query
= $stateParams
.q
;
34 var page
= $stateParams
.p ? $stateParams
.p
: 1;
35 var limit
= $stateParams
.n ? $stateParams
.n
: 10;
36 Model
.search(query
, page
, limit
, d
.resolve
,
38 $state
.go('404', null, { location
: false })
46 .controller('SearchCtrl', function($scope
, $state
, $stateParams
, entities
) {
48 vm
.entities
= entities
;
49 vm
.query
= $stateParams
.q
;
51 $scope
.$
on('change-page', function(e
, page
, limit
) {
52 $state
.go('search', {q
: vm
.query
, p
: page
, l
: limit
});
56 .directive('uiSearchField', function($document
) {
60 controller
: function($scope
, $state
, $stateParams
, $location
, Model
) {
62 vm
.search
= function() {
67 Model
.search(vm
.query
, 1, 8,
77 vm
.reset
= function() {
84 vm
.update
= function(e
) {
87 } else if(e
.keyCode
== 40) {
89 } else if(e
.keyCode
== 27) {
91 } else if(e
.keyCode
== 13) {
96 vm
.selectUp
= function() {
97 if(vm
.activeItem
>= 0) {
102 vm
.selectDown
= function() {
103 if(vm
.activeItem
< vm
.results
.results
.length
) {
108 vm
.selectEnter
= function(e
) {
109 if(vm
.activeItem
>= 0 && vm
.activeItem
< vm
.results
.results
.length
) {
110 $location
.url(vm
.results
.results
[vm
.activeItem
].web_url
);
112 $state
.go('search', {q
: vm
.query
, p
: 1});
117 vm
.selectEscape
= function() {
121 vm
.setActive
= function(index
) {
122 vm
.activeItem
= index
;
127 $scope
.$
watch(function() {
128 return $stateParams
.q
;
134 templateUrl
: 'directives/ui/search-field.html',
135 link
: function ($scope
, element
, attrs
, ctrl
) {
136 $document
.bind('click', function (event
) {
137 var isChild
= $
(element
).has(event
.target
).length
> 0;
138 var isSelf
= element
[0] == event
.target
;
139 var isInside
= isChild || isSelf
;
151 .directive('uiFilters', function() {
160 templateUrl
: '/directives/ui-filter-button-vis.html',
161 link
: function ($scope
, element
, attrs
) {
162 $scope
.toggle
= function() {
163 $scope
.property
= !$scope
.property
;
169 .filter('visibility', function() {
170 return function(input
, visibilityFilter
) {
172 input
.forEach(function(entry
) {
173 if(visibilityFilter
.public == false && entry
.visibility
== "public") {
176 if(visibilityFilter
.protected == false && entry
.visibility
== "protected") {
179 if(visibilityFilter
.private == false && entry
.visibility
== "private") {
188 .directive('uiSummary', function($rootScope
, $location
, $anchorScroll
) {
195 templateUrl
: '/directives/ui-summary.html',
196 link
: function ($scope
, element
, attrs
) {
197 $scope
.goTo
= function(entity
) {
198 $location
.hash(entity
.id
);
201 $scope
.textToId
= function(text
) {
202 return text
.replace(/ /g
, '-').replace(/[^A
-Za
-z_
-]/g
, '');
205 $rootScope
.reloadSummary
= function() {
206 var h
= angular
.element(document
.querySelectorAll(
207 $scope
.target
+ ' h1, ' +
208 $scope
.target
+ ' h2, ' +
209 $scope
.target
+ ' h3, ' +
210 $scope
.target
+ ' h4, ' +
211 $scope
.target
+ ' h5, ' +
212 $scope
.target
+ ' h6 '));
214 $scope
.headings
= [];
215 angular
.forEach(h
, function(heading
) {
216 var head
= angular
.element(heading
);
217 if(!head
.is(':visible')) { return ; }
218 var text
= head
.text().trim();
219 var id
= $scope
.textToId(text
);
220 if(!head
.attr('id')) {
223 id
= head
.attr('id');
225 $scope
.headings
.push({
228 level
: parseInt(head
[0].nodeName
[1])
234 $scope
.$
watch('target', function() {
235 setTimeout(function() {
236 $rootScope
.reloadSummary();
243 .directive('uiFilterForm', function() {
248 visibilityFilter
: '='
251 templateUrl
: '/directives/ui-filter-form.html'
255 .directive('uiFilterField', function() {
262 templateUrl
: '/directives/ui-filter-field.html'
266 .directive('uiFilterGroupVis', function() {
273 templateUrl
: '/directives/ui-filter-group-vis.html'
277 .directive('uiFilterButtonVis', function() {
286 templateUrl
: '/directives/ui-filter-button-vis.html',
287 link
: function ($scope
, element
, attrs
) {
288 $scope
.toggle
= function() {
289 $scope
.property
= !$scope
.property
;
297 .directive('uiPagination', function() {
306 controller
: function($scope
) {
309 $scope
.$
watch('vm.pagination', function(pagination
) {
310 if(!pagination
) return;
311 vm
.computePages(pagination
);
314 vm
.computePages
= function(pagination
) {
317 var page
= pagination
.page
;
318 var start
= page
- Math
.floor(len
/ 2);
319 var end
= page
+ Math
.floor(len
/ 2);
322 end
= Math
.min(pagination
.max
, end
+ Math
.abs(start
) + 1)
324 } else if(end
> pagination
.max
) {
325 start
= Math
.max(1, start
- Math
.abs(end
- pagination
.max
))
326 end
= pagination
.max
;
329 for(var i
= start
; i
<= end
; i
++) {
334 vm
.changePage
= function(page
, limit
) {
335 if(page
<= 0 || page
> vm
.pagination
.max
) return;
336 var suffix
= vm
.suffix ? vm
.suffix
: '';
337 $scope
.$
emit('change-page' + suffix
, page
, limit
);
341 templateUrl
: 'directives/ui/pagination.html'