1
|
angular.module('pvpk')
|
2
|
.controller('searchController', ['$rootScope', '$scope', '$location', 'config', 'Device', function ($rootScope, $scope, $location, config, Device) {
|
3
|
|
4
|
this.$onInit = function () {
|
5
|
$scope.config = config;
|
6
|
$scope.locations = [];
|
7
|
$scope.showSearchLoading = false;
|
8
|
|
9
|
$rootScope.$emit('setSearchFromUrl', null);
|
10
|
};
|
11
|
|
12
|
$scope.searchLocations = function () {
|
13
|
var params = $location.search();
|
14
|
params.q = $scope.search.q;
|
15
|
params.isDirection = $scope.search.isDirection ? 1 : null;
|
16
|
$location.search(params);
|
17
|
|
18
|
if (!$scope.search.q || $scope.search.q.length <= 1) {
|
19
|
$scope.locations = [];
|
20
|
return;
|
21
|
}
|
22
|
|
23
|
$scope.showSearchLoading = true;
|
24
|
|
25
|
Device.query({
|
26
|
address: $scope.search.q,
|
27
|
showDirection: $scope.search.isDirection ? 1 : 0
|
28
|
}, function (data) {
|
29
|
$scope.locations = data;
|
30
|
$scope.showSearchLoading = false;
|
31
|
}, function (response) {
|
32
|
$scope.showSearchLoading = false;
|
33
|
console.log('Error api all Devices');
|
34
|
$rootScope.handleErrorResponse(response);
|
35
|
});
|
36
|
};
|
37
|
|
38
|
$rootScope.$on('setSearchFromUrl', function (event, args) {
|
39
|
var params = $location.search();
|
40
|
$scope.search = {
|
41
|
q: params.q,
|
42
|
isDirection: params.isDirection ? !!+params.isDirection : false
|
43
|
};
|
44
|
$scope.searchLocations();
|
45
|
});
|
46
|
|
47
|
$scope.selectDevice = function (id, direction) {
|
48
|
$rootScope.$emit('activeMarker', {id: id});
|
49
|
$rootScope.$emit('infoLocation', {id: id, direction: direction});
|
50
|
};
|
51
|
|
52
|
}]);
|