2022-11-15 15:05:32 +08:00
|
|
|
|
/**
|
|
|
|
|
* YourTeam 上方 Tab 頁籤
|
|
|
|
|
*
|
|
|
|
|
* 使用方式:
|
|
|
|
|
* <button type="button" data-tabname="testTab" data-target="testDiv1">頁籤按鈕1</button>
|
|
|
|
|
* <button type="button" data-tabname="testTab" data-target="testDiv2">頁籤按鈕2</button>
|
|
|
|
|
*
|
|
|
|
|
* <div id="testDiv1" data-tabname="testTab" data-tabrole="child"></div>
|
|
|
|
|
* <div id="testDiv2" data-tabname="testTab" data-tabrole="child"></div>
|
|
|
|
|
* */
|
|
|
|
|
var YT = YT || {};
|
2022-11-15 12:59:27 +08:00
|
|
|
|
|
2022-11-16 17:23:00 +08:00
|
|
|
|
var _ytTabInited = [];
|
2022-11-16 14:53:22 +08:00
|
|
|
|
|
2022-11-16 17:23:00 +08:00
|
|
|
|
$(function () {
|
2022-11-15 18:47:16 +08:00
|
|
|
|
})
|
|
|
|
|
|
2022-11-16 17:23:00 +08:00
|
|
|
|
/**
|
|
|
|
|
* 初始全頁面 yt tab
|
|
|
|
|
* */
|
2022-11-15 18:47:16 +08:00
|
|
|
|
function initTabsByEle() {
|
2022-12-01 19:32:32 +08:00
|
|
|
|
/*_ytTabInited = [];*/
|
2022-12-09 18:20:41 +08:00
|
|
|
|
$("[data-tabname]:not([data-tabrole=child])").each(function (index, value) {
|
2022-11-15 12:59:27 +08:00
|
|
|
|
let tabName = $(value).data("tabname");
|
2022-12-01 19:32:32 +08:00
|
|
|
|
if (_ytTabInited.indexOf(tabName) == -1) {
|
|
|
|
|
var ytTab = new YT.Tab({ tabName: tabName })
|
|
|
|
|
_ytTabInited.push(tabName);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 12:59:27 +08:00
|
|
|
|
})
|
2022-11-15 18:47:16 +08:00
|
|
|
|
}
|
2022-11-15 12:59:27 +08:00
|
|
|
|
|
2022-11-15 15:05:32 +08:00
|
|
|
|
class YourTeamTab {
|
2022-11-15 12:59:27 +08:00
|
|
|
|
constructor(option = {}) {
|
|
|
|
|
this.tabName = option.tabName ?? null;
|
2023-01-09 19:15:27 +08:00
|
|
|
|
this.activeEle = null;
|
|
|
|
|
this.eventArg = { cancel: false };
|
2022-11-15 15:05:32 +08:00
|
|
|
|
this.init()
|
2023-01-09 19:15:27 +08:00
|
|
|
|
|
2022-11-15 12:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-16 14:53:22 +08:00
|
|
|
|
init = function () {
|
|
|
|
|
if (_ytTabInited.indexOf(this.tabName) != -1) {
|
|
|
|
|
_ytTabInited.splice($.inArray(this.tabName, _ytTabInited), 1);
|
|
|
|
|
$(`[data-tabname=${this.tabName}][data-target]`).off("click").unbind("click");
|
|
|
|
|
}
|
2022-11-15 12:59:27 +08:00
|
|
|
|
this.event();
|
2022-11-15 18:47:16 +08:00
|
|
|
|
$(`[data-tabname=${this.tabName}][data-tabrole=child]`).css("display", "none");
|
2022-12-08 17:27:32 +08:00
|
|
|
|
if ($(`[data-tabname=${this.tabName}][data-target].active`).length == 0) {
|
2023-01-09 19:15:27 +08:00
|
|
|
|
let parTabEle = $(`[data-tabname=${this.tabName}][data-target]`).first();
|
|
|
|
|
if (parTabEle.data("toggle") == "tab" || !parTabEle.data("toggle")) {
|
|
|
|
|
$(`[data-tabname=${this.tabName}][data-target]`).first().trigger("click");
|
|
|
|
|
}
|
2022-12-08 17:27:32 +08:00
|
|
|
|
} else {
|
|
|
|
|
$(`[data-tabname=${this.tabName}][data-target].active`).first().trigger("click");
|
|
|
|
|
}
|
2023-01-09 19:15:27 +08:00
|
|
|
|
|
2022-11-16 14:53:22 +08:00
|
|
|
|
_ytTabInited.push(this.tabName);
|
2023-01-09 19:15:27 +08:00
|
|
|
|
this.setThisObj();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setThisObj = function () {
|
|
|
|
|
$(`[data-tabname=${this.tabName}]`).each((idx, ele) => {
|
|
|
|
|
$(ele)[0]._ytTab = this;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setActItem = function (element) {
|
|
|
|
|
|
|
|
|
|
this.activeEle = element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updActClass = function () {
|
|
|
|
|
$(`[data-tabname=${this.tabName}]:not([data-tabrole=child])`).removeClass("active");
|
|
|
|
|
$(this.activeEle).addClass("active");
|
2022-11-15 12:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 15:05:32 +08:00
|
|
|
|
event = function () {
|
|
|
|
|
let clsObj = this;
|
2022-11-16 17:23:00 +08:00
|
|
|
|
|
|
|
|
|
// Tab Item 按鈕 click
|
2023-01-09 19:15:27 +08:00
|
|
|
|
$(`[data-tabname=${this.tabName}]`).on("click", function (e) {
|
2022-11-15 12:59:27 +08:00
|
|
|
|
let target = $(this).data("target");
|
|
|
|
|
let obj = this;
|
2023-01-09 19:15:27 +08:00
|
|
|
|
clsObj.eventArg.cancel = false;
|
|
|
|
|
$(obj).trigger("yt:tab:prechange", clsObj.eventArg)
|
|
|
|
|
|
|
|
|
|
if (clsObj.eventArg.cancel) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clsObj.activeEle = obj;
|
|
|
|
|
clsObj.updActClass();
|
2022-11-16 17:23:00 +08:00
|
|
|
|
// 觸發 yt:tab:change事件
|
|
|
|
|
$(obj).trigger("yt:tab:change");
|
2022-12-09 18:20:41 +08:00
|
|
|
|
if (target) {
|
|
|
|
|
let tabName = $(target).data("tabname");
|
|
|
|
|
if (tabName) {
|
|
|
|
|
// 找出該觸發對象 block
|
|
|
|
|
if ($(target).data("tabrole") == "child") {
|
|
|
|
|
$(obj).trigger("yt:tab:show");
|
|
|
|
|
$(`[data-tabname='${tabName}'][data-tabrole='child']`).css("opacity", 0).hide();
|
|
|
|
|
$(target).show().animate({ opacity: 1 }, 200, function () {
|
|
|
|
|
$(obj).trigger("yt:tab:shown");
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-11-15 12:59:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-15 15:05:32 +08:00
|
|
|
|
|
2023-01-09 19:15:27 +08:00
|
|
|
|
$.fn.YTTab = function (method,...arg) {
|
|
|
|
|
let tabObj = $(this)[0]._ytTab;
|
|
|
|
|
|
|
|
|
|
if (!tabObj) {
|
|
|
|
|
let tabName = $(this).data("tabname");
|
|
|
|
|
if (tabName && _ytTabInited.indexOf(tabName) == -1) {
|
|
|
|
|
let ytTab = new YT.Tab({ tabName: tabName })
|
|
|
|
|
_ytTabInited.push(tabName);
|
|
|
|
|
tabObj = ytTab;
|
|
|
|
|
} else {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (method == "set") {
|
|
|
|
|
tabObj.setActItem(this);
|
|
|
|
|
tabObj.updActClass();
|
|
|
|
|
} else if (method == "setAndClick") {
|
|
|
|
|
tabObj.setActItem(this);
|
|
|
|
|
$(this).click();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tabObj;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 15:05:32 +08:00
|
|
|
|
YT.Tab = YT.Tab || YourTeamTab;
|