Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // +---------------------------------------------------------------------------+
- // | This file is part of a Revolution Linux project. |
- // | Copyright © 2007 Revolution Linux Inc. |
- // | |
- // | For the full copyright and license information, please view the LICENSE |
- // | file that was distributed with this source code. |
- // +---------------------------------------------------------------------------+
- /**
- * RSorter is a class used to sort a given list of data using what I think is
- * the selection sort. I have not yet tested it on very large list but right now
- * it's quite efficient.
- * @package revolution linux
- * @subpackage data
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @copyright Revolution Linux
- * @since 1.0.0
- * @version 1.0.0
- */
- var RSorter = RBase.extend(
- {
- /**
- * @var object The selector used to find a single row of data.
- */
- rowSelector : '',
- /**
- * @var array The sub selector used to find each sortable fields.
- */
- txtSelector : null,
- /**
- * Constructor. Set the row and text selectors and the options. The text
- * selectors are not mandatory and they can be added manually using the
- * set column method.
- * @param string The row selector.
- * @param object The options.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- initialize : function(rowSelector, options) {
- this.rowSelector = rowSelector;
- this.txtSelector = new Array();
- this.options = Object.extend({
- onBegin : null,
- onComplete : null
- }, options || {});
- return this;
- },
- /**
- * Make a column sortable using the sub selector and an identifier called
- * a criteria which will be used in the future to start the sorting.
- * @param string The criteria name.
- * @param string The column selector.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- setColumn : function(criteria, selector) {
- this.txtSelector[criteria] = {selector : selector, mode : 'asc'};
- },
- /**
- * Return a column data if existent.
- * @param string The criteria name.
- * @return object The column selector infos.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- getColumn : function(criteria) {
- return this.txtSelector[criteria] !== undefined ? this.txtSelector[criteria] : null;
- },
- /**
- * Sort the list using a given criteria and specify a mode to sort which can
- * be asc, desc or toggle which switch the last order.
- * @param string The criteria name.
- * @param bool True to order the list in descending mode.
- * @return void
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- sort : function(criteria, mode) {
- var column = this.txtSelector[criteria];
- if (column == null) {
- // somehow we are trying to sort by a non existent field so we stop
- // right here instead of throwing an error
- return;
- }
- mode = mode === undefined ? 'asc' : mode;
- switch (mode.toLowerCase()) {
- case 'asc' : column.mode = 'asc'; break;
- case 'desc' : column.mode = 'desc'; break;
- case 'toggle' : column.mode = column.mode == 'asc' ? 'desc' : 'asc'; break;
- }
- var texts = [];
- var elems = [];
- // the first step consists in creating an array which contains only the
- // texts and the elements so we can work directly on this array
- $ES(this.rowSelector).each(function(elem) {
- elems.push(elem);
- texts.push(elem.getElements(column.selector)[0].getText());
- }.bind(this));
- // now we sort the list and put this list into a new array
- for (var i = 0; i < elems.length; i++) {
- for (var j = i; j < elems.length; j++) {
- if (this.compare(texts[i], texts[j], column.mode == 'desc') == false) {
- texts = this.swap(texts, i, j);
- elems = this.swap(elems, i, j);
- }
- }
- }
- // now all we have to do is rebuild the tree and the list will be
- // correctly sorted
- for (var i = 0; i < elems.length; i++) {
- elems[i].injectAfter(i == 0 ? elems[0] : elems[i - 1]);
- }
- },
- /**
- * Compare two text value and indicate if the first value is bigger than
- * the second. The result is inverted if desc is true.
- * @param string The first value.
- * @param string The second value.
- * @param bool True for descending order.
- * @return bool True if the first value is bigger than the second.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- compare : function(v1, v2, desc) {
- return (desc) ? v1 > v2 : v1 < v2;
- },
- /**
- * Switch two elements inside an array and return the new array.
- * @param array The array.
- * @param int The first index.
- * @param int The second index.
- * @return array The new array.
- * @author Jean-Philippe Dery (jeanphilippe.dery@revolutionlinux.com)
- * @since 1.0.0
- */
- swap : function(arr, i, j) {
- var t = arr[j];
- arr[j] = arr[i];
- arr[i] = t;
- return arr;
- }
- });
- // ----------- Example
- var sorter = null;
- new RSelector().start({
- '#ambulance-index' : function() {
- // load the sorter object on the list of ambulances and allow a
- // sort by vehicle number, model and permit number
- sorter = new RSorter('#ambulance-index .cards .card');
- sorter.setColumn('vehicle', 'span.vehicle');
- sorter.setColumn('model', 'span.model');
- sorter.setColumn('permit', 'span.permit');
- },
- '#ambulance-index #fe-sort-vehicle:click' : function() { sorter.sort('vehicle') },
- '#ambulance-index #fe-sort-model:click' : function() { sorter.sort('model') },
- '#ambulance-index #fe-sort-permit:click' : function() { sorter.sort('permit') },
- });
Add Comment
Please, Sign In to add comment