tableServerProvider.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. App.TableServerProvider = Em.Mixin.create({
  20. tableName: '',
  21. updaterBinding: 'App.router.updateController',
  22. filteringComplete: true,
  23. filterConditions: [],
  24. filterWaitingTime: 500,
  25. timeOut: null,
  26. /**
  27. * total number of entities in table
  28. */
  29. totalCount: 0,
  30. /**
  31. * Request error data
  32. *
  33. */
  34. requestError: null,
  35. filteredContent: function () {
  36. return this.get('content');
  37. }.property('content'),
  38. pageContent: function () {
  39. return this.get('filteredContent');
  40. }.property('filteredContent'),
  41. /**
  42. * request latest data filtered by new parameters
  43. * called when trigger property(<code>refreshTriggers</code>) is changed
  44. */
  45. refresh: function () {
  46. var self = this;
  47. this.set('filteringComplete', false);
  48. var updaterMethodName = this.get('updater.tableUpdaterMap')[this.get('tableName')];
  49. this.get('updater')[updaterMethodName](function () {
  50. self.set('filteringComplete', true);
  51. self.propertyDidChange('pageContent');
  52. }, function() {
  53. self.set('requestError', arguments);
  54. });
  55. return true;
  56. },
  57. /**
  58. * reset filters value by column to which filter belongs
  59. * @param columns {Array}
  60. */
  61. resetFilterByColumns: function (columns) {
  62. var filterConditions = this.get('filterConditions');
  63. columns.forEach(function (iColumn) {
  64. var filterCondition = filterConditions.findProperty('iColumn', iColumn);
  65. if (filterCondition) {
  66. filterCondition.value = '';
  67. this.saveFilterConditions(filterCondition.iColumn, filterCondition.value, filterCondition.type, filterCondition.skipFilter);
  68. }
  69. }, this);
  70. },
  71. /**
  72. * Apply each filter to each row
  73. * @param iColumn {Number}
  74. * @param value {String}
  75. * @param type {String}
  76. */
  77. updateFilter: function (iColumn, value, type) {
  78. var self = this;
  79. if (!this.get('filteringComplete')) {
  80. clearTimeout(this.get('timeOut'));
  81. this.set('timeOut', setTimeout(function() {
  82. self.updateFilter(iColumn, value, type);
  83. }, this.get('filterWaitingTime')));
  84. } else {
  85. clearTimeout(this.get('timeOut'));
  86. this.saveFilterConditions(iColumn, value, type, false);
  87. this.refresh();
  88. }
  89. },
  90. /**
  91. * save filter conditions to local storage
  92. * @param iColumn {Number}
  93. * @param value {String|Array}
  94. * @param type {String}
  95. * @param skipFilter {Boolean}
  96. */
  97. saveFilterConditions: function (iColumn, value, type, skipFilter) {
  98. var filterCondition = this.get('filterConditions').findProperty('iColumn', iColumn);
  99. if (filterCondition) {
  100. filterCondition.value = value;
  101. filterCondition.skipFilter = skipFilter;
  102. } else {
  103. filterCondition = {
  104. skipFilter: skipFilter,
  105. iColumn: iColumn,
  106. value: value,
  107. type: type
  108. };
  109. this.get('filterConditions').push(filterCondition);
  110. }
  111. App.db.setFilterConditions(this.get('controller.name'), this.get('filterConditions'));
  112. }
  113. });