1/**
2 * Controller for server
3 *
4 * @module app/serverHealth
5 * @exports inventoryOverviewController
6 * @name inventoryOverviewController
7 */
8
9window.angular && (function (angular) {
10    'use strict';
11
12    angular
13        .module('app.serverHealth')
14        .controller('inventoryOverviewController', [
15            '$scope',
16            '$window',
17            'APIUtils',
18            'dataService',
19            function($scope, $window, APIUtils, dataService){
20                $scope.dataService = dataService;
21                $scope.hardwares = [];
22                $scope.originalData = {};
23                $scope.customSearch = "";
24                $scope.searchTerms = [];
25                $scope.loading = false;
26
27                $scope.loading = true;
28                APIUtils.getHardwares(function(data, originalData){
29                    $scope.hardwares = data;
30                    $scope.originalData = JSON.stringify(originalData);
31                    $scope.loading = false;
32                });
33
34                $scope.clear = function(){
35                    $scope.customSearch = "";
36                    $scope.searchTerms = [];
37                }
38
39                $scope.doSearchOnEnter = function (event) {
40                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
41                    if (event.keyCode === 13 &&
42                        search.length >= 2) {
43                        $scope.searchTerms = $scope.customSearch.split(" ");
44                    }else{
45                        if(search.length == 0){
46                            $scope.searchTerms = [];
47                        }
48                    }
49                };
50
51                $scope.doSearchOnClick = function() {
52                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
53                    if (search.length >= 2) {
54                        $scope.searchTerms = $scope.customSearch.split(" ");
55                    }else{
56                        if(search.length == 0){
57                            $scope.searchTerms = [];
58                        }
59                    }
60                }
61
62                $scope.filterBySearchTerms = function(hardware){
63
64                    if(!$scope.searchTerms.length) return true;
65
66                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
67                        if(hardware.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
68                    }
69                    return true;
70                }
71            }
72        ]
73    );
74
75})(angular);
76