Debian Codegraph @

js/widgets/widget-staticRow.js

raw symbols SCIP
1 /*! widget: staticRow - updated 10/31/2015 (v2.24.0) *//*
2 * Version 1.2 mod by Rob Garrison (requires tablesorter v2.16+)
3 * Requires:
4 * jQuery v1.4+
5 * tablesorter plugin, v2.8+, available at http://mottie.github.com/tablesorter/docs/
6 *
7 * Copyright (c) 2011 Nils Luxton
8 * Licensed under the MIT license:
9 * http://www.opensource.org/licenses/mit-license.php
10 *
11 */
12 /*jshint browser:true, jquery:true, unused:false */
13 /*global jQuery: false */
14 ;(function($) {
15 'use strict';
16 var ts = $.tablesorter,
17
18 // add/refresh row indexes
19 addIndexes = function(table) {
20 var $tr, wo, v, indx, rows,
21 c = table.config;
22 // 'Index' the static rows, saving their current (starting) position in the
23 // table inside a data() param on the <tr> element itself for later use.
24 if (c) {
25 wo = c.widgetOptions;
26 c.$tbodies.each(function() {
27 $tr = $(this).children();
28 rows = $tr.length;
29 $tr.filter(wo.staticRow_class).each(function() {
30 $tr = $(this);
31 indx = $tr.data(wo.staticRow_index);
32 if (typeof indx !== 'undefined') {
33 v = parseFloat(indx);
34 // percentage of total rows
35 indx = (/%/.test(indx)) ? Math.round(v / 100 * rows) : v;
36 } else {
37 indx = $tr.index();
38 }
39 // row indexing starts over within each tbody
40 $tr.data( wo.staticRow_data, indx );
41 });
42 });
43 }
44 };
45
46 ts.addWidget({
47 // Give the new Widget an ID to be used in the tablesorter() call, as follows:
48 // $('#myElement').tablesorter({ widgets: ['zebra', 'staticRow'] });
49 id: 'staticRow',
50
51 options: {
52 staticRow_class : '.static',
53 staticRow_data : 'static-index',
54 staticRow_index : 'row-index',
55 staticRow_event : 'staticRowsRefresh'
56 },
57
58 init: function(table, thisWidget, c, wo) {
59 addIndexes(table);
60 // refresh static rows after updates
61 c.$table
62 .unbind( ('updateComplete.tsstaticrows ' + wo.staticRow_event).replace(/\s+/g, ' ') )
63 .bind('updateComplete.tsstaticrows ' + wo.staticRow_event, function() {
64 addIndexes(table);
65 ts.applyWidget( table );
66 });
67 },
68
69 format: function(table, c, wo) {
70 // Loop thru static rows, moving them to their original 'indexed' position,
71 // & repeat until no more re-shuffling is needed
72 var targetIndex, $thisRow, indx, numRows, $tbody, hasShuffled, $rows, max;
73
74 c.$tbodies.each(function() {
75 $tbody = $.tablesorter.processTbody(table, $(this), true); // remove tbody
76 hasShuffled = true;
77 indx = 0;
78 $rows = $tbody.children(wo.staticRow_class);
79 numRows = $tbody.children('tr').length - 1;
80 max = $rows.length;
81
82 // don't allow the while loop to cycle more times than the set number of static rows
83 while (hasShuffled && indx < max) {
84 hasShuffled = false;
85 /*jshint loopfunc:true */
86 $rows.each(function() {
87 targetIndex = $(this).data(wo.staticRow_data);
88 // allow setting target index >> num rows to always make a row last
89 targetIndex = targetIndex >= numRows ? numRows : targetIndex < 0 ? 0 : targetIndex;
90 if (targetIndex !== $(this).index()) {
91 hasShuffled = true;
92 $thisRow = $(this).detach();
93
94 if (targetIndex >= numRows) {
95 // Are we trying to be the last row?
96 $thisRow.appendTo( $tbody );
97 } else if (targetIndex === 0) {
98 // Are we trying to be the first row?
99 $thisRow.prependTo( $tbody );
100 } else {
101 // No, we want to be somewhere in the middle!
102 $thisRow.insertBefore( $tbody.find('tr:eq(' + targetIndex + ')') );
103 }
104 }
105 });
106 indx++;
107 }
108
109 $.tablesorter.processTbody(table, $tbody, false); // restore tbody
110 });
111
112 c.$table.triggerHandler('staticRowsComplete', table);
113 },
114
115 remove : function(table, c, wo) {
116 c.$table.unbind( ('updateComplete.tsstaticrows ' + wo.staticRow_event).replace(/\s+/g, ' ') );
117 }
118
119 });
120
121 })(jQuery);