Mercurial > nebulaweb3
comparison default/node_modules/tablesaw/dist/stackonly/tablesaw.stackonly.jquery.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 (function (root, factory) { | |
5 if (typeof define === 'function' && define.amd) { | |
6 define(["jquery"], function (jQuery) { | |
7 return (root.Tablesaw = factory(jQuery, root)); | |
8 }); | |
9 } else if (typeof exports === 'object') { | |
10 if( "document" in root ) { | |
11 module.exports = factory(require('jquery'), root); | |
12 } else { | |
13 // special jQuery case for CommonJS (pass in a window) | |
14 module.exports = factory(require('jquery')(root), root); | |
15 } | |
16 } else { | |
17 root.Tablesaw = factory(jQuery, root); | |
18 } | |
19 }(typeof window !== "undefined" ? window : this, function ($, window) { | |
20 "use strict"; | |
21 | |
22 var document = window.document; | |
23 | |
24 var domContentLoadedTriggered = false; | |
25 document.addEventListener("DOMContentLoaded", function() { | |
26 domContentLoadedTriggered = true; | |
27 }); | |
28 | |
29 var Tablesaw = { | |
30 i18n: { | |
31 modeStack: "Stack", | |
32 modeSwipe: "Swipe", | |
33 modeToggle: "Toggle", | |
34 modeSwitchColumnsAbbreviated: "Cols", | |
35 modeSwitchColumns: "Columns", | |
36 columnToggleButton: "Columns", | |
37 columnToggleError: "No eligible columns.", | |
38 sort: "Sort", | |
39 swipePreviousColumn: "Previous column", | |
40 swipeNextColumn: "Next column" | |
41 }, | |
42 // cut the mustard | |
43 mustard: | |
44 "head" in document && // IE9+, Firefox 4+, Safari 5.1+, Mobile Safari 4.1+, Opera 11.5+, Android 2.3+ | |
45 (!window.blackberry || window.WebKitPoint) && // only WebKit Blackberry (OS 6+) | |
46 !window.operamini, | |
47 $: $, | |
48 _init: function(element) { | |
49 Tablesaw.$(element || document).trigger("enhance.tablesaw"); | |
50 }, | |
51 init: function(element) { | |
52 if (!domContentLoadedTriggered) { | |
53 if ("addEventListener" in document) { | |
54 // Use raw DOMContentLoaded instead of shoestring (may have issues in Android 2.3, exhibited by stack table) | |
55 document.addEventListener("DOMContentLoaded", function() { | |
56 Tablesaw._init(element); | |
57 }); | |
58 } | |
59 } else { | |
60 Tablesaw._init(element); | |
61 } | |
62 } | |
63 }; | |
64 | |
65 $(document).on("enhance.tablesaw", function() { | |
66 // Extend i18n config, if one exists. | |
67 if (typeof TablesawConfig !== "undefined" && TablesawConfig.i18n) { | |
68 Tablesaw.i18n = $.extend(Tablesaw.i18n, TablesawConfig.i18n || {}); | |
69 } | |
70 | |
71 Tablesaw.i18n.modes = [ | |
72 Tablesaw.i18n.modeStack, | |
73 Tablesaw.i18n.modeSwipe, | |
74 Tablesaw.i18n.modeToggle | |
75 ]; | |
76 }); | |
77 | |
78 if (Tablesaw.mustard) { | |
79 $(document.documentElement).addClass("tablesaw-enhanced"); | |
80 } | |
81 | |
82 (function() { | |
83 var pluginName = "tablesaw"; | |
84 var classes = { | |
85 toolbar: "tablesaw-bar" | |
86 }; | |
87 var events = { | |
88 create: "tablesawcreate", | |
89 destroy: "tablesawdestroy", | |
90 refresh: "tablesawrefresh", | |
91 resize: "tablesawresize" | |
92 }; | |
93 var defaultMode = "stack"; | |
94 var initSelector = "table"; | |
95 var initFilterSelector = "[data-tablesaw],[data-tablesaw-mode],[data-tablesaw-sortable]"; | |
96 var defaultConfig = {}; | |
97 | |
98 Tablesaw.events = events; | |
99 | |
100 var Table = function(element) { | |
101 if (!element) { | |
102 throw new Error("Tablesaw requires an element."); | |
103 } | |
104 | |
105 this.table = element; | |
106 this.$table = $(element); | |
107 | |
108 // only one <thead> and <tfoot> are allowed, per the specification | |
109 this.$thead = this.$table | |
110 .children() | |
111 .filter("thead") | |
112 .eq(0); | |
113 | |
114 // multiple <tbody> are allowed, per the specification | |
115 this.$tbody = this.$table.children().filter("tbody"); | |
116 | |
117 this.mode = this.$table.attr("data-tablesaw-mode") || defaultMode; | |
118 | |
119 this.$toolbar = null; | |
120 | |
121 this.attributes = { | |
122 subrow: "data-tablesaw-subrow", | |
123 ignorerow: "data-tablesaw-ignorerow" | |
124 }; | |
125 | |
126 this.init(); | |
127 }; | |
128 | |
129 Table.prototype.init = function() { | |
130 if (!this.$thead.length) { | |
131 throw new Error("tablesaw: a <thead> is required, but none was found."); | |
132 } | |
133 | |
134 if (!this.$thead.find("th").length) { | |
135 throw new Error("tablesaw: no header cells found. Are you using <th> inside of <thead>?"); | |
136 } | |
137 | |
138 // assign an id if there is none | |
139 if (!this.$table.attr("id")) { | |
140 this.$table.attr("id", pluginName + "-" + Math.round(Math.random() * 10000)); | |
141 } | |
142 | |
143 this.createToolbar(); | |
144 | |
145 this._initCells(); | |
146 | |
147 this.$table.data(pluginName, this); | |
148 | |
149 this.$table.trigger(events.create, [this]); | |
150 }; | |
151 | |
152 Table.prototype.getConfig = function(pluginSpecificConfig) { | |
153 // shoestring extend doesn’t support arbitrary args | |
154 var configs = $.extend(defaultConfig, pluginSpecificConfig || {}); | |
155 return $.extend(configs, typeof TablesawConfig !== "undefined" ? TablesawConfig : {}); | |
156 }; | |
157 | |
158 Table.prototype._getPrimaryHeaderRow = function() { | |
159 return this._getHeaderRows().eq(0); | |
160 }; | |
161 | |
162 Table.prototype._getHeaderRows = function() { | |
163 return this.$thead | |
164 .children() | |
165 .filter("tr") | |
166 .filter(function() { | |
167 return !$(this).is("[data-tablesaw-ignorerow]"); | |
168 }); | |
169 }; | |
170 | |
171 Table.prototype._getRowIndex = function($row) { | |
172 return $row.prevAll().length; | |
173 }; | |
174 | |
175 Table.prototype._getHeaderRowIndeces = function() { | |
176 var self = this; | |
177 var indeces = []; | |
178 this._getHeaderRows().each(function() { | |
179 indeces.push(self._getRowIndex($(this))); | |
180 }); | |
181 return indeces; | |
182 }; | |
183 | |
184 Table.prototype._getPrimaryHeaderCells = function($row) { | |
185 return ($row || this._getPrimaryHeaderRow()).find("th"); | |
186 }; | |
187 | |
188 Table.prototype._$getCells = function(th) { | |
189 var self = this; | |
190 return $(th) | |
191 .add(th.cells) | |
192 .filter(function() { | |
193 var $t = $(this); | |
194 var $row = $t.parent(); | |
195 var hasColspan = $t.is("[colspan]"); | |
196 // no subrows or ignored rows (keep cells in ignored rows that do not have a colspan) | |
197 return ( | |
198 !$row.is("[" + self.attributes.subrow + "]") && | |
199 (!$row.is("[" + self.attributes.ignorerow + "]") || !hasColspan) | |
200 ); | |
201 }); | |
202 }; | |
203 | |
204 Table.prototype._getVisibleColspan = function() { | |
205 var colspan = 0; | |
206 this._getPrimaryHeaderCells().each(function() { | |
207 var $t = $(this); | |
208 if ($t.css("display") !== "none") { | |
209 colspan += parseInt($t.attr("colspan"), 10) || 1; | |
210 } | |
211 }); | |
212 return colspan; | |
213 }; | |
214 | |
215 Table.prototype.getColspanForCell = function($cell) { | |
216 var visibleColspan = this._getVisibleColspan(); | |
217 var visibleSiblingColumns = 0; | |
218 if ($cell.closest("tr").data("tablesaw-rowspanned")) { | |
219 visibleSiblingColumns++; | |
220 } | |
221 | |
222 $cell.siblings().each(function() { | |
223 var $t = $(this); | |
224 var colColspan = parseInt($t.attr("colspan"), 10) || 1; | |
225 | |
226 if ($t.css("display") !== "none") { | |
227 visibleSiblingColumns += colColspan; | |
228 } | |
229 }); | |
230 // console.log( $cell[ 0 ], visibleColspan, visibleSiblingColumns ); | |
231 | |
232 return visibleColspan - visibleSiblingColumns; | |
233 }; | |
234 | |
235 Table.prototype.isCellInColumn = function(header, cell) { | |
236 return $(header) | |
237 .add(header.cells) | |
238 .filter(function() { | |
239 return this === cell; | |
240 }).length; | |
241 }; | |
242 | |
243 Table.prototype.updateColspanCells = function(cls, header, userAction) { | |
244 var self = this; | |
245 var primaryHeaderRow = self._getPrimaryHeaderRow(); | |
246 | |
247 // find persistent column rowspans | |
248 this.$table.find("[rowspan][data-tablesaw-priority]").each(function() { | |
249 var $t = $(this); | |
250 if ($t.attr("data-tablesaw-priority") !== "persist") { | |
251 return; | |
252 } | |
253 | |
254 var $row = $t.closest("tr"); | |
255 var rowspan = parseInt($t.attr("rowspan"), 10); | |
256 if (rowspan > 1) { | |
257 $row = $row.next(); | |
258 | |
259 $row.data("tablesaw-rowspanned", true); | |
260 | |
261 rowspan--; | |
262 } | |
263 }); | |
264 | |
265 this.$table | |
266 .find("[colspan],[data-tablesaw-maxcolspan]") | |
267 .filter(function() { | |
268 // is not in primary header row | |
269 return $(this).closest("tr")[0] !== primaryHeaderRow[0]; | |
270 }) | |
271 .each(function() { | |
272 var $cell = $(this); | |
273 | |
274 if (userAction === undefined || self.isCellInColumn(header, this)) { | |
275 } else { | |
276 // if is not a user action AND the cell is not in the updating column, kill it | |
277 return; | |
278 } | |
279 | |
280 var colspan = self.getColspanForCell($cell); | |
281 | |
282 if (cls && userAction !== undefined) { | |
283 // console.log( colspan === 0 ? "addClass" : "removeClass", $cell ); | |
284 $cell[colspan === 0 ? "addClass" : "removeClass"](cls); | |
285 } | |
286 | |
287 // cache original colspan | |
288 var maxColspan = parseInt($cell.attr("data-tablesaw-maxcolspan"), 10); | |
289 if (!maxColspan) { | |
290 $cell.attr("data-tablesaw-maxcolspan", $cell.attr("colspan")); | |
291 } else if (colspan > maxColspan) { | |
292 colspan = maxColspan; | |
293 } | |
294 | |
295 // console.log( this, "setting colspan to ", colspan ); | |
296 $cell.attr("colspan", colspan); | |
297 }); | |
298 }; | |
299 | |
300 Table.prototype._findPrimaryHeadersForCell = function(cell) { | |
301 var $headerRow = this._getPrimaryHeaderRow(); | |
302 var $headers = this._getPrimaryHeaderCells($headerRow); | |
303 var headerRowIndex = this._getRowIndex($headerRow); | |
304 var results = []; | |
305 | |
306 for (var rowNumber = 0; rowNumber < this.headerMapping.length; rowNumber++) { | |
307 if (rowNumber === headerRowIndex) { | |
308 continue; | |
309 } | |
310 for (var colNumber = 0; colNumber < this.headerMapping[rowNumber].length; colNumber++) { | |
311 if (this.headerMapping[rowNumber][colNumber] === cell) { | |
312 results.push($headers[colNumber]); | |
313 } | |
314 } | |
315 } | |
316 return results; | |
317 }; | |
318 | |
319 // used by init cells | |
320 Table.prototype.getRows = function() { | |
321 var self = this; | |
322 return this.$table.find("tr").filter(function() { | |
323 return $(this) | |
324 .closest("table") | |
325 .is(self.$table); | |
326 }); | |
327 }; | |
328 | |
329 // used by sortable | |
330 Table.prototype.getBodyRows = function(tbody) { | |
331 return (tbody ? $(tbody) : this.$tbody).children().filter("tr"); | |
332 }; | |
333 | |
334 Table.prototype.getHeaderCellIndex = function(cell) { | |
335 var lookup = this.headerMapping[0]; | |
336 for (var colIndex = 0; colIndex < lookup.length; colIndex++) { | |
337 if (lookup[colIndex] === cell) { | |
338 return colIndex; | |
339 } | |
340 } | |
341 | |
342 return -1; | |
343 }; | |
344 | |
345 Table.prototype._initCells = function() { | |
346 // re-establish original colspans | |
347 this.$table.find("[data-tablesaw-maxcolspan]").each(function() { | |
348 var $t = $(this); | |
349 $t.attr("colspan", $t.attr("data-tablesaw-maxcolspan")); | |
350 }); | |
351 | |
352 var $rows = this.getRows(); | |
353 var columnLookup = []; | |
354 | |
355 $rows.each(function(rowNumber) { | |
356 columnLookup[rowNumber] = []; | |
357 }); | |
358 | |
359 $rows.each(function(rowNumber) { | |
360 var coltally = 0; | |
361 var $t = $(this); | |
362 var children = $t.children(); | |
363 | |
364 children.each(function() { | |
365 var colspan = parseInt( | |
366 this.getAttribute("data-tablesaw-maxcolspan") || this.getAttribute("colspan"), | |
367 10 | |
368 ); | |
369 var rowspan = parseInt(this.getAttribute("rowspan"), 10); | |
370 | |
371 // set in a previous rowspan | |
372 while (columnLookup[rowNumber][coltally]) { | |
373 coltally++; | |
374 } | |
375 | |
376 columnLookup[rowNumber][coltally] = this; | |
377 | |
378 // TODO? both colspan and rowspan | |
379 if (colspan) { | |
380 for (var k = 0; k < colspan - 1; k++) { | |
381 coltally++; | |
382 columnLookup[rowNumber][coltally] = this; | |
383 } | |
384 } | |
385 if (rowspan) { | |
386 for (var j = 1; j < rowspan; j++) { | |
387 columnLookup[rowNumber + j][coltally] = this; | |
388 } | |
389 } | |
390 | |
391 coltally++; | |
392 }); | |
393 }); | |
394 | |
395 var headerRowIndeces = this._getHeaderRowIndeces(); | |
396 for (var colNumber = 0; colNumber < columnLookup[0].length; colNumber++) { | |
397 for (var headerIndex = 0, k = headerRowIndeces.length; headerIndex < k; headerIndex++) { | |
398 var headerCol = columnLookup[headerRowIndeces[headerIndex]][colNumber]; | |
399 | |
400 var rowNumber = headerRowIndeces[headerIndex]; | |
401 var rowCell; | |
402 | |
403 if (!headerCol.cells) { | |
404 headerCol.cells = []; | |
405 } | |
406 | |
407 while (rowNumber < columnLookup.length) { | |
408 rowCell = columnLookup[rowNumber][colNumber]; | |
409 | |
410 if (headerCol !== rowCell) { | |
411 headerCol.cells.push(rowCell); | |
412 } | |
413 | |
414 rowNumber++; | |
415 } | |
416 } | |
417 } | |
418 | |
419 this.headerMapping = columnLookup; | |
420 }; | |
421 | |
422 Table.prototype.refresh = function() { | |
423 this._initCells(); | |
424 | |
425 this.$table.trigger(events.refresh, [this]); | |
426 }; | |
427 | |
428 Table.prototype._getToolbarAnchor = function() { | |
429 var $parent = this.$table.parent(); | |
430 if ($parent.is(".tablesaw-overflow")) { | |
431 return $parent; | |
432 } | |
433 return this.$table; | |
434 }; | |
435 | |
436 Table.prototype._getToolbar = function($anchor) { | |
437 if (!$anchor) { | |
438 $anchor = this._getToolbarAnchor(); | |
439 } | |
440 return $anchor.prev().filter("." + classes.toolbar); | |
441 }; | |
442 | |
443 Table.prototype.createToolbar = function() { | |
444 // Insert the toolbar | |
445 // TODO move this into a separate component | |
446 var $anchor = this._getToolbarAnchor(); | |
447 var $toolbar = this._getToolbar($anchor); | |
448 if (!$toolbar.length) { | |
449 $toolbar = $("<div>") | |
450 .addClass(classes.toolbar) | |
451 .insertBefore($anchor); | |
452 } | |
453 this.$toolbar = $toolbar; | |
454 | |
455 if (this.mode) { | |
456 this.$toolbar.addClass("tablesaw-mode-" + this.mode); | |
457 } | |
458 }; | |
459 | |
460 Table.prototype.destroy = function() { | |
461 // Don’t remove the toolbar, just erase the classes on it. | |
462 // Some of the table features are not yet destroy-friendly. | |
463 this._getToolbar().each(function() { | |
464 this.className = this.className.replace(/\btablesaw-mode\-\w*\b/gi, ""); | |
465 }); | |
466 | |
467 var tableId = this.$table.attr("id"); | |
468 $(document).off("." + tableId); | |
469 $(window).off("." + tableId); | |
470 | |
471 // other plugins | |
472 this.$table.trigger(events.destroy, [this]); | |
473 | |
474 this.$table.removeData(pluginName); | |
475 }; | |
476 | |
477 // Collection method. | |
478 $.fn[pluginName] = function() { | |
479 return this.each(function() { | |
480 var $t = $(this); | |
481 | |
482 if ($t.data(pluginName)) { | |
483 return; | |
484 } | |
485 | |
486 new Table(this); | |
487 }); | |
488 }; | |
489 | |
490 var $doc = $(document); | |
491 $doc.on("enhance.tablesaw", function(e) { | |
492 // Cut the mustard | |
493 if (Tablesaw.mustard) { | |
494 $(e.target) | |
495 .find(initSelector) | |
496 .filter(initFilterSelector) | |
497 [pluginName](); | |
498 } | |
499 }); | |
500 | |
501 // Avoid a resize during scroll: | |
502 // Some Mobile devices trigger a resize during scroll (sometimes when | |
503 // doing elastic stretch at the end of the document or from the | |
504 // location bar hide) | |
505 var isScrolling = false; | |
506 var scrollTimeout; | |
507 $doc.on("scroll.tablesaw", function() { | |
508 isScrolling = true; | |
509 | |
510 window.clearTimeout(scrollTimeout); | |
511 scrollTimeout = window.setTimeout(function() { | |
512 isScrolling = false; | |
513 }, 300); // must be greater than the resize timeout below | |
514 }); | |
515 | |
516 var resizeTimeout; | |
517 $(window).on("resize", function() { | |
518 if (!isScrolling) { | |
519 window.clearTimeout(resizeTimeout); | |
520 resizeTimeout = window.setTimeout(function() { | |
521 $doc.trigger(events.resize); | |
522 }, 150); // must be less than the scrolling timeout above. | |
523 } | |
524 }); | |
525 | |
526 Tablesaw.Table = Table; | |
527 })(); | |
528 | |
529 (function() { | |
530 var classes = { | |
531 stackTable: "tablesaw-stack", | |
532 cellLabels: "tablesaw-cell-label", | |
533 cellContentLabels: "tablesaw-cell-content" | |
534 }; | |
535 | |
536 var data = { | |
537 key: "tablesaw-stack" | |
538 }; | |
539 | |
540 var attrs = { | |
541 labelless: "data-tablesaw-no-labels", | |
542 hideempty: "data-tablesaw-hide-empty" | |
543 }; | |
544 | |
545 var Stack = function(element, tablesaw) { | |
546 this.tablesaw = tablesaw; | |
547 this.$table = $(element); | |
548 | |
549 this.labelless = this.$table.is("[" + attrs.labelless + "]"); | |
550 this.hideempty = this.$table.is("[" + attrs.hideempty + "]"); | |
551 | |
552 this.$table.data(data.key, this); | |
553 }; | |
554 | |
555 Stack.prototype.init = function() { | |
556 this.$table.addClass(classes.stackTable); | |
557 | |
558 if (this.labelless) { | |
559 return; | |
560 } | |
561 | |
562 var self = this; | |
563 | |
564 this.$table | |
565 .find("th, td") | |
566 .filter(function() { | |
567 return !$(this).closest("thead").length; | |
568 }) | |
569 .filter(function() { | |
570 return ( | |
571 !$(this) | |
572 .closest("tr") | |
573 .is("[" + attrs.labelless + "]") && | |
574 (!self.hideempty || !!$(this).html()) | |
575 ); | |
576 }) | |
577 .each(function() { | |
578 var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels); | |
579 var $cell = $(this); | |
580 | |
581 $(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) { | |
582 var $header = $(this.cloneNode(true)); | |
583 // TODO decouple from sortable better | |
584 // Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b | |
585 // to preserve structural html in headers, like <a> | |
586 var $sortableButton = $header.find(".tablesaw-sortable-btn"); | |
587 $header.find(".tablesaw-sortable-arrow").remove(); | |
588 | |
589 // TODO decouple from checkall better | |
590 var $checkall = $header.find("[data-tablesaw-checkall]"); | |
591 $checkall.closest("label").remove(); | |
592 if ($checkall.length) { | |
593 $newHeader = $([]); | |
594 return; | |
595 } | |
596 | |
597 if (index > 0) { | |
598 $newHeader.append(document.createTextNode(", ")); | |
599 } | |
600 $newHeader.append( | |
601 $sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes | |
602 ); | |
603 }); | |
604 | |
605 if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) { | |
606 $cell.wrapInner("<span class='" + classes.cellContentLabels + "'></span>"); | |
607 } | |
608 | |
609 // Update if already exists. | |
610 var $label = $cell.find("." + classes.cellLabels); | |
611 if (!$label.length) { | |
612 $cell.prepend($newHeader); | |
613 } else { | |
614 // only if changed | |
615 $label.replaceWith($newHeader); | |
616 } | |
617 }); | |
618 }; | |
619 | |
620 Stack.prototype.destroy = function() { | |
621 this.$table.removeClass(classes.stackTable); | |
622 this.$table.find("." + classes.cellLabels).remove(); | |
623 this.$table.find("." + classes.cellContentLabels).each(function() { | |
624 $(this).replaceWith(this.childNodes); | |
625 }); | |
626 }; | |
627 | |
628 // on tablecreate, init | |
629 $(document) | |
630 .on(Tablesaw.events.create, function(e, tablesaw) { | |
631 if (tablesaw.mode === "stack") { | |
632 var table = new Stack(tablesaw.table, tablesaw); | |
633 table.init(); | |
634 } | |
635 }) | |
636 .on(Tablesaw.events.refresh, function(e, tablesaw) { | |
637 if (tablesaw.mode === "stack") { | |
638 $(tablesaw.table) | |
639 .data(data.key) | |
640 .init(); | |
641 } | |
642 }) | |
643 .on(Tablesaw.events.destroy, function(e, tablesaw) { | |
644 if (tablesaw.mode === "stack") { | |
645 $(tablesaw.table) | |
646 .data(data.key) | |
647 .destroy(); | |
648 } | |
649 }); | |
650 | |
651 Tablesaw.Stack = Stack; | |
652 })(); | |
653 | |
654 return Tablesaw; | |
655 })); |