sort_view.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. classNames: ['sort-wrapper'],
  30. willInsertElement:function () {
  31. if(this.get('parentView.tableFilteringComplete')){
  32. this.get('parentView').set('filteringComplete', true);
  33. }
  34. },
  35. /**
  36. * Load sort statuses from local storage
  37. * Works only after finish filtering in the parent View
  38. */
  39. loadSortStatuses: function() {
  40. var statuses = App.db.getSortingStatuses(this.get('controller.name'));
  41. if (!this.get('parentView.filteringComplete')) return;
  42. if (statuses) {
  43. var childViews = this.get('childViews');
  44. var self = this;
  45. statuses.forEach(function(st) {
  46. if (st.status != 'sorting') {
  47. var sortOrder = false;
  48. if(st.status == 'sorting_desc') {
  49. sortOrder = true;
  50. }
  51. self.sort(childViews.findProperty('name', st.name), sortOrder);
  52. childViews.findProperty('name', st.name).set('status', (sortOrder)?'sorting_desc':'sorting_asc');
  53. self.get('controller').set('sortingColumn', childViews.findProperty('name', st.name));
  54. }
  55. else {
  56. childViews.findProperty('name', st.name).set('status', st.status);
  57. }
  58. });
  59. }
  60. this.get('parentView').showProperPage();
  61. }.observes('parentView.filteringComplete'),
  62. /**
  63. * Save sort statuses to local storage
  64. * Works only after finish filtering in the parent View
  65. */
  66. saveSortStatuses: function() {
  67. if (!this.get('parentView.filteringComplete')) return;
  68. var statuses = [];
  69. this.get('childViews').forEach(function(childView) {
  70. statuses.push({
  71. name: childView.get('name'),
  72. status: childView.get('status')
  73. });
  74. });
  75. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  76. }.observes('childViews.@each.status'),
  77. /**
  78. * sort content by property
  79. * @param property
  80. * @param order true - DESC, false - ASC
  81. */
  82. sort: function(property, order, returnSorted){
  83. returnSorted = returnSorted ? true : false;
  84. var content = this.get('content').toArray();
  85. var sortFunc = this.getSortFunc(property, order);
  86. this.resetSort();
  87. content.sort(sortFunc);
  88. if(returnSorted){
  89. return content;
  90. }else{
  91. this.set('content', content);
  92. }
  93. },
  94. isSorting: false,
  95. onContentChange: function () {
  96. if (!this.get('isSorting') && this.get('content.length')) {
  97. this.get('childViews').forEach(function (view) {
  98. if (view.status !== 'sorting') {
  99. var status = view.get('status');
  100. this.set('isSorting', true);
  101. this.sort(view, status == 'sorting_desc');
  102. this.set('isSorting', false);
  103. view.set('status', status);
  104. }
  105. }, this);
  106. }
  107. }.observes('content.length'),
  108. /**
  109. * reset all sorts fields
  110. */
  111. resetSort: function(){
  112. this.get('childViews').setEach('status', 'sorting');
  113. },
  114. /**
  115. * determines sort function depending on the type of sort field
  116. * @param property
  117. * @param order
  118. * @return {*}
  119. */
  120. getSortFunc: function(property, order){
  121. var func;
  122. switch (property.get('type')){
  123. case 'ip':
  124. func = function (a, b) {
  125. var a = misc.ipToInt(a.get(property.get('name')));
  126. var b = misc.ipToInt(b.get(property.get('name')));
  127. if(order){
  128. return b - a;
  129. } else {
  130. return a - b;
  131. }
  132. };
  133. break;
  134. case 'number':
  135. func = function (a, b) {
  136. var a = parseFloat(a.get(property.get('name')));
  137. var b = parseFloat(b.get(property.get('name')));
  138. if(order){
  139. return b - a;
  140. } else {
  141. return a - b;
  142. }
  143. };
  144. break;
  145. default:
  146. func = function(a,b){
  147. if(order){
  148. if (a.get(property.get('name')) > b.get(property.get('name')))
  149. return -1;
  150. if (a.get(property.get('name')) < b.get(property.get('name')))
  151. return 1;
  152. return 0;
  153. } else {
  154. if (a.get(property.get('name')) < b.get(property.get('name')))
  155. return -1;
  156. if (a.get(property.get('name')) > b.get(property.get('name')))
  157. return 1;
  158. return 0;
  159. }
  160. }
  161. }
  162. return func;
  163. }
  164. });
  165. /**
  166. * particular view that contain sort field properties:
  167. * name - name of property in content table
  168. * type(optional) - specific type to sort
  169. * displayName - label to display
  170. * @type {*}
  171. */
  172. var fieldView = Em.View.extend({
  173. template:Em.Handlebars.compile('<span {{bindAttr class="view.status :column-name"}}>{{view.displayName}}</span>'),
  174. classNameBindings: ['viewNameClass'],
  175. tagName: 'th',
  176. name: null,
  177. displayName: null,
  178. status: 'sorting',
  179. viewNameClass: function () {
  180. return 'sort-view-' + this.get('column');
  181. }.property(),
  182. type: null,
  183. column: 0,
  184. /**
  185. * callback that run sorting and define order of sorting
  186. * @param event
  187. */
  188. click: function(event){
  189. if(this.get('status') === 'sorting_desc'){
  190. this.get('parentView').sort(this, false);
  191. this.set('status', 'sorting_asc');
  192. }
  193. else {
  194. this.get('parentView').sort(this, true);
  195. this.set('status', 'sorting_desc');
  196. }
  197. this.get('controller').set('sortingColumn', this);
  198. }
  199. });
  200. /**
  201. * Result object, which will be accessible outside
  202. * @type {Object}
  203. */
  204. module.exports = {
  205. wrapperView: wrapperView,
  206. fieldView: fieldView
  207. };