Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- SortTable
- version 2
- 7th April 2007
- Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
- Instructions:
- Download this file
- Add <script src="sorttable.js"></script> to your HTML
- Add class="sortable" to any table you'd like to make sortable
- Click on the headers to sort
- Thanks to many, many people for contributions and suggestions.
- Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
- This basically means: do what you want with it.
- */
- var stIsIE = /*@cc_on!@*/ false;
- sorttable = {
- init: function() {
- // quit if this function has already been called
- if (arguments.callee.done) return;
- // flag this function so we don't do the same thing twice
- arguments.callee.done = true;
- // kill the timer
- if (_timer) clearInterval(_timer);
- if (!document.createElement || !document.getElementsByTagName) return;
- sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
- forEach(document.getElementsByTagName('table'), function(table) {
- if (table.className.search(/\bsortable\b/) != -1) {
- sorttable.makeSortable(table);
- }
- });
- },
- makeSortable: function(table) {
- if (typeof(table) === 'undefined') {
- return false;
- }
- var jTable, jTableRows, jTableChild, jDocument = false;
- jTable = jQuery(table).not(".sortable_nosort");
- jDocument = jQuery(document);
- // Do not provide sorttable support, for this table
- if (!jTable.length || jTable.hasClass('sortable_nosort')) {
- return false;
- };
- if (jTable.hasClass('stacktable-result')) {
- return false;
- }
- // Update the CHILD TABLE with "sortable_nosort"
- if (jTableChild = jTable.find('table.sortable_nosort')) {
- jTableChild.find('tr').addClass('sorttable_row_nosort');
- jTableChild.parents("td").addClass('sorttable_cell_nosort');
- // jTableChild.parents("td").parent('tr').addClass('sorttable_row_nosort');
- };
- jTableRows = jTable.find('tr:not(.sorttable_row_nosort)');
- if (!jTable.find('thead').length) {
- var the = false;
- // table doesn't have a tHead. Since it should have, create one and
- // put the first table row in it.
- the = jQuery("<thead></thead>");
- the.append(jTableRows);
- jTable.insertBefore(the, jTable.children().first());
- };
- // Update the Table VARIABLE
- table = jTable.get(0);
- // Safari doesn't support table.tHead, sigh
- if (table.tHead == null) table.tHead = jTable.find('thead')[0];
- // can't cope with two header rows
- if (table.tHead.rows.length != 1) return false;
- // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
- // "total" rows, for example). This is B&R, since what you're supposed
- // to do is put them in a tfoot. So, if there are sortbottom rows,
- // for backwards compatibility, move them to tfoot (creating it if needed).
- sortbottomrows = [];
- var table_rows = jQuery.makeArray(jTableRows);
- for (var i = 0; i < table_rows.length; i++) {
- var table_row_item = jQuery(table_rows[i]);
- if (table_row_item.prop('class').search(/\bsortbottom\b/) != -1) {
- sortbottomrows[sortbottomrows.length] = table_row_item;
- };
- };
- if (sortbottomrows) {
- if (table.tFoot == null) {
- var tfo = jQuery("<tfoot></tfoot>");
- // table doesn't have a tfoot. Create one.
- jTable.append(tfo);
- };
- for (var i = 0; i < sortbottomrows.length; i++) {
- tfo.append(sortbottomrows[i]);
- };
- delete sortbottomrows;
- };
- // work through each column and calculate its type
- var headrow = table.tHead.rows[0].cells;
- var headrowchanges = [];
- for (var i = 0; i < headrow.length; i++) {
- var headrow_item = headrow[i];
- var j_headrow_item = jQuery(headrow_item);
- var j_headrow_item_className = j_headrow_item.prop('class');
- // manually override the type with a sorttable_type attribute
- // skip this col
- if (!j_headrow_item_className.match(/\bsorttable_nosort\b/)) {
- mtch = j_headrow_item_className.match(/\bsorttable_([a-z0-9]+)\b/);
- if (mtch) {
- override = mtch[1];
- };
- if (mtch && typeof sorttable["sort_" + override] == 'function') {
- headrow_item.sorttable_sortfunction = sorttable["sort_" + override];
- } else {
- headrow_item.sorttable_sortfunction = sorttable.guessType(table, i);
- };
- // make it clickable to sort
- headrow_item.sorttable_columnindex = i;
- headrow_item.sorttable_tbody = table.tBodies[0];
- // This ROW has not seen any changes
- headrowchanges[i] = false;
- // Grab TBODY element
- var headrow_tbody = headrow_item.sorttable_tbody;
- // Locate TH Element, and Relative TR, TD elements.
- var Table_Header_Cell = jQuery(this);
- // If we have TABLE BODY
- if (headrow_tbody) {
- // Get the Table Body
- var Table_TBODY = jQuery(headrow_tbody);
- // http://stackoverflow.com/questions/6139407/getting-td-by-index-with-jquery
- var Table_Rows_Cells = Table_TBODY.find("tr td:eq(" + i + ")");
- var Table_Rows_Cells_Inputs = Table_Rows_Cells.find("input:visible, select:visible");
- // Detect changes made in these cells
- if (Table_Rows_Cells_Inputs && Table_Rows_Cells_Inputs.length && (!Table_Rows_Cells.attr('sorttable_customkey') || Table_Rows_Cells.hasClass('sorttable_keychanges'))) {
- Table_Rows_Cells_Inputs.change(function() {
- headrowchanges[i] = true;
- });
- };
- delete Table_TBODY;
- delete Table_Rows_Cells;
- delete Table_Rows_Cells_Inputs;
- };
- dean_addEvent(headrow_item, "click", function(e) {
- var sortrevind, sortfwdind = false;
- var j_current_headrow = jQuery(this);
- var headrow_changed = (typeof(headrowchanges[i]) !== 'undefined') ? headrowchanges[i] : false;
- // Regular Sorting with Existing Data
- var bsorttable_sorted = (this.className.search(/\bsorttable_sorted\b/) != -1);
- var bsorttable_sorted_match = (bsorttable_sorted && !headrow_changed);
- if (bsorttable_sorted) {
- this.className = this.className.replace('sorttable_sorted', 'sorttable_sorted_reverse');
- jDocument.find("#sorttable_sortfwdind").remove();
- sortrevind = jQuery("<span></span>")[0];
- sortrevind.id = "sorttable_sortrevind";
- sortrevind.innerHTML = stIsIE ? ' <font face="webdings">5</font>' : ' ▴';
- this.appendChild(sortrevind);
- if (bsorttable_sorted_match) {
- // if we're already sorted by this column, just
- // reverse the table, which is quicker
- // console.log("REVERSE SORT bsorttable_sorted");
- sorttable.reverse(this.sorttable_tbody);
- return true;
- };
- };
- delete bsorttable_sorted_match;
- // Reverse Sorting with Existing Data
- var bsorttable_sorted_reverse = (this.className.search(/\bsorttable_sorted_reverse\b/) != -1);
- var bsorttable_sorted_reverse_match = (bsorttable_sorted_reverse && !headrow_changed);
- if (bsorttable_sorted_reverse) {
- this.className = this.className.replace('sorttable_sorted_reverse', 'sorttable_sorted');
- jDocument.find('#sorttable_sortrevind').remove();
- sortfwdind = jQuery("<span></span>")[0];
- sortfwdind.id = "sorttable_sortfwdind";
- sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾';
- this.appendChild(sortfwdind);
- if (bsorttable_sorted_reverse_match) {
- // if we're already sorted by this column in reverse, just
- // re-reverse the table, which is quicker
- // console.log("REVERSE SORT bsorttable_sorted_reverse");
- sorttable.reverse(this.sorttable_tbody);
- return true;
- };
- };
- delete bsorttable_sorted_reverse_match;
- // remove sorttable_sorted classes
- theadrow = this.parentNode;
- forEach(theadrow.childNodes, function(cell) {
- if (cell.nodeType == 1) { // an element
- cell.className = cell.className.replace('sorttable_sorted_reverse', '');
- cell.className = cell.className.replace('sorttable_sorted', '');
- }
- });
- sortfwdind = jDocument.find("#sorttable_sortfwdind");
- if (sortfwdind && sortfwdind.length) {
- sortfwdind.remove();
- }
- sortrevind = jDocument.find("#sorttable_sortrevind");
- if (sortrevind && sortrevind.length) {
- sortrevind.remove();
- };
- j_current_headrow.addClass('sorttable_sorted');
- sortfwdind = jQuery("<span></span>")[0];
- sortfwdind.id = "sorttable_sortfwdind";
- sortfwdind.innerHTML = stIsIE ? ' <font face="webdings">6</font>' : ' ▾';
- this.appendChild(sortfwdind);
- // build an array to sort. This is a Schwartzian transform thing,
- // i.e., we "decorate" each row with the actual sort key,
- // sort based on the sort keys, and then put the rows back in order
- // which is a lot faster because you only do getInnerText once per row
- var col, rows, headrow_tbody, row_array = [];
- col = this.sorttable_columnindex;
- headrow_tbody = jQuery(this.sorttable_tbody);
- rows = this.sorttable_tbody.rows;
- var row_sorttable_key, row_sorttable_key_checked = false;
- for (var j = 0; j < rows.length; j++) {
- var rowitem, jrowitem, cellitem, jcellitem = false;
- // The Table ROW
- rowitem = rows[j];
- jrowitem = jQuery(rowitem);
- // Do not sort this row ....
- if (jrowitem.hasClass('sorttable_row_nosort')) {
- console.warn("Skipping no sorttable row");
- continue;
- };
- // The Table Row, Cell Item
- cellitem = rows[j].cells[col];
- if (typeof(cellitem) !== 'undefined') {
- jcellitem = jQuery(cellitem);
- } else {
- jcellitem = jrowitem.find_sorttable_cell_by_index(col);
- cellitem = jcellitem.get(0);
- };
- if(!jrowitem.attr('id')) {
- var new_row_id = 'table-' + jTable.index() + "-row-" + j;
- jrowitem.attr('id', new_row_id);
- delete new_row_id;
- };
- // Check if we are re-sorting this LIST
- if (!row_sorttable_key && !row_sorttable_key_checked) {
- var jrowitem_data, jrowitem_data_isundefined = false;
- // Get DATA from this ROW ITEM
- jrowitem_data_is_undefined = typeof(jrowitem.data()) === 'undefined';
- jrowitem_data = !jrowitem_data_is_undefined ? jrowitem.data() : {};
- // Do not check the DATA any more
- if (jrowitem_data_is_undefined) {
- row_sorttable_key_checked = true;
- row_sorttable_key = false;
- } else {
- var jrowitem_sorttable_key, sorttable_original = false;
- jrowitem_sorttable_key = jrowitem_data['sorttable_row_key'];
- jrowitem_sorttable_key = Number(jrowitem_sorttable_key);
- sorttable_original = jrowitem_data['sorttable_original'];
- if (jrowitem_sorttable_key && sorttable_original) {
- row_sorttable_key = true;
- };
- delete jrowitem_sorttable_key, sorttable_original;
- }
- delete jrowitem_data, jrowitem_data_isundefined;
- };
- var rowInnerText, getRowInnerText = false;
- row_sorttable_key = typeof(row_sorttable_key) !== 'undefined' ? row_sorttable_key : false;
- getRowInnerText = (!jcellitem.hasClass('sorttable_cell_nosort') || !row_sorttable_key);
- // console.log("getRowInnerText = " + getRowInnerText);
- if (getRowInnerText) {
- // Get the INNER TEXT on this ROW, Cell
- rowInnerText = sorttable.getInnerText(cellitem);
- // Ensure we default this to ""
- var rowInnerTextType = typeof(rowInnerText);
- if (typeof(rowInnerText) === 'undefined') {
- rowInnerText = '';
- };
- // Image, Link Sortting
- if (!rowInnerText.length && (jcellitem && jcellitem.length)) {
- var jcellitem_img = jcellitem.find('img').first();
- if (jcellitem_img && jcellitem_img.length) {
- rowInnerText = jcellitem_img.attr('src');
- };
- // If we still do not have rowInnerText
- if (!rowInnerText) {
- var jcellitem_link = jcellitem.find('a').first();
- if (jcellitem_link && jcellitem_link.length) {
- rowInnerText = jcellitem_link.text().trim();
- };
- delete jcellitem_link;
- };
- delete jcellitem_img;
- };
- } else {
- rowInnerText = '';
- };
- // Add the "sorttable_row_index"
- if(!jrowitem.attr('data-sorttable_row_index')) {
- jrowitem.attr('data-sorttable_row_index', false);
- }
- // Update the ROW ARRAY with row_array_item
- var row_array_item = [rowInnerText, jrowitem];
- // Update row_array, with row_array_item
- // row_array[row_array.length] = row_array_item;
- row_array.push(row_array_item);
- delete row_array_item, rowInnerText;
- delete rowitem, jrowitem, cellitem, jcellitem;
- }
- /* If you want a stable sort, uncomment the following line */
- // sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
- /* and comment out this one */
- row_array.sort(this.sorttable_sortfunction);
- // console.log(row_array);
- if (row_sorttable_key) {
- var new_row_array = jQuery.Group_Sorttable_Keys(row_array);
- // console.log(new_row_array);
- if (new_row_array && new_row_array.length) {
- row_array = new_row_array;
- };
- };
- // Empty the MEMORY for col, rows
- delete col, rows;
- // Delete the ROW sorttable key
- delete row_sorttable_key;
- // The Sorttable TBODY
- var tb = this.sorttable_tbody;
- // Check if this table contains zebra
- if (jTable && jTable.length) {
- var table_zebra = jTable.find('tr.alt').length > 0;
- } else {
- var table_zebra = false;
- }
- var row_array_sorttable = jQuery.Make_TBODY_Sorttable(row_array, table_zebra);
- // Sort in regular fashion :P
- row_array_sorttable.regular_sort();
- // console.log(bsorttable_sorted_reverse);
- // Make sure we reverse the search
- if (bsorttable_sorted_reverse && this.sorttable_tbody) {
- sorttable.reverse(this.sorttable_tbody);
- };
- // delete row_array from js memory
- delete row_array;
- // nothing has changed on this row
- headrowchanges[i] = false;
- });
- };
- // Delete this DATA
- delete headrow_item, j_headrow_item, j_headrow_item_className;
- };
- },
- guessType: function(table, column) {
- // guess the type of a column based on its first non-blank row
- sortfn = sorttable.sort_alpha;
- for (var i = 0; i < table.tBodies[0].rows.length; i++) {
- text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
- if (text != '') {
- if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
- return sorttable.sort_numeric;
- }
- // check for a date: dd/mm/yyyy or dd/mm/yy
- // can have / or . or - as separator
- // can be mm/dd as well
- possdate = text.match(sorttable.DATE_RE)
- if (possdate) {
- // looks like a date
- first = parseInt(possdate[1]);
- second = parseInt(possdate[2]);
- if (first > 12) {
- // definitely dd/mm
- return sorttable.sort_ddmm;
- } else if (second > 12) {
- return sorttable.sort_mmdd;
- } else {
- // looks like a date, but we can't tell which, so assume
- // that it's dd/mm (English imperialism!) and keep looking
- sortfn = sorttable.sort_ddmm;
- }
- }
- }
- }
- return sortfn;
- },
- getInnerText: function(node) {
- var jNode = jQuery(node);
- var jNodeInnerText = false;
- // StackTable Key Column, or we have something that is UNDEFINED
- if (jNode.hasClass('st-key') || typeof(jNode) === 'undefined') {
- return '';
- };
- var node_is_jquery_obj = false;
- // No node
- if (typeof(node) == 'undefined') {
- return '';
- };
- // Validate the Node
- node_is_jquery_obj = (node.getAttribute);
- node_is_jquery_obj = node_is_jquery_obj ? (jNode instanceof jQuery && jNode.length && typeof(jNode.prop('tagName')) !== 'undefined') : false;
- // Check whether jNode is jQuery ELEMENT that EXISTS within the current WEBPAGE
- if (node_is_jquery_obj) {
- var jNodeElement = false;
- // Get only TEXT from this ELEMENT, and EXCLUDE CHILDREN
- if (jNode.prop('tagName') == 'TD') {
- jNodeElement = jNode.clone().children().remove().end();
- };
- // If this element is "TD" (element to be SORTTED).
- if (jNodeElement && !jNode.attr('sorttable_customkey')) {
- var jNodeElementText = jNodeElement.text().trim().length;
- // Look at the SELECTION, for SELECTED OPTION
- if (jNode.find('select') && !jNodeElementText.length) {
- jNodeInnerText = jNode.find('select').first().find('option:selected').text().trim();
- };
- // Clean up mem
- delete jNodeElementText;
- };
- // Clean up mem
- delete jNodeElement;
- };
- // We have found "jNodeInnerText", then just return it
- if (jNodeInnerText) {
- return jNodeInnerText;
- };
- // Clean up mem, we won't be using these after this point
- delete jNode;
- delete jNodeInnerText;
- // gets the text we want to use for sorting for a cell.
- // strips leading and trailing whitespace.
- // this is *not* a generic getInnerText function; it's special to sorttable.
- // for example, you can override the cell text with a customkey attribute.
- // it also gets .value for <input> fields.
- hasInputs = (typeof node.getElementsByTagName == 'function') &&
- node.getElementsByTagName('input').length;
- if (node.getAttribute && node.getAttribute("sorttable_customkey") != null) {
- return node.getAttribute("sorttable_customkey");
- } else if (typeof node.textContent != 'undefined' && !hasInputs) {
- return node.textContent.replace(/^\s+|\s+$/g, '');
- } else if (typeof node.innerText != 'undefined' && !hasInputs) {
- return node.innerText.replace(/^\s+|\s+$/g, '');
- } else if (typeof node.text != 'undefined' && !hasInputs) {
- return node.text.replace(/^\s+|\s+$/g, '');
- } else {
- switch (node.nodeType) {
- case 3:
- if (node.nodeName.toLowerCase() == 'input') {
- return node.value.replace(/^\s+|\s+$/g, '');
- }
- case 4:
- return node.nodeValue.replace(/^\s+|\s+$/g, '');
- break;
- case 1:
- case 11:
- var innerText = '';
- for (var i = 0; i < node.childNodes.length; i++) {
- innerText += sorttable.getInnerText(node.childNodes[i]);
- }
- return innerText.replace(/^\s+|\s+$/g, '');
- break;
- default:
- return '';
- };
- };
- },
- reverse: function(tbody) {
- var sorttable_rows = jQuery.Make_TBODY_Sorttable(tbody);
- // console.log("REVERSE");
- sorttable_rows.reverse_sort();
- },
- /* sort functions
- each sort function takes two parameters, a and b
- you are comparing a[0] and b[0] */
- sort_numeric: function(a, b) {
- aa = parseFloat(a[0].replace(/[^0-9.-]/g, ''));
- if (isNaN(aa)) aa = 0;
- bb = parseFloat(b[0].replace(/[^0-9.-]/g, ''));
- if (isNaN(bb)) bb = 0;
- return aa - bb;
- },
- sort_alpha: function(a, b) {
- if (a[0] == b[0]) return 0;
- if (a[0] < b[0]) return -1;
- return 1;
- },
- sort_ddmm: function(a, b) {
- mtch = a[0].match(sorttable.DATE_RE);
- y = mtch[3];
- m = mtch[2];
- d = mtch[1];
- if (m.length == 1) m = '0' + m;
- if (d.length == 1) d = '0' + d;
- dt1 = y + m + d;
- mtch = b[0].match(sorttable.DATE_RE);
- y = mtch[3];
- m = mtch[2];
- d = mtch[1];
- if (m.length == 1) m = '0' + m;
- if (d.length == 1) d = '0' + d;
- dt2 = y + m + d;
- if (dt1 == dt2) return 0;
- if (dt1 < dt2) return -1;
- return 1;
- },
- //kryogenix sorttable - how to sort date columns that have empty cells
- //http://stackoverflow.com/questions/9815426/kryogenix-sorttable-how-to-sort-date-columns-that-have-empty-cells
- sort_mmdd: function(a, b) {
- mtch = a[0].match(sorttable.DATE_RE);
- if ((mtch == null) || (mtch == undefined)) {
- y = 0;
- d = 0;
- m = 0;
- } else {
- y = mtch[3];
- d = mtch[2];
- m = mtch[1];
- }
- if (m.length == 1) m = '0' + m;
- if (d.length == 1) d = '0' + d;
- dt1 = y + m + d;
- mtch = b[0].match(sorttable.DATE_RE);
- if ((mtch == null) || (mtch == undefined)) {
- y = 0;
- d = 0;
- m = 0;
- } else {
- y = mtch[3];
- d = mtch[2];
- m = mtch[1];
- }
- if (m.length == 1) m = '0' + m;
- if (d.length == 1) d = '0' + d;
- dt2 = y + m + d;
- if (dt1 == dt2) return 0;
- if (dt1 < dt2) return -1;
- return 1;
- },
- shaker_sort: function(list, comp_func) {
- // A stable sort function to allow multi-level sorting of data
- // see: http://en.wikipedia.org/wiki/Cocktail_sort
- // thanks to Joseph Nahmias
- var b = 0;
- var t = list.length - 1;
- var swap = true;
- while (swap) {
- swap = false;
- for (var i = b; i < t; ++i) {
- if (comp_func(list[i], list[i + 1]) > 0) {
- var q = list[i];
- list[i] = list[i + 1];
- list[i + 1] = q;
- swap = true;
- }
- } // for
- t--;
- if (!swap) break;
- for (var i = t; i > b; --i) {
- if (comp_func(list[i], list[i - 1]) < 0) {
- var q = list[i];
- list[i] = list[i - 1];
- list[i - 1] = q;
- swap = true;
- }
- } // for
- b++;
- } // while(swap)
- }
- }
- /* ******************************************************************
- Supporting functions: bundled here to avoid depending on a library
- ****************************************************************** */
- // Dean Edwards/Matthias Miller/John Resig
- /* for Mozilla/Opera9 */
- if (document.addEventListener) {
- document.addEventListener("DOMContentLoaded", sorttable.init, false);
- }
- /* for Internet Explorer */
- /*@cc_on @*/
- /*@if (@_win32)
- document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
- var script = document.getElementById("__ie_onload");
- script.onreadystatechange = function() {
- if (this.readyState == "complete") {
- sorttable.init(); // call the onload handler
- }
- };
- /*@end @*/
- /* for Safari */
- if (/WebKit/i.test(navigator.userAgent)) { // sniff
- var _timer = setInterval(function() {
- if (/loaded|complete/.test(document.readyState)) {
- sorttable.init(); // call the onload handler
- }
- }, 10);
- }
- /* for other browsers */
- window.onload = sorttable.init;
- // written by Dean Edwards, 2005
- // with input from Tino Zijdel, Matthias Miller, Diego Perini
- // http://dean.edwards.name/weblog/2005/10/add-event/
- function dean_addEvent(element, type, handler) {
- if (element.addEventListener) {
- element.addEventListener(type, handler, false);
- } else {
- // assign each event handler a unique ID
- if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
- // create a hash table of event types for the element
- if (!element.events) element.events = {};
- // create a hash table of event handlers for each element/event pair
- var handlers = element.events[type];
- if (!handlers) {
- handlers = element.events[type] = {};
- // store the existing event handler (if there is one)
- if (element["on" + type]) {
- handlers[0] = element["on" + type];
- }
- }
- // store the event handler in the hash table
- handlers[handler.$$guid] = handler;
- // assign a global event handler to do all the work
- element["on" + type] = handleEvent;
- }
- };
- // a counter used to create unique IDs
- dean_addEvent.guid = 1;
- function removeEvent(element, type, handler) {
- if (element.removeEventListener) {
- element.removeEventListener(type, handler, false);
- } else {
- // delete the event handler from the hash table
- if (element.events && element.events[type]) {
- delete element.events[type][handler.$$guid];
- }
- }
- };
- function handleEvent(event) {
- var returnValue = true;
- // grab the event object (IE uses a global event object)
- event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
- // get a reference to the hash table of event handlers
- var handlers = this.events[event.type];
- // execute each event handler
- for (var i in handlers) {
- this.$$handleEvent = handlers[i];
- if (this.$$handleEvent(event) === false) {
- returnValue = false;
- }
- }
- return returnValue;
- };
- function fixEvent(event) {
- // add W3C standard event methods
- event.preventDefault = fixEvent.preventDefault;
- event.stopPropagation = fixEvent.stopPropagation;
- return event;
- };
- fixEvent.preventDefault = function() {
- this.returnValue = false;
- };
- fixEvent.stopPropagation = function() {
- this.cancelBubble = true;
- }
- // Dean's forEach: http://dean.edwards.name/base/forEach.js
- /*
- forEach, version 1.0
- Copyright 2006, Dean Edwards
- License: http://www.opensource.org/licenses/mit-license.php
- */
- // array-like enumeration
- if (!Array.forEach) { // mozilla already supports this
- Array.forEach = function(array, block, context) {
- for (var i = 0; i < array.length; i++) {
- block.call(context, array[i], i, array);
- }
- };
- }
- // generic enumeration
- Function.prototype.forEach = function(object, block, context) {
- for (var key in object) {
- if (typeof this.prototype[key] == "undefined") {
- block.call(context, object[key], key, object);
- }
- }
- };
- // character enumeration
- String.forEach = function(string, block, context) {
- Array.forEach(string.split(""), function(chr, index) {
- block.call(context, chr, index, string);
- });
- };
- // globally resolve forEach enumeration
- var forEach = function(object, block, context) {
- if (object) {
- var resolve = Object; // default
- if (object instanceof Function) {
- // functions have a "length" property
- resolve = Function;
- } else if (object.forEach instanceof Function) {
- // the object implements a custom forEach method so use that
- object.forEach(block, context);
- return;
- } else if (typeof object == "string") {
- // the object is a string
- resolve = String;
- } else if (typeof object.length == "number") {
- // the object is array-like
- resolve = Array;
- }
- resolve.forEach(object, block, context);
- }
- };
- jQuery.Make_TBODY_Sorttable = function(sorttable_argument, zebra_table) {
- var new_rows, new_row_indexes, jtbody, jtbodyrows = false;
- new_rows = [];
- new_row_indexes = [];
- var sorttable_argument_org = sorttable_argument;
- // console.log(sorttable_argument);
- if (sorttable_argument instanceof Array) {
- var first_row = sorttable_argument[0][1];
- jtbodyrows = sorttable_argument; // get the rows
- jtbody = jQuery(first_row).parents('tbody');
- } else {
- jtbody = jQuery(sorttable_argument);
- jtbodyrows = jtbody.find("tr:not(.sorttable_row_nosort)").get();
- };
- tbody = jtbody.get(0);
- // console.log(tbody);
- // reverse the rows in a tbody
- for (var i = 0; i < jtbodyrows.length; i++) {
- var tbodyrow, tbodyrow_index;
- tbodyrow = jQuery(tbody.rows[i]);
- tbodyrow_index = tbodyrow.get_sorttable_index();
- var tbodyrow_original = tbodyrow_index ? tbodyrow.is_sorttable_original() : false;
- var skip_tbody_index = ((tbodyrow_index && new_row_indexes.length) && jQuery.inArray(tbodyrow_index, new_row_indexes));
- // Check if we should skip this ROW ITEM
- if (skip_tbody_index && !tbodyrow_original) {
- // console.warn("Skipping the TBODY ITEM " + tbodyrow_index);
- continue;
- };
- // Add "tbodyrow_index" to "new_row_indexes"
- if (tbodyrow_index) {
- new_row_indexes.push(tbodyrow_index);
- };
- // Declare some varaibles ....
- var sorttable_tbody_rows, tbody_item_row = false;
- // Get the SORTTABLE ROW ITEMS, relative to the current row
- sorttable_tbody_rows = tbodyrow.get_sorttable_row_items();
- // If we have More than ONE row, and this ROW is not our ORIGINAL ROW ...
- if (tbodyrow_index && sorttable_tbody_rows.length > 1 && !tbodyrow_original) {
- // console.warn("Skipping the NON ORIGINAL ROW");
- continue;
- }
- tbody_item_row = {
- main: tbodyrow.get(0),
- subs: [],
- index: tbodyrow_index,
- };
- if (sorttable_tbody_rows && sorttable_tbody_rows.length) {
- tbody_item_row['subs'] = sorttable_tbody_rows.get();
- tbody_item_row['subs'].shift();
- };
- // console.log(tbody_item_row);
- new_rows[new_rows.length] = tbody_item_row;
- delete tbodyrow, tbodyrow_index;
- delete sorttable_tbody_rows, tbody_item_row;
- };
- // console.log(new_rows);
- var sorttable_row_func = function(jtbody, new_rows, zebra_table) {
- var rows = new_rows;
- var tbody = jtbody;
- var zebra_key = 0;
- var zebra_table = typeof(zebra_table) !== 'undefined' ? zebra_table : false;
- this.tbody_append_row_item = function(tbody_item_row) {
- var main_row_item;
- if (zebra_table) {
- main_row_item = this.zebra_row_item(tbody_item_row.main);
- } else {
- main_row_item = tbody_item_row.main;
- }
- tbody.append(main_row_item);
- var main_zebra_class = this.get_zebra_class(main_row_item);
- if (tbody_item_row.subs.length > 0) {
- for (var rs = 0; rs < tbody_item_row.subs.length; rs++) {
- var sub_row_item = tbody_item_row.subs[rs];
- if (zebra_table) {
- sub_row_item.removeClass('alt');
- sub_row_item.addClass(main_zebra_class);
- };
- tbody.append(sub_row_item);
- };
- };
- return true;
- };
- this.regular_sort = function() {
- // console.log(rows);
- for(var r = 0; r < rows.length; r++) {
- var tbody_item_row = rows[r];
- this.tbody_append_row_item(tbody_item_row);
- delete tbody_item_row;
- };
- };
- this.reverse_sort = function() {
- var tbody_reverse_sort = [];
- for (var r = rows.length - 1; r >= 0; r--) {
- var tbody_item_row = rows[r];
- tbody_reverse_sort.push(tbody_item_row);
- delete tbody_item_row;
- };
- for(var rb = 0; rb < tbody_reverse_sort.length; rb++) {
- var tbody_item_row = tbody_reverse_sort[rb];
- this.tbody_append_row_item(tbody_item_row);
- delete tbody_item_row;
- };
- delete tbody_reverse_sort;
- };
- this.get_zebra_class = function(row_item) {
- var zebra_class = '';
- row_item = jQuery(row_item);
- if (row_item && row_item.hasClass('alt')) {
- zebra_class = 'alt';
- };
- return zebra_class;
- }
- this.zebra_row_item = function(row_item) {
- // Re-Zebra the ROW
- if ((row_item && row_item.length)) {
- row_item.removeClass('alt'); // Remove "alt"
- if (zebra_key > 0) {
- row_item.addClass('alt');
- zebra_key = 0;
- } else {
- zebra_key = zebra_key + 1;
- };
- row_item = row_item.get(0);
- };
- return row_item;
- };
- this.get_zebra_key = function() {
- return zebra_key;
- }
- };
- var sorttable_row_object = new sorttable_row_func(jtbody, new_rows, zebra_table);
- return sorttable_row_object;
- }
- jQuery.fn.get_sorttable_indexes = function() {
- var jrowitems, sorttable_indexes = false;
- jrowitems = jQuery(this);
- sorttable_indexes = [];
- jrowitems.each(function(row_index, row_item) {
- var sorttable_index_item = jQuery(row_item).attr('data-sorttable_row_key');
- sorttable_indexes.push(sorttable_index_item);
- delete sorttable_index_item;
- });
- delete jrowitems;
- return sorttable_indexes;
- };
- jQuery.fn.get_sorttable_index = function() {
- var jrowitem, sorttable_row_index;
- jrowitem = jQuery(this);
- if (!jrowitem.length) {
- return false;
- }
- sorttable_row_index = jrowitem.attr('data-sorttable_row_index');
- if ((typeof(sorttable_row_index) === 'undefined') || !sorttable_row_index) {
- console.warn("The sorttable_row_index cannot be approved");
- return false;
- };
- delete jrowitem, sorttable_row_index;
- return sorttable_row_index;
- };
- jQuery.fn.get_sorttable_row_items = function() {
- var jrowitem, sorttable_row_index, jrowitems = false;
- jrowitem = jQuery(this);
- sorttable_row_index = jrowitem.get_sorttable_index();
- if (sorttable_row_index) {
- jrowitems = jrowitem.parent('tbody').find(sorttable_row_index);;
- };
- delete jrowitem, sorttable_row_index, jrowitems;
- return jrowitems;
- };
- jQuery.fn.is_sorttable_original = function() {
- var jrowitem, is_sorttable_original = false;
- jrowitem = jQuery(this);
- is_sorttable_original = jrowitem.attr('data-sorttable_original');
- is_sorttable_original = (typeof(is_sorttable_original) !== 'undefined') ? is_sorttable_original : false;
- return is_sorttable_original;
- };
- jQuery.Group_Sorttable_Keys = function(row_array) {
- var row_item_list = jQuery.Create_Row_Item_List(row_array);
- if (!row_item_list || !row_item_list.length) {
- console.warn("row_item_list was EMPTY");
- return false;
- };
- var i, N = 0;
- var new_row_array = [];
- var expecting_sorttable_keys = [];
- N = row_item_list.length;
- for (i = 0; i < N; i++) {
- var row_list_item = row_item_list.items[i];
- var row_tbody = row_list_item.row.parent('tbody');
- if (!row_list_item.row.is_sorttable_original() || row_list_item.row.hasClass(row_list_item.new_class_index)) {
- continue;
- };
- // Get ROWS linked to this SORTTABLE ROW KEY
- row_list_item_pattern = "tr[data-sorttable_row_key='" + row_list_item.data['sorttable_row_key'] + "']";
- linked_row_items = row_tbody.find(row_list_item_pattern);
- linked_row_items = jQuery(linked_row_items);
- linked_row_items.addClass(row_list_item.new_class_index);
- linked_row_items.attr('data-sorttable_row_index', '.' + row_list_item.new_class_index);
- // Update our new ROW ITEMS
- new_row_array.push([row_list_item.text, linked_row_items]);
- expecting_sorttable_keys.push(row_list_item.data['sorttable_row_key']);
- // Clear the garbage collection
- delete row_list_item_pattern, linked_row_items;
- };
- delete i, N;
- return new_row_array;
- };
- jQuery.Create_Row_Item_List = function(row_array) {
- if (typeof(row_array) === 'undefined' || !row_array.length) {
- console.warn("No row_array supplied to Create_Row_Item_List");
- return false;
- };
- var new_row_item_list = {
- items: [],
- length: 0,
- };
- var row_index, row_len = 0;
- row_len = row_array.length;
- for (row_index = 0; row_index < row_len; row_index++) {
- var row_index_item, row_text_item, row_html_item = false;
- row_index_item = row_array[row_index];
- row_html_item = jQuery(row_index_item[1]);
- row_text_item = row_index_item[0];
- var new_row_item = {
- index: row_index,
- row: row_html_item,
- data: row_html_item.data(),
- text: row_text_item,
- new_class_index: false,
- };
- new_row_item.new_class_index = "sorttable_row_key-index-" + new_row_item.data['sorttable_row_key'];
- delete row_index_item, row_text_item, row_html_item;
- new_row_item_list.items.push(new_row_item);
- delete new_row_item;
- };
- delete row_index, row_len;
- new_row_item_list.length = new_row_item_list.items.length;
- return new_row_item_list;
- };
- jQuery.fn.find_sorttable_cell_by_index = function(column_index) {
- var rowItem = jQuery(this);
- if (rowItem.prop('tagName') != 'TR') {
- console.error("rowItem is not <TR> Element");
- return false;
- };
- var current_index = 0;
- var rowItemCells = rowItem.children("td");
- var located_cell_item = false;
- rowItemCells.each(function(cell_index, cell_item) {
- cell_item = jQuery(cell_item);
- var cell_item_colspan = cell_item.attr('colspan');
- cell_item_colspan = Number(cell_item_colspan);
- if (cell_item_colspan && cell_item_colspan > 0) {
- current_index = current_index + cell_item_colspan;
- current_index = Number(current_index);
- } else {
- current_index = current_index + cell_index;
- };
- if (current_index >= column_index || cell_index == column_index) {
- located_cell_item = cell_item;
- return false;
- };
- });
- return located_cell_item;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement