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