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

Statements: 36.96% (17 / 46)      Branches: 20% (7 / 35)      Functions: 44.44% (4 / 9)      Lines: 36.96% (17 / 46)      Ignored: none     

All files » app/mixins/common/ » table_server_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    1                                     1 1   1                                                                         6 6       6 6       6               7     7 1 1 1 1 1     7                                                                                                                
'use strict';
 
;require.register("mixins/common/table_server_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');
  var validator = require('utils/validator');
 
  App.TableServerMixin = Em.Mixin.create({
    queryParams: [],
    resetStartIndex: false,
 
    /**
     * filterProps support follow types of filter:
     * MATCH - match of RegExp
     * EQUAL - equality "="
     * LESS - "<"
     * MORE - ">"
     * MULTIPLE - multiple values to compare
     * CUSTOM - substitute values with keys "{#}" in alias
     */
    filterProps: [],
 
    /**
     * include "from" and "page_size"
     */
    paginationProps: [{
      name: 'displayLength',
      key: 'page_size',
      value: '25',
      type: 'EQUAL'
    }, {
      name: 'startIndex',
      key: 'from',
      value: 0,
      type: 'EQUAL'
    }],
 
    sortProps: [],
 
    /**
     * update values of pagination properties from local db and return them
     * @return {array}
     */
    getPaginationProps: function getPaginationProps() {
      var displayLength = App.db.getDisplayLength(this.get('name'));
      Iif (displayLength) {
        this.get('paginationProps').findProperty('name', 'displayLength').value = displayLength;
      }
 
      var startIndex = App.db.getStartIndex(this.get('name'));
      Iif (!Em.isNone(startIndex)) {
        startIndex = startIndex > 0 ? startIndex - 1 : startIndex;
        this.get('paginationProps').findProperty('name', 'startIndex').value = startIndex;
      }
      return this.get('paginationProps').slice(0);
    },
 
    /**
     * get sort properties from local db
     * @return {Array}
     */
    getSortProps: function getSortProps() {
      var savedSortConditions = App.db.getSortingStatuses(this.get('name')) || [],
          sortProperties = this.get('sortProps'),
          sortParams = [];
      savedSortConditions.forEach(function (sort) {
        var property = sortProperties.findProperty('name', sort.name);
        Eif (property && (sort.status === 'sorting_asc' || sort.status === 'sorting_desc')) {
          property.value = sort.status.replace('sorting_', '');
          property.type = 'SORT';
          sortParams.push(property);
        }
      });
      return sortParams;
    },
    /**
     * get filter properties from local db
     * @return {Array}
     */
    getFilterProps: function getFilterProps() {
      var savedFilterConditions = App.db.getFilterConditions(this.get('name')) || [],
          filterProperties = this.get('filterProps'),
          filterParams = [],
          colPropAssoc = this.get('colPropAssoc');
 
      savedFilterConditions.forEach(function (filter) {
        var property = filterProperties.findProperty('name', colPropAssoc[filter.iColumn]);
 
        if (property && filter.value && filter.value.length > 0 && !filter.skipFilter) {
          property.isFilter = true;
 
          if (filter.type === 'range') {
            //range value should contain array of two element with start and end values accordingly
            filter.value.forEach(function (val, index) {
              if (val) {
                property.type = index === 0 ? "MORE" : "LESS";
                property.value = val;
                filterParams.push(property);
              }
            });
          } else if (filter.type === 'string') {
            property.value = this.getRegExp(filter.value);
            filterParams.push(property);
          } else {
            property.value = filter.value;
            filterParams.push(property);
          }
        }
      }, this);
      return filterParams;
    },
 
    getQueryParameters: function getQueryParameters() {
      var queryParams = [];
 
      queryParams.pushObjects(this.getPaginationProps());
      queryParams.pushObjects(this.getSortProps());
      queryParams.pushObjects(this.getFilterProps());
      this.set('queryParams', queryParams);
      return queryParams;
    },
 
    getRegExp: function getRegExp(value) {
      value = validator.isValidMatchesRegexp(value) ? value.replace(/(\.+\*?|(\.\*)+)$/, '') + '.*' : '^$';
      value = /^\.\*/.test(value) || value == '^$' ? value : '.*' + value;
      return value;
    }
 
  });
});