Code coverage report for app/mixins/common/table_server_view_mixin.js

Statements: 83.67% (41 / 49)      Branches: 62.16% (23 / 37)      Functions: 76.92% (10 / 13)      Lines: 87.23% (41 / 47)      Ignored: none     

All files » app/mixins/common/ » table_server_view_mixin.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 170 171 172 173    1   1                                     1         1                                                           3 3     3 1                       4 1     3 3 3 1 1 1     2 2   2 2   3                                           4 4 4 4 1   4             3 3 3             4             12 6   12 6     12 12                         9 2 2 2        
'use strict';
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
;require.register("mixins/common/table_server_view_mixin", 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');
 
  /**
   * Mixin should be used for Em.View of table that uses server to filter, sort, paginate content
   */
  App.TableServerViewMixin = Em.Mixin.create({
    filteringComplete: true,
    timeOut: null,
    /**
     * filter delay time, used to combine filters change into one content update
     */
    filterWaitingTime: 500,
 
    /**
     * count of filtered items
     */
    filteredCount: Em.computed.alias('controller.filteredCount'),
    /**
     * total count of items
     */
    totalCount: Em.computed.alias('controller.totalCount'),
 
    /**
     * data requested from server
     */
    content: Em.computed.alias('controller.content'),
 
    /**
     * content already filtered on server-side
     */
    filteredContent: Em.computed.alias('content'),
    /**
     * sort and slice recieved content by pagination parameters
     */
    pageContent: function () {
      var content = this.get('filteredContent').toArray();
      Iif (content.length > this.get('endIndex') - this.get('startIndex') + 1) {
        content = content.slice(0, this.get('endIndex') - this.get('startIndex') + 1);
      }
      return content.sort(function (a, b) {
        return a.get('index') - b.get('index');
      });
    }.property('filteredContent.length'),
 
    /**
     * compute applied filter and run content update from server
     * @param iColumn
     * @param value
     * @param type
     */
    updateFilter: function updateFilter(iColumn, value, type) {
      // Do not even trigger update flow if it's a blank update
      if (this.isBlankFilterUpdate(iColumn, value, type)) {
        return;
      }
 
      var self = this;
      this.set('controller.resetStartIndex', false);
      if (!this.get('filteringComplete')) {
        clearTimeout(this.get('timeOut'));
        this.set('timeOut', setTimeout(function () {
          self.updateFilter(iColumn, value, type);
        }, this.get('filterWaitingTime')));
      } else {
        clearTimeout(this.get('timeOut'));
        this.set('controller.resetStartIndex', true);
        //save filter only when it's applied
        this.saveFilterConditions(iColumn, value, type, false);
        this.refresh();
      }
      return true;
    },
 
    updateComboFilter: function updateComboFilter(filterConditions) {
      clearTimeout(this.get('timeOut'));
      this.set('controller.resetStartIndex', true);
      this.set('filterConditions', filterConditions);
      this.saveAllFilterConditions();
      this.refresh();
    },
 
    /**
     * 1) Has previous saved filter
     * 2) Value to update is empty
     * 3) Value to update is same as before
     * Returns true if (!1&&2 || 1&&3)
     * @param iColumn
     * @param value
     * @param type
     * @returns {boolean|*}
     */
    isBlankFilterUpdate: function isBlankFilterUpdate(iColumn, value, type) {
      var result = false;
      var filterConfitions = this.get('filterConditions');
      var filterCondition = filterConfitions ? filterConfitions.findProperty('iColumn', iColumn) : null;
      if (!filterCondition && Em.isEmpty(value) || filterCondition && filterCondition.value == value || filterCondition && _typeof(filterCondition.value) == 'object' && JSON.stringify(filterCondition.value) == JSON.stringify(value)) {
        result = true;
      }
      return result;
    },
 
    /**
     * success callback for updater request
     */
    updaterSuccessCb: function updaterSuccessCb() {
      this.set('filteringComplete', true);
      this.propertyDidChange('pageContent');
      App.loadTimer.finish('Hosts Page');
    },
 
    /**
     * error callback for updater request
     */
    updaterErrorCb: function updaterErrorCb() {
      this.set('requestError', arguments);
    },
 
    /**
     * synchronize properties of view with controller to generate query parameters
     */
    updatePagination: function updatePagination(key) {
      if (!Em.isNone(this.get('displayLength'))) {
        App.db.setDisplayLength(this.get('controller.name'), this.get('displayLength'));
      }
      if (!Em.isNone(this.get('startIndex'))) {
        App.db.setStartIndex(this.get('controller.name'), this.get('startIndex'));
      }
 
      Eif (key !== 'SKIP_REFRESH') {
        this.refresh();
      }
    },
 
    /**
     * Placeholder for `saveStartIndex`
     */
    saveStartIndex: Em.K,
 
    /**
     * when new filter applied page index should be reset to first page
     */
    resetStartIndex: function () {
      if (this.get('controller.resetStartIndex') && this.get('filteredCount') > 0) {
        this.set('startIndex', 1);
        this.saveStartIndex();
        this.updatePagination('SKIP_REFRESH');
      }
    }.observes('controller.resetStartIndex')
  });
});