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