layui.define(['jquery', 'element'], function(exports) {
"use strict";
var MOD_NAME = 'tab',
$ = layui.jquery,
element = layui.element;
var pearTab = function(opt) {
this.option = opt;
};
var tabData = new Array();
var tabDataCurrent = 0;
var contextTabDOM;
pearTab.prototype.render = function(opt) {
var option = {
elem: opt.elem,
data: opt.data,
tool: opt.tool,
roll: opt.roll,
index: opt.index,
width: opt.width,
height: opt.height,
tabMax: opt.tabMax,
session: opt.session ? opt.session : false,
preload: opt.preload ? opt.preload : false,
closeEvent: opt.closeEvent,
success: opt.success ? opt.success : function(id) {}
}
if (option.session) {
if (sessionStorage.getItem(option.elem + "-pear-tab-data") != null) {
tabData = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-data"));
option.data = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-data"));
tabDataCurrent = sessionStorage.getItem(option.elem + "-pear-tab-data-current");
tabData.forEach(function(item, index) {
if (item.id == tabDataCurrent) {
option.index = index;
}
})
} else {
tabData = opt.data;
}
}
var lastIndex;
var tab = createTab(option);
$("#" + option.elem).html(tab);
$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-prev").click(function() {
rollPage("left", option);
})
$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-next").click(function() {
rollPage("right", option);
})
element.init();
toolEvent(option);
$("#" + option.elem).width(opt.width);
$("#" + option.elem).height(opt.height);
$("#" + option.elem).css({
position: "relative"
});
closeEvent(option);
option.success(sessionStorage.getItem(option.elem + "-pear-tab-data-current"));
$("body .layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title").on("contextmenu", "li",
function(e) {
// 获取当前元素位置
var top = e.clientY;
var left = e.clientX;
var menuWidth = 100;
var currentId = $(this).attr("lay-id");
var menu = "
";
contextTabDOM = $(this);
var isOutsideBounds = (left + menuWidth) > $(window).width();
if (isOutsideBounds) {
left = $(window).width() - menuWidth;
}
// 初始化
layer.open({
type: 1,
title: false,
shade: false,
skin: 'pear-tab-menu',
closeBtn: false,
area: [menuWidth + 'px', '108px'],
fixed: true,
anim: false,
isOutAnim: false,
offset: [top, left],
content: menu, //iframe的url,
success: function(layero, index) {
layer.close(lastIndex);
lastIndex = index;
menuEvent(option, index);
var timer;
$(layero).on('mouseout', function() {
timer = setTimeout(function() {
layer.close(index);
}, 30)
});
$(layero).on('mouseover', function() {
clearTimeout(timer);
});
// 清除 item 右击
$(layero).on('contextmenu', function() {
return false;
})
}
});
return false;
})
mousewheelAndTouchmoveHandler(option)
return new pearTab(option);
}
pearTab.prototype.click = function(callback) {
var elem = this.option.elem;
var option = this.option;
element.on('tab(' + this.option.elem + ')', function(data) {
var id = $("#" + elem + " .layui-tab-title .layui-this").attr("lay-id");
sessionStorage.setItem(option.elem + "-pear-tab-data-current", id);
if (!option.preload) {
var $iframe = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-content").find(
"iframe[id='" + id + "']");
var iframeUrl = $iframe.attr("src");
if (!iframeUrl || iframeUrl === "about:blank") {
// 获取 url 并重载
tabData.forEach(function(item, index) {
if (item.id === id) {
iframeUrl = item.url;
}
})
tabIframeLoading(elem);
$iframe.attr("src", iframeUrl);
}
}
callback(id);
});
}
pearTab.prototype.positionTab = function() {
var $tabTitle = $('.layui-tab[lay-filter=' + this.option.elem + '] .layui-tab-title');
var autoLeft = 0;
$tabTitle.children("li").each(function() {
if ($(this).hasClass('layui-this')) {
return false;
} else {
autoLeft += $(this).outerWidth();
}
});
$tabTitle.animate({
scrollLeft: autoLeft - $tabTitle.width() / 3
}, 200);
}
pearTab.prototype.clear = function() {
sessionStorage.removeItem(this.option.elem + "-pear-tab-data");
sessionStorage.removeItem(this.option.elem + "-pear-tab-data-current");
}
pearTab.prototype.addTab = function(opt) {
var title = '';
if (opt.close) {
title += '' + opt.title +
'ဆ';
} else {
title += '' + opt.title +
'ဆ';
}
element.tabAdd(this.option.elem, {
title: title,
content: '