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
27                APIUtils.getHardwares(function(data, originalData){
28                    $scope.hardwares = data;
29                    $scope.originalData = JSON.stringify(originalData);
30                });
31
32                $scope.doSearchOnEnter = function (event) {
33                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
34                    if (event.keyCode === 13 &&
35                        search.length >= 2) {
36                        $scope.searchTerms = $scope.customSearch.split(" ");
37                    }else{
38                        if(search.length == 0){
39                            $scope.searchTerms = [];
40                        }
41                    }
42                };
43
44                $scope.doSearchOnClick = function() {
45                    var search = $scope.customSearch.replace(/^\s+/g,'').replace(/\s+$/g,'');
46                    if (search.length >= 2) {
47                        $scope.searchTerms = $scope.customSearch.split(" ");
48                    }else{
49                        if(search.length == 0){
50                            $scope.searchTerms = [];
51                        }
52                    }
53                }
54
55                $scope.filterBySearchTerms = function(hardware){
56
57                    if(!$scope.searchTerms.length) return true;
58
59                    for(var i = 0, length = $scope.searchTerms.length; i < length; i++){
60                        if(hardware.search_text.indexOf($scope.searchTerms[i].toLowerCase()) == -1) return false;
61                    }
62                    return true;
63                }
64            }
65        ]
66    );
67
68})(angular);
69