tableServerProvider.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. this.set('controller.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. this.saveFilterConditions(iColumn, value, type, false);
  79. this.refresh();
  80. },
  81. /**
  82. * save filter conditions to local storage
  83. * @param iColumn {Number}
  84. * @param value {String|Array}
  85. * @param type {String}
  86. * @param skipFilter {Boolean}
  87. */
  88. saveFilterConditions: function (iColumn, value, type, skipFilter) {
  89. var filterCondition = this.get('filterConditions').findProperty('iColumn', iColumn);
  90. if (filterCondition) {
  91. filterCondition.value = value;
  92. filterCondition.skipFilter = skipFilter;
  93. } else {
  94. filterCondition = {
  95. skipFilter: skipFilter,
  96. iColumn: iColumn,
  97. value: value,
  98. type: type
  99. };
  100. this.get('filterConditions').push(filterCondition);
  101. }
  102. App.db.setFilterConditions(this.get('controller.name'), this.get('filterConditions'));
  103. }
  104. });