comparison default/node_modules/tablesaw/dist/tablesaw.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1d038bc9b3d2
1 /*! Tablesaw - v3.0.8 - 2018-01-25
2 * https://github.com/filamentgroup/tablesaw
3 * Copyright (c) 2018 Filament Group; Licensed MIT */
4 /*! Shoestring - v2.0.0 - 2017-02-14
5 * http://github.com/filamentgroup/shoestring/
6 * Copyright (c) 2017 Scott Jehl, Filament Group, Inc; Licensed MIT & GPLv2 */
7 (function( factory ) {
8 if( typeof define === 'function' && define.amd ) {
9 // AMD. Register as an anonymous module.
10 define( [ 'shoestring' ], factory );
11 } else if (typeof module === 'object' && module.exports) {
12 // Node/CommonJS
13 module.exports = factory();
14 } else {
15 // Browser globals
16 factory();
17 }
18 }(function () {
19 var win = typeof window !== "undefined" ? window : this;
20 var doc = win.document;
21
22
23 /**
24 * The shoestring object constructor.
25 *
26 * @param {string,object} prim The selector to find or element to wrap.
27 * @param {object} sec The context in which to match the `prim` selector.
28 * @returns shoestring
29 * @this window
30 */
31 function shoestring( prim, sec ){
32 var pType = typeof( prim ),
33 ret = [],
34 sel;
35
36 // return an empty shoestring object
37 if( !prim ){
38 return new Shoestring( ret );
39 }
40
41 // ready calls
42 if( prim.call ){
43 return shoestring.ready( prim );
44 }
45
46 // handle re-wrapping shoestring objects
47 if( prim.constructor === Shoestring && !sec ){
48 return prim;
49 }
50
51 // if string starting with <, make html
52 if( pType === "string" && prim.indexOf( "<" ) === 0 ){
53 var dfrag = doc.createElement( "div" );
54
55 dfrag.innerHTML = prim;
56
57 // TODO depends on children (circular)
58 return shoestring( dfrag ).children().each(function(){
59 dfrag.removeChild( this );
60 });
61 }
62
63 // if string, it's a selector, use qsa
64 if( pType === "string" ){
65 if( sec ){
66 return shoestring( sec ).find( prim );
67 }
68
69 sel = doc.querySelectorAll( prim );
70
71 return new Shoestring( sel, prim );
72 }
73
74 // array like objects or node lists
75 if( Object.prototype.toString.call( pType ) === '[object Array]' ||
76 (win.NodeList && prim instanceof win.NodeList) ){
77
78 return new Shoestring( prim, prim );
79 }
80
81 // if it's an array, use all the elements
82 if( prim.constructor === Array ){
83 return new Shoestring( prim, prim );
84 }
85
86 // otherwise assume it's an object the we want at an index
87 return new Shoestring( [prim], prim );
88 }
89
90 var Shoestring = function( ret, prim ) {
91 this.length = 0;
92 this.selector = prim;
93 shoestring.merge(this, ret);
94 };
95
96 // TODO only required for tests
97 Shoestring.prototype.reverse = [].reverse;
98
99 // For adding element set methods
100 shoestring.fn = Shoestring.prototype;
101
102 shoestring.Shoestring = Shoestring;
103
104 // For extending objects
105 // TODO move to separate module when we use prototypes
106 shoestring.extend = function( first, second ){
107 for( var i in second ){
108 if( second.hasOwnProperty( i ) ){
109 first[ i ] = second[ i ];
110 }
111 }
112
113 return first;
114 };
115
116 // taken directly from jQuery
117 shoestring.merge = function( first, second ) {
118 var len, j, i;
119
120 len = +second.length,
121 j = 0,
122 i = first.length;
123
124 for ( ; j < len; j++ ) {
125 first[ i++ ] = second[ j ];
126 }
127
128 first.length = i;
129
130 return first;
131 };
132
133 // expose
134 win.shoestring = shoestring;
135
136
137
138 /**
139 * Iterates over `shoestring` collections.
140 *
141 * @param {function} callback The callback to be invoked on each element and index
142 * @return shoestring
143 * @this shoestring
144 */
145 shoestring.fn.each = function( callback ){
146 return shoestring.each( this, callback );
147 };
148
149 shoestring.each = function( collection, callback ) {
150 var val;
151 for( var i = 0, il = collection.length; i < il; i++ ){
152 val = callback.call( collection[i], i, collection[i] );
153 if( val === false ){
154 break;
155 }
156 }
157
158 return collection;
159 };
160
161
162
163 /**
164 * Check for array membership.
165 *
166 * @param {object} needle The thing to find.
167 * @param {object} haystack The thing to find the needle in.
168 * @return {boolean}
169 * @this window
170 */
171 shoestring.inArray = function( needle, haystack ){
172 var isin = -1;
173 for( var i = 0, il = haystack.length; i < il; i++ ){
174 if( haystack.hasOwnProperty( i ) && haystack[ i ] === needle ){
175 isin = i;
176 }
177 }
178 return isin;
179 };
180
181
182
183 /**
184 * Bind callbacks to be run when the DOM is "ready".
185 *
186 * @param {function} fn The callback to be run
187 * @return shoestring
188 * @this shoestring
189 */
190 shoestring.ready = function( fn ){
191 if( ready && fn ){
192 fn.call( doc );
193 }
194 else if( fn ){
195 readyQueue.push( fn );
196 }
197 else {
198 runReady();
199 }
200
201 return [doc];
202 };
203
204 // TODO necessary?
205 shoestring.fn.ready = function( fn ){
206 shoestring.ready( fn );
207 return this;
208 };
209
210 // Empty and exec the ready queue
211 var ready = false,
212 readyQueue = [],
213 runReady = function(){
214 if( !ready ){
215 while( readyQueue.length ){
216 readyQueue.shift().call( doc );
217 }
218 ready = true;
219 }
220 };
221
222 // If DOM is already ready at exec time, depends on the browser.
223 // From: https://github.com/mobify/mobifyjs/blob/526841be5509e28fc949038021799e4223479f8d/src/capture.js#L128
224 if (doc.attachEvent ? doc.readyState === "complete" : doc.readyState !== "loading") {
225 runReady();
226 } else {
227 doc.addEventListener( "DOMContentLoaded", runReady, false );
228 doc.addEventListener( "readystatechange", runReady, false );
229 win.addEventListener( "load", runReady, false );
230 }
231
232
233
234 /**
235 * Checks the current set of elements against the selector, if one matches return `true`.
236 *
237 * @param {string} selector The selector to check.
238 * @return {boolean}
239 * @this {shoestring}
240 */
241 shoestring.fn.is = function( selector ){
242 var ret = false, self = this, parents, check;
243
244 // assume a dom element
245 if( typeof selector !== "string" ){
246 // array-like, ie shoestring objects or element arrays
247 if( selector.length && selector[0] ){
248 check = selector;
249 } else {
250 check = [selector];
251 }
252
253 return _checkElements(this, check);
254 }
255
256 parents = this.parent();
257
258 if( !parents.length ){
259 parents = shoestring( doc );
260 }
261
262 parents.each(function( i, e ) {
263 var children;
264
265 children = e.querySelectorAll( selector );
266
267 ret = _checkElements( self, children );
268 });
269
270 return ret;
271 };
272
273 function _checkElements(needles, haystack){
274 var ret = false;
275
276 needles.each(function() {
277 var j = 0;
278
279 while( j < haystack.length ){
280 if( this === haystack[j] ){
281 ret = true;
282 }
283
284 j++;
285 }
286 });
287
288 return ret;
289 }
290
291
292
293 /**
294 * Get data attached to the first element or set data values on all elements in the current set.
295 *
296 * @param {string} name The data attribute name.
297 * @param {any} value The value assigned to the data attribute.
298 * @return {any|shoestring}
299 * @this shoestring
300 */
301 shoestring.fn.data = function( name, value ){
302 if( name !== undefined ){
303 if( value !== undefined ){
304 return this.each(function(){
305 if( !this.shoestringData ){
306 this.shoestringData = {};
307 }
308
309 this.shoestringData[ name ] = value;
310 });
311 }
312 else {
313 if( this[ 0 ] ) {
314 if( this[ 0 ].shoestringData ) {
315 return this[ 0 ].shoestringData[ name ];
316 }
317 }
318 }
319 }
320 else {
321 return this[ 0 ] ? this[ 0 ].shoestringData || {} : undefined;
322 }
323 };
324
325
326 /**
327 * Remove data associated with `name` or all the data, for each element in the current set.
328 *
329 * @param {string} name The data attribute name.
330 * @return shoestring
331 * @this shoestring
332 */
333 shoestring.fn.removeData = function( name ){
334 return this.each(function(){
335 if( name !== undefined && this.shoestringData ){
336 this.shoestringData[ name ] = undefined;
337 delete this.shoestringData[ name ];
338 } else {
339 this[ 0 ].shoestringData = {};
340 }
341 });
342 };
343
344
345
346 /**
347 * An alias for the `shoestring` constructor.
348 */
349 win.$ = shoestring;
350
351
352
353 /**
354 * Add a class to each DOM element in the set of elements.
355 *
356 * @param {string} className The name of the class to be added.
357 * @return shoestring
358 * @this shoestring
359 */
360 shoestring.fn.addClass = function( className ){
361 var classes = className.replace(/^\s+|\s+$/g, '').split( " " );
362
363 return this.each(function(){
364 for( var i = 0, il = classes.length; i < il; i++ ){
365 if( this.className !== undefined &&
366 (this.className === "" ||
367 !this.className.match( new RegExp( "(^|\\s)" + classes[ i ] + "($|\\s)"))) ){
368 this.className += " " + classes[ i ];
369 }
370 }
371 });
372 };
373
374
375
376 /**
377 * Add elements matching the selector to the current set.
378 *
379 * @param {string} selector The selector for the elements to add from the DOM
380 * @return shoestring
381 * @this shoestring
382 */
383 shoestring.fn.add = function( selector ){
384 var ret = [];
385 this.each(function(){
386 ret.push( this );
387 });
388
389 shoestring( selector ).each(function(){
390 ret.push( this );
391 });
392
393 return shoestring( ret );
394 };
395
396
397
398 /**
399 * Insert an element or HTML string as the last child of each element in the set.
400 *
401 * @param {string|HTMLElement} fragment The HTML or HTMLElement to insert.
402 * @return shoestring
403 * @this shoestring
404 */
405 shoestring.fn.append = function( fragment ){
406 if( typeof( fragment ) === "string" || fragment.nodeType !== undefined ){
407 fragment = shoestring( fragment );
408 }
409
410 return this.each(function( i ){
411 for( var j = 0, jl = fragment.length; j < jl; j++ ){
412 this.appendChild( i > 0 ? fragment[ j ].cloneNode( true ) : fragment[ j ] );
413 }
414 });
415 };
416
417
418
419 /**
420 * Insert the current set as the last child of the elements matching the selector.
421 *
422 * @param {string} selector The selector after which to append the current set.
423 * @return shoestring
424 * @this shoestring
425 */
426 shoestring.fn.appendTo = function( selector ){
427 return this.each(function(){
428 shoestring( selector ).append( this );
429 });
430 };
431
432
433
434 /**
435 * Get the value of the first element of the set or set the value of all the elements in the set.
436 *
437 * @param {string} name The attribute name.
438 * @param {string} value The new value for the attribute.
439 * @return {shoestring|string|undefined}
440 * @this {shoestring}
441 */
442 shoestring.fn.attr = function( name, value ){
443 var nameStr = typeof( name ) === "string";
444
445 if( value !== undefined || !nameStr ){
446 return this.each(function(){
447 if( nameStr ){
448 this.setAttribute( name, value );
449 } else {
450 for( var i in name ){
451 if( name.hasOwnProperty( i ) ){
452 this.setAttribute( i, name[ i ] );
453 }
454 }
455 }
456 });
457 } else {
458 return this[ 0 ] ? this[ 0 ].getAttribute( name ) : undefined;
459 }
460 };
461
462
463
464 /**
465 * Insert an element or HTML string before each element in the current set.
466 *
467 * @param {string|HTMLElement} fragment The HTML or HTMLElement to insert.
468 * @return shoestring
469 * @this shoestring
470 */
471 shoestring.fn.before = function( fragment ){
472 if( typeof( fragment ) === "string" || fragment.nodeType !== undefined ){
473 fragment = shoestring( fragment );
474 }
475
476 return this.each(function( i ){
477 for( var j = 0, jl = fragment.length; j < jl; j++ ){
478 this.parentNode.insertBefore( i > 0 ? fragment[ j ].cloneNode( true ) : fragment[ j ], this );
479 }
480 });
481 };
482
483
484
485 /**
486 * Get the children of the current collection.
487 * @return shoestring
488 * @this shoestring
489 */
490 shoestring.fn.children = function(){
491 var ret = [],
492 childs,
493 j;
494 this.each(function(){
495 childs = this.children;
496 j = -1;
497
498 while( j++ < childs.length-1 ){
499 if( shoestring.inArray( childs[ j ], ret ) === -1 ){
500 ret.push( childs[ j ] );
501 }
502 }
503 });
504 return shoestring(ret);
505 };
506
507
508
509 /**
510 * Find an element matching the selector in the set of the current element and its parents.
511 *
512 * @param {string} selector The selector used to identify the target element.
513 * @return shoestring
514 * @this shoestring
515 */
516 shoestring.fn.closest = function( selector ){
517 var ret = [];
518
519 if( !selector ){
520 return shoestring( ret );
521 }
522
523 this.each(function(){
524 var element, $self = shoestring( element = this );
525
526 if( $self.is(selector) ){
527 ret.push( this );
528 return;
529 }
530
531 while( element.parentElement ) {
532 if( shoestring(element.parentElement).is(selector) ){
533 ret.push( element.parentElement );
534 break;
535 }
536
537 element = element.parentElement;
538 }
539 });
540
541 return shoestring( ret );
542 };
543
544
545
546 shoestring.cssExceptions = {
547 'float': [ 'cssFloat' ]
548 };
549
550
551
552 (function() {
553 var cssExceptions = shoestring.cssExceptions;
554
555 // IE8 uses marginRight instead of margin-right
556 function convertPropertyName( str ) {
557 return str.replace( /\-([A-Za-z])/g, function ( match, character ) {
558 return character.toUpperCase();
559 });
560 }
561
562 function _getStyle( element, property ) {
563 return win.getComputedStyle( element, null ).getPropertyValue( property );
564 }
565
566 var vendorPrefixes = [ '', '-webkit-', '-ms-', '-moz-', '-o-', '-khtml-' ];
567
568 /**
569 * Private function for getting the computed style of an element.
570 *
571 * **NOTE** Please use the [css](../css.js.html) method instead.
572 *
573 * @method _getStyle
574 * @param {HTMLElement} element The element we want the style property for.
575 * @param {string} property The css property we want the style for.
576 */
577 shoestring._getStyle = function( element, property ) {
578 var convert, value, j, k;
579
580 if( cssExceptions[ property ] ) {
581 for( j = 0, k = cssExceptions[ property ].length; j < k; j++ ) {
582 value = _getStyle( element, cssExceptions[ property ][ j ] );
583
584 if( value ) {
585 return value;
586 }
587 }
588 }
589
590 for( j = 0, k = vendorPrefixes.length; j < k; j++ ) {
591 convert = convertPropertyName( vendorPrefixes[ j ] + property );
592
593 // VendorprefixKeyName || key-name
594 value = _getStyle( element, convert );
595
596 if( convert !== property ) {
597 value = value || _getStyle( element, property );
598 }
599
600 if( vendorPrefixes[ j ] ) {
601 // -vendorprefix-key-name
602 value = value || _getStyle( element, vendorPrefixes[ j ] + property );
603 }
604
605 if( value ) {
606 return value;
607 }
608 }
609
610 return undefined;
611 };
612 })();
613
614
615
616 (function() {
617 var cssExceptions = shoestring.cssExceptions;
618
619 // IE8 uses marginRight instead of margin-right
620 function convertPropertyName( str ) {
621 return str.replace( /\-([A-Za-z])/g, function ( match, character ) {
622 return character.toUpperCase();
623 });
624 }
625
626 /**
627 * Private function for setting the style of an element.
628 *
629 * **NOTE** Please use the [css](../css.js.html) method instead.
630 *
631 * @method _setStyle
632 * @param {HTMLElement} element The element we want to style.
633 * @param {string} property The property being used to style the element.
634 * @param {string} value The css value for the style property.
635 */
636 shoestring._setStyle = function( element, property, value ) {
637 var convertedProperty = convertPropertyName(property);
638
639 element.style[ property ] = value;
640
641 if( convertedProperty !== property ) {
642 element.style[ convertedProperty ] = value;
643 }
644
645 if( cssExceptions[ property ] ) {
646 for( var j = 0, k = cssExceptions[ property ].length; j<k; j++ ) {
647 element.style[ cssExceptions[ property ][ j ] ] = value;
648 }
649 }
650 };
651 })();
652
653
654
655 /**
656 * Get the compute style property of the first element or set the value of a style property
657 * on all elements in the set.
658 *
659 * @method _setStyle
660 * @param {string} property The property being used to style the element.
661 * @param {string|undefined} value The css value for the style property.
662 * @return {string|shoestring}
663 * @this shoestring
664 */
665 shoestring.fn.css = function( property, value ){
666 if( !this[0] ){
667 return;
668 }
669
670 if( typeof property === "object" ) {
671 return this.each(function() {
672 for( var key in property ) {
673 if( property.hasOwnProperty( key ) ) {
674 shoestring._setStyle( this, key, property[key] );
675 }
676 }
677 });
678 } else {
679 // assignment else retrieve first
680 if( value !== undefined ){
681 return this.each(function(){
682 shoestring._setStyle( this, property, value );
683 });
684 }
685
686 return shoestring._getStyle( this[0], property );
687 }
688 };
689
690
691
692 /**
693 * Returns the indexed element wrapped in a new `shoestring` object.
694 *
695 * @param {integer} index The index of the element to wrap and return.
696 * @return shoestring
697 * @this shoestring
698 */
699 shoestring.fn.eq = function( index ){
700 if( this[index] ){
701 return shoestring( this[index] );
702 }
703
704 return shoestring([]);
705 };
706
707
708
709 /**
710 * Filter out the current set if they do *not* match the passed selector or
711 * the supplied callback returns false
712 *
713 * @param {string,function} selector The selector or boolean return value callback used to filter the elements.
714 * @return shoestring
715 * @this shoestring
716 */
717 shoestring.fn.filter = function( selector ){
718 var ret = [];
719
720 this.each(function( index ){
721 var wsel;
722
723 if( typeof selector === 'function' ) {
724 if( selector.call( this, index ) !== false ) {
725 ret.push( this );
726 }
727 } else {
728 if( !this.parentNode ){
729 var context = shoestring( doc.createDocumentFragment() );
730
731 context[ 0 ].appendChild( this );
732 wsel = shoestring( selector, context );
733 } else {
734 wsel = shoestring( selector, this.parentNode );
735 }
736
737 if( shoestring.inArray( this, wsel ) > -1 ){
738 ret.push( this );
739 }
740 }
741 });
742
743 return shoestring( ret );
744 };
745
746
747
748 /**
749 * Find descendant elements of the current collection.
750 *
751 * @param {string} selector The selector used to find the children
752 * @return shoestring
753 * @this shoestring
754 */
755 shoestring.fn.find = function( selector ){
756 var ret = [],
757 finds;
758 this.each(function(){
759 finds = this.querySelectorAll( selector );
760
761 for( var i = 0, il = finds.length; i < il; i++ ){
762 ret = ret.concat( finds[i] );
763 }
764 });
765 return shoestring( ret );
766 };
767
768
769
770 /**
771 * Returns the first element of the set wrapped in a new `shoestring` object.
772 *
773 * @return shoestring
774 * @this shoestring
775 */
776 shoestring.fn.first = function(){
777 return this.eq( 0 );
778 };
779
780
781
782 /**
783 * Returns the raw DOM node at the passed index.
784 *
785 * @param {integer} index The index of the element to wrap and return.
786 * @return {HTMLElement|undefined|array}
787 * @this shoestring
788 */
789 shoestring.fn.get = function( index ){
790
791 // return an array of elements if index is undefined
792 if( index === undefined ){
793 var elements = [];
794
795 for( var i = 0; i < this.length; i++ ){
796 elements.push( this[ i ] );
797 }
798
799 return elements;
800 } else {
801 return this[ index ];
802 }
803 };
804
805
806
807 var set = function( html ){
808 if( typeof html === "string" || typeof html === "number" ){
809 return this.each(function(){
810 this.innerHTML = "" + html;
811 });
812 } else {
813 var h = "";
814 if( typeof html.length !== "undefined" ){
815 for( var i = 0, l = html.length; i < l; i++ ){
816 h += html[i].outerHTML;
817 }
818 } else {
819 h = html.outerHTML;
820 }
821 return this.each(function(){
822 this.innerHTML = h;
823 });
824 }
825 };
826 /**
827 * Gets or sets the `innerHTML` from all the elements in the set.
828 *
829 * @param {string|undefined} html The html to assign
830 * @return {string|shoestring}
831 * @this shoestring
832 */
833 shoestring.fn.html = function( html ){
834 if( typeof html !== "undefined" ){
835 return set.call( this, html );
836 } else { // get
837 var pile = "";
838
839 this.each(function(){
840 pile += this.innerHTML;
841 });
842
843 return pile;
844 }
845 };
846
847
848
849 (function() {
850 function _getIndex( set, test ) {
851 var i, result, element;
852
853 for( i = result = 0; i < set.length; i++ ) {
854 element = set.item ? set.item(i) : set[i];
855
856 if( test(element) ){
857 return result;
858 }
859
860 // ignore text nodes, etc
861 // NOTE may need to be more permissive
862 if( element.nodeType === 1 ){
863 result++;
864 }
865 }
866
867 return -1;
868 }
869
870 /**
871 * Find the index in the current set for the passed selector.
872 * Without a selector it returns the index of the first node within the array of its siblings.
873 *
874 * @param {string|undefined} selector The selector used to search for the index.
875 * @return {integer}
876 * @this {shoestring}
877 */
878 shoestring.fn.index = function( selector ){
879 var self, children;
880
881 self = this;
882
883 // no arg? check the children, otherwise check each element that matches
884 if( selector === undefined ){
885 children = ( ( this[ 0 ] && this[0].parentNode ) || doc.documentElement).childNodes;
886
887 // check if the element matches the first of the set
888 return _getIndex(children, function( element ) {
889 return self[0] === element;
890 });
891 } else {
892
893 // check if the element matches the first selected node from the parent
894 return _getIndex(self, function( element ) {
895 return element === (shoestring( selector, element.parentNode )[ 0 ]);
896 });
897 }
898 };
899 })();
900
901
902
903 /**
904 * Insert the current set before the elements matching the selector.
905 *
906 * @param {string} selector The selector before which to insert the current set.
907 * @return shoestring
908 * @this shoestring
909 */
910 shoestring.fn.insertBefore = function( selector ){
911 return this.each(function(){
912 shoestring( selector ).before( this );
913 });
914 };
915
916
917
918 /**
919 * Returns the last element of the set wrapped in a new `shoestring` object.
920 *
921 * @return shoestring
922 * @this shoestring
923 */
924 shoestring.fn.last = function(){
925 return this.eq( this.length - 1 );
926 };
927
928
929
930 /**
931 * Returns a `shoestring` object with the set of siblings of each element in the original set.
932 *
933 * @return shoestring
934 * @this shoestring
935 */
936 shoestring.fn.next = function(){
937
938 var result = [];
939
940 // TODO need to implement map
941 this.each(function() {
942 var children, item, found;
943
944 // get the child nodes for this member of the set
945 children = shoestring( this.parentNode )[0].childNodes;
946
947 for( var i = 0; i < children.length; i++ ){
948 item = children.item( i );
949
950 // found the item we needed (found) which means current item value is
951 // the next node in the list, as long as it's viable grab it
952 // NOTE may need to be more permissive
953 if( found && item.nodeType === 1 ){
954 result.push( item );
955 break;
956 }
957
958 // find the current item and mark it as found
959 if( item === this ){
960 found = true;
961 }
962 }
963 });
964
965 return shoestring( result );
966 };
967
968
969
970 /**
971 * Removes elements from the current set.
972 *
973 * @param {string} selector The selector to use when removing the elements.
974 * @return shoestring
975 * @this shoestring
976 */
977 shoestring.fn.not = function( selector ){
978 var ret = [];
979
980 this.each(function(){
981 var found = shoestring( selector, this.parentNode );
982
983 if( shoestring.inArray(this, found) === -1 ){
984 ret.push( this );
985 }
986 });
987
988 return shoestring( ret );
989 };
990
991
992
993 /**
994 * Returns the set of first parents for each element in the current set.
995 *
996 * @return shoestring
997 * @this shoestring
998 */
999 shoestring.fn.parent = function(){
1000 var ret = [],
1001 parent;
1002
1003 this.each(function(){
1004 // no parent node, assume top level
1005 // jQuery parent: return the document object for <html> or the parent node if it exists
1006 parent = (this === doc.documentElement ? doc : this.parentNode);
1007
1008 // if there is a parent and it's not a document fragment
1009 if( parent && parent.nodeType !== 11 ){
1010 ret.push( parent );
1011 }
1012 });
1013
1014 return shoestring(ret);
1015 };
1016
1017
1018
1019 /**
1020 * Add an HTML string or element before the children of each element in the current set.
1021 *
1022 * @param {string|HTMLElement} fragment The HTML string or element to add.
1023 * @return shoestring
1024 * @this shoestring
1025 */
1026 shoestring.fn.prepend = function( fragment ){
1027 if( typeof( fragment ) === "string" || fragment.nodeType !== undefined ){
1028 fragment = shoestring( fragment );
1029 }
1030
1031 return this.each(function( i ){
1032
1033 for( var j = 0, jl = fragment.length; j < jl; j++ ){
1034 var insertEl = i > 0 ? fragment[ j ].cloneNode( true ) : fragment[ j ];
1035 if ( this.firstChild ){
1036 this.insertBefore( insertEl, this.firstChild );
1037 } else {
1038 this.appendChild( insertEl );
1039 }
1040 }
1041 });
1042 };
1043
1044
1045
1046 /**
1047 * Returns a `shoestring` object with the set of *one* siblingx before each element in the original set.
1048 *
1049 * @return shoestring
1050 * @this shoestring
1051 */
1052 shoestring.fn.prev = function(){
1053
1054 var result = [];
1055
1056 // TODO need to implement map
1057 this.each(function() {
1058 var children, item, found;
1059
1060 // get the child nodes for this member of the set
1061 children = shoestring( this.parentNode )[0].childNodes;
1062
1063 for( var i = children.length -1; i >= 0; i-- ){
1064 item = children.item( i );
1065
1066 // found the item we needed (found) which means current item value is
1067 // the next node in the list, as long as it's viable grab it
1068 // NOTE may need to be more permissive
1069 if( found && item.nodeType === 1 ){
1070 result.push( item );
1071 break;
1072 }
1073
1074 // find the current item and mark it as found
1075 if( item === this ){
1076 found = true;
1077 }
1078 }
1079 });
1080
1081 return shoestring( result );
1082 };
1083
1084
1085
1086 /**
1087 * Returns a `shoestring` object with the set of *all* siblings before each element in the original set.
1088 *
1089 * @return shoestring
1090 * @this shoestring
1091 */
1092 shoestring.fn.prevAll = function(){
1093
1094 var result = [];
1095
1096 this.each(function() {
1097 var $previous = shoestring( this ).prev();
1098
1099 while( $previous.length ){
1100 result.push( $previous[0] );
1101 $previous = $previous.prev();
1102 }
1103 });
1104
1105 return shoestring( result );
1106 };
1107
1108
1109
1110 /**
1111 * Remove an attribute from each element in the current set.
1112 *
1113 * @param {string} name The name of the attribute.
1114 * @return shoestring
1115 * @this shoestring
1116 */
1117 shoestring.fn.removeAttr = function( name ){
1118 return this.each(function(){
1119 this.removeAttribute( name );
1120 });
1121 };
1122
1123
1124
1125 /**
1126 * Remove a class from each DOM element in the set of elements.
1127 *
1128 * @param {string} className The name of the class to be removed.
1129 * @return shoestring
1130 * @this shoestring
1131 */
1132 shoestring.fn.removeClass = function( cname ){
1133 var classes = cname.replace(/^\s+|\s+$/g, '').split( " " );
1134
1135 return this.each(function(){
1136 var newClassName, regex;
1137
1138 for( var i = 0, il = classes.length; i < il; i++ ){
1139 if( this.className !== undefined ){
1140 regex = new RegExp( "(^|\\s)" + classes[ i ] + "($|\\s)", "gmi" );
1141 newClassName = this.className.replace( regex, " " );
1142
1143 this.className = newClassName.replace(/^\s+|\s+$/g, '');
1144 }
1145 }
1146 });
1147 };
1148
1149
1150
1151 /**
1152 * Remove the current set of elements from the DOM.
1153 *
1154 * @return shoestring
1155 * @this shoestring
1156 */
1157 shoestring.fn.remove = function(){
1158 return this.each(function(){
1159 if( this.parentNode ) {
1160 this.parentNode.removeChild( this );
1161 }
1162 });
1163 };
1164
1165
1166
1167 /**
1168 * Replace each element in the current set with that argument HTML string or HTMLElement.
1169 *
1170 * @param {string|HTMLElement} fragment The value to assign.
1171 * @return shoestring
1172 * @this shoestring
1173 */
1174 shoestring.fn.replaceWith = function( fragment ){
1175 if( typeof( fragment ) === "string" ){
1176 fragment = shoestring( fragment );
1177 }
1178
1179 var ret = [];
1180
1181 if( fragment.length > 1 ){
1182 fragment = fragment.reverse();
1183 }
1184 this.each(function( i ){
1185 var clone = this.cloneNode( true ),
1186 insertEl;
1187 ret.push( clone );
1188
1189 // If there is no parentNode, this is pointless, drop it.
1190 if( !this.parentNode ){ return; }
1191
1192 if( fragment.length === 1 ){
1193 insertEl = i > 0 ? fragment[ 0 ].cloneNode( true ) : fragment[ 0 ];
1194 this.parentNode.replaceChild( insertEl, this );
1195 } else {
1196 for( var j = 0, jl = fragment.length; j < jl; j++ ){
1197 insertEl = i > 0 ? fragment[ j ].cloneNode( true ) : fragment[ j ];
1198 this.parentNode.insertBefore( insertEl, this.nextSibling );
1199 }
1200 this.parentNode.removeChild( this );
1201 }
1202 });
1203
1204 return shoestring( ret );
1205 };
1206
1207
1208
1209 /**
1210 * Get all of the sibling elements for each element in the current set.
1211 *
1212 * @return shoestring
1213 * @this shoestring
1214 */
1215 shoestring.fn.siblings = function(){
1216
1217 if( !this.length ) {
1218 return shoestring( [] );
1219 }
1220
1221 var sibs = [], el = this[ 0 ].parentNode.firstChild;
1222
1223 do {
1224 if( el.nodeType === 1 && el !== this[ 0 ] ) {
1225 sibs.push( el );
1226 }
1227
1228 el = el.nextSibling;
1229 } while( el );
1230
1231 return shoestring( sibs );
1232 };
1233
1234
1235
1236 var getText = function( elem ){
1237 var node,
1238 ret = "",
1239 i = 0,
1240 nodeType = elem.nodeType;
1241
1242 if ( !nodeType ) {
1243 // If no nodeType, this is expected to be an array
1244 while ( (node = elem[i++]) ) {
1245 // Do not traverse comment nodes
1246 ret += getText( node );
1247 }
1248 } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
1249 // Use textContent for elements
1250 // innerText usage removed for consistency of new lines (jQuery #11153)
1251 if ( typeof elem.textContent === "string" ) {
1252 return elem.textContent;
1253 } else {
1254 // Traverse its children
1255 for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) {
1256 ret += getText( elem );
1257 }
1258 }
1259 } else if ( nodeType === 3 || nodeType === 4 ) {
1260 return elem.nodeValue;
1261 }
1262 // Do not include comment or processing instruction nodes
1263
1264 return ret;
1265 };
1266
1267 /**
1268 * Recursively retrieve the text content of the each element in the current set.
1269 *
1270 * @return shoestring
1271 * @this shoestring
1272 */
1273 shoestring.fn.text = function() {
1274
1275 return getText( this );
1276 };
1277
1278
1279
1280
1281 /**
1282 * Get the value of the first element or set the value of all elements in the current set.
1283 *
1284 * @param {string} value The value to set.
1285 * @return shoestring
1286 * @this shoestring
1287 */
1288 shoestring.fn.val = function( value ){
1289 var el;
1290 if( value !== undefined ){
1291 return this.each(function(){
1292 if( this.tagName === "SELECT" ){
1293 var optionSet, option,
1294 options = this.options,
1295 values = [],
1296 i = options.length,
1297 newIndex;
1298
1299 values[0] = value;
1300 while ( i-- ) {
1301 option = options[ i ];
1302 if ( (option.selected = shoestring.inArray( option.value, values ) >= 0) ) {
1303 optionSet = true;
1304 newIndex = i;
1305 }
1306 }
1307 // force browsers to behave consistently when non-matching value is set
1308 if ( !optionSet ) {
1309 this.selectedIndex = -1;
1310 } else {
1311 this.selectedIndex = newIndex;
1312 }
1313 } else {
1314 this.value = value;
1315 }
1316 });
1317 } else {
1318 el = this[0];
1319
1320 if( el.tagName === "SELECT" ){
1321 if( el.selectedIndex < 0 ){ return ""; }
1322 return el.options[ el.selectedIndex ].value;
1323 } else {
1324 return el.value;
1325 }
1326 }
1327 };
1328
1329
1330
1331 /**
1332 * Private function for setting/getting the offset property for height/width.
1333 *
1334 * **NOTE** Please use the [width](width.js.html) or [height](height.js.html) methods instead.
1335 *
1336 * @param {shoestring} set The set of elements.
1337 * @param {string} name The string "height" or "width".
1338 * @param {float|undefined} value The value to assign.
1339 * @return shoestring
1340 * @this window
1341 */
1342 shoestring._dimension = function( set, name, value ){
1343 var offsetName;
1344
1345 if( value === undefined ){
1346 offsetName = name.replace(/^[a-z]/, function( letter ) {
1347 return letter.toUpperCase();
1348 });
1349
1350 return set[ 0 ][ "offset" + offsetName ];
1351 } else {
1352 // support integer values as pixels
1353 value = typeof value === "string" ? value : value + "px";
1354
1355 return set.each(function(){
1356 this.style[ name ] = value;
1357 });
1358 }
1359 };
1360
1361
1362
1363 /**
1364 * Gets the width value of the first element or sets the width for the whole set.
1365 *
1366 * @param {float|undefined} value The value to assign.
1367 * @return shoestring
1368 * @this shoestring
1369 */
1370 shoestring.fn.width = function( value ){
1371 return shoestring._dimension( this, "width", value );
1372 };
1373
1374
1375
1376 /**
1377 * Wraps the child elements in the provided HTML.
1378 *
1379 * @param {string} html The wrapping HTML.
1380 * @return shoestring
1381 * @this shoestring
1382 */
1383 shoestring.fn.wrapInner = function( html ){
1384 return this.each(function(){
1385 var inH = this.innerHTML;
1386
1387 this.innerHTML = "";
1388 shoestring( this ).append( shoestring( html ).html( inH ) );
1389 });
1390 };
1391
1392
1393
1394 function initEventCache( el, evt ) {
1395 if ( !el.shoestringData ) {
1396 el.shoestringData = {};
1397 }
1398 if ( !el.shoestringData.events ) {
1399 el.shoestringData.events = {};
1400 }
1401 if ( !el.shoestringData.loop ) {
1402 el.shoestringData.loop = {};
1403 }
1404 if ( !el.shoestringData.events[ evt ] ) {
1405 el.shoestringData.events[ evt ] = [];
1406 }
1407 }
1408
1409 function addToEventCache( el, evt, eventInfo ) {
1410 var obj = {};
1411 obj.isCustomEvent = eventInfo.isCustomEvent;
1412 obj.callback = eventInfo.callfunc;
1413 obj.originalCallback = eventInfo.originalCallback;
1414 obj.namespace = eventInfo.namespace;
1415
1416 el.shoestringData.events[ evt ].push( obj );
1417
1418 if( eventInfo.customEventLoop ) {
1419 el.shoestringData.loop[ evt ] = eventInfo.customEventLoop;
1420 }
1421 }
1422
1423 /**
1424 * Bind a callback to an event for the currrent set of elements.
1425 *
1426 * @param {string} evt The event(s) to watch for.
1427 * @param {object,function} data Data to be included with each event or the callback.
1428 * @param {function} originalCallback Callback to be invoked when data is define.d.
1429 * @return shoestring
1430 * @this shoestring
1431 */
1432 shoestring.fn.bind = function( evt, data, originalCallback ){
1433
1434 if( typeof data === "function" ){
1435 originalCallback = data;
1436 data = null;
1437 }
1438
1439 var evts = evt.split( " " );
1440
1441 // NOTE the `triggeredElement` is purely for custom events from IE
1442 function encasedCallback( e, namespace, triggeredElement ){
1443 var result;
1444
1445 if( e._namespace && e._namespace !== namespace ) {
1446 return;
1447 }
1448
1449 e.data = data;
1450 e.namespace = e._namespace;
1451
1452 var returnTrue = function(){
1453 return true;
1454 };
1455
1456 e.isDefaultPrevented = function(){
1457 return false;
1458 };
1459
1460 var originalPreventDefault = e.preventDefault;
1461 var preventDefaultConstructor = function(){
1462 if( originalPreventDefault ) {
1463 return function(){
1464 e.isDefaultPrevented = returnTrue;
1465 originalPreventDefault.call(e);
1466 };
1467 } else {
1468 return function(){
1469 e.isDefaultPrevented = returnTrue;
1470 e.returnValue = false;
1471 };
1472 }
1473 };
1474
1475 // thanks https://github.com/jonathantneal/EventListener
1476 e.target = triggeredElement || e.target || e.srcElement;
1477 e.preventDefault = preventDefaultConstructor();
1478 e.stopPropagation = e.stopPropagation || function () {
1479 e.cancelBubble = true;
1480 };
1481
1482 result = originalCallback.apply(this, [ e ].concat( e._args ) );
1483
1484 if( result === false ){
1485 e.preventDefault();
1486 e.stopPropagation();
1487 }
1488
1489 return result;
1490 }
1491
1492 return this.each(function(){
1493 var domEventCallback,
1494 customEventCallback,
1495 customEventLoop,
1496 oEl = this;
1497
1498 for( var i = 0, il = evts.length; i < il; i++ ){
1499 var split = evts[ i ].split( "." ),
1500 evt = split[ 0 ],
1501 namespace = split.length > 0 ? split[ 1 ] : null;
1502
1503 domEventCallback = function( originalEvent ) {
1504 if( oEl.ssEventTrigger ) {
1505 originalEvent._namespace = oEl.ssEventTrigger._namespace;
1506 originalEvent._args = oEl.ssEventTrigger._args;
1507
1508 oEl.ssEventTrigger = null;
1509 }
1510 return encasedCallback.call( oEl, originalEvent, namespace );
1511 };
1512 customEventCallback = null;
1513 customEventLoop = null;
1514
1515 initEventCache( this, evt );
1516
1517 this.addEventListener( evt, domEventCallback, false );
1518
1519 addToEventCache( this, evt, {
1520 callfunc: customEventCallback || domEventCallback,
1521 isCustomEvent: !!customEventCallback,
1522 customEventLoop: customEventLoop,
1523 originalCallback: originalCallback,
1524 namespace: namespace
1525 });
1526 }
1527 });
1528 };
1529
1530 shoestring.fn.on = shoestring.fn.bind;
1531
1532
1533
1534
1535 /**
1536 * Unbind a previous bound callback for an event.
1537 *
1538 * @param {string} event The event(s) the callback was bound to..
1539 * @param {function} callback Callback to unbind.
1540 * @return shoestring
1541 * @this shoestring
1542 */
1543 shoestring.fn.unbind = function( event, callback ){
1544
1545
1546 var evts = event ? event.split( " " ) : [];
1547
1548 return this.each(function(){
1549 if( !this.shoestringData || !this.shoestringData.events ) {
1550 return;
1551 }
1552
1553 if( !evts.length ) {
1554 unbindAll.call( this );
1555 } else {
1556 var split, evt, namespace;
1557 for( var i = 0, il = evts.length; i < il; i++ ){
1558 split = evts[ i ].split( "." ),
1559 evt = split[ 0 ],
1560 namespace = split.length > 0 ? split[ 1 ] : null;
1561
1562 if( evt ) {
1563 unbind.call( this, evt, namespace, callback );
1564 } else {
1565 unbindAll.call( this, namespace, callback );
1566 }
1567 }
1568 }
1569 });
1570 };
1571
1572 function unbind( evt, namespace, callback ) {
1573 var bound = this.shoestringData.events[ evt ];
1574 if( !(bound && bound.length) ) {
1575 return;
1576 }
1577
1578 var matched = [], j, jl;
1579 for( j = 0, jl = bound.length; j < jl; j++ ) {
1580 if( !namespace || namespace === bound[ j ].namespace ) {
1581 if( callback === undefined || callback === bound[ j ].originalCallback ) {
1582 this.removeEventListener( evt, bound[ j ].callback, false );
1583 matched.push( j );
1584 }
1585 }
1586 }
1587
1588 for( j = 0, jl = matched.length; j < jl; j++ ) {
1589 this.shoestringData.events[ evt ].splice( j, 1 );
1590 }
1591 }
1592
1593 function unbindAll( namespace, callback ) {
1594 for( var evtKey in this.shoestringData.events ) {
1595 unbind.call( this, evtKey, namespace, callback );
1596 }
1597 }
1598
1599 shoestring.fn.off = shoestring.fn.unbind;
1600
1601
1602 /**
1603 * Bind a callback to an event for the currrent set of elements, unbind after one occurence.
1604 *
1605 * @param {string} event The event(s) to watch for.
1606 * @param {function} callback Callback to invoke on the event.
1607 * @return shoestring
1608 * @this shoestring
1609 */
1610 shoestring.fn.one = function( event, callback ){
1611 var evts = event.split( " " );
1612
1613 return this.each(function(){
1614 var thisevt, cbs = {}, $t = shoestring( this );
1615
1616 for( var i = 0, il = evts.length; i < il; i++ ){
1617 thisevt = evts[ i ];
1618
1619 cbs[ thisevt ] = function( e ){
1620 var $t = shoestring( this );
1621
1622 for( var j in cbs ) {
1623 $t.unbind( j, cbs[ j ] );
1624 }
1625
1626 return callback.apply( this, [ e ].concat( e._args ) );
1627 };
1628
1629 $t.bind( thisevt, cbs[ thisevt ] );
1630 }
1631 });
1632 };
1633
1634
1635
1636 /**
1637 * Trigger an event on the first element in the set, no bubbling, no defaults.
1638 *
1639 * @param {string} event The event(s) to trigger.
1640 * @param {object} args Arguments to append to callback invocations.
1641 * @return shoestring
1642 * @this shoestring
1643 */
1644 shoestring.fn.triggerHandler = function( event, args ){
1645 var e = event.split( " " )[ 0 ],
1646 el = this[ 0 ],
1647 ret;
1648
1649 // See this.fireEvent( 'on' + evts[ i ], document.createEventObject() ); instead of click() etc in trigger.
1650 if( doc.createEvent && el.shoestringData && el.shoestringData.events && el.shoestringData.events[ e ] ){
1651 var bindings = el.shoestringData.events[ e ];
1652 for (var i in bindings ){
1653 if( bindings.hasOwnProperty( i ) ){
1654 event = doc.createEvent( "Event" );
1655 event.initEvent( e, true, true );
1656 event._args = args;
1657 args.unshift( event );
1658
1659 ret = bindings[ i ].originalCallback.apply( event.target, args );
1660 }
1661 }
1662 }
1663
1664 return ret;
1665 };
1666
1667
1668
1669 /**
1670 * Trigger an event on each of the DOM elements in the current set.
1671 *
1672 * @param {string} event The event(s) to trigger.
1673 * @param {object} args Arguments to append to callback invocations.
1674 * @return shoestring
1675 * @this shoestring
1676 */
1677 shoestring.fn.trigger = function( event, args ){
1678 var evts = event.split( " " );
1679
1680 return this.each(function(){
1681 var split, evt, namespace;
1682 for( var i = 0, il = evts.length; i < il; i++ ){
1683 split = evts[ i ].split( "." ),
1684 evt = split[ 0 ],
1685 namespace = split.length > 0 ? split[ 1 ] : null;
1686
1687 if( evt === "click" ){
1688 if( this.tagName === "INPUT" && this.type === "checkbox" && this.click ){
1689 this.click();
1690 return false;
1691 }
1692 }
1693
1694 if( doc.createEvent ){
1695 var event = doc.createEvent( "Event" );
1696 event.initEvent( evt, true, true );
1697 event._args = args;
1698 event._namespace = namespace;
1699
1700 this.dispatchEvent( event );
1701 }
1702 }
1703 });
1704 };
1705
1706
1707
1708 return shoestring;
1709 }));
1710
1711 (function (root, factory) {
1712 if (typeof define === 'function' && define.amd) {
1713 define(["shoestring"], function (shoestring) {
1714 return (root.Tablesaw = factory(shoestring, root));
1715 });
1716 } else if (typeof exports === 'object') {
1717 module.exports = factory(require('shoestring'), root);
1718 } else {
1719 root.Tablesaw = factory(shoestring, root);
1720 }
1721 }(typeof window !== "undefined" ? window : this, function ($, window) {
1722 "use strict";
1723
1724 var document = window.document;
1725 var domContentLoadedTriggered = false;
1726 document.addEventListener("DOMContentLoaded", function() {
1727 domContentLoadedTriggered = true;
1728 });
1729
1730 var Tablesaw = {
1731 i18n: {
1732 modeStack: "Stack",
1733 modeSwipe: "Swipe",
1734 modeToggle: "Toggle",
1735 modeSwitchColumnsAbbreviated: "Cols",
1736 modeSwitchColumns: "Columns",
1737 columnToggleButton: "Columns",
1738 columnToggleError: "No eligible columns.",
1739 sort: "Sort",
1740 swipePreviousColumn: "Previous column",
1741 swipeNextColumn: "Next column"
1742 },
1743 // cut the mustard
1744 mustard:
1745 "head" in document && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+
1746 (!window.blackberry || window.WebKitPoint) && // only WebKit Blackberry (OS 6+)
1747 !window.operamini,
1748 $: $,
1749 _init: function(element) {
1750 Tablesaw.$(element || document).trigger("enhance.tablesaw");
1751 },
1752 init: function(element) {
1753 if (!domContentLoadedTriggered) {
1754 if ("addEventListener" in document) {
1755 // Use raw DOMContentLoaded instead of shoestring (may have issues in Android 2.3, exhibited by stack table)
1756 document.addEventListener("DOMContentLoaded", function() {
1757 Tablesaw._init(element);
1758 });
1759 }
1760 } else {
1761 Tablesaw._init(element);
1762 }
1763 }
1764 };
1765
1766 $(document).on("enhance.tablesaw", function() {
1767 // Extend i18n config, if one exists.
1768 if (typeof TablesawConfig !== "undefined" && TablesawConfig.i18n) {
1769 Tablesaw.i18n = $.extend(Tablesaw.i18n, TablesawConfig.i18n || {});
1770 }
1771
1772 Tablesaw.i18n.modes = [
1773 Tablesaw.i18n.modeStack,
1774 Tablesaw.i18n.modeSwipe,
1775 Tablesaw.i18n.modeToggle
1776 ];
1777 });
1778
1779 if (Tablesaw.mustard) {
1780 $(document.documentElement).addClass("tablesaw-enhanced");
1781 }
1782
1783 (function() {
1784 var pluginName = "tablesaw";
1785 var classes = {
1786 toolbar: "tablesaw-bar"
1787 };
1788 var events = {
1789 create: "tablesawcreate",
1790 destroy: "tablesawdestroy",
1791 refresh: "tablesawrefresh",
1792 resize: "tablesawresize"
1793 };
1794 var defaultMode = "stack";
1795 var initSelector = "table";
1796 var initFilterSelector = "[data-tablesaw],[data-tablesaw-mode],[data-tablesaw-sortable]";
1797 var defaultConfig = {};
1798
1799 Tablesaw.events = events;
1800
1801 var Table = function(element) {
1802 if (!element) {
1803 throw new Error("Tablesaw requires an element.");
1804 }
1805
1806 this.table = element;
1807 this.$table = $(element);
1808
1809 // only one <thead> and <tfoot> are allowed, per the specification
1810 this.$thead = this.$table
1811 .children()
1812 .filter("thead")
1813 .eq(0);
1814
1815 // multiple <tbody> are allowed, per the specification
1816 this.$tbody = this.$table.children().filter("tbody");
1817
1818 this.mode = this.$table.attr("data-tablesaw-mode") || defaultMode;
1819
1820 this.$toolbar = null;
1821
1822 this.attributes = {
1823 subrow: "data-tablesaw-subrow",
1824 ignorerow: "data-tablesaw-ignorerow"
1825 };
1826
1827 this.init();
1828 };
1829
1830 Table.prototype.init = function() {
1831 if (!this.$thead.length) {
1832 throw new Error("tablesaw: a <thead> is required, but none was found.");
1833 }
1834
1835 if (!this.$thead.find("th").length) {
1836 throw new Error("tablesaw: no header cells found. Are you using <th> inside of <thead>?");
1837 }
1838
1839 // assign an id if there is none
1840 if (!this.$table.attr("id")) {
1841 this.$table.attr("id", pluginName + "-" + Math.round(Math.random() * 10000));
1842 }
1843
1844 this.createToolbar();
1845
1846 this._initCells();
1847
1848 this.$table.data(pluginName, this);
1849
1850 this.$table.trigger(events.create, [this]);
1851 };
1852
1853 Table.prototype.getConfig = function(pluginSpecificConfig) {
1854 // shoestring extend doesn’t support arbitrary args
1855 var configs = $.extend(defaultConfig, pluginSpecificConfig || {});
1856 return $.extend(configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {});
1857 };
1858
1859 Table.prototype._getPrimaryHeaderRow = function() {
1860 return this._getHeaderRows().eq(0);
1861 };
1862
1863 Table.prototype._getHeaderRows = function() {
1864 return this.$thead
1865 .children()
1866 .filter("tr")
1867 .filter(function() {
1868 return !$(this).is("[data-tablesaw-ignorerow]");
1869 });
1870 };
1871
1872 Table.prototype._getRowIndex = function($row) {
1873 return $row.prevAll().length;
1874 };
1875
1876 Table.prototype._getHeaderRowIndeces = function() {
1877 var self = this;
1878 var indeces = [];
1879 this._getHeaderRows().each(function() {
1880 indeces.push(self._getRowIndex($(this)));
1881 });
1882 return indeces;
1883 };
1884
1885 Table.prototype._getPrimaryHeaderCells = function($row) {
1886 return ($row || this._getPrimaryHeaderRow()).find("th");
1887 };
1888
1889 Table.prototype._$getCells = function(th) {
1890 var self = this;
1891 return $(th)
1892 .add(th.cells)
1893 .filter(function() {
1894 var $t = $(this);
1895 var $row = $t.parent();
1896 var hasColspan = $t.is("[colspan]");
1897 // no subrows or ignored rows (keep cells in ignored rows that do not have a colspan)
1898 return (
1899 !$row.is("[" + self.attributes.subrow + "]") &&
1900 (!$row.is("[" + self.attributes.ignorerow + "]") || !hasColspan)
1901 );
1902 });
1903 };
1904
1905 Table.prototype._getVisibleColspan = function() {
1906 var colspan = 0;
1907 this._getPrimaryHeaderCells().each(function() {
1908 var $t = $(this);
1909 if ($t.css("display") !== "none") {
1910 colspan += parseInt($t.attr("colspan"), 10) || 1;
1911 }
1912 });
1913 return colspan;
1914 };
1915
1916 Table.prototype.getColspanForCell = function($cell) {
1917 var visibleColspan = this._getVisibleColspan();
1918 var visibleSiblingColumns = 0;
1919 if ($cell.closest("tr").data("tablesaw-rowspanned")) {
1920 visibleSiblingColumns++;
1921 }
1922
1923 $cell.siblings().each(function() {
1924 var $t = $(this);
1925 var colColspan = parseInt($t.attr("colspan"), 10) || 1;
1926
1927 if ($t.css("display") !== "none") {
1928 visibleSiblingColumns += colColspan;
1929 }
1930 });
1931 // console.log( $cell[ 0 ], visibleColspan, visibleSiblingColumns );
1932
1933 return visibleColspan - visibleSiblingColumns;
1934 };
1935
1936 Table.prototype.isCellInColumn = function(header, cell) {
1937 return $(header)
1938 .add(header.cells)
1939 .filter(function() {
1940 return this === cell;
1941 }).length;
1942 };
1943
1944 Table.prototype.updateColspanCells = function(cls, header, userAction) {
1945 var self = this;
1946 var primaryHeaderRow = self._getPrimaryHeaderRow();
1947
1948 // find persistent column rowspans
1949 this.$table.find("[rowspan][data-tablesaw-priority]").each(function() {
1950 var $t = $(this);
1951 if ($t.attr("data-tablesaw-priority") !== "persist") {
1952 return;
1953 }
1954
1955 var $row = $t.closest("tr");
1956 var rowspan = parseInt($t.attr("rowspan"), 10);
1957 if (rowspan > 1) {
1958 $row = $row.next();
1959
1960 $row.data("tablesaw-rowspanned", true);
1961
1962 rowspan--;
1963 }
1964 });
1965
1966 this.$table
1967 .find("[colspan],[data-tablesaw-maxcolspan]")
1968 .filter(function() {
1969 // is not in primary header row
1970 return $(this).closest("tr")[0] !== primaryHeaderRow[0];
1971 })
1972 .each(function() {
1973 var $cell = $(this);
1974
1975 if (userAction === undefined || self.isCellInColumn(header, this)) {
1976 } else {
1977 // if is not a user action AND the cell is not in the updating column, kill it
1978 return;
1979 }
1980
1981 var colspan = self.getColspanForCell($cell);
1982
1983 if (cls && userAction !== undefined) {
1984 // console.log( colspan === 0 ? "addClass" : "removeClass", $cell );
1985 $cell[colspan === 0 ? "addClass" : "removeClass"](cls);
1986 }
1987
1988 // cache original colspan
1989 var maxColspan = parseInt($cell.attr("data-tablesaw-maxcolspan"), 10);
1990 if (!maxColspan) {
1991 $cell.attr("data-tablesaw-maxcolspan", $cell.attr("colspan"));
1992 } else if (colspan > maxColspan) {
1993 colspan = maxColspan;
1994 }
1995
1996 // console.log( this, "setting colspan to ", colspan );
1997 $cell.attr("colspan", colspan);
1998 });
1999 };
2000
2001 Table.prototype._findPrimaryHeadersForCell = function(cell) {
2002 var $headerRow = this._getPrimaryHeaderRow();
2003 var $headers = this._getPrimaryHeaderCells($headerRow);
2004 var headerRowIndex = this._getRowIndex($headerRow);
2005 var results = [];
2006
2007 for (var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++) {
2008 if (rowNumber === headerRowIndex) {
2009 continue;
2010 }
2011 for (var colNumber = 0; colNumber < this.headerMapping[rowNumber].length; colNumber++) {
2012 if (this.headerMapping[rowNumber][colNumber] === cell) {
2013 results.push($headers[colNumber]);
2014 }
2015 }
2016 }
2017 return results;
2018 };
2019
2020 // used by init cells
2021 Table.prototype.getRows = function() {
2022 var self = this;
2023 return this.$table.find("tr").filter(function() {
2024 return $(this)
2025 .closest("table")
2026 .is(self.$table);
2027 });
2028 };
2029
2030 // used by sortable
2031 Table.prototype.getBodyRows = function(tbody) {
2032 return (tbody ? $(tbody) : this.$tbody).children().filter("tr");
2033 };
2034
2035 Table.prototype.getHeaderCellIndex = function(cell) {
2036 var lookup = this.headerMapping[0];
2037 for (var colIndex = 0; colIndex < lookup.length; colIndex++) {
2038 if (lookup[colIndex] === cell) {
2039 return colIndex;
2040 }
2041 }
2042
2043 return -1;
2044 };
2045
2046 Table.prototype._initCells = function() {
2047 // re-establish original colspans
2048 this.$table.find("[data-tablesaw-maxcolspan]").each(function() {
2049 var $t = $(this);
2050 $t.attr("colspan", $t.attr("data-tablesaw-maxcolspan"));
2051 });
2052
2053 var $rows = this.getRows();
2054 var columnLookup = [];
2055
2056 $rows.each(function(rowNumber) {
2057 columnLookup[rowNumber] = [];
2058 });
2059
2060 $rows.each(function(rowNumber) {
2061 var coltally = 0;
2062 var $t = $(this);
2063 var children = $t.children();
2064
2065 children.each(function() {
2066 var colspan = parseInt(
2067 this.getAttribute("data-tablesaw-maxcolspan") || this.getAttribute("colspan"),
2068 10
2069 );
2070 var rowspan = parseInt(this.getAttribute("rowspan"), 10);
2071
2072 // set in a previous rowspan
2073 while (columnLookup[rowNumber][coltally]) {
2074 coltally++;
2075 }
2076
2077 columnLookup[rowNumber][coltally] = this;
2078
2079 // TODO? both colspan and rowspan
2080 if (colspan) {
2081 for (var k = 0; k < colspan - 1; k++) {
2082 coltally++;
2083 columnLookup[rowNumber][coltally] = this;
2084 }
2085 }
2086 if (rowspan) {
2087 for (var j = 1; j < rowspan; j++) {
2088 columnLookup[rowNumber + j][coltally] = this;
2089 }
2090 }
2091
2092 coltally++;
2093 });
2094 });
2095
2096 var headerRowIndeces = this._getHeaderRowIndeces();
2097 for (var colNumber = 0; colNumber < columnLookup[0].length; colNumber++) {
2098 for (var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++) {
2099 var headerCol = columnLookup[headerRowIndeces[headerIndex]][colNumber];
2100
2101 var rowNumber = headerRowIndeces[headerIndex];
2102 var rowCell;
2103
2104 if (!headerCol.cells) {
2105 headerCol.cells = [];
2106 }
2107
2108 while (rowNumber < columnLookup.length) {
2109 rowCell = columnLookup[rowNumber][colNumber];
2110
2111 if (headerCol !== rowCell) {
2112 headerCol.cells.push(rowCell);
2113 }
2114
2115 rowNumber++;
2116 }
2117 }
2118 }
2119
2120 this.headerMapping = columnLookup;
2121 };
2122
2123 Table.prototype.refresh = function() {
2124 this._initCells();
2125
2126 this.$table.trigger(events.refresh, [this]);
2127 };
2128
2129 Table.prototype._getToolbarAnchor = function() {
2130 var $parent = this.$table.parent();
2131 if ($parent.is(".tablesaw-overflow")) {
2132 return $parent;
2133 }
2134 return this.$table;
2135 };
2136
2137 Table.prototype._getToolbar = function($anchor) {
2138 if (!$anchor) {
2139 $anchor = this._getToolbarAnchor();
2140 }
2141 return $anchor.prev().filter("." + classes.toolbar);
2142 };
2143
2144 Table.prototype.createToolbar = function() {
2145 // Insert the toolbar
2146 // TODO move this into a separate component
2147 var $anchor = this._getToolbarAnchor();
2148 var $toolbar = this._getToolbar($anchor);
2149 if (!$toolbar.length) {
2150 $toolbar = $("<div>")
2151 .addClass(classes.toolbar)
2152 .insertBefore($anchor);
2153 }
2154 this.$toolbar = $toolbar;
2155
2156 if (this.mode) {
2157 this.$toolbar.addClass("tablesaw-mode-" + this.mode);
2158 }
2159 };
2160
2161 Table.prototype.destroy = function() {
2162 // Don’t remove the toolbar, just erase the classes on it.
2163 // Some of the table features are not yet destroy-friendly.
2164 this._getToolbar().each(function() {
2165 this.className = this.className.replace(/\btablesaw-mode\-\w*\b/gi, "");
2166 });
2167
2168 var tableId = this.$table.attr("id");
2169 $(document).off("." + tableId);
2170 $(window).off("." + tableId);
2171
2172 // other plugins
2173 this.$table.trigger(events.destroy, [this]);
2174
2175 this.$table.removeData(pluginName);
2176 };
2177
2178 // Collection method.
2179 $.fn[pluginName] = function() {
2180 return this.each(function() {
2181 var $t = $(this);
2182
2183 if ($t.data(pluginName)) {
2184 return;
2185 }
2186
2187 new Table(this);
2188 });
2189 };
2190
2191 var $doc = $(document);
2192 $doc.on("enhance.tablesaw", function(e) {
2193 // Cut the mustard
2194 if (Tablesaw.mustard) {
2195 $(e.target)
2196 .find(initSelector)
2197 .filter(initFilterSelector)
2198 [pluginName]();
2199 }
2200 });
2201
2202 // Avoid a resize during scroll:
2203 // Some Mobile devices trigger a resize during scroll (sometimes when
2204 // doing elastic stretch at the end of the document or from the
2205 // location bar hide)
2206 var isScrolling = false;
2207 var scrollTimeout;
2208 $doc.on("scroll.tablesaw", function() {
2209 isScrolling = true;
2210
2211 window.clearTimeout(scrollTimeout);
2212 scrollTimeout = window.setTimeout(function() {
2213 isScrolling = false;
2214 }, 300); // must be greater than the resize timeout below
2215 });
2216
2217 var resizeTimeout;
2218 $(window).on("resize", function() {
2219 if (!isScrolling) {
2220 window.clearTimeout(resizeTimeout);
2221 resizeTimeout = window.setTimeout(function() {
2222 $doc.trigger(events.resize);
2223 }, 150); // must be less than the scrolling timeout above.
2224 }
2225 });
2226
2227 Tablesaw.Table = Table;
2228 })();
2229
2230 (function() {
2231 var classes = {
2232 stackTable: "tablesaw-stack",
2233 cellLabels: "tablesaw-cell-label",
2234 cellContentLabels: "tablesaw-cell-content"
2235 };
2236
2237 var data = {
2238 key: "tablesaw-stack"
2239 };
2240
2241 var attrs = {
2242 labelless: "data-tablesaw-no-labels",
2243 hideempty: "data-tablesaw-hide-empty"
2244 };
2245
2246 var Stack = function(element, tablesaw) {
2247 this.tablesaw = tablesaw;
2248 this.$table = $(element);
2249
2250 this.labelless = this.$table.is("[" + attrs.labelless + "]");
2251 this.hideempty = this.$table.is("[" + attrs.hideempty + "]");
2252
2253 this.$table.data(data.key, this);
2254 };
2255
2256 Stack.prototype.init = function() {
2257 this.$table.addClass(classes.stackTable);
2258
2259 if (this.labelless) {
2260 return;
2261 }
2262
2263 var self = this;
2264
2265 this.$table
2266 .find("th, td")
2267 .filter(function() {
2268 return !$(this).closest("thead").length;
2269 })
2270 .filter(function() {
2271 return (
2272 !$(this)
2273 .closest("tr")
2274 .is("[" + attrs.labelless + "]") &&
2275 (!self.hideempty || !!$(this).html())
2276 );
2277 })
2278 .each(function() {
2279 var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels);
2280 var $cell = $(this);
2281
2282 $(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) {
2283 var $header = $(this.cloneNode(true));
2284 // TODO decouple from sortable better
2285 // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b
2286 // to preserve structural html in headers, like <a>
2287 var $sortableButton = $header.find(".tablesaw-sortable-btn");
2288 $header.find(".tablesaw-sortable-arrow").remove();
2289
2290 // TODO decouple from checkall better
2291 var $checkall = $header.find("[data-tablesaw-checkall]");
2292 $checkall.closest("label").remove();
2293 if ($checkall.length) {
2294 $newHeader = $([]);
2295 return;
2296 }
2297
2298 if (index > 0) {
2299 $newHeader.append(document.createTextNode(", "));
2300 }
2301 $newHeader.append(
2302 $sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes
2303 );
2304 });
2305
2306 if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) {
2307 $cell.wrapInner("<span class='" + classes.cellContentLabels + "'></span>");
2308 }
2309
2310 // Update if already exists.
2311 var $label = $cell.find("." + classes.cellLabels);
2312 if (!$label.length) {
2313 $cell.prepend($newHeader);
2314 } else {
2315 // only if changed
2316 $label.replaceWith($newHeader);
2317 }
2318 });
2319 };
2320
2321 Stack.prototype.destroy = function() {
2322 this.$table.removeClass(classes.stackTable);
2323 this.$table.find("." + classes.cellLabels).remove();
2324 this.$table.find("." + classes.cellContentLabels).each(function() {
2325 $(this).replaceWith(this.childNodes);
2326 });
2327 };
2328
2329 // on tablecreate, init
2330 $(document)
2331 .on(Tablesaw.events.create, function(e, tablesaw) {
2332 if (tablesaw.mode === "stack") {
2333 var table = new Stack(tablesaw.table, tablesaw);
2334 table.init();
2335 }
2336 })
2337 .on(Tablesaw.events.refresh, function(e, tablesaw) {
2338 if (tablesaw.mode === "stack") {
2339 $(tablesaw.table)
2340 .data(data.key)
2341 .init();
2342 }
2343 })
2344 .on(Tablesaw.events.destroy, function(e, tablesaw) {
2345 if (tablesaw.mode === "stack") {
2346 $(tablesaw.table)
2347 .data(data.key)
2348 .destroy();
2349 }
2350 });
2351
2352 Tablesaw.Stack = Stack;
2353 })();
2354
2355 (function() {
2356 var pluginName = "tablesawbtn",
2357 methods = {
2358 _create: function() {
2359 return $(this).each(function() {
2360 $(this)
2361 .trigger("beforecreate." + pluginName)
2362 [pluginName]("_init")
2363 .trigger("create." + pluginName);
2364 });
2365 },
2366 _init: function() {
2367 var oEl = $(this),
2368 sel = this.getElementsByTagName("select")[0];
2369
2370 if (sel) {
2371 // TODO next major version: remove .btn-select
2372 $(this)
2373 .addClass("btn-select tablesaw-btn-select")
2374 [pluginName]("_select", sel);
2375 }
2376 return oEl;
2377 },
2378 _select: function(sel) {
2379 var update = function(oEl, sel) {
2380 var opts = $(sel).find("option");
2381 var label = document.createElement("span");
2382 var el;
2383 var children;
2384 var found = false;
2385
2386 label.setAttribute("aria-hidden", "true");
2387 label.innerHTML = "&#160;";
2388
2389 opts.each(function() {
2390 var opt = this;
2391 if (opt.selected) {
2392 label.innerHTML = opt.text;
2393 }
2394 });
2395
2396 children = oEl.childNodes;
2397 if (opts.length > 0) {
2398 for (var i = 0, l = children.length; i < l; i++) {
2399 el = children[i];
2400
2401 if (el && el.nodeName.toUpperCase() === "SPAN") {
2402 oEl.replaceChild(label, el);
2403 found = true;
2404 }
2405 }
2406
2407 if (!found) {
2408 oEl.insertBefore(label, oEl.firstChild);
2409 }
2410 }
2411 };
2412
2413 update(this, sel);
2414 // todo should this be tablesawrefresh?
2415 $(this).on("change refresh", function() {
2416 update(this, sel);
2417 });
2418 }
2419 };
2420
2421 // Collection method.
2422 $.fn[pluginName] = function(arrg, a, b, c) {
2423 return this.each(function() {
2424 // if it's a method
2425 if (arrg && typeof arrg === "string") {
2426 return $.fn[pluginName].prototype[arrg].call(this, a, b, c);
2427 }
2428
2429 // don't re-init
2430 if ($(this).data(pluginName + "active")) {
2431 return $(this);
2432 }
2433
2434 $(this).data(pluginName + "active", true);
2435
2436 $.fn[pluginName].prototype._create.call(this);
2437 });
2438 };
2439
2440 // add methods
2441 $.extend($.fn[pluginName].prototype, methods);
2442
2443 // TODO OOP this and add to Tablesaw object
2444 })();
2445
2446 (function() {
2447 var data = {
2448 key: "tablesaw-coltoggle"
2449 };
2450
2451 var ColumnToggle = function(element) {
2452 this.$table = $(element);
2453
2454 if (!this.$table.length) {
2455 return;
2456 }
2457
2458 this.tablesaw = this.$table.data("tablesaw");
2459
2460 this.attributes = {
2461 btnTarget: "data-tablesaw-columntoggle-btn-target",
2462 set: "data-tablesaw-columntoggle-set"
2463 };
2464
2465 this.classes = {
2466 columnToggleTable: "tablesaw-columntoggle",
2467 columnBtnContain: "tablesaw-columntoggle-btnwrap tablesaw-advance",
2468 columnBtn: "tablesaw-columntoggle-btn tablesaw-nav-btn down",
2469 popup: "tablesaw-columntoggle-popup",
2470 priorityPrefix: "tablesaw-priority-"
2471 };
2472
2473 this.set = [];
2474 this.$headers = this.tablesaw._getPrimaryHeaderCells();
2475
2476 this.$table.data(data.key, this);
2477 };
2478
2479 // Column Toggle Sets (one column chooser can control multiple tables)
2480 ColumnToggle.prototype.initSet = function() {
2481 var set = this.$table.attr(this.attributes.set);
2482 if (set) {
2483 // Should not include the current table
2484 var table = this.$table[0];
2485 this.set = $("table[" + this.attributes.set + "='" + set + "']")
2486 .filter(function() {
2487 return this !== table;
2488 })
2489 .get();
2490 }
2491 };
2492
2493 ColumnToggle.prototype.init = function() {
2494 if (!this.$table.length) {
2495 return;
2496 }
2497
2498 var tableId,
2499 id,
2500 $menuButton,
2501 $popup,
2502 $menu,
2503 $btnContain,
2504 self = this;
2505
2506 var cfg = this.tablesaw.getConfig({
2507 getColumnToggleLabelTemplate: function(text) {
2508 return "<label><input type='checkbox' checked>" + text + "</label>";
2509 }
2510 });
2511
2512 this.$table.addClass(this.classes.columnToggleTable);
2513
2514 tableId = this.$table.attr("id");
2515 id = tableId + "-popup";
2516 $btnContain = $("<div class='" + this.classes.columnBtnContain + "'></div>");
2517 // TODO next major version: remove .btn
2518 $menuButton = $(
2519 "<a href='#" +
2520 id +
2521 "' class='btn tablesaw-btn btn-micro " +
2522 this.classes.columnBtn +
2523 "' data-popup-link>" +
2524 "<span>" +
2525 Tablesaw.i18n.columnToggleButton +
2526 "</span></a>"
2527 );
2528 $popup = $("<div class='" + this.classes.popup + "' id='" + id + "'></div>");
2529 $menu = $("<div class='btn-group'></div>");
2530
2531 this.$popup = $popup;
2532
2533 var hasNonPersistentHeaders = false;
2534 this.$headers.each(function() {
2535 var $this = $(this),
2536 priority = $this.attr("data-tablesaw-priority"),
2537 $cells = self.tablesaw._$getCells(this);
2538
2539 if (priority && priority !== "persist") {
2540 $cells.addClass(self.classes.priorityPrefix + priority);
2541
2542 $(cfg.getColumnToggleLabelTemplate($this.text()))
2543 .appendTo($menu)
2544 .find('input[type="checkbox"]')
2545 .data("tablesaw-header", this);
2546
2547 hasNonPersistentHeaders = true;
2548 }
2549 });
2550
2551 if (!hasNonPersistentHeaders) {
2552 $menu.append("<label>" + Tablesaw.i18n.columnToggleError + "</label>");
2553 }
2554
2555 $menu.appendTo($popup);
2556
2557 function onToggleCheckboxChange(checkbox) {
2558 var checked = checkbox.checked;
2559
2560 var header = self.getHeaderFromCheckbox(checkbox);
2561 var $cells = self.tablesaw._$getCells(header);
2562
2563 $cells[!checked ? "addClass" : "removeClass"]("tablesaw-toggle-cellhidden");
2564 $cells[checked ? "addClass" : "removeClass"]("tablesaw-toggle-cellvisible");
2565
2566 self.updateColspanCells(header, checked);
2567
2568 self.$table.trigger("tablesawcolumns");
2569 }
2570
2571 // bind change event listeners to inputs - TODO: move to a private method?
2572 $menu.find('input[type="checkbox"]').on("change", function(e) {
2573 onToggleCheckboxChange(e.target);
2574
2575 if (self.set.length) {
2576 var index;
2577 $(self.$popup)
2578 .find("input[type='checkbox']")
2579 .each(function(j) {
2580 if (this === e.target) {
2581 index = j;
2582 return false;
2583 }
2584 });
2585
2586 $(self.set).each(function() {
2587 var checkbox = $(this)
2588 .data(data.key)
2589 .$popup.find("input[type='checkbox']")
2590 .get(index);
2591 if (checkbox) {
2592 checkbox.checked = e.target.checked;
2593 onToggleCheckboxChange(checkbox);
2594 }
2595 });
2596 }
2597 });
2598
2599 $menuButton.appendTo($btnContain);
2600
2601 // Use a different target than the toolbar
2602 var $btnTarget = $(this.$table.attr(this.attributes.btnTarget));
2603 $btnContain.appendTo($btnTarget.length ? $btnTarget : this.tablesaw.$toolbar);
2604
2605 function closePopup(event) {
2606 // Click came from inside the popup, ignore.
2607 if (event && $(event.target).closest("." + self.classes.popup).length) {
2608 return;
2609 }
2610
2611 $(document).off("click." + tableId);
2612 $menuButton.removeClass("up").addClass("down");
2613 $btnContain.removeClass("visible");
2614 }
2615
2616 var closeTimeout;
2617 function openPopup() {
2618 $btnContain.addClass("visible");
2619 $menuButton.removeClass("down").addClass("up");
2620
2621 $(document).off("click." + tableId, closePopup);
2622
2623 window.clearTimeout(closeTimeout);
2624 closeTimeout = window.setTimeout(function() {
2625 $(document).on("click." + tableId, closePopup);
2626 }, 15);
2627 }
2628
2629 $menuButton.on("click.tablesaw", function(event) {
2630 event.preventDefault();
2631
2632 if (!$btnContain.is(".visible")) {
2633 openPopup();
2634 } else {
2635 closePopup();
2636 }
2637 });
2638
2639 $popup.appendTo($btnContain);
2640
2641 this.$menu = $menu;
2642
2643 // Fix for iOS not rendering shadows correctly when using `-webkit-overflow-scrolling`
2644 var $overflow = this.$table.closest(".tablesaw-overflow");
2645 if ($overflow.css("-webkit-overflow-scrolling")) {
2646 var timeout;
2647 $overflow.on("scroll", function() {
2648 var $div = $(this);
2649 window.clearTimeout(timeout);
2650 timeout = window.setTimeout(function() {
2651 $div.css("-webkit-overflow-scrolling", "auto");
2652 window.setTimeout(function() {
2653 $div.css("-webkit-overflow-scrolling", "touch");
2654 }, 0);
2655 }, 100);
2656 });
2657 }
2658
2659 $(window).on(Tablesaw.events.resize + "." + tableId, function() {
2660 self.refreshToggle();
2661 });
2662
2663 this.initSet();
2664 this.refreshToggle();
2665 };
2666
2667 ColumnToggle.prototype.getHeaderFromCheckbox = function(checkbox) {
2668 return $(checkbox).data("tablesaw-header");
2669 };
2670
2671 ColumnToggle.prototype.refreshToggle = function() {
2672 var self = this;
2673 var invisibleColumns = 0;
2674 this.$menu.find("input").each(function() {
2675 var header = self.getHeaderFromCheckbox(this);
2676 this.checked =
2677 self.tablesaw
2678 ._$getCells(header)
2679 .eq(0)
2680 .css("display") === "table-cell";
2681 });
2682
2683 this.updateColspanCells();
2684 };
2685
2686 ColumnToggle.prototype.updateColspanCells = function(header, userAction) {
2687 this.tablesaw.updateColspanCells("tablesaw-toggle-cellhidden", header, userAction);
2688 };
2689
2690 ColumnToggle.prototype.destroy = function() {
2691 this.$table.removeClass(this.classes.columnToggleTable);
2692 this.$table.find("th, td").each(function() {
2693 var $cell = $(this);
2694 $cell.removeClass("tablesaw-toggle-cellhidden").removeClass("tablesaw-toggle-cellvisible");
2695
2696 this.className = this.className.replace(/\bui\-table\-priority\-\d\b/g, "");
2697 });
2698 };
2699
2700 // on tablecreate, init
2701 $(document).on(Tablesaw.events.create, function(e, tablesaw) {
2702 if (tablesaw.mode === "columntoggle") {
2703 var table = new ColumnToggle(tablesaw.table);
2704 table.init();
2705 }
2706 });
2707
2708 $(document).on(Tablesaw.events.destroy, function(e, tablesaw) {
2709 if (tablesaw.mode === "columntoggle") {
2710 $(tablesaw.table)
2711 .data(data.key)
2712 .destroy();
2713 }
2714 });
2715
2716 $(document).on(Tablesaw.events.refresh, function(e, tablesaw) {
2717 if (tablesaw.mode === "columntoggle") {
2718 $(tablesaw.table)
2719 .data(data.key)
2720 .refreshPriority();
2721 }
2722 });
2723
2724 Tablesaw.ColumnToggle = ColumnToggle;
2725 })();
2726
2727 (function() {
2728 function getSortValue(cell) {
2729 var text = [];
2730 $(cell.childNodes).each(function() {
2731 var $el = $(this);
2732 if ($el.is("input, select")) {
2733 text.push($el.val());
2734 } else if ($el.is(".tablesaw-cell-label")) {
2735 } else {
2736 text.push(($el.text() || "").replace(/^\s+|\s+$/g, ""));
2737 }
2738 });
2739
2740 return text.join("");
2741 }
2742
2743 var pluginName = "tablesaw-sortable",
2744 initSelector = "table[data-" + pluginName + "]",
2745 sortableSwitchSelector = "[data-" + pluginName + "-switch]",
2746 attrs = {
2747 sortCol: "data-tablesaw-sortable-col",
2748 defaultCol: "data-tablesaw-sortable-default-col",
2749 numericCol: "data-tablesaw-sortable-numeric",
2750 subRow: "data-tablesaw-subrow",
2751 ignoreRow: "data-tablesaw-ignorerow"
2752 },
2753 classes = {
2754 head: pluginName + "-head",
2755 ascend: pluginName + "-ascending",
2756 descend: pluginName + "-descending",
2757 switcher: pluginName + "-switch",
2758 tableToolbar: "tablesaw-bar-section",
2759 sortButton: pluginName + "-btn"
2760 },
2761 methods = {
2762 _create: function(o) {
2763 return $(this).each(function() {
2764 var init = $(this).data(pluginName + "-init");
2765 if (init) {
2766 return false;
2767 }
2768 $(this)
2769 .data(pluginName + "-init", true)
2770 .trigger("beforecreate." + pluginName)
2771 [pluginName]("_init", o)
2772 .trigger("create." + pluginName);
2773 });
2774 },
2775 _init: function() {
2776 var el = $(this);
2777 var tblsaw = el.data("tablesaw");
2778 var heads;
2779 var $switcher;
2780
2781 function addClassToHeads(h) {
2782 $.each(h, function(i, v) {
2783 $(v).addClass(classes.head);
2784 });
2785 }
2786
2787 function makeHeadsActionable(h, fn) {
2788 $.each(h, function(i, col) {
2789 var b = $("<button class='" + classes.sortButton + "'/>");
2790 b.on("click", { col: col }, fn);
2791 $(col)
2792 .wrapInner(b)
2793 .find("button")
2794 .append("<span class='tablesaw-sortable-arrow'>");
2795 });
2796 }
2797
2798 function clearOthers(headcells) {
2799 $.each(headcells, function(i, v) {
2800 var col = $(v);
2801 col.removeAttr(attrs.defaultCol);
2802 col.removeClass(classes.ascend);
2803 col.removeClass(classes.descend);
2804 });
2805 }
2806
2807 function headsOnAction(e) {
2808 if ($(e.target).is("a[href]")) {
2809 return;
2810 }
2811
2812 e.stopPropagation();
2813 var headCell = $(e.target).closest("[" + attrs.sortCol + "]"),
2814 v = e.data.col,
2815 newSortValue = heads.index(headCell[0]);
2816
2817 clearOthers(
2818 headCell
2819 .closest("thead")
2820 .find("th")
2821 .filter(function() {
2822 return this !== headCell[0];
2823 })
2824 );
2825 if (headCell.is("." + classes.descend) || !headCell.is("." + classes.ascend)) {
2826 el[pluginName]("sortBy", v, true);
2827 newSortValue += "_asc";
2828 } else {
2829 el[pluginName]("sortBy", v);
2830 newSortValue += "_desc";
2831 }
2832 if ($switcher) {
2833 $switcher
2834 .find("select")
2835 .val(newSortValue)
2836 .trigger("refresh");
2837 }
2838
2839 e.preventDefault();
2840 }
2841
2842 function handleDefault(heads) {
2843 $.each(heads, function(idx, el) {
2844 var $el = $(el);
2845 if ($el.is("[" + attrs.defaultCol + "]")) {
2846 if (!$el.is("." + classes.descend)) {
2847 $el.addClass(classes.ascend);
2848 }
2849 }
2850 });
2851 }
2852
2853 function addSwitcher(heads) {
2854 $switcher = $("<div>")
2855 .addClass(classes.switcher)
2856 .addClass(classes.tableToolbar);
2857
2858 var html = ["<label>" + Tablesaw.i18n.sort + ":"];
2859
2860 // TODO next major version: remove .btn
2861 html.push('<span class="btn tablesaw-btn"><select>');
2862 heads.each(function(j) {
2863 var $t = $(this);
2864 var isDefaultCol = $t.is("[" + attrs.defaultCol + "]");
2865 var isDescending = $t.is("." + classes.descend);
2866
2867 var hasNumericAttribute = $t.is("[" + attrs.numericCol + "]");
2868 var numericCount = 0;
2869 // Check only the first four rows to see if the column is numbers.
2870 var numericCountMax = 5;
2871
2872 $(this.cells.slice(0, numericCountMax)).each(function() {
2873 if (!isNaN(parseInt(getSortValue(this), 10))) {
2874 numericCount++;
2875 }
2876 });
2877 var isNumeric = numericCount === numericCountMax;
2878 if (!hasNumericAttribute) {
2879 $t.attr(attrs.numericCol, isNumeric ? "" : "false");
2880 }
2881
2882 html.push(
2883 "<option" +
2884 (isDefaultCol && !isDescending ? " selected" : "") +
2885 ' value="' +
2886 j +
2887 '_asc">' +
2888 $t.text() +
2889 " " +
2890 (isNumeric ? "&#x2191;" : "(A-Z)") +
2891 "</option>"
2892 );
2893 html.push(
2894 "<option" +
2895 (isDefaultCol && isDescending ? " selected" : "") +
2896 ' value="' +
2897 j +
2898 '_desc">' +
2899 $t.text() +
2900 " " +
2901 (isNumeric ? "&#x2193;" : "(Z-A)") +
2902 "</option>"
2903 );
2904 });
2905 html.push("</select></span></label>");
2906
2907 $switcher.html(html.join(""));
2908
2909 var $firstChild = tblsaw.$toolbar.children().eq(0);
2910 if ($firstChild.length) {
2911 $switcher.insertBefore($firstChild);
2912 } else {
2913 $switcher.appendTo(tblsaw.$toolbar);
2914 }
2915 $switcher.find(".tablesaw-btn").tablesawbtn();
2916 $switcher.find("select").on("change", function() {
2917 var val = $(this)
2918 .val()
2919 .split("_"),
2920 head = heads.eq(val[0]);
2921
2922 clearOthers(head.siblings());
2923 el[pluginName]("sortBy", head.get(0), val[1] === "asc");
2924 });
2925 }
2926
2927 el.addClass(pluginName);
2928
2929 heads = el
2930 .children()
2931 .filter("thead")
2932 .find("th[" + attrs.sortCol + "]");
2933
2934 addClassToHeads(heads);
2935 makeHeadsActionable(heads, headsOnAction);
2936 handleDefault(heads);
2937
2938 if (el.is(sortableSwitchSelector)) {
2939 addSwitcher(heads);
2940 }
2941 },
2942 sortRows: function(rows, colNum, ascending, col, tbody) {
2943 function convertCells(cellArr, belongingToTbody) {
2944 var cells = [];
2945 $.each(cellArr, function(i, cell) {
2946 var row = cell.parentNode;
2947 var $row = $(row);
2948 // next row is a subrow
2949 var subrows = [];
2950 var $next = $row.next();
2951 while ($next.is("[" + attrs.subRow + "]")) {
2952 subrows.push($next[0]);
2953 $next = $next.next();
2954 }
2955
2956 var tbody = row.parentNode;
2957
2958 // current row is a subrow
2959 if ($row.is("[" + attrs.subRow + "]")) {
2960 } else if (tbody === belongingToTbody) {
2961 cells.push({
2962 element: cell,
2963 cell: getSortValue(cell),
2964 row: row,
2965 subrows: subrows.length ? subrows : null,
2966 ignored: $row.is("[" + attrs.ignoreRow + "]")
2967 });
2968 }
2969 });
2970 return cells;
2971 }
2972
2973 function getSortFxn(ascending, forceNumeric) {
2974 var fn,
2975 regex = /[^\-\+\d\.]/g;
2976 if (ascending) {
2977 fn = function(a, b) {
2978 if (a.ignored || b.ignored) {
2979 return 0;
2980 }
2981 if (forceNumeric) {
2982 return (
2983 parseFloat(a.cell.replace(regex, "")) - parseFloat(b.cell.replace(regex, ""))
2984 );
2985 } else {
2986 return a.cell.toLowerCase() > b.cell.toLowerCase() ? 1 : -1;
2987 }
2988 };
2989 } else {
2990 fn = function(a, b) {
2991 if (a.ignored || b.ignored) {
2992 return 0;
2993 }
2994 if (forceNumeric) {
2995 return (
2996 parseFloat(b.cell.replace(regex, "")) - parseFloat(a.cell.replace(regex, ""))
2997 );
2998 } else {
2999 return a.cell.toLowerCase() < b.cell.toLowerCase() ? 1 : -1;
3000 }
3001 };
3002 }
3003 return fn;
3004 }
3005
3006 function convertToRows(sorted) {
3007 var newRows = [],
3008 i,
3009 l;
3010 for (i = 0, l = sorted.length; i < l; i++) {
3011 newRows.push(sorted[i].row);
3012 if (sorted[i].subrows) {
3013 newRows.push(sorted[i].subrows);
3014 }
3015 }
3016 return newRows;
3017 }
3018
3019 var fn;
3020 var sorted;
3021 var cells = convertCells(col.cells, tbody);
3022
3023 var customFn = $(col).data("tablesaw-sort");
3024
3025 fn =
3026 (customFn && typeof customFn === "function" ? customFn(ascending) : false) ||
3027 getSortFxn(
3028 ascending,
3029 $(col).is("[" + attrs.numericCol + "]") &&
3030 !$(col).is("[" + attrs.numericCol + '="false"]')
3031 );
3032
3033 sorted = cells.sort(fn);
3034
3035 rows = convertToRows(sorted);
3036
3037 return rows;
3038 },
3039 makeColDefault: function(col, a) {
3040 var c = $(col);
3041 c.attr(attrs.defaultCol, "true");
3042 if (a) {
3043 c.removeClass(classes.descend);
3044 c.addClass(classes.ascend);
3045 } else {
3046 c.removeClass(classes.ascend);
3047 c.addClass(classes.descend);
3048 }
3049 },
3050 sortBy: function(col, ascending) {
3051 var el = $(this);
3052 var colNum;
3053 var tbl = el.data("tablesaw");
3054 tbl.$tbody.each(function() {
3055 var tbody = this;
3056 var $tbody = $(this);
3057 var rows = tbl.getBodyRows(tbody);
3058 var sortedRows;
3059 var map = tbl.headerMapping[0];
3060 var j, k;
3061
3062 // find the column number that we’re sorting
3063 for (j = 0, k = map.length; j < k; j++) {
3064 if (map[j] === col) {
3065 colNum = j;
3066 break;
3067 }
3068 }
3069
3070 sortedRows = el[pluginName]("sortRows", rows, colNum, ascending, col, tbody);
3071
3072 // replace Table rows
3073 for (j = 0, k = sortedRows.length; j < k; j++) {
3074 $tbody.append(sortedRows[j]);
3075 }
3076 });
3077
3078 el[pluginName]("makeColDefault", col, ascending);
3079
3080 el.trigger("tablesaw-sorted");
3081 }
3082 };
3083
3084 // Collection method.
3085 $.fn[pluginName] = function(arrg) {
3086 var args = Array.prototype.slice.call(arguments, 1),
3087 returnVal;
3088
3089 // if it's a method
3090 if (arrg && typeof arrg === "string") {
3091 returnVal = $.fn[pluginName].prototype[arrg].apply(this[0], args);
3092 return typeof returnVal !== "undefined" ? returnVal : $(this);
3093 }
3094 // check init
3095 if (!$(this).data(pluginName + "-active")) {
3096 $(this).data(pluginName + "-active", true);
3097 $.fn[pluginName].prototype._create.call(this, arrg);
3098 }
3099 return $(this);
3100 };
3101 // add methods
3102 $.extend($.fn[pluginName].prototype, methods);
3103
3104 $(document).on(Tablesaw.events.create, function(e, Tablesaw) {
3105 if (Tablesaw.$table.is(initSelector)) {
3106 Tablesaw.$table[pluginName]();
3107 }
3108 });
3109
3110 // TODO OOP this and add to Tablesaw object
3111 })();
3112
3113 (function() {
3114 var classes = {
3115 hideBtn: "disabled",
3116 persistWidths: "tablesaw-fix-persist",
3117 hiddenCol: "tablesaw-swipe-cellhidden",
3118 persistCol: "tablesaw-swipe-cellpersist",
3119 allColumnsVisible: "tablesaw-all-cols-visible"
3120 };
3121 var attrs = {
3122 disableTouchEvents: "data-tablesaw-no-touch",
3123 ignorerow: "data-tablesaw-ignorerow",
3124 subrow: "data-tablesaw-subrow"
3125 };
3126
3127 function createSwipeTable(tbl, $table) {
3128 var tblsaw = $table.data("tablesaw");
3129
3130 var $btns = $("<div class='tablesaw-advance'></div>");
3131 // TODO next major version: remove .btn
3132 var $prevBtn = $(
3133 "<a href='#' class='btn tablesaw-nav-btn tablesaw-btn btn-micro left'>" +
3134 Tablesaw.i18n.swipePreviousColumn +
3135 "</a>"
3136 ).appendTo($btns);
3137 // TODO next major version: remove .btn
3138 var $nextBtn = $(
3139 "<a href='#' class='btn tablesaw-nav-btn tablesaw-btn btn-micro right'>" +
3140 Tablesaw.i18n.swipeNextColumn +
3141 "</a>"
3142 ).appendTo($btns);
3143
3144 var $headerCells = tbl._getPrimaryHeaderCells();
3145 var $headerCellsNoPersist = $headerCells.not('[data-tablesaw-priority="persist"]');
3146 var headerWidths = [];
3147 var $head = $(document.head || "head");
3148 var tableId = $table.attr("id");
3149
3150 if (!$headerCells.length) {
3151 throw new Error("tablesaw swipe: no header cells found.");
3152 }
3153
3154 $table.addClass("tablesaw-swipe");
3155
3156 function initMinHeaderWidths() {
3157 $table.css({
3158 width: "1px"
3159 });
3160
3161 // remove any hidden columns
3162 $table.find("." + classes.hiddenCol).removeClass(classes.hiddenCol);
3163
3164 headerWidths = [];
3165 // Calculate initial widths
3166 $headerCells.each(function() {
3167 headerWidths.push(this.offsetWidth);
3168 });
3169
3170 // reset props
3171 $table.css({
3172 width: ""
3173 });
3174 }
3175
3176 initMinHeaderWidths();
3177
3178 $btns.appendTo(tblsaw.$toolbar);
3179
3180 if (!tableId) {
3181 tableId = "tableswipe-" + Math.round(Math.random() * 10000);
3182 $table.attr("id", tableId);
3183 }
3184
3185 function showColumn(headerCell) {
3186 tblsaw._$getCells(headerCell).removeClass(classes.hiddenCol);
3187 }
3188
3189 function hideColumn(headerCell) {
3190 tblsaw._$getCells(headerCell).addClass(classes.hiddenCol);
3191 }
3192
3193 function persistColumn(headerCell) {
3194 tblsaw._$getCells(headerCell).addClass(classes.persistCol);
3195 }
3196
3197 function isPersistent(headerCell) {
3198 return $(headerCell).is('[data-tablesaw-priority="persist"]');
3199 }
3200
3201 function unmaintainWidths() {
3202 $table.removeClass(classes.persistWidths);
3203 $("#" + tableId + "-persist").remove();
3204 }
3205
3206 function maintainWidths() {
3207 var prefix = "#" + tableId + ".tablesaw-swipe ",
3208 styles = [],
3209 tableWidth = $table.width(),
3210 hash = [],
3211 newHash;
3212
3213 // save persistent column widths (as long as they take up less than 75% of table width)
3214 $headerCells.each(function(index) {
3215 var width;
3216 if (isPersistent(this)) {
3217 width = this.offsetWidth;
3218
3219 if (width < tableWidth * 0.75) {
3220 hash.push(index + "-" + width);
3221 styles.push(
3222 prefix +
3223 " ." +
3224 classes.persistCol +
3225 ":nth-child(" +
3226 (index + 1) +
3227 ") { width: " +
3228 width +
3229 "px; }"
3230 );
3231 }
3232 }
3233 });
3234 newHash = hash.join("_");
3235
3236 if (styles.length) {
3237 $table.addClass(classes.persistWidths);
3238 var $style = $("#" + tableId + "-persist");
3239 // If style element not yet added OR if the widths have changed
3240 if (!$style.length || $style.data("tablesaw-hash") !== newHash) {
3241 // Remove existing
3242 $style.remove();
3243
3244 $("<style>" + styles.join("\n") + "</style>")
3245 .attr("id", tableId + "-persist")
3246 .data("tablesaw-hash", newHash)
3247 .appendTo($head);
3248 }
3249 }
3250 }
3251
3252 function getNext() {
3253 var next = [],
3254 checkFound;
3255
3256 $headerCellsNoPersist.each(function(i) {
3257 var $t = $(this),
3258 isHidden = $t.css("display") === "none" || $t.is("." + classes.hiddenCol);
3259
3260 if (!isHidden && !checkFound) {
3261 checkFound = true;
3262 next[0] = i;
3263 } else if (isHidden && checkFound) {
3264 next[1] = i;
3265
3266 return false;
3267 }
3268 });
3269
3270 return next;
3271 }
3272
3273 function getPrev() {
3274 var next = getNext();
3275 return [next[1] - 1, next[0] - 1];
3276 }
3277
3278 function nextpair(fwd) {
3279 return fwd ? getNext() : getPrev();
3280 }
3281
3282 function canAdvance(pair) {
3283 return pair[1] > -1 && pair[1] < $headerCellsNoPersist.length;
3284 }
3285
3286 function matchesMedia() {
3287 var matchMedia = $table.attr("data-tablesaw-swipe-media");
3288 return !matchMedia || ("matchMedia" in window && window.matchMedia(matchMedia).matches);
3289 }
3290
3291 function fakeBreakpoints() {
3292 if (!matchesMedia()) {
3293 return;
3294 }
3295
3296 var containerWidth = $table.parent().width(),
3297 persist = [],
3298 sum = 0,
3299 sums = [],
3300 visibleNonPersistantCount = $headerCells.length;
3301
3302 $headerCells.each(function(index) {
3303 var $t = $(this),
3304 isPersist = $t.is('[data-tablesaw-priority="persist"]');
3305
3306 persist.push(isPersist);
3307 sum += headerWidths[index];
3308 sums.push(sum);
3309
3310 // is persistent or is hidden
3311 if (isPersist || sum > containerWidth) {
3312 visibleNonPersistantCount--;
3313 }
3314 });
3315
3316 // We need at least one column to swipe.
3317 var needsNonPersistentColumn = visibleNonPersistantCount === 0;
3318
3319 $headerCells.each(function(index) {
3320 if (sums[index] > containerWidth) {
3321 hideColumn(this);
3322 }
3323 });
3324
3325 $headerCells.each(function(index) {
3326 if (persist[index]) {
3327 // for visual box-shadow
3328 persistColumn(this);
3329 return;
3330 }
3331
3332 if (sums[index] <= containerWidth || needsNonPersistentColumn) {
3333 needsNonPersistentColumn = false;
3334 showColumn(this);
3335 tblsaw.updateColspanCells(classes.hiddenCol, this, true);
3336 }
3337 });
3338
3339 unmaintainWidths();
3340
3341 $table.trigger("tablesawcolumns");
3342 }
3343
3344 function advance(fwd) {
3345 var pair = nextpair(fwd);
3346 if (canAdvance(pair)) {
3347 if (isNaN(pair[0])) {
3348 if (fwd) {
3349 pair[0] = 0;
3350 } else {
3351 pair[0] = $headerCellsNoPersist.length - 1;
3352 }
3353 }
3354
3355 // TODO just blindly hiding the previous column and showing the next column can result in
3356 // column content overflow
3357 maintainWidths();
3358 hideColumn($headerCellsNoPersist.get(pair[0]));
3359 tblsaw.updateColspanCells(classes.hiddenCol, $headerCellsNoPersist.get(pair[0]), false);
3360
3361 showColumn($headerCellsNoPersist.get(pair[1]));
3362 tblsaw.updateColspanCells(classes.hiddenCol, $headerCellsNoPersist.get(pair[1]), true);
3363
3364 $table.trigger("tablesawcolumns");
3365 }
3366 }
3367
3368 $prevBtn.add($nextBtn).on("click", function(e) {
3369 advance(!!$(e.target).closest($nextBtn).length);
3370 e.preventDefault();
3371 });
3372
3373 function getCoord(event, key) {
3374 return (event.touches || event.originalEvent.touches)[0][key];
3375 }
3376
3377 if (!$table.is("[" + attrs.disableTouchEvents + "]")) {
3378 $table.on("touchstart.swipetoggle", function(e) {
3379 var originX = getCoord(e, "pageX");
3380 var originY = getCoord(e, "pageY");
3381 var x;
3382 var y;
3383 var scrollTop = window.pageYOffset;
3384
3385 $(window).off(Tablesaw.events.resize, fakeBreakpoints);
3386
3387 $(this)
3388 .on("touchmove.swipetoggle", function(e) {
3389 x = getCoord(e, "pageX");
3390 y = getCoord(e, "pageY");
3391 })
3392 .on("touchend.swipetoggle", function() {
3393 var cfg = tbl.getConfig({
3394 swipeHorizontalThreshold: 30,
3395 swipeVerticalThreshold: 30
3396 });
3397
3398 // This config code is a little awkward because shoestring doesn’t support deep $.extend
3399 // Trying to work around when devs only override one of (not both) horizontalThreshold or
3400 // verticalThreshold in their TablesawConfig.
3401 // @TODO major version bump: remove cfg.swipe, move to just use the swipePrefix keys
3402 var verticalThreshold = cfg.swipe
3403 ? cfg.swipe.verticalThreshold
3404 : cfg.swipeVerticalThreshold;
3405 var horizontalThreshold = cfg.swipe
3406 ? cfg.swipe.horizontalThreshold
3407 : cfg.swipeHorizontalThreshold;
3408
3409 var isPageScrolled = Math.abs(window.pageYOffset - scrollTop) >= verticalThreshold;
3410 var isVerticalSwipe = Math.abs(y - originY) >= verticalThreshold;
3411
3412 if (!isVerticalSwipe && !isPageScrolled) {
3413 if (x - originX < -1 * horizontalThreshold) {
3414 advance(true);
3415 }
3416 if (x - originX > horizontalThreshold) {
3417 advance(false);
3418 }
3419 }
3420
3421 window.setTimeout(function() {
3422 $(window).on(Tablesaw.events.resize, fakeBreakpoints);
3423 }, 300);
3424
3425 $(this).off("touchmove.swipetoggle touchend.swipetoggle");
3426 });
3427 });
3428 }
3429
3430 $table
3431 .on("tablesawcolumns.swipetoggle", function() {
3432 var canGoPrev = canAdvance(getPrev());
3433 var canGoNext = canAdvance(getNext());
3434 $prevBtn[canGoPrev ? "removeClass" : "addClass"](classes.hideBtn);
3435 $nextBtn[canGoNext ? "removeClass" : "addClass"](classes.hideBtn);
3436
3437 tblsaw.$toolbar[!canGoPrev && !canGoNext ? "addClass" : "removeClass"](
3438 classes.allColumnsVisible
3439 );
3440 })
3441 .on("tablesawnext.swipetoggle", function() {
3442 advance(true);
3443 })
3444 .on("tablesawprev.swipetoggle", function() {
3445 advance(false);
3446 })
3447 .on(Tablesaw.events.destroy + ".swipetoggle", function() {
3448 var $t = $(this);
3449
3450 $t.removeClass("tablesaw-swipe");
3451 tblsaw.$toolbar.find(".tablesaw-advance").remove();
3452 $(window).off(Tablesaw.events.resize, fakeBreakpoints);
3453
3454 $t.off(".swipetoggle");
3455 })
3456 .on(Tablesaw.events.refresh, function() {
3457 unmaintainWidths();
3458 initMinHeaderWidths();
3459 fakeBreakpoints();
3460 });
3461
3462 fakeBreakpoints();
3463 $(window).on(Tablesaw.events.resize, fakeBreakpoints);
3464 }
3465
3466 // on tablecreate, init
3467 $(document).on(Tablesaw.events.create, function(e, tablesaw) {
3468 if (tablesaw.mode === "swipe") {
3469 createSwipeTable(tablesaw, tablesaw.$table);
3470 }
3471 });
3472
3473 // TODO OOP this and add to Tablesaw object
3474 })();
3475
3476 (function() {
3477 var MiniMap = {
3478 attr: {
3479 init: "data-tablesaw-minimap"
3480 },
3481 show: function(table) {
3482 var mq = table.getAttribute(MiniMap.attr.init);
3483
3484 if (mq === "") {
3485 // value-less but exists
3486 return true;
3487 } else if (mq && "matchMedia" in window) {
3488 // has a mq value
3489 return window.matchMedia(mq).matches;
3490 }
3491
3492 return false;
3493 }
3494 };
3495
3496 function createMiniMap($table) {
3497 var tblsaw = $table.data("tablesaw");
3498 var $btns = $('<div class="tablesaw-advance minimap">');
3499 var $dotNav = $('<ul class="tablesaw-advance-dots">').appendTo($btns);
3500 var hideDot = "tablesaw-advance-dots-hide";
3501 var $headerCells = $table.data("tablesaw")._getPrimaryHeaderCells();
3502
3503 // populate dots
3504 $headerCells.each(function() {
3505 $dotNav.append("<li><i></i></li>");
3506 });
3507
3508 $btns.appendTo(tblsaw.$toolbar);
3509
3510 function showHideNav() {
3511 if (!MiniMap.show($table[0])) {
3512 $btns.css("display", "none");
3513 return;
3514 }
3515 $btns.css("display", "block");
3516
3517 // show/hide dots
3518 var dots = $dotNav.find("li").removeClass(hideDot);
3519 $table.find("thead th").each(function(i) {
3520 if ($(this).css("display") === "none") {
3521 dots.eq(i).addClass(hideDot);
3522 }
3523 });
3524 }
3525
3526 // run on init and resize
3527 showHideNav();
3528 $(window).on(Tablesaw.events.resize, showHideNav);
3529
3530 $table
3531 .on("tablesawcolumns.minimap", function() {
3532 showHideNav();
3533 })
3534 .on(Tablesaw.events.destroy + ".minimap", function() {
3535 var $t = $(this);
3536
3537 tblsaw.$toolbar.find(".tablesaw-advance").remove();
3538 $(window).off(Tablesaw.events.resize, showHideNav);
3539
3540 $t.off(".minimap");
3541 });
3542 }
3543
3544 // on tablecreate, init
3545 $(document).on(Tablesaw.events.create, function(e, tablesaw) {
3546 if (
3547 (tablesaw.mode === "swipe" || tablesaw.mode === "columntoggle") &&
3548 tablesaw.$table.is("[ " + MiniMap.attr.init + "]")
3549 ) {
3550 createMiniMap(tablesaw.$table);
3551 }
3552 });
3553
3554 // TODO OOP this better
3555 Tablesaw.MiniMap = MiniMap;
3556 })();
3557
3558 (function() {
3559 var S = {
3560 selectors: {
3561 init: "table[data-tablesaw-mode-switch]"
3562 },
3563 attributes: {
3564 excludeMode: "data-tablesaw-mode-exclude"
3565 },
3566 classes: {
3567 main: "tablesaw-modeswitch",
3568 toolbar: "tablesaw-bar-section"
3569 },
3570 modes: ["stack", "swipe", "columntoggle"],
3571 init: function(table) {
3572 var $table = $(table);
3573 var tblsaw = $table.data("tablesaw");
3574 var ignoreMode = $table.attr(S.attributes.excludeMode);
3575 var $toolbar = tblsaw.$toolbar;
3576 var $switcher = $("<div>").addClass(S.classes.main + " " + S.classes.toolbar);
3577
3578 var html = [
3579 '<label><span class="abbreviated">' +
3580 Tablesaw.i18n.modeSwitchColumnsAbbreviated +
3581 '</span><span class="longform">' +
3582 Tablesaw.i18n.modeSwitchColumns +
3583 "</span>:"
3584 ],
3585 dataMode = $table.attr("data-tablesaw-mode"),
3586 isSelected;
3587
3588 // TODO next major version: remove .btn
3589 html.push('<span class="btn tablesaw-btn"><select>');
3590 for (var j = 0, k = S.modes.length; j < k; j++) {
3591 if (ignoreMode && ignoreMode.toLowerCase() === S.modes[j]) {
3592 continue;
3593 }
3594
3595 isSelected = dataMode === S.modes[j];
3596
3597 html.push(
3598 "<option" +
3599 (isSelected ? " selected" : "") +
3600 ' value="' +
3601 S.modes[j] +
3602 '">' +
3603 Tablesaw.i18n.modes[j] +
3604 "</option>"
3605 );
3606 }
3607 html.push("</select></span></label>");
3608
3609 $switcher.html(html.join(""));
3610
3611 var $otherToolbarItems = $toolbar.find(".tablesaw-advance").eq(0);
3612 if ($otherToolbarItems.length) {
3613 $switcher.insertBefore($otherToolbarItems);
3614 } else {
3615 $switcher.appendTo($toolbar);
3616 }
3617
3618 $switcher.find(".tablesaw-btn").tablesawbtn();
3619 $switcher.find("select").on("change", function(event) {
3620 return S.onModeChange.call(table, event, $(this).val());
3621 });
3622 },
3623 onModeChange: function(event, val) {
3624 var $table = $(this);
3625 var tblsaw = $table.data("tablesaw");
3626 var $switcher = tblsaw.$toolbar.find("." + S.classes.main);
3627
3628 $switcher.remove();
3629 tblsaw.destroy();
3630
3631 $table.attr("data-tablesaw-mode", val);
3632 $table.tablesaw();
3633 }
3634 };
3635
3636 $(document).on(Tablesaw.events.create, function(e, Tablesaw) {
3637 if (Tablesaw.$table.is(S.selectors.init)) {
3638 S.init(Tablesaw.table);
3639 }
3640 });
3641
3642 // TODO OOP this and add to Tablesaw object
3643 })();
3644
3645 (function() {
3646 var pluginName = "tablesawCheckAll";
3647
3648 function CheckAll(tablesaw) {
3649 this.tablesaw = tablesaw;
3650 this.$table = tablesaw.$table;
3651
3652 this.attr = "data-tablesaw-checkall";
3653 this.checkAllSelector = "[" + this.attr + "]";
3654 this.forceCheckedSelector = "[" + this.attr + "-checked]";
3655 this.forceUncheckedSelector = "[" + this.attr + "-unchecked]";
3656 this.checkboxSelector = 'input[type="checkbox"]';
3657
3658 this.$triggers = null;
3659 this.$checkboxes = null;
3660
3661 if (this.$table.data(pluginName)) {
3662 return;
3663 }
3664 this.$table.data(pluginName, this);
3665 this.init();
3666 }
3667
3668 CheckAll.prototype._filterCells = function($checkboxes) {
3669 return $checkboxes
3670 .filter(function() {
3671 return !$(this)
3672 .closest("tr")
3673 .is("[data-tablesaw-subrow],[data-tablesaw-ignorerow]");
3674 })
3675 .find(this.checkboxSelector)
3676 .not(this.checkAllSelector);
3677 };
3678
3679 // With buttons you can use a scoping selector like: data-tablesaw-checkall="#my-scoped-id input[type='checkbox']"
3680 CheckAll.prototype.getCheckboxesForButton = function(button) {
3681 return this._filterCells($($(button).attr(this.attr)));
3682 };
3683
3684 CheckAll.prototype.getCheckboxesForCheckbox = function(checkbox) {
3685 return this._filterCells($($(checkbox).closest("th")[0].cells));
3686 };
3687
3688 CheckAll.prototype.init = function() {
3689 var self = this;
3690 this.$table.find(this.checkAllSelector).each(function() {
3691 var $trigger = $(this);
3692 if ($trigger.is(self.checkboxSelector)) {
3693 self.addCheckboxEvents(this);
3694 } else {
3695 self.addButtonEvents(this);
3696 }
3697 });
3698 };
3699
3700 CheckAll.prototype.addButtonEvents = function(trigger) {
3701 var self = this;
3702
3703 // Update body checkboxes when header checkbox is changed
3704 $(trigger).on("click", function(event) {
3705 event.preventDefault();
3706
3707 var $checkboxes = self.getCheckboxesForButton(this);
3708
3709 var allChecked = true;
3710 $checkboxes.each(function() {
3711 if (!this.checked) {
3712 allChecked = false;
3713 }
3714 });
3715
3716 var setChecked;
3717 if ($(this).is(self.forceCheckedSelector)) {
3718 setChecked = true;
3719 } else if ($(this).is(self.forceUncheckedSelector)) {
3720 setChecked = false;
3721 } else {
3722 setChecked = allChecked ? false : true;
3723 }
3724
3725 $checkboxes.each(function() {
3726 this.checked = setChecked;
3727
3728 $(this).trigger("change." + pluginName);
3729 });
3730 });
3731 };
3732
3733 CheckAll.prototype.addCheckboxEvents = function(trigger) {
3734 var self = this;
3735
3736 // Update body checkboxes when header checkbox is changed
3737 $(trigger).on("change", function() {
3738 var setChecked = this.checked;
3739
3740 self.getCheckboxesForCheckbox(this).each(function() {
3741 this.checked = setChecked;
3742 });
3743 });
3744
3745 var $checkboxes = self.getCheckboxesForCheckbox(trigger);
3746
3747 // Update header checkbox when body checkboxes are changed
3748 $checkboxes.on("change." + pluginName, function() {
3749 var checkedCount = 0;
3750 $checkboxes.each(function() {
3751 if (this.checked) {
3752 checkedCount++;
3753 }
3754 });
3755
3756 var allSelected = checkedCount === $checkboxes.length;
3757
3758 trigger.checked = allSelected;
3759
3760 // only indeterminate if some are selected (not all and not none)
3761 trigger.indeterminate = checkedCount !== 0 && !allSelected;
3762 });
3763 };
3764
3765 // on tablecreate, init
3766 $(document).on(Tablesaw.events.create, function(e, tablesaw) {
3767 new CheckAll(tablesaw);
3768 });
3769
3770 Tablesaw.CheckAll = CheckAll;
3771 })();
3772
3773 return Tablesaw;
3774 }));