Debian Codegraph @

js/widgets/widget-vertical-group.js

raw symbols SCIP
1 /*! Widget: vertical-group (BETA) - updated 12/13/2017 (v2.29.1) */
2 /* Requires tablesorter and jQuery
3 * Originally by @aavmurphy (Andrew Murphy)
4 * Adapted for tablesorter by Rob Garrison - see #1469 & #1470
5 *
6 * This widget is licensed under the same terms at mottie/tablesorter itself, i.e. free to use
7 */
8 /* jshint browser:true, jquery:true, unused:false */
9 /* global jQuery:false */
10 ;(function($) {
11 'use strict';
12
13 var ts = $.tablesorter,
14 tscss = ts.css;
15
16 $.extend( ts.css, {
17 verticalGroupHeader: 'tablesorter-vertical-group',
18 verticalGroupHide: 'tablesorter-vertical-group-hide',
19 verticalGroupShow: 'tablesorter-vertical-group-show'
20 });
21
22 ts.addWidget({
23 id: 'vertical-group',
24 priority: 99,
25 init: verticalGroup,
26 format: verticalGroup
27 });
28
29 function cleanUp( el ) {
30 el.removeClass(tscss.verticalGroupHide + ' ' + tscss.verticalGroupShow);
31 }
32
33 function setZebra( wo, $cell, indx ) {
34 var $row = $cell.parent();
35 $row
36 .removeClass( wo.zebra[ (indx + 1) % 2 ] )
37 .addClass( wo.zebra[ indx % 2] );
38 }
39
40 function verticalGroup( table, c, wo ) {
41 // -------------------------------------------------------------------------
42 // loop thru the header row,
43 // - look for .vertical-group
44 //
45 // loop thru the rows
46 //
47 // set ALWAYS_SHOW = FALSE
48 // loop thru the 1st 4 columns
49 // if this cell does not exist, skip to next row
50 // if ALWAYS_SHOW, then this cell is SHOW
51 // else if this column does not have '.vertical-group', then this cell is SHOW
52 // else if this cell is NOT the same as the cell-above, then this cell is SHOW
53 // else this cell is HIDE
54 // if this cell is SHOW, set ALWAYS_SHOW
55 // if this cell is SHOW,
56 // then
57 // set the cell class to .vertical_group_show
58 // else
59 // set the cell class to vertical_group_show
60 //
61 // TO DO add/remove classes so as not to clobber other existing classes
62 // TO DO add classes
63 //
64 // .vertical-group-show { background-color: white !important; }
65 // .vertical-group-hide { visibility: hidden; border-top: white !important;background-color: white !important; }
66 //
67 // this is all because of stripped tables
68 // - background-colour show be the table's background colour (or the first row's)
69 // - the border-color needs to be the same
70 //
71 // ------------------------------------------------------------------------------------------------
72 var zebra_index = -1, // increments at start of loop
73 rows = table.tBodies[0].rows,
74 has_zebra = ts.hasWidget( table, 'zebra'),
75 is_vertical_group_col = [],
76 last_row = [];
77
78 if ( wo.vertical_group_lock ) {
79 return;
80 }
81 wo.vertical_group_lock = true;
82
83 is_vertical_group_col = $.map( c.$headerIndexed, function( el ) {
84 return el.hasClass( tscss.verticalGroupHeader ) ? 1 : '';
85 });
86
87 if ( is_vertical_group_col.join('') === '' ) {
88 cleanUp( $(rows).find( '.' + tscss.verticalGroupHide + ',.' + tscss.verticalGroupShow ) );
89 wo.vertical_group_lock = false;
90 return;
91 }
92
93 for (var i = 0; i < rows.length; i++) {
94 var always_show_cell = false;
95
96 for (var j = 0; j < c.columns; j++ ) {
97 if ( !is_vertical_group_col[ j ] || !rows[ i ].cells[ j ] ) {
98 zebra_index++;
99 continue;
100 }
101
102 var $cell = $( rows[ i ].cells[ j ] ),
103 // only group if column is sorted
104 isSorted = ts.isValueInArray( j, c.sortList ), // returns equivalent of an indexOf value
105 cell_data = $cell.html();
106
107 if ( isSorted < 0 ) {
108 cleanUp( $cell );
109 } else if ( !always_show_cell && cell_data === last_row[j] ) {
110 if ( !$cell.hasClass(tscss.verticalGroupHide) ) {
111 $cell.addClass( tscss.verticalGroupHide );
112 }
113 if ( has_zebra ) {
114 setZebra( wo, $cell, zebra_index );
115 }
116 $cell.removeClass( tscss.verticalGroupShow );
117 } else if (isSorted === 0) {
118 // only show cells from the first sorted column
119 always_show_cell = true; // show
120 if ( !$cell.hasClass( tscss.verticalGroupShow ) ) {
121 $cell.addClass( tscss.verticalGroupShow );
122 }
123 $cell.removeClass( tscss.verticalGroupHide );
124 if ( has_zebra ) {
125 // only adjust striping based on the first sorted column
126 setZebra( wo, $cell, isSorted ? zebra_index : ++zebra_index );
127 }
128 }
129 last_row[j] = cell_data;
130 }
131 }
132 wo.vertical_group_lock = false;
133 }
134
135 })(jQuery);