tableServerProvider.js 3.1 KB

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