tableServerProvider.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * Return pagination information displayed on the page
  36. * @type {String}
  37. */
  38. paginationInfo: function () {
  39. return this.t('tableView.filters.paginationInfo').format(this.get('startIndex'), this.get('endIndex'), this.get('totalCount'));
  40. }.property('totalCount', 'endIndex'),
  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. if (!this.get('filteringComplete')) return false;
  48. this.set('filteringComplete', false);
  49. this.get('updater')[this.get('updater.tableUpdaterMap')[this.get('tableName')]](function () {
  50. self.set('filteringComplete', true);
  51. self.propertyDidChange('pageContent');
  52. });
  53. return true;
  54. },
  55. /**
  56. * reset filters value by column to which filter belongs
  57. * @param columns {Array}
  58. */
  59. resetFilterByColumns: function (columns) {
  60. var filterConditions = this.get('filterConditions');
  61. columns.forEach(function (iColumn) {
  62. var filterCondition = filterConditions.findProperty('iColumn', iColumn);
  63. if (filterCondition) {
  64. filterCondition.value = '';
  65. }
  66. });
  67. this.saveFilterConditions();
  68. },
  69. /**
  70. * Apply each filter to each row
  71. * @param iColumn {Number}
  72. * @param value {String}
  73. * @param type {String}
  74. */
  75. updateFilter: function (iColumn, value, type) {
  76. this.saveFilterConditions(iColumn, value, type, false);
  77. this.refresh();
  78. },
  79. /**
  80. * save filter conditions to local storage
  81. * @param iColumn {Number}
  82. * @param value {String|Array}
  83. * @param type {String}
  84. * @param skipFilter {Boolean}
  85. */
  86. saveFilterConditions: function (iColumn, value, type, skipFilter) {
  87. var filterCondition = this.get('filterConditions').findProperty('iColumn', iColumn);
  88. if (filterCondition) {
  89. filterCondition.value = value;
  90. filterCondition.skipFilter = skipFilter;
  91. } else {
  92. filterCondition = {
  93. skipFilter: skipFilter,
  94. iColumn: iColumn,
  95. value: value,
  96. type: type
  97. };
  98. this.get('filterConditions').push(filterCondition);
  99. }
  100. App.db.setFilterConditions(this.get('controller.name'), this.get('filterConditions'));
  101. }
  102. });