sort_view.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 misc = require('utils/misc');
  19. var App = require('app');
  20. /**
  21. * Wrapper View for all sort components. Layout template and common actions are located inside of it.
  22. * Logic specific for sort fields
  23. * located in inner view - <code>fieldView</code>.
  24. *
  25. * @type {*}
  26. */
  27. var wrapperView = Em.View.extend({
  28. tagName: 'tr',
  29. /**
  30. * Load sort statuses from local storage
  31. * Works only after finish filtering in the parent View
  32. */
  33. loadSortStatuses: function() {
  34. var statuses = App.db.getSortingStatuses(this.get('controller.name'));
  35. if (!this.get('parentView.filteringComplete')) return;
  36. if (statuses) {
  37. var childViews = this.get('childViews');
  38. var self = this;
  39. statuses.forEach(function(st) {
  40. if (st.status != 'sorting') {
  41. var sortOrder = false;
  42. if(st.status == 'sorting_desc') {
  43. sortOrder = true;
  44. }
  45. self.sort(childViews.findProperty('name', st.name), sortOrder);
  46. childViews.findProperty('name', st.name).set('status', (sortOrder)?'sorting_desc':'sorting_asc');
  47. }
  48. else {
  49. childViews.findProperty('name', st.name).set('status', st.status);
  50. }
  51. });
  52. }
  53. this.get('parentView').showProperPage();
  54. }.observes('parentView.filteringComplete'),
  55. /**
  56. * Save sort statuses to local storage
  57. * Works only after finish filtering in the parent View
  58. */
  59. saveSortStatuses: function() {
  60. if (!this.get('parentView.filteringComplete')) return;
  61. var statuses = [];
  62. this.get('childViews').forEach(function(childView) {
  63. statuses.push({
  64. name: childView.get('name'),
  65. status: childView.get('status')
  66. });
  67. });
  68. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  69. }.observes('childViews.@each.status'),
  70. /**
  71. * sort content by property
  72. * @param property
  73. * @param order: true - DESC, false - ASC
  74. */
  75. sort: function(property, order){
  76. var content = this.get('content').toArray();
  77. var sortFunc = this.getSortFunc(property, order);
  78. this.resetSort();
  79. content.sort(sortFunc);
  80. this.set('content', content);
  81. },
  82. /**
  83. * reset all sorts fields
  84. */
  85. resetSort: function(){
  86. this.get('childViews').setEach('status', 'sorting');
  87. },
  88. /**
  89. * determines sort function depending on the type of sort field
  90. * @param property
  91. * @param order
  92. * @return {*}
  93. */
  94. getSortFunc: function(property, order){
  95. var func;
  96. switch (property.get('type')){
  97. case 'ip':
  98. func = function (a, b) {
  99. var a = misc.ipToInt(a.get(property.get('name')));
  100. var b = misc.ipToInt(b.get(property.get('name')));
  101. if(order){
  102. return b - a;
  103. } else {
  104. return a - b;
  105. }
  106. };
  107. break;
  108. case 'number':
  109. func = function (a, b) {
  110. var a = parseFloat(a.get(property.get('name')));
  111. var b = parseFloat(b.get(property.get('name')));
  112. if(order){
  113. return b - a;
  114. } else {
  115. return a - b;
  116. }
  117. }
  118. break;
  119. default:
  120. func = function(a,b){
  121. if(order){
  122. if (a.get(property.get('name')) > b.get(property.get('name')))
  123. return -1;
  124. if (a.get(property.get('name')) < b.get(property.get('name')))
  125. return 1;
  126. return 0;
  127. } else {
  128. if (a.get(property.get('name')) < b.get(property.get('name')))
  129. return -1;
  130. if (a.get(property.get('name')) > b.get(property.get('name')))
  131. return 1;
  132. return 0;
  133. }
  134. }
  135. }
  136. return func;
  137. }
  138. });
  139. /**
  140. * particular view that contain sort field properties:
  141. * name - name of property in content table
  142. * type(optional) - specific type to sort
  143. * displayName - label to display
  144. * @type {*}
  145. */
  146. var fieldView = Em.View.extend({
  147. template:Em.Handlebars.compile('{{view.displayName}}'),
  148. classNameBindings: ['status'],
  149. tagName: 'th',
  150. name: null,
  151. displayName: null,
  152. status: 'sorting',
  153. type: null,
  154. /**
  155. * callback that run sorting and define order of sorting
  156. * @param event
  157. */
  158. click: function(event){
  159. if(this.get('status') === 'sorting_desc'){
  160. this.get('parentView').sort(this, false);
  161. this.set('status', 'sorting_asc');
  162. }
  163. else {
  164. this.get('parentView').sort(this, true);
  165. this.set('status', 'sorting_desc');
  166. }
  167. }
  168. });
  169. /**
  170. * Result object, which will be accessible outside
  171. * @type {Object}
  172. */
  173. module.exports = {
  174. wrapperView: wrapperView,
  175. fieldView: fieldView
  176. };