wide/static/js/tabs.js

198 lines
5.6 KiB
JavaScript
Raw Normal View History

2015-01-01 05:06:33 +03:00
/*
2018-01-01 07:35:13 +03:00
* Copyright (c) 2014-2018, b3log.org & hacpai.com
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* http://www.apache.org/licenses/LICENSE-2.0
2015-01-01 05:06:33 +03:00
*
2014-11-12 18:13:14 +03:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2014-11-12 18:13:14 +03:00
2015-12-08 18:48:16 +03:00
/*
* @file tabs.js
*
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @version 1.0.0.1, Dec 8, 2015
*/
2014-10-20 13:32:06 +04:00
var Tabs = function (obj) {
2014-10-12 08:15:51 +04:00
obj._$tabsPanel = $(obj.id + " > .tabs-panel");
obj._$tabs = $(obj.id + " > .tabs");
2014-10-21 18:48:33 +04:00
obj._stack = [];
2014-08-29 13:24:08 +04:00
this.obj = obj;
this.obj.STACKSIZE = 64;
2014-08-29 13:24:08 +04:00
this._init(obj);
2014-12-23 12:25:07 +03:00
// DOM 元素存在时,应顺序入栈
var _it = this;
$(obj.id + " > .tabs > div").each(function () {
var id = $(this).data("index");
if (obj._stack.length === _it.obj.STACKSIZE) {
obj._stack.splice(0, 1);
}
if (obj._stack[obj._stack.length - 1] !== id) {
_it.obj._stack.push(id);
}
});
2014-08-27 14:10:12 +04:00
};
2014-08-29 13:24:08 +04:00
$.extend(Tabs.prototype, {
2014-10-20 13:32:06 +04:00
_init: function (obj) {
2014-08-29 13:24:08 +04:00
var _that = this;
2014-10-20 13:32:06 +04:00
obj._$tabs.on("click", "div", function (event) {
2015-01-04 10:31:03 +03:00
if ($(this).hasClass('current')) {
return false;
}
2014-08-29 13:24:08 +04:00
var id = $(this).data("index");
_that.setCurrent(id);
2014-09-19 13:58:29 +04:00
if (typeof (obj.clickAfter) === "function") {
obj.clickAfter(id);
}
2014-08-29 13:24:08 +04:00
});
2014-10-20 13:32:06 +04:00
obj._$tabs.on("click", ".ico-close", function (event) {
2014-11-01 07:05:52 +03:00
var id = $(this).parent().data("index"),
isRemove = true;
if (typeof obj.removeBefore === 'function') {
isRemove = obj.removeBefore(id);
}
if (isRemove) {
_that.del(id);
}
2014-08-29 13:24:08 +04:00
event.stopPropagation();
});
},
2014-10-20 13:32:06 +04:00
_hasId: function (id) {
var $tabs = this.obj._$tabs;
if ($tabs.find('div[data-index="' + id + '"]').length === 0) {
2014-10-20 13:32:06 +04:00
return false;
}
return true;
},
add: function (data) {
// 添加当前 tab
if (this.getCurrentId() === data.id) {
return false;
}
// 当前 tab 已经存在
if (this._hasId(data.id)) {
this.setCurrent(data.id);
return false;
}
2014-08-29 13:24:08 +04:00
var $tabsPanel = this.obj._$tabsPanel,
$tabs = this.obj._$tabs;
2014-10-24 05:41:05 +04:00
$tabs.append('<div data-index="' + data.id + '">'
2014-12-13 15:08:35 +03:00
+ data.title + ' <span class="ico-close font-ico"></span></div>');
2014-08-29 13:24:08 +04:00
$tabsPanel.append('<div data-index="' + data.id + '">' + data.content
+ '</div>');
2014-10-21 18:48:33 +04:00
this.setCurrent(data.id);
2014-10-20 13:32:06 +04:00
if (typeof data.after === 'function') {
data.after();
}
2014-08-29 13:24:08 +04:00
},
2014-10-20 13:32:06 +04:00
del: function (id) {
2014-08-29 13:24:08 +04:00
var $tabsPanel = this.obj._$tabsPanel,
$tabs = this.obj._$tabs,
stack = this.obj._stack,
prevId = null;
2014-11-01 07:05:52 +03:00
2014-08-29 13:24:08 +04:00
$tabs.children("div[data-index='" + id + "']").remove();
$tabsPanel.children("div[data-index='" + id + "']").remove();
// 移除堆栈中该 id
for (var i = 0; i < stack.length; i++) {
if (id === stack[i]) {
stack.splice(i, 1);
2015-01-03 11:01:24 +03:00
i--;
}
2014-08-29 13:24:08 +04:00
}
2014-11-01 07:05:52 +03:00
prevId = stack[stack.length - 1];
2014-08-29 13:24:08 +04:00
if (typeof this.obj.removeAfter === 'function') {
this.obj.removeAfter(id, prevId);
2014-08-29 13:24:08 +04:00
}
2014-09-19 13:58:29 +04:00
2014-09-12 13:50:46 +04:00
this.setCurrent(prevId);
2014-08-29 13:24:08 +04:00
},
2014-10-20 13:32:06 +04:00
getCurrentId: function () {
2014-08-29 13:24:08 +04:00
var $tabs = this.obj._$tabs;
return $tabs.children(".current").data("index");
},
setCurrent: function (path) {
if (!path) {
2014-08-29 13:24:08 +04:00
return false;
}
var $tabsPanel = this.obj._$tabsPanel,
$tabs = this.obj._$tabs;
var $currentTab = $tabs.children(".current");
if ($currentTab.data("index") === path) {
2014-08-29 13:24:08 +04:00
return false;
}
// tab 顺序入栈,如栈满则清除
var stack = this.obj._stack;
if (stack.length === this.obj.STACKSIZE) {
stack.splice(0, 1);
}
if (stack[stack.length - 1] !== path) {
this.obj._stack.push(path);
2014-10-21 18:48:33 +04:00
}
2014-08-29 13:24:08 +04:00
$tabs.children("div").removeClass("current");
$tabsPanel.children("div").hide();
$tabs.children('div[data-index="' + path + '"]').addClass("current");
$tabsPanel.children('div[data-index="' + path + '"]').show();
if (typeof this.obj.setAfter === 'function') {
this.obj.setAfter();
}
var id = this.getCurrentId();
if ("startPage" === id) {
return;
}
// set tree node selected
var tId = tree.getTIdByPath(id);
var node = tree.fileTree.getNodeByTId(tId);
tree.fileTree.selectNode(node);
wide.curNode = node;
for (var i = 0, ii = editors.data.length; i < ii; i++) {
if (editors.data[i].id === id) {
wide.curEditor = editors.data[i].editor;
break;
}
}
if (wide.curEditor) {
var cursor = wide.curEditor.getCursor();
wide.curEditor.setCursor(cursor);
wide.curEditor.focus();
wide.refreshOutline();
$(".footer .cursor").text('| ' + (cursor.line + 1) + ':' + (cursor.ch + 1) + ' |');
}
2014-08-29 13:24:08 +04:00
}
2014-08-27 14:10:12 +04:00
});