sort_view.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 b - a;
  132. } else {
  133. return a - b;
  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 b_p - a_p;
  145. } else {
  146. return a_p - b_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 = function (a, b) {
  162. var a_summary = a.get('summary'),
  163. b_summary = b.get('summary'),
  164. st_order = a.get('severityOrder'),
  165. ret = 0;
  166. for (var i = 0; i < st_order.length; i++) {
  167. var a_v = Em.isNone(a_summary[st_order[i]]) ? 0 : a_summary[st_order[i]],
  168. b_v = Em.isNone(b_summary[st_order[i]]) ? 0 : b_summary[st_order[i]];
  169. ret = b_v - a_v;
  170. if (ret !== 0) {
  171. break;
  172. }
  173. }
  174. if (order) {
  175. return ret;
  176. }
  177. else {
  178. return -ret;
  179. }
  180. };
  181. break;
  182. default:
  183. func = function (a, b) {
  184. var a_p = a.get(property.get('name'));
  185. var b_p = b.get(property.get('name'));
  186. a_p = Em.isNone(a_p) ? '' : '' + a_p;
  187. b_p = Em.isNone(b_p) ? '' : '' + b_p;
  188. return order ? a_p.localeCompare(b_p) : b_p.localeCompare(a_p)
  189. };
  190. }
  191. return func;
  192. }
  193. });
  194. /**
  195. * view that carry on sorting on server-side via <code>refresh()</code> in parentView
  196. * @type {*}
  197. */
  198. var serverWrapperView = Em.View.extend({
  199. tagName: 'tr',
  200. classNames: ['sort-wrapper'],
  201. willInsertElement: function () {
  202. this.loadSortStatuses();
  203. },
  204. /**
  205. * Initialize and save sorting statuses: publicHostName sorting_asc
  206. */
  207. loadSortStatuses: function () {
  208. var statuses = [];
  209. var childViews = this.get('childViews');
  210. childViews.forEach(function (childView) {
  211. var sortStatus = (childView.get('name') == 'publicHostName' && childView.get('status') == 'sorting') ? 'sorting_asc' : childView.get('status');
  212. statuses.push({
  213. name: childView.get('name'),
  214. status: sortStatus
  215. });
  216. childView.set('status', sortStatus);
  217. });
  218. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  219. this.get('controller').set('sortingColumn', childViews.findProperty('name', 'publicHostName'));
  220. },
  221. /**
  222. * Save sort statuses to local storage
  223. * Works only after finish filtering in the parent View
  224. */
  225. saveSortStatuses: function () {
  226. var statuses = [];
  227. this.get('childViews').forEach(function (childView) {
  228. statuses.push({
  229. name: childView.get('name'),
  230. status: childView.get('status')
  231. });
  232. });
  233. App.db.setSortingStatuses(this.get('controller.name'), statuses);
  234. },
  235. /**
  236. * sort content by property
  237. * @param property {object}
  238. * @param order {Boolean} true - DESC, false - ASC
  239. */
  240. sort: function (property, order) {
  241. var status = order ? 'sorting_desc' : 'sorting_asc';
  242. this.resetSort();
  243. this.get('childViews').findProperty('name', property.get('name')).set('status', status);
  244. this.saveSortStatuses();
  245. this.get('parentView').refresh();
  246. },
  247. /**
  248. * reset all sorts fields
  249. */
  250. resetSort: function () {
  251. this.get('childViews').setEach('status', 'sorting');
  252. }
  253. });
  254. /**
  255. * particular view that contain sort field properties:
  256. * name - name of property in content table
  257. * type(optional) - specific type to sort
  258. * displayName - label to display
  259. * @type {*}
  260. */
  261. var fieldView = Em.View.extend({
  262. template: Em.Handlebars.compile('<span {{bindAttr class="view.status :column-name"}}>{{view.displayName}}</span>'),
  263. classNameBindings: ['viewNameClass'],
  264. tagName: 'th',
  265. name: null,
  266. displayName: null,
  267. status: 'sorting',
  268. viewNameClass: function () {
  269. return 'sort-view-' + this.get('column');
  270. }.property(),
  271. type: null,
  272. column: 0,
  273. /**
  274. * callback that run sorting and define order of sorting
  275. * @param event
  276. */
  277. click: function (event) {
  278. this.get('parentView').sort(this, (this.get('status') !== 'sorting_desc'));
  279. this.get('controller').set('sortingColumn', this);
  280. }
  281. });
  282. /**
  283. * Result object, which will be accessible outside
  284. * @type {Object}
  285. */
  286. module.exports = {
  287. serverWrapperView: serverWrapperView,
  288. wrapperView: wrapperView,
  289. fieldView: fieldView
  290. };