table_server_mixin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. //TODO integrate mixin into mainHostController to avoid code duplication
  21. App.TableServerMixin = Em.Mixin.create({
  22. queryParams: [],
  23. resetStartIndex: false,
  24. /**
  25. * filterProps support follow types of filter:
  26. * MATCH - match of RegExp
  27. * EQUAL - equality "="
  28. * LESS - "<"
  29. * MORE - ">"
  30. * MULTIPLE - multiple values to compare
  31. * CUSTOM - substitute values with keys "{#}" in alias
  32. */
  33. filterProps: [],
  34. /**
  35. * include "from" nad "page_size"
  36. */
  37. paginationProps: [
  38. {
  39. name: 'displayLength',
  40. key: 'page_size',
  41. value: '25',
  42. type: 'EQUAL'
  43. },
  44. {
  45. name: 'startIndex',
  46. key: 'from',
  47. value: 0,
  48. type: 'EQUAL'
  49. }
  50. ],
  51. sortProps: [],
  52. /**
  53. * update values of pagination properties from local db and return them
  54. * @return {array}
  55. */
  56. getPaginationProps: function () {
  57. var displayLength = App.db.getDisplayLength(this.get('name'));
  58. if (displayLength) {
  59. this.get('paginationProps').findProperty('name', 'displayLength').value = displayLength;
  60. }
  61. var startIndex = App.db.getStartIndex(this.get('name'));
  62. if (!Em.isNone(startIndex)) {
  63. startIndex = (startIndex > 0) ? startIndex - 1 : startIndex;
  64. this.get('paginationProps').findProperty('name', 'startIndex').value = startIndex;
  65. }
  66. return this.get('paginationProps');
  67. },
  68. /**
  69. * get sort properties from local db
  70. * @return {Array}
  71. */
  72. getSortProps: function () {
  73. var savedSortConditions = App.db.getSortingStatuses(this.get('name')) || [],
  74. sortProperties = this.get('sortProps'),
  75. sortParams = [];
  76. savedSortConditions.forEach(function (sort) {
  77. var property = sortProperties.findProperty('name', sort.name);
  78. if (property && (sort.status === 'sorting_asc' || sort.status === 'sorting_desc')) {
  79. property.value = sort.status.replace('sorting_', '');
  80. property.type = 'SORT';
  81. sortParams.push(property);
  82. }
  83. });
  84. return sortParams;
  85. },
  86. /**
  87. * get filter properties from local db
  88. * @return {Array}
  89. */
  90. getFilterProps: function () {
  91. var savedFilterConditions = App.db.getFilterConditions(this.get('name')) || [],
  92. filterProperties = this.get('filterProps'),
  93. filterParams = [],
  94. colPropAssoc = this.get('colPropAssoc');
  95. savedFilterConditions.forEach(function (filter) {
  96. var property = filterProperties.findProperty('name', colPropAssoc[filter.iColumn]);
  97. if (property && filter.value.length > 0 && !filter.skipFilter) {
  98. property.isFilter = true;
  99. if (filter.type === 'range') {
  100. //range value should contain array of two element with start and end values accordingly
  101. filter.value.forEach(function (val, index) {
  102. if (val) {
  103. property.type = (index === 0) ? "MORE" : "LESS";
  104. property.value = val;
  105. filterParams.push(property);
  106. }
  107. });
  108. } else if (filter.type === 'string') {
  109. property.value = this.getRegExp(filter.value);
  110. filterParams.push(property);
  111. } else {
  112. property.value = filter.value;
  113. filterParams.push(property);
  114. }
  115. }
  116. }, this);
  117. return filterParams;
  118. },
  119. getQueryParameters: function () {
  120. var queryParams = [];
  121. queryParams.pushObjects(this.getPaginationProps());
  122. queryParams.pushObjects(this.getSortProps());
  123. queryParams.pushObjects(this.getFilterProps());
  124. this.set('queryParams', queryParams);
  125. return queryParams;
  126. },
  127. getRegExp: function (value) {
  128. value = validator.isValidMatchesRegexp(value) ? value.replace(/(\.+\*?|(\.\*)+)$/, '') + '.*' : '^$';
  129. value = /^\.\*/.test(value) || value == '^$' ? value : '.*' + value;
  130. return value;
  131. }
  132. });