sort_view.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 stringUtils = require('utils/string_utils');
  20. var App = require('app');
  21. /**
  22. * Wrapper View for all sort components. Layout template and common actions are located inside of it.
  23. * Logic specific for sort fields
  24. * located in inner view - <code>fieldView</code>.
  25. *
  26. * @type {*}
  27. */
  28. var wrapperView = Em.View.extend({
  29. tagName: 'tr',
  30. classNames: ['sort-wrapper'],
  31. willInsertElement: function () {
  32. if (this.get('parentView.tableFilteringComplete')) {
  33. this.get('parentView').set('filteringComplete', true);
  34. }
  35. },
  36. /**
  37. * Load sort statuses from local storage
  38. * Works only after finish filtering in the parent View
  39. */
  40. loadSortStatuses: function () {
  41. var statuses = App.db.getSortingStatuses(this.get('controller.name'));
  42. if (!this.get('parentView.filteringComplete')) return;
  43. if (statuses) {
  44. var childViews = this.get('childViews');
  45. var self = this;
  46. statuses.forEach(function (st) {
  47. if (st.status != 'sorting') {
  48. var sortOrder = false;
  49. if (st.status == 'sorting_desc') {
  50. sortOrder = true;
  51. }
  52. self.sort(childViews.findProperty('name', st.name), sortOrder);
  53. childViews.findProperty('name', st.name).set('status', (sortOrder) ? 'sorting_desc' : 'sorting_asc');
  54. self.get('controller').set('sortingColumn', childViews.findProperty('name', st.name));
  55. }
  56. else {
  57. childViews.findProperty('name', st.name).set('status', st.status);
  58. }
  59. });
  60. }
  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. },
  77. /**
  78. * sort content by property
  79. * @param property {object}
  80. * @param order {Boolean} true - DESC, false - ASC
  81. * @param returnSorted {Boolean}
  82. */
  83. sort: function (property, order, returnSorted) {
  84. var content = this.get('content').toArray();
  85. var sortFunc = this.getSortFunc(property, order);
  86. var status = order ? 'sorting_desc' : 'sorting_asc';
  87. this.resetSort();
  88. this.get('childViews').findProperty('name', property.get('name')).set('status', status);
  89. this.saveSortStatuses(property, order);
  90. content.sort(sortFunc);
  91. if (!!returnSorted) {
  92. return content;
  93. } else {
  94. this.set('content', content);
  95. }
  96. },
  97. isSorting: false,
  98. onContentChange: function () {
  99. if (!this.get('isSorting') && this.get('content.length')) {
  100. this.get('childViews').forEach(function (view) {
  101. if (view.status !== 'sorting') {
  102. var status = view.get('status');
  103. this.set('isSorting', true);
  104. this.sort(view, status == 'sorting_desc');
  105. this.set('isSorting', false);
  106. view.set('status', status);
  107. }
  108. }, this);
  109. }
  110. }.observes('content.length'),
  111. /**
  112. * reset all sorts fields
  113. */
  114. resetSort: function () {
  115. this.get('childViews').setEach('status', 'sorting');
  116. },
  117. /**
  118. * determines sort function depending on the type of sort field
  119. * @param property
  120. * @param order
  121. * @return {*}
  122. */
  123. getSortFunc: function (property, order) {
  124. var func;
  125. switch (property.get('type')) {
  126. case 'ip':
  127. func = function (a, b) {
  128. var a = misc.ipToInt(a.get(property.get('name')));
  129. var b = misc.ipToInt(b.get(property.get('name')));
  130. if (order) {
  131. return a - b;
  132. } else {
  133. return b - a;
  134. }
  135. };
  136. break;
  137. case 'number':
  138. func = function (a, b) {
  139. var a_p = a.get(property.get('name'));
  140. var b_p = b.get(property.get('name'));
  141. a_p = Em.isNone(a_p) ? -Infinity : parseFloat(a_p);
  142. b_p = Em.isNone(b_p) ? -Infinity : parseFloat(b_p);
  143. if (order) {
  144. return a_p - b_p;
  145. } else {
  146. return b_p - a_p
  147. }
  148. };
  149. break;
  150. case 'version':
  151. func = function (a, b) {
  152. var res = stringUtils.compareVersions(a.get(property.get('name')), b.get(property.get('name')));
  153. if (order) {
  154. return -res;
  155. } else {
  156. return res;
  157. }
  158. };
  159. break;
  160. case 'alert_status':
  161. func = App.AlertDefinition.getSortDefinitionsByStatus(order);
  162. break;
  163. default:
  164. func = function (a, b) {
  165. var a_p = a.get(property.get('name'));
  166. var b_p = b.get(property.get('name'));
  167. a_p = Em.isNone(a_p) ? '' : '' + a_p;
  168. b_p = Em.isNone(b_p) ? '' : '' + b_p;
  169. return order ? b_p.localeCompare(a_p) : a_p.localeCompare(b_p);
  170. };
  171. }
  172. return func;
  173. }
  174. });
  175. /**
  176. * view that carry on sorting on server-side via <code>refresh()</code> in parentView
  177. * @type {*}
  178. */
  179. var serverWrapperView = Em.View.extend({
  180. tagName: 'tr',
  181. classNames: ['sort-wrapper'],
  182. willInsertElement: function () {
  183. this.loadSortStatuses();
  184. },
  185. /**
  186. * Initialize and save sorting statuses: hostName sorting_asc
  187. */
  188. loadSortStatuses: function () {
  189. var statuses = [];
  190. var childViews = this.get('childViews');
  191. childViews.forEach(function (childView) {
  192. var sortStatus = (childView.get('name') == 'hostName' && childView.get('status') == 'sorting') ? 'sorting_asc' : childView.get('status');
  193. statuses.push({
  194. name: childView.get('name'),
  195. status: sortStatus
  196. });
  197. childView.set('status', sortStatus);
  198. });
  199. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  200. this.get('controller').set('sortingColumn', childViews.findProperty('name', 'hostName'));
  201. },
  202. /**
  203. * Save sort statuses to local storage
  204. * Works only after finish filtering in the parent View
  205. */
  206. saveSortStatuses: function () {
  207. var statuses = [];
  208. this.get('childViews').forEach(function (childView) {
  209. statuses.push({
  210. name: childView.get('name'),
  211. status: childView.get('status')
  212. });
  213. });
  214. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  215. },
  216. /**
  217. * sort content by property
  218. * @param property {object}
  219. * @param order {Boolean} true - DESC, false - ASC
  220. */
  221. sort: function (property, order) {
  222. var status = order ? 'sorting_desc' : 'sorting_asc';
  223. this.resetSort();
  224. this.get('childViews').findProperty('name', property.get('name')).set('status', status);
  225. this.saveSortStatuses();
  226. this.get('parentView').refresh();
  227. },
  228. /**
  229. * reset all sorts fields
  230. */
  231. resetSort: function () {
  232. this.get('childViews').setEach('status', 'sorting');
  233. }
  234. });
  235. /**
  236. * particular view that contain sort field properties:
  237. * name - name of property in content table
  238. * type(optional) - specific type to sort
  239. * displayName - label to display
  240. * @type {*}
  241. */
  242. var fieldView = Em.View.extend({
  243. template: Em.Handlebars.compile('<span {{bindAttr class="view.status :column-name"}}>{{view.displayName}}</span>'),
  244. classNameBindings: ['viewNameClass'],
  245. tagName: 'th',
  246. name: null,
  247. displayName: null,
  248. status: 'sorting',
  249. viewNameClass: function () {
  250. return 'sort-view-' + this.get('column');
  251. }.property(),
  252. type: null,
  253. column: 0,
  254. /**
  255. * callback that run sorting and define order of sorting
  256. * @param event
  257. */
  258. click: function (event) {
  259. this.get('parentView').sort(this, (this.get('status') !== 'sorting_desc'));
  260. this.get('controller').set('sortingColumn', this);
  261. }
  262. });
  263. /**
  264. * Result object, which will be accessible outside
  265. * @type {Object}
  266. */
  267. module.exports = {
  268. serverWrapperView: serverWrapperView,
  269. wrapperView: wrapperView,
  270. fieldView: fieldView
  271. };