TeaWeb/js/utils/tab.ts

95 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-02-27 16:20:49 +00:00
interface JQuery {
asTabWidget() : JQuery;
tabify() : this;
changeElementType(type: string) : JQuery;
}
2018-04-16 18:38:35 +00:00
if(typeof (customElements) !== "undefined") {
class X_Tab extends HTMLElement {}
class X_Entry extends HTMLElement {}
class X_Tag extends HTMLElement {}
class X_Content extends HTMLElement {}
customElements.define('x-tab', X_Tab, { extends: 'div' });
customElements.define('x-entry', X_Entry, { extends: 'div' });
customElements.define('x-tag', X_Tag, { extends: 'div' });
customElements.define('x-content', X_Content, { extends: 'div' });
} else {
console.warn("Could not defied tab customElements!");
}
2018-02-27 16:20:49 +00:00
var TabFunctions = {
tabify(template: JQuery) : JQuery {
console.log("Tabify:");
console.log(template);
let tag = $.spawn("div");
tag.addClass("tab");
let header = $.spawn("div");
header.addClass("tab-header");
let content = $.spawn("div");
content.addClass("tab-content");
2018-03-07 18:06:52 +00:00
let silentContent = $.spawn("div");
silentContent.addClass("tab-content-invisible");
2018-02-27 16:20:49 +00:00
template.find("x-entry").each(function () {
let hentry = $.spawn("div");
hentry.addClass("entry");
2018-03-07 18:06:52 +00:00
hentry.append($(this).find("x-tag").clone(true, true));
2018-02-27 16:20:49 +00:00
const _this = $(this);
2018-03-07 18:06:52 +00:00
const _entryContent = _this.find("x-content").clone(true, true);
silentContent.append(_entryContent);
2018-02-27 16:20:49 +00:00
hentry.on("click", function () {
2018-03-07 18:06:52 +00:00
if(hentry.hasClass("selected")) return;
2018-02-27 16:20:49 +00:00
tag.find(".tab-header .selected").removeClass("selected");
hentry.addClass("selected");
2018-03-07 18:06:52 +00:00
content.children().appendTo(silentContent);
console.log(silentContent);
2018-02-27 16:20:49 +00:00
content.empty();
2018-03-07 18:06:52 +00:00
content.append(_entryContent);
//console.log(_this.find("x-content"));
//content.append(_this.find("x-content"));
2018-02-27 16:20:49 +00:00
});
console.log(this);
header.append(hentry);
});
header.find(".entry").first().trigger("click");
tag.append(header);
tag.append(content);
2018-03-07 18:06:52 +00:00
tag.append(silentContent);
2018-02-27 16:20:49 +00:00
return tag;
}
}
if(!$.fn.asTabWidget) {
$.fn.asTabWidget = function () : JQuery {
if($(this).prop("tagName") == "X-TAB")
return TabFunctions.tabify($(this));
else {
throw "Invalid tag! " + $(this).prop("tagName");
}
}
}
if(!$.fn.tabify) {
$.fn.tabify = function () {
try {
let self = this.asTabWidget();
this.replaceWith(self);
} catch(object) {}
this.find("x-tab").each(function () {
$(this).replaceWith($(this).asTabWidget());
});
return this;
}
}