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