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 = false;
21
22      $scope.loading = true;
23      APIUtils.getHardwares(function(data, originalData) {
24        $scope.hardwares = data;
25        $scope.originalData = JSON.stringify(originalData);
26        $scope.loading = false;
27      });
28
29      $scope.clear = function() {
30        $scope.customSearch = '';
31        $scope.searchTerms = [];
32      };
33
34      $scope.doSearchOnEnter = function(event) {
35        var search =
36            $scope.customSearch.replace(/^\s+/g, '').replace(/\s+$/g, '');
37        if (event.keyCode === 13 && search.length >= 2) {
38          $scope.searchTerms = $scope.customSearch.split(' ');
39        } else {
40          if (search.length == 0) {
41            $scope.searchTerms = [];
42          }
43        }
44      };
45
46      $scope.doSearchOnClick = function() {
47        var search =
48            $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(
64                  $scope.searchTerms[i].toLowerCase()) == -1)
65            return false;
66        }
67        return true;
68      };
69    }
70  ]);
71
72})(angular);
73