tableServerProvider.js 3.4 KB

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