jellyfin-web/dashboard-ui/thirdparty/jquerymobile-1.4.5/jqm.table.js

191 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-02-14 11:32:29 -07:00
define(['jqmwidget'], function () {
2015-12-14 08:43:03 -07:00
var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/;
$.extend($.mobile, {
// Namespace used framework-wide for data-attrs. Default is no namespace
// Retrieve an attribute from an element and perform some massaging of the value
getAttribute: function (element, key) {
var data;
element = element.jquery ? element[0] : element;
if (element && element.getAttribute) {
data = element.getAttribute("data-" + key);
}
// Copied from core's src/data.js:dataAttr()
// Convert from a string to a proper data type
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test(data) ? JSON.parse(data) :
data;
} catch (err) { }
return data;
}
});
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
$.widget("mobile.table", {
options: {
enhanced: false
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_create: function () {
if (!this.options.enhanced) {
this.element.addClass("ui-table");
}
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// extend here, assign on refresh > _setHeaders
$.extend(this, {
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// Expose headers and allHeaders properties on the widget
// headers references the THs within the first TR in the table
headers: undefined,
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// allHeaders references headers, plus all THs in the thead, which may
// include several rows, or not
allHeaders: undefined
});
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
this._refresh(true);
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_setHeaders: function () {
var trs = this.element.find("thead tr");
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
this.headers = this.element.find("tr:eq(0)").children();
this.allHeaders = this.headers.add(trs.children());
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
refresh: function () {
this._refresh();
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
rebuild: $.noop,
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_refresh: function ( /* create */) {
var table = this.element,
2015-08-31 16:47:47 -07:00
trs = table.find("thead tr");
2016-02-14 11:32:29 -07:00
// updating headers on refresh (fixes #5880)
this._setHeaders();
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// Iterate over the trs
trs.each(function () {
var columnCount = 0;
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// Iterate over the children of the tr
$(this).children().each(function () {
var span = parseInt(this.getAttribute("colspan"), 10),
2015-08-31 16:47:47 -07:00
selector = ":nth-child(" + (columnCount + 1) + ")",
j;
2016-02-14 11:32:29 -07:00
this.setAttribute("data-colstart", columnCount + 1);
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
if (span) {
for (j = 0; j < span - 1; j++) {
columnCount++;
selector += ", :nth-child(" + (columnCount + 1) + ")";
}
}
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// Store "cells" data on header as a reference to all cells in the
// same column as this TH
$(this).data("cells", table.find("tr").not(trs.eq(0)).not(this).children(selector));
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
columnCount++;
});
});
}
});
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
$.widget("mobile.table", $.mobile.table, {
options: {
mode: "reflow"
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_create: function () {
this._super();
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
// If it's not reflow mode, return here.
if (this.options.mode !== "reflow") {
return;
}
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
if (!this.options.enhanced) {
this.element.addClass("ui-table-reflow");
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
this._updateReflow();
}
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
rebuild: function () {
this._super();
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
if (this.options.mode === "reflow") {
this._refresh(false);
}
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_refresh: function (create) {
this._super(create);
if (!create && this.options.mode === "reflow") {
this._updateReflow();
}
},
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
_updateReflow: function () {
var table = this,
2015-08-31 16:47:47 -07:00
opts = this.options;
2016-02-14 11:32:29 -07:00
// get headers in reverse order so that top-level headers are appended last
$(table.allHeaders.get().reverse()).each(function () {
var cells = $(this).data("cells"),
2015-08-31 16:47:47 -07:00
colstart = $.mobile.getAttribute(this, "colstart"),
hierarchyClass = cells.not(this).filter("thead th").length && " ui-table-cell-label-top",
contents = $(this).clone().contents(),
iteration, filter;
2016-02-14 11:32:29 -07:00
if (contents.length > 0) {
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
if (hierarchyClass) {
iteration = parseInt(this.getAttribute("colspan"), 10);
filter = "";
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
if (iteration) {
filter = "td:nth-child(" + iteration + "n + " + (colstart) + ")";
}
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
table._addLabels(cells.filter(filter),
2015-12-14 08:43:03 -07:00
"ui-table-cell-label" + hierarchyClass, contents);
2016-02-14 11:32:29 -07:00
} else {
table._addLabels(cells, "ui-table-cell-label", contents);
}
}
});
},
_addLabels: function (cells, label, contents) {
if (contents.length === 1 && contents[0].nodeName.toLowerCase() === "abbr") {
contents = contents.eq(0).attr("title");
}
// .not fixes #6006
cells
2015-08-31 16:47:47 -07:00
.not(":has(b." + label + ")")
.prepend($("<b class='" + label + "'></b>").append(contents));
2016-02-14 11:32:29 -07:00
}
});
2015-08-31 16:47:47 -07:00
2016-02-14 11:32:29 -07:00
});