|
1
|
1 |
(function(){ |
|
|
2 |
'use strict'; |
|
|
3 |
|
|
|
4 |
angular.module('ammicoSearch',['ngResource', 'ngRoute']) |
|
|
5 |
.config(function ($routeProvider) { |
|
|
6 |
$routeProvider |
|
|
7 |
.when('/', { |
|
|
8 |
templateUrl: 'search/search.html', |
|
|
9 |
controller: 'searchCtrl' |
|
|
10 |
}); |
|
|
11 |
}) |
|
2
|
12 |
.controller('searchCtrl', function($scope, $location, $routeParams, searchApi){ |
|
1
|
13 |
console.log('search 6',$scope, $location, $routeParams); |
|
|
14 |
$scope.q = $routeParams.q || ''; |
|
2
|
15 |
searchApi.searchResource({q:$scope.q, of: 'json', synthesis: 'false', nresults:'50', callback: 'JSON_CALLBACK'}).$promise.then( |
|
1
|
16 |
//success |
|
|
17 |
function( data ){ |
|
6
|
18 |
// TEMP : add random pict |
|
|
19 |
for(var i=data.hits.length-1; i>=0; i--){ |
|
|
20 |
data.hits[i].url = 'http://placekitten.com/g/' + (100+Math.floor((Math.random()*600)+1)) + '/' + (100+Math.floor((Math.random()*600)+1)); |
|
|
21 |
} |
|
1
|
22 |
$scope.results = data; |
|
|
23 |
}, |
|
|
24 |
//error |
|
|
25 |
function( error ){ |
|
2
|
26 |
alert('Erreur avec la requĂȘte', error); |
|
1
|
27 |
} |
|
|
28 |
); |
|
|
29 |
|
|
2
|
30 |
$scope.addItem = function(i){ |
|
|
31 |
console.log('addItem',i); |
|
|
32 |
if($scope.results.hits.length>0 && 0<=i && i<$scope.results.hits.length){ |
|
|
33 |
|
|
|
34 |
} |
|
|
35 |
}; |
|
1
|
36 |
}) |
|
|
37 |
.filter('meta', function() { |
|
|
38 |
return function(input, metaName) { |
|
|
39 |
var nb = input.length, i = 0, found = false; |
|
|
40 |
while(found===false && i<nb){ |
|
|
41 |
if(input[i].name===metaName){ |
|
|
42 |
found = true; |
|
|
43 |
return input[i].value; |
|
|
44 |
} |
|
|
45 |
i++; |
|
|
46 |
} |
|
2
|
47 |
return ''; |
|
1
|
48 |
}; |
|
|
49 |
}); |
|
|
50 |
|
|
|
51 |
})(); |