Guest User

Untitled

a guest
Mar 9th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. // +---------------------------------------------------------------------------+
  2. // | This file is part of a Revolution Linux project. |
  3. // | Copyright © 2007 Revolution Linux Inc. |
  4. // | |
  5. // | For the full copyright and license information, please view the LICENSE |
  6. // | file that was distributed with this source code. |
  7. // +---------------------------------------------------------------------------+
  8.  
  9. /**
  10. * RSorter is a class used to sort a given list of data using what I think is
  11. * the selection sort. I have not yet tested it on very large list but right now
  12. * it's quite efficient.
  13. * @package revolution linux
  14. * @subpackage data
  15. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  16. * @copyright Revolution Linux
  17. * @since 1.0.0
  18. * @version 1.0.0
  19. */
  20. var RSorter = RBase.extend(
  21. {
  22. /**
  23. * @var object The selector used to find a single row of data.
  24. */
  25. rowSelector : '',
  26.  
  27. /**
  28. * @var array The sub selector used to find each sortable fields.
  29. */
  30. txtSelector : null,
  31.  
  32. /**
  33. * Constructor. Set the row and text selectors and the options. The text
  34. * selectors are not mandatory and they can be added manually using the
  35. * set column method.
  36. * @param string The row selector.
  37. * @param object The options.
  38. * @return void
  39. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  40. * @since 1.0.0
  41. */
  42. initialize : function(rowSelector, options) {
  43. this.rowSelector = rowSelector;
  44. this.txtSelector = new Array();
  45. this.options = Object.extend({
  46. onBegin : null,
  47. onComplete : null
  48. }, options || {});
  49. return this;
  50. },
  51.  
  52. /**
  53. * Make a column sortable using the sub selector and an identifier called
  54. * a criteria which will be used in the future to start the sorting.
  55. * @param string The criteria name.
  56. * @param string The column selector.
  57. * @return void
  58. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  59. * @since 1.0.0
  60. */
  61. setColumn : function(criteria, selector) {
  62. this.txtSelector[criteria] = {selector : selector, mode : 'asc'};
  63. },
  64.  
  65. /**
  66. * Return a column data if existent.
  67. * @param string The criteria name.
  68. * @return object The column selector infos.
  69. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  70. * @since 1.0.0
  71. */
  72. getColumn : function(criteria) {
  73. return this.txtSelector[criteria] !== undefined ? this.txtSelector[criteria] : null;
  74. },
  75.  
  76. /**
  77. * Sort the list using a given criteria and specify a mode to sort which can
  78. * be asc, desc or toggle which switch the last order.
  79. * @param string The criteria name.
  80. * @param bool True to order the list in descending mode.
  81. * @return void
  82. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  83. * @since 1.0.0
  84. */
  85. sort : function(criteria, mode) {
  86. var column = this.txtSelector[criteria];
  87. if (column == null) {
  88. // somehow we are trying to sort by a non existent field so we stop
  89. // right here instead of throwing an error
  90. return;
  91. }
  92. mode = mode === undefined ? 'asc' : mode;
  93. switch (mode.toLowerCase()) {
  94. case 'asc' : column.mode = 'asc'; break;
  95. case 'desc' : column.mode = 'desc'; break;
  96. case 'toggle' : column.mode = column.mode == 'asc' ? 'desc' : 'asc'; break;
  97. }
  98.  
  99. var texts = [];
  100. var elems = [];
  101. // the first step consists in creating an array which contains only the
  102. // texts and the elements so we can work directly on this array
  103. $ES(this.rowSelector).each(function(elem) {
  104. elems.push(elem);
  105. texts.push(elem.getElements(column.selector)[0].getText());
  106. }.bind(this));
  107. // now we sort the list and put this list into a new array
  108. for (var i = 0; i < elems.length; i++) {
  109. for (var j = i; j < elems.length; j++) {
  110. if (this.compare(texts[i], texts[j], column.mode == 'desc') == false) {
  111. texts = this.swap(texts, i, j);
  112. elems = this.swap(elems, i, j);
  113. }
  114. }
  115. }
  116. // now all we have to do is rebuild the tree and the list will be
  117. // correctly sorted
  118. for (var i = 0; i < elems.length; i++) {
  119. elems[i].injectAfter(i == 0 ? elems[0] : elems[i - 1]);
  120. }
  121. },
  122.  
  123. /**
  124. * Compare two text value and indicate if the first value is bigger than
  125. * the second. The result is inverted if desc is true.
  126. * @param string The first value.
  127. * @param string The second value.
  128. * @param bool True for descending order.
  129. * @return bool True if the first value is bigger than the second.
  130. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  131. * @since 1.0.0
  132. */
  133. compare : function(v1, v2, desc) {
  134. return (desc) ? v1 > v2 : v1 < v2;
  135. },
  136.  
  137. /**
  138. * Switch two elements inside an array and return the new array.
  139. * @param array The array.
  140. * @param int The first index.
  141. * @param int The second index.
  142. * @return array The new array.
  143. * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
  144. * @since 1.0.0
  145. */
  146. swap : function(arr, i, j) {
  147. var t = arr[j];
  148. arr[j] = arr[i];
  149. arr[i] = t;
  150. return arr;
  151. }
  152. });
  153.  
  154. // ----------- Example
  155.  
  156. var sorter = null;
  157. new RSelector().start({
  158. '#ambulance-index' : function() {
  159. // load the sorter object on the list of ambulances and allow a
  160. // sort by vehicle number, model and permit number
  161. sorter = new RSorter('#ambulance-index .cards .card');
  162. sorter.setColumn('vehicle', 'span.vehicle');
  163. sorter.setColumn('model', 'span.model');
  164. sorter.setColumn('permit', 'span.permit');
  165. },
  166. '#ambulance-index #fe-sort-vehicle:click' : function() { sorter.sort('vehicle') },
  167. '#ambulance-index #fe-sort-model:click' : function() { sorter.sort('model') },
  168. '#ambulance-index #fe-sort-permit:click' : function() { sorter.sort('permit') },
  169.  
  170. });
Add Comment
Please, Sign In to add comment