table_server_view_mixin.js 3.8 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. /**
  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: function () {
  33. return this.get('controller.filteredCount');
  34. }.property('controller.filteredCount'),
  35. /**
  36. * total count of items
  37. */
  38. totalCount: function () {
  39. return this.get('controller.totalCount');
  40. }.property('controller.totalCount'),
  41. /**
  42. * data requested from server
  43. */
  44. content: function () {
  45. return this.get('controller.content');
  46. }.property('controller.content'),
  47. /**
  48. * content already filtered on server-side
  49. */
  50. filteredContent: function () {
  51. return this.get('content');
  52. }.property('content'),
  53. /**
  54. * sort and slice recieved content by pagination parameters
  55. */
  56. pageContent: function () {
  57. var content = this.get('filteredContent');
  58. if (content.length > ((this.get('endIndex') - this.get('startIndex')) + 1)) {
  59. content = content.slice(0, (this.get('endIndex') - this.get('startIndex')) + 1);
  60. }
  61. return content.sort(function (a, b) {
  62. return a.get('index') - b.get('index');
  63. });
  64. }.property('filteredContent'),
  65. /**
  66. * compute applied filters and run content update from server
  67. * @param iColumn
  68. * @param value
  69. * @param type
  70. */
  71. updateFilter: function (iColumn, value, type) {
  72. var self = this;
  73. this.set('controller.resetStartIndex', false);
  74. this.saveFilterConditions(iColumn, value, type, false);
  75. if (!this.get('filteringComplete')) {
  76. clearTimeout(this.get('timeOut'));
  77. this.set('timeOut', setTimeout(function () {
  78. self.updateFilter(iColumn, value, type);
  79. }, this.get('filterWaitingTime')));
  80. } else {
  81. clearTimeout(this.get('timeOut'));
  82. this.set('controller.resetStartIndex', true);
  83. this.refresh();
  84. }
  85. },
  86. /**
  87. * synchronize properties of view with controller to generate query parameters
  88. */
  89. updatePagination: function (key) {
  90. if (!Em.isNone(this.get('displayLength'))) {
  91. App.db.setDisplayLength(this.get('controller.name'), this.get('displayLength'));
  92. this.get('controller.paginationProps').findProperty('name', 'displayLength').value = this.get('displayLength');
  93. }
  94. if (!Em.isNone(this.get('startIndex'))) {
  95. App.db.setStartIndex(this.get('controller.name'), this.get('startIndex'));
  96. this.get('controller.paginationProps').findProperty('name', 'startIndex').value = this.get('startIndex');
  97. }
  98. if (key !== 'SKIP_REFRESH') {
  99. this.refresh();
  100. }
  101. },
  102. /**
  103. * when new filter applied page index should be reset to first page
  104. */
  105. resetStartIndex: function () {
  106. if (this.get('controller.resetStartIndex') && this.get('filteredCount') > 0) {
  107. this.set('startIndex', 1);
  108. }
  109. }.observes('controller.resetStartIndex')
  110. });