Code coverage report for app/views/common/search_box_view.js

Statements: 86.05% (37 / 43)      Branches: 50% (4 / 8)      Functions: 77.78% (14 / 18)      Lines: 86.05% (37 / 43)      Ignored: none     

All files » app/views/common/ » search_box_view.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169    1                                     1   1             7 7 7 7       2       2       3           1                                         6 6 6 6 6   6 6 6 6               1                                           1 1 1                         1 1 1                   2   2 2   2 2 2 2 1               2                   2      
'use strict';
 
;require.register("views/common/search_box_view", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  App.SearchBoxView = Em.View.extend({
    templateName: require('templates/main/host/combo_search_box'),
    errMsg: '',
    classNames: ['col-sm-12'],
    keyFilterMap: [],
 
    didInsertElement: function didInsertElement() {
      this.initVS();
      this.restoreComboFilterQuery();
      this.showHideClearButton();
      this.initOpenVSButton();
    },
 
    showErrMsg: function showErrMsg(category) {
      this.set('errMsg', category.attributes.value + " " + Em.I18n.t('hosts.combo.search.invalidCategory'));
    },
 
    clearErrMsg: function clearErrMsg() {
      this.set('errMsg', '');
    },
 
    initOpenVSButton: function initOpenVSButton() {
      $('.VS-open-box button').click(function () {
        $('.VS-open-box .popup-arrow-up, .search-box-row').toggleClass('hide');
      });
    },
 
    initVS: function initVS() {
      window.visualSearch = VS.init({
        container: $('#combo_search_box'),
        query: '',
        showFacets: true,
        delay: 500,
        placeholder: Em.I18n.t('common.search'),
        unquotable: ['text'],
        callbacks: {
          search: this.search.bind(this),
          facetMatches: this.facetMatches.bind(this),
          valueMatches: this.valueMatches.bind(this)
        }
      });
    },
 
    /**
     * 'search' callback for visualsearch.js
     * @param query
     * @param searchCollection
     */
    search: function search(query, searchCollection) {
      this.clearErrMsg();
      this.showHideClearButton();
      var invalidFacet = this.findInvalidFacet(searchCollection);
      Eif (invalidFacet) {
        this.showErrMsg(invalidFacet);
      }
      var tableView = this.get('parentView');
      App.db.setComboSearchQuery(tableView.get('controller.name'), query);
      var filterConditions = this.createFilterConditions(searchCollection);
      tableView.updateComboFilter(filterConditions);
    },
 
    /**
     * 'facetMatches' callback for visualsearch.js
     * @param callback
     */
    facetMatches: function facetMatches(callback) {
      callback(this.get('keyFilterMap').mapProperty('label'), { preserveOrder: true });
    },
 
    valueMatches: Em.K,
 
    /**
     *
     * @param {Array} values
     * @param {string} facetValue
     */
    rejectUsedValues: function rejectUsedValues(values, facetValue) {
      return values.reject(function (item) {
        return visualSearch.searchQuery.values(facetValue).indexOf(item) >= 0;
      });
    },
 
    /**
     *
     * @param {object} searchCollection
     * @returns {!object}
     */
    findInvalidFacet: function findInvalidFacet(searchCollection) {
      var map = this.get('keyFilterMap');
      return searchCollection.models.find(function (facet) {
        return !map.someProperty('label', facet.attributes.category);
      });
    },
 
    showHideClearButton: function showHideClearButton() {
      if (visualSearch.searchQuery.length > 0) {
        $('.VS-cancel-search-box').removeClass('hide');
      } else {
        $('.VS-cancel-search-box').addClass('hide');
      }
    },
 
    restoreComboFilterQuery: function restoreComboFilterQuery() {
      var query = App.db.getComboSearchQuery(this.get('parentView.controller.name'));
      Eif (query) {
        visualSearch.searchBox.setQuery(query);
      }
    },
 
    /**
     *
     * @param {object} searchCollection
     * @returns {Array}
     */
    createFilterConditions: function createFilterConditions(searchCollection) {
      var _this = this;
 
      var filterConditions = [];
      var map = this.get('keyFilterMap');
 
      searchCollection.models.forEach(function (model) {
        var filter = model.attributes;
        var category = map.findProperty('label', filter.category);
        if (category) {
          filterConditions.push({
            skipFilter: false,
            iColumn: category.column,
            value: _this.mapLabelToValue(category.key, filter.value),
            type: category.type
          });
        }
      });
      return filterConditions;
    },
 
    /**
     *
     * @param {string} category
     * @param {string} label
     * @returns {string}
     */
    mapLabelToValue: function mapLabelToValue(category, label) {
      return label;
    }
  });
});