1
+ /*! tablesorter column reorder - beta testing
2
+ * Requires tablesorter v2.8+ and jQuery 1.7+
3
+ * by Rob Garrison
4
+ */
5
+ /*jshint browser:true, jquery:true, unused:false */
6
+ /*global jQuery: false */
7
+ ; ( function ( $ ) {
8
+ "use strict" ;
9
+
10
+ $ . tablesorter . addWidget ( {
11
+ id : 'reorder' ,
12
+ priority : 70 ,
13
+ options : {
14
+ reorder_axis : 'xy' , // x or xy
15
+ reorder_delay : 300 ,
16
+ reorder_helperClass : 'tablesorter-reorder-helper' ,
17
+ reorder_helperBar : 'tablesorter-reorder-helper-bar' ,
18
+ reorder_noReorder : 'reorder-false' ,
19
+ reorder_blocked : 'reorder-block-left reorder-block-end' ,
20
+ reorder_complete : null // callback
21
+ } ,
22
+ init : function ( table , thisWidget , c , wo ) {
23
+ var i , timer , $helper , $bar , clickOffset ,
24
+ lastIndx = - 1 ,
25
+ ts = $ . tablesorter ,
26
+ endIndex = - 1 ,
27
+ startIndex = - 1 ,
28
+ t = wo . reorder_blocked . split ( ' ' ) ,
29
+ noReorderLeft = t [ 0 ] || 'reorder-block-left' ,
30
+ noReorderLast = t [ 1 ] || 'reorder-block-end' ,
31
+ lastOffset = c . $headers . not ( '.' + noReorderLeft ) . first ( ) ,
32
+ offsets = c . $headers . map ( function ( i ) {
33
+ var s , $t = $ ( this ) ;
34
+ if ( $t . hasClass ( noReorderLeft ) ) {
35
+ s = lastOffset ;
36
+ $t = s ;
37
+ //lastOffset = $t;
38
+ }
39
+ lastOffset = $t ;
40
+ return $t . offset ( ) . left ;
41
+ } ) . get ( ) ,
42
+ len = offsets . length ,
43
+ startReorder = function ( e , $th ) {
44
+ var p = $th . position ( ) ,
45
+ r = $th . parent ( ) . position ( ) ,
46
+ i = startIndex = $th . index ( ) ;
47
+ clickOffset = [ e . pageX - p . left , e . pageY - r . top ] ;
48
+ $helper = c . $table . clone ( ) ;
49
+ $helper . find ( '> thead > tr:first' ) . children ( '[data-column!=' + i + ']' ) . remove ( ) ;
50
+ $helper . find ( 'thead tr:gt(0), caption, colgroup, tbody, tfoot' ) . remove ( ) ;
51
+ $helper
52
+ . css ( {
53
+ position : 'absolute' ,
54
+ zIndex : 1 ,
55
+ left : p . left - clickOffset [ 0 ] ,
56
+ top : r . top - clickOffset [ 1 ] ,
57
+ width : $th . outerWidth ( )
58
+ } )
59
+ . appendTo ( 'body' )
60
+ . find ( 'th, td' ) . addClass ( wo . reorder_helperClass ) ;
61
+ $bar = $ ( '<div class="' + wo . reorder_helperBar + '" />' )
62
+ . css ( {
63
+ position : 'absolute' ,
64
+ top : c . $table . find ( 'thead' ) . offset ( ) . top ,
65
+ height : $th . closest ( 'thead' ) . outerHeight ( ) + c . $table . find ( 'tbody' ) . height ( )
66
+ } )
67
+ . appendTo ( 'body' ) ;
68
+ positionBar ( e ) ;
69
+ lastIndx = endIndex ;
70
+ } ,
71
+ positionBar = function ( e ) {
72
+ for ( i = 0 ; i <= len ; i ++ ) {
73
+ if ( i > 0 && e . pageX < offsets [ i - 1 ] + ( offsets [ i ] - offsets [ i - 1 ] ) / 2 && ! c . $headers . eq ( i ) . hasClass ( noReorderLeft ) ) {
74
+ endIndex = i - 1 ;
75
+ // endIndex = offsets.lastIndexOf( offsets[i-1] ); // lastIndexOf not supported by IE8 and older
76
+ if ( endIndex >= 0 && lastIndx === endIndex ) { return false ; }
77
+ lastIndx = endIndex ;
78
+ if ( c . debug ) {
79
+ console . log ( endIndex === 0 ? 'target before column 0' : endIndex === len ? 'target after last column' : 'target between columns ' + startIndex + ' and ' + endIndex ) ;
80
+ }
81
+ $bar . css ( 'left' , offsets [ i - 1 ] ) ;
82
+ return false ;
83
+ }
84
+ }
85
+ if ( endIndex < 0 ) {
86
+ endIndex = len ;
87
+ $bar . css ( 'left' , offsets [ len ] ) ;
88
+ }
89
+ } ,
90
+ finishReorder = function ( ) {
91
+ $helper . remove ( ) ;
92
+ $bar . remove ( ) ;
93
+ // finish reorder
94
+ var adj , s = startIndex ,
95
+ rows = c . $table . find ( 'tr' ) ,
96
+ cols ;
97
+ startIndex = - 1 ; // stop mousemove updates
98
+ if ( s > - 1 && endIndex > - 1 && s != endIndex && s + 1 !== endIndex ) {
99
+ adj = endIndex !== 0 ;
100
+ if ( c . debug ) {
101
+ console . log ( 'Inserting column ' + s + ( adj ? ' after' : ' before' ) + ' column ' + ( endIndex - adj ? 1 : 0 ) ) ;
102
+ }
103
+ rows . each ( function ( ) {
104
+ cols = $ ( this ) . children ( ) ;
105
+ cols . eq ( s ) [ adj ? 'insertAfter' : 'insertBefore' ] ( cols . eq ( endIndex - ( adj ? 1 : 0 ) ) ) ;
106
+ } ) ;
107
+ cols = [ ] ;
108
+ // stored header info needs to be modified too!
109
+ for ( i = 0 ; i < len ; i ++ ) {
110
+ if ( i === s ) { continue ; }
111
+ if ( i === endIndex - ( adj ? 1 : 0 ) ) {
112
+ if ( ! adj ) { cols . push ( c . headerContent [ s ] ) ; }
113
+ cols . push ( c . headerContent [ i ] ) ;
114
+ if ( adj ) { cols . push ( c . headerContent [ s ] ) ; }
115
+ } else {
116
+ cols . push ( c . headerContent [ i ] ) ;
117
+ }
118
+ }
119
+ c . headerContent = cols ;
120
+ // cols = c.headerContent.splice(s, 1);
121
+ // c.headerContent.splice(endIndex - (adj ? 1 : 0), 0, cols);
122
+ c . $table . trigger ( 'updateAll' , [ true , wo . reorder_complete ] ) ;
123
+ }
124
+ endIndex = - 1 ;
125
+ } ,
126
+ mdown = function ( e , el ) {
127
+ var $t = $ ( el ) , evt = e ;
128
+ if ( $t . hasClass ( wo . reorder_noReorder ) ) { return ; }
129
+ timer = setTimeout ( function ( ) {
130
+ $t . addClass ( 'tablesorter-reorder' ) ;
131
+ startReorder ( evt , $t ) ;
132
+ } , wo . reorder_delay ) ;
133
+ } ;
134
+
135
+ console . log ( c . $headers . last ( ) . hasClass ( noReorderLast ) ) ;
136
+
137
+ if ( c . $headers . last ( ) . hasClass ( noReorderLast ) ) {
138
+ offsets . push ( offsets [ offsets . length - 1 ] ) ;
139
+ } else {
140
+ offsets . push ( c . $table . offset ( ) . left + c . $table . outerWidth ( ) ) ;
141
+ }
142
+
143
+ c . $headers . not ( '.' + wo . reorder_noReorder ) . bind ( 'mousedown.reorder' , function ( e ) {
144
+ mdown ( e , this ) ;
145
+ } ) ;
146
+
147
+ $ ( document )
148
+ . bind ( 'mousemove.reorder' , function ( e ) {
149
+ if ( startIndex !== - 1 ) {
150
+ var c = { left : e . pageX - clickOffset [ 0 ] } ;
151
+ endIndex = - 1 ;
152
+ if ( / y / . test ( wo . reorder_axis ) ) {
153
+ c . top = e . pageY - clickOffset [ 1 ] ;
154
+ }
155
+ $helper . css ( c ) ;
156
+ positionBar ( e ) ;
157
+ }
158
+ } )
159
+ . add ( c . $headers )
160
+ . bind ( 'mouseup.reorder' , function ( ) {
161
+ clearTimeout ( timer ) ;
162
+ if ( startIndex !== - 1 && endIndex !== - 1 ) {
163
+ finishReorder ( ) ;
164
+ } else {
165
+ startIndex = - 1 ;
166
+ }
167
+ } ) ;
168
+
169
+ // has sticky headers?
170
+ c . $table . bind ( 'stickyHeadersInit' , function ( ) {
171
+ wo . $sticky . find ( 'thead' ) . children ( ) . not ( '.' + wo . reorder_noReorder ) . bind ( 'mousedown.reorder' , function ( e ) {
172
+ mdown ( e , this ) ;
173
+ } ) ;
174
+ } ) ;
175
+
176
+ }
177
+ } ) ;
178
+
179
+ // add mouse coordinates
180
+ $x = $ ( '#main h1:last' ) ; $ ( document ) . mousemove ( function ( e ) { $x . html ( e . pageX ) ; } ) ;
181
+
182
+ } ) ( jQuery ) ;
0 commit comments