13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* tablesaw: A set of plugins for responsive tables
* Sortable column headers
* Copyright (c) 2013 Filament Group, Inc.
* MIT License
*/
 
;(function( $ ) {
    function getSortValue( cell ) {
        return $.map( cell.childNodes, function( el ) {
                var $el = $( el );
                if( $el.is( 'input, select' ) ) {
                    return $el.val();
                } else if( $el.hasClass( 'tablesaw-cell-label' ) ) {
                    return;
                }
                return $.trim( $el.text() );
            }).join( '' );
    }
 
    var pluginName = "tablesaw-sortable",
        initSelector = "table[data-" + pluginName + "]",
        sortableSwitchSelector = "[data-" + pluginName + "-switch]",
        attrs = {
            defaultCol: "data-tablesaw-sortable-default-col",
            numericCol: "data-tablesaw-sortable-numeric"
        },
        classes = {
            head: pluginName + "-head",
            ascend: pluginName + "-ascending",
            descend: pluginName + "-descending",
            switcher: pluginName + "-switch",
            tableToolbar: 'tablesaw-toolbar',
            sortButton: pluginName + "-btn"
        },
        methods = {
            _create: function( o ){
                return $( this ).each(function() {
                    var init = $( this ).data( "init" + pluginName );
                    if( init ) {
                        return false;
                    }
                    $( this )
                        .data( "init"+ pluginName, true )
                        .trigger( "beforecreate." + pluginName )
                        [ pluginName ]( "_init" , o )
                        .trigger( "create." + pluginName );
                });
            },
            _init: function(){
                var el = $( this ),
                    heads,
                    $switcher;
 
                var addClassToTable = function(){
                        el.addClass( pluginName );
                    },
                    addClassToHeads = function( h ){
                        $.each( h , function( i , v ){
                            $( v ).addClass( classes.head );
                        });
                    },
                    makeHeadsActionable = function( h , fn ){
                        $.each( h , function( i , v ){
                            var b = $( "<button class='" + classes.sortButton + "'/>" );
                            b.bind( "click" , { col: v } , fn );
                            $( v ).wrapInner( b );
                        });
                    },
                    clearOthers = function( sibs ){
                        $.each( sibs , function( i , v ){
                            var col = $( v );
                            col.removeAttr( attrs.defaultCol );
                            col.removeClass( classes.ascend );
                            col.removeClass( classes.descend );
                        });
                    },
                    headsOnAction = function( e ){
                        if( $( e.target ).is( 'a[href]' ) ) {
                            return;
                        }
 
                        e.stopPropagation();
                        var head = $( this ).parent(),
                            v = e.data.col,
                            newSortValue = heads.index( head );
 
                        clearOthers( head.siblings() );
                        if( head.hasClass( classes.descend ) ){
                            el[ pluginName ]( "sortBy" , v , true);
                            newSortValue += '_asc';
                        } else {
                            el[ pluginName ]( "sortBy" , v );
                            newSortValue += '_desc';
                        }
                        if( $switcher ) {
                            $switcher.find( 'select' ).val( newSortValue ).trigger( 'refresh' );
                        }
 
                        e.preventDefault();
                    },
                    handleDefault = function( heads ){
                        $.each( heads , function( idx , el ){
                            var $el = $( el );
                            if( $el.is( "[" + attrs.defaultCol + "]" ) ){
                                if( !$el.hasClass( classes.descend ) ) {
                                    $el.addClass( classes.ascend );
                                }
                            }
                        });
                    },
                    addSwitcher = function( heads ){
                        $switcher = $( '<div>' ).addClass( classes.switcher ).addClass( classes.tableToolbar ).html(function() {
                            var html = [ '<label>' + Tablesaw.i18n.sort + ':' ];
 
                            html.push( '<span class="btn btn-small">&#160;<select>' );
                            heads.each(function( j ) {
                                var $t = $( this );
                                var isDefaultCol = $t.is( "[" + attrs.defaultCol + "]" );
                                var isDescending = $t.hasClass( classes.descend );
 
                                var hasNumericAttribute = $t.is( '[data-sortable-numeric]' );
                                var numericCount = 0;
                                // Check only the first four rows to see if the column is numbers.
                                var numericCountMax = 5;
 
                                $( this.cells ).slice( 0, numericCountMax ).each(function() {
                                    if( !isNaN( parseInt( getSortValue( this ), 10 ) ) ) {
                                        numericCount++;
                                    }
                                });
                                var isNumeric = numericCount === numericCountMax;
                                if( !hasNumericAttribute ) {
                                    $t.attr( "data-sortable-numeric", isNumeric ? "" : "false" );
                                }
 
                                html.push( '<option' + ( isDefaultCol && !isDescending ? ' selected' : '' ) + ' value="' + j + '_asc">' + $t.text() + ' ' + ( isNumeric ? '&#x2191;' : '(A-Z)' ) + '</option>' );
                                html.push( '<option' + ( isDefaultCol && isDescending ? ' selected' : '' ) + ' value="' + j + '_desc">' + $t.text() + ' ' + ( isNumeric ? '&#x2193;' : '(Z-A)' ) + '</option>' );
                            });
                            html.push( '</select></span></label>' );
 
                            return html.join('');
                        });
 
                        var $toolbar = el.prev().filter( '.tablesaw-bar' ),
                            $firstChild = $toolbar.children().eq( 0 );
 
                        if( $firstChild.length ) {
                            $switcher.insertBefore( $firstChild );
                        } else {
                            $switcher.appendTo( $toolbar );
                        }
                        $switcher.find( '.btn' ).tablesawbtn();
                        $switcher.find( 'select' ).on( 'change', function() {
                            var val = $( this ).val().split( '_' ),
                                head = heads.eq( val[ 0 ] );
 
                            clearOthers( head.siblings() );
                            el[ pluginName ]( 'sortBy', head.get( 0 ), val[ 1 ] === 'asc' );
                        });
                    };
 
                    addClassToTable();
                    heads = el.find( "thead th[data-" + pluginName + "-col]" );
                    addClassToHeads( heads );
                    makeHeadsActionable( heads , headsOnAction );
                    handleDefault( heads );
 
                    if( el.is( sortableSwitchSelector ) ) {
                        addSwitcher( heads, el.find('tbody tr:nth-child(-n+3)') );
                    }
            },
            getColumnNumber: function( col ){
                return $( col ).prevAll().length;
            },
            getTableRows: function(){
                return $( this ).find( "tbody tr" );
            },
            sortRows: function( rows , colNum , ascending, col ){
                var cells, fn, sorted;
                var getCells = function( rows ){
                        var cells = [];
                        $.each( rows , function( i , r ){
                            var element = $( r ).children().get( colNum );
                            cells.push({
                                element: element,
                                cell: getSortValue( element ),
                                rowNum: i
                            });
                        });
                        return cells;
                    },
                    getSortFxn = function( ascending, forceNumeric ){
                        var fn,
                            regex = /[^\-\+\d\.]/g;
                        if( ascending ){
                            fn = function( a , b ){
                                if( forceNumeric ) {
                                    return parseFloat( a.cell.replace( regex, '' ) ) - parseFloat( b.cell.replace( regex, '' ) );
                                } else {
                                    return a.cell.toLowerCase() > b.cell.toLowerCase() ? 1 : -1;
                                }
                            };
                        } else {
                            fn = function( a , b ){
                                if( forceNumeric ) {
                                    return parseFloat( b.cell.replace( regex, '' ) ) - parseFloat( a.cell.replace( regex, '' ) );
                                } else {
                                    return a.cell.toLowerCase() < b.cell.toLowerCase() ? 1 : -1;
                                }
                            };
                        }
                        return fn;
                    },
                    applyToRows = function( sorted , rows ){
                        var newRows = [], i, l, cur;
                        for( i = 0, l = sorted.length ; i < l ; i++ ){
                            cur = sorted[ i ].rowNum;
                            newRows.push( rows[cur] );
                        }
                        return newRows;
                    };
 
                cells = getCells( rows );
                var customFn = $( col ).data( 'tablesaw-sort' );
                fn = ( customFn && typeof customFn === "function" ? customFn( ascending ) : false ) ||
                    getSortFxn( ascending, $( col ).is( '[data-sortable-numeric]' ) && !$( col ).is( '[data-sortable-numeric="false"]' ) );
                sorted = cells.sort( fn );
                rows = applyToRows( sorted , rows );
                return rows;
            },
            replaceTableRows: function( rows ){
                var el = $( this ),
                    body = el.find( "tbody" );
                body.html( rows );
            },
            makeColDefault: function( col , a ){
                var c = $( col );
                c.attr( attrs.defaultCol , "true" );
                if( a ){
                    c.removeClass( classes.descend );
                    c.addClass( classes.ascend );
                } else {
                    c.removeClass( classes.ascend );
                    c.addClass( classes.descend );
                }
            },
            sortBy: function( col , ascending ){
                var el = $( this ), colNum, rows;
 
                colNum = el[ pluginName ]( "getColumnNumber" , col );
                rows = el[ pluginName ]( "getTableRows" );
                rows = el[ pluginName ]( "sortRows" , rows , colNum , ascending, col );
                el[ pluginName ]( "replaceTableRows" , rows );
                el[ pluginName ]( "makeColDefault" , col , ascending );
            }
        };
 
    // Collection method.
    $.fn[ pluginName ] = function( arrg ) {
        var args = Array.prototype.slice.call( arguments , 1),
            returnVal;
 
        // if it's a method
        if( arrg && typeof( arrg ) === "string" ){
            returnVal = $.fn[ pluginName ].prototype[ arrg ].apply( this[0], args );
            return (typeof returnVal !== "undefined")? returnVal:$(this);
        }
        // check init
        if( !$( this ).data( pluginName + "data" ) ){
            $( this ).data( pluginName + "active", true );
            $.fn[ pluginName ].prototype._create.call( this , arrg );
        }
        return $(this);
    };
    // add methods
    $.extend( $.fn[ pluginName ].prototype, methods );
 
    $( document ).on( "tablesawcreate", function( e, Tablesaw ) {
        if( Tablesaw.$table.is( initSelector ) ) {
            Tablesaw.$table[ pluginName ]();
        }
    });
 
}( jQuery ));