table_server_mixin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. App.TableServerMixin = Em.Mixin.create({
  21. queryParams: [],
  22. resetStartIndex: false,
  23. /**
  24. * filterProps support follow types of filter:
  25. * MATCH - match of RegExp
  26. * EQUAL - equality "="
  27. * LESS - "<"
  28. * MORE - ">"
  29. * MULTIPLE - multiple values to compare
  30. * CUSTOM - substitute values with keys "{#}" in alias
  31. */
  32. filterProps: [],
  33. /**
  34. * include "from" and "page_size"
  35. */
  36. paginationProps: [
  37. {
  38. name: 'displayLength',
  39. key: 'page_size',
  40. value: '25',
  41. type: 'EQUAL'
  42. },
  43. {
  44. name: 'startIndex',
  45. key: 'from',
  46. value: 0,
  47. type: 'EQUAL'
  48. }
  49. ],
  50. sortProps: [],
  51. /**
  52. * update values of pagination properties from local db and return them
  53. * @return {array}
  54. */
  55. getPaginationProps: function () {
  56. var displayLength = App.db.getDisplayLength(this.get('name'));
  57. if (displayLength) {
  58. this.get('paginationProps').findProperty('name', 'displayLength').value = displayLength;
  59. }
  60. var startIndex = App.db.getStartIndex(this.get('name'));
  61. if (!Em.isNone(startIndex)) {
  62. startIndex = (startIndex > 0) ? startIndex - 1 : startIndex;
  63. this.get('paginationProps').findProperty('name', 'startIndex').value = startIndex;
  64. }
  65. return this.get('paginationProps');
  66. },
  67. /**
  68. * get sort properties from local db
  69. * @return {Array}
  70. */
  71. getSortProps: function () {
  72. var savedSortConditions = App.db.getSortingStatuses(this.get('name')) || [],
  73. sortProperties = this.get('sortProps'),
  74. sortParams = [];
  75. savedSortConditions.forEach(function (sort) {
  76. var property = sortProperties.findProperty('name', sort.name);
  77. if (property && (sort.status === 'sorting_asc' || sort.status === 'sorting_desc')) {
  78. property.value = sort.status.replace('sorting_', '');
  79. property.type = 'SORT';
  80. sortParams.push(property);
  81. }
  82. });
  83. return sortParams;
  84. },
  85. /**
  86. * get filter properties from local db
  87. * @return {Array}
  88. */
  89. getFilterProps: function () {
  90. var savedFilterConditions = App.db.getFilterConditions(this.get('name')) || [],
  91. filterProperties = this.get('filterProps'),
  92. filterParams = [],
  93. colPropAssoc = this.get('colPropAssoc');
  94. savedFilterConditions.forEach(function (filter) {
  95. var property = filterProperties.findProperty('name', colPropAssoc[filter.iColumn]);
  96. if (property && filter.value && filter.value.length > 0 && !filter.skipFilter) {
  97. property.isFilter = true;
  98. if (filter.type === 'range') {
  99. //range value should contain array of two element with start and end values accordingly
  100. filter.value.forEach(function (val, index) {
  101. if (val) {
  102. property.type = (index === 0) ? "MORE" : "LESS";
  103. property.value = val;
  104. filterParams.push(property);
  105. }
  106. });
  107. } else if (filter.type === 'string') {
  108. property.value = this.getRegExp(filter.value);
  109. filterParams.push(property);
  110. } else {
  111. property.value = filter.value;
  112. filterParams.push(property);
  113. }
  114. }
  115. }, this);
  116. return filterParams;
  117. },
  118. getQueryParameters: function () {
  119. var queryParams = [];
  120. queryParams.pushObjects(this.getPaginationProps());
  121. queryParams.pushObjects(this.getSortProps());
  122. queryParams.pushObjects(this.getFilterProps());
  123. this.set('queryParams', queryParams);
  124. return queryParams;
  125. },
  126. getRegExp: function (value) {
  127. value = validator.isValidMatchesRegexp(value) ? value.replace(/(\.+\*?|(\.\*)+)$/, '') + '.*' : '^$';
  128. value = /^\.\*/.test(value) || value == '^$' ? value : '.*' + value;
  129. return value;
  130. }
  131. });