table_server_view_mixin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /**
  20. * Mixin should be used for Em.View of table that uses server to filter, sort, paginate content
  21. */
  22. App.TableServerViewMixin = Em.Mixin.create({
  23. filteringComplete: true,
  24. timeOut: null,
  25. /**
  26. * filter delay time, used to combine filters change into one content update
  27. */
  28. filterWaitingTime: 500,
  29. /**
  30. * count of filtered items
  31. */
  32. filteredCount: Em.computed.alias('controller.filteredCount'),
  33. /**
  34. * total count of items
  35. */
  36. totalCount: Em.computed.alias('controller.totalCount'),
  37. /**
  38. * data requested from server
  39. */
  40. content: Em.computed.alias('controller.content'),
  41. /**
  42. * content already filtered on server-side
  43. */
  44. filteredContent: Em.computed.alias('content'),
  45. /**
  46. * sort and slice recieved content by pagination parameters
  47. */
  48. pageContent: function () {
  49. var content = this.get('filteredContent').toArray();
  50. if (content.length > ((this.get('endIndex') - this.get('startIndex')) + 1)) {
  51. content = content.slice(0, (this.get('endIndex') - this.get('startIndex')) + 1);
  52. }
  53. return content.sort(function (a, b) {
  54. return a.get('index') - b.get('index');
  55. });
  56. }.property('filteredContent'),
  57. /**
  58. * compute applied filter and run content update from server
  59. * @param iColumn
  60. * @param value
  61. * @param type
  62. */
  63. updateFilter: function (iColumn, value, type) {
  64. // Do not even trigger update flow if it's a blank update
  65. if (this.isBlankFilterUpdate(iColumn, value, type)) { return; }
  66. var self = this;
  67. this.set('controller.resetStartIndex', false);
  68. if (!this.get('filteringComplete')) {
  69. clearTimeout(this.get('timeOut'));
  70. this.set('timeOut', setTimeout(function () {
  71. self.updateFilter(iColumn, value, type);
  72. }, this.get('filterWaitingTime')));
  73. } else {
  74. clearTimeout(this.get('timeOut'));
  75. this.set('controller.resetStartIndex', true);
  76. //save filter only when it's applied
  77. this.saveFilterConditions(iColumn, value, type, false);
  78. this.refresh();
  79. }
  80. return true;
  81. },
  82. updateComboFilter: function(filterConditions) {
  83. clearTimeout(this.get('timeOut'));
  84. this.set('controller.resetStartIndex', true);
  85. this.set('filterConditions', filterConditions);
  86. this.saveAllFilterConditions();
  87. this.refresh();
  88. },
  89. /**
  90. * 1) Has previous saved filter
  91. * 2) Value to update is empty
  92. * 3) Value to update is same as before
  93. * Returns true if (!1&&2 || 1&&3)
  94. * @param iColumn
  95. * @param value
  96. * @param type
  97. * @returns {boolean|*}
  98. */
  99. isBlankFilterUpdate: function(iColumn, value, type) {
  100. var result = false;
  101. var filterConfitions = this.get('filterConditions');
  102. var filterCondition = filterConfitions? filterConfitions.findProperty('iColumn', iColumn) : null;
  103. if ((!filterCondition && Em.isEmpty(value))
  104. || (filterCondition && filterCondition.value == value)
  105. || (filterCondition && typeof filterCondition.value == 'object'
  106. && JSON.stringify(filterCondition.value) == JSON.stringify(value))) {
  107. result = true;
  108. }
  109. return result;
  110. },
  111. /**
  112. * success callback for updater request
  113. */
  114. updaterSuccessCb: function () {
  115. this.set('filteringComplete', true);
  116. this.propertyDidChange('pageContent');
  117. App.loadTimer.finish('Hosts Page');
  118. },
  119. /**
  120. * error callback for updater request
  121. */
  122. updaterErrorCb: function () {
  123. this.set('requestError', arguments);
  124. },
  125. /**
  126. * synchronize properties of view with controller to generate query parameters
  127. */
  128. updatePagination: function (key) {
  129. if (!Em.isNone(this.get('displayLength'))) {
  130. App.db.setDisplayLength(this.get('controller.name'), this.get('displayLength'));
  131. this.get('controller.paginationProps').findProperty('name', 'displayLength').value = this.get('displayLength');
  132. }
  133. if (!Em.isNone(this.get('startIndex'))) {
  134. App.db.setStartIndex(this.get('controller.name'), this.get('startIndex'));
  135. this.get('controller.paginationProps').findProperty('name', 'startIndex').value = this.get('startIndex');
  136. }
  137. if (key !== 'SKIP_REFRESH') {
  138. this.refresh();
  139. }
  140. },
  141. /**
  142. * when new filter applied page index should be reset to first page
  143. */
  144. resetStartIndex: function () {
  145. if (this.get('controller.resetStartIndex') && this.get('filteredCount') > 0) {
  146. this.set('startIndex', 1);
  147. }
  148. }.observes('controller.resetStartIndex')
  149. });