Debian Codegraph @

beta-testing/widget-reorder.js

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