diff default/node_modules/tablesaw/src/tables.stack.js @ 0:1d038bc9b3d2 default tip

Up:default
author Liny <dev@neowd.com>
date Sat, 31 May 2025 09:21:51 +0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/default/node_modules/tablesaw/src/tables.stack.js	Sat May 31 09:21:51 2025 +0800
@@ -0,0 +1,131 @@
+/*
+* tablesaw: A set of plugins for responsive tables
+* Stack: switches from column layout to rows with inline labels
+* Copyright (c) 2013 Filament Group, Inc.
+* MIT License
+*/
+
+(function() {
+	var classes = {
+		stackTable: "tablesaw-stack",
+		cellLabels: "tablesaw-cell-label",
+		cellContentLabels: "tablesaw-cell-content"
+	};
+
+	var data = {
+		key: "tablesaw-stack"
+	};
+
+	var attrs = {
+		labelless: "data-tablesaw-no-labels",
+		hideempty: "data-tablesaw-hide-empty"
+	};
+
+	var Stack = function(element, tablesaw) {
+		this.tablesaw = tablesaw;
+		this.$table = $(element);
+
+		this.labelless = this.$table.is("[" + attrs.labelless + "]");
+		this.hideempty = this.$table.is("[" + attrs.hideempty + "]");
+
+		this.$table.data(data.key, this);
+	};
+
+	Stack.prototype.init = function() {
+		this.$table.addClass(classes.stackTable);
+
+		if (this.labelless) {
+			return;
+		}
+
+		var self = this;
+
+		this.$table
+			.find("th, td")
+			.filter(function() {
+				return !$(this).closest("thead").length;
+			})
+			.filter(function() {
+				return (
+					!$(this)
+						.closest("tr")
+						.is("[" + attrs.labelless + "]") &&
+					(!self.hideempty || !!$(this).html())
+				);
+			})
+			.each(function() {
+				var $newHeader = $(document.createElement("b")).addClass(classes.cellLabels);
+				var $cell = $(this);
+
+				$(self.tablesaw._findPrimaryHeadersForCell(this)).each(function(index) {
+					var $header = $(this.cloneNode(true));
+					// TODO decouple from sortable better
+					// Changed from .text() in https://github.com/filamentgroup/tablesaw/commit/b9c12a8f893ec192830ec3ba2d75f062642f935b
+					// to preserve structural html in headers, like <a>
+					var $sortableButton = $header.find(".tablesaw-sortable-btn");
+					$header.find(".tablesaw-sortable-arrow").remove();
+
+					// TODO decouple from checkall better
+					var $checkall = $header.find("[data-tablesaw-checkall]");
+					$checkall.closest("label").remove();
+					if ($checkall.length) {
+						$newHeader = $([]);
+						return;
+					}
+
+					if (index > 0) {
+						$newHeader.append(document.createTextNode(", "));
+					}
+					$newHeader.append(
+						$sortableButton.length ? $sortableButton[0].childNodes : $header[0].childNodes
+					);
+				});
+
+				if ($newHeader.length && !$cell.find("." + classes.cellContentLabels).length) {
+					$cell.wrapInner("<span class='" + classes.cellContentLabels + "'></span>");
+				}
+
+				// Update if already exists.
+				var $label = $cell.find("." + classes.cellLabels);
+				if (!$label.length) {
+					$cell.prepend($newHeader);
+				} else {
+					// only if changed
+					$label.replaceWith($newHeader);
+				}
+			});
+	};
+
+	Stack.prototype.destroy = function() {
+		this.$table.removeClass(classes.stackTable);
+		this.$table.find("." + classes.cellLabels).remove();
+		this.$table.find("." + classes.cellContentLabels).each(function() {
+			$(this).replaceWith(this.childNodes);
+		});
+	};
+
+	// on tablecreate, init
+	$(document)
+		.on(Tablesaw.events.create, function(e, tablesaw) {
+			if (tablesaw.mode === "stack") {
+				var table = new Stack(tablesaw.table, tablesaw);
+				table.init();
+			}
+		})
+		.on(Tablesaw.events.refresh, function(e, tablesaw) {
+			if (tablesaw.mode === "stack") {
+				$(tablesaw.table)
+					.data(data.key)
+					.init();
+			}
+		})
+		.on(Tablesaw.events.destroy, function(e, tablesaw) {
+			if (tablesaw.mode === "stack") {
+				$(tablesaw.table)
+					.data(data.key)
+					.destroy();
+			}
+		});
+
+	Tablesaw.Stack = Stack;
+})();