| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- /** iwb_admin-v1.1.0 MIT License By http://www.iwbnet.com e-mail:yueyy@iwbnet.com */
- /**
- * Name:tab.js
- * Author:Van
- * E-mail:yueyy@iwbnet.com
- * Website:http://www.iwbnet.com
- * LICENSE:MIT
- */
- layui.define(['jquery', 'element', 'nprogress', 'utils'], function (exports) {
- var $ = layui.jquery,
- //modName = 'tab',
- element = layui.element,
- utils = layui.utils,
- doc = $(document),
- win = $(window),
- renderType = {
- page: 'page',
- iframe: 'iframe'
- };
- //私用对象
- var tab = {
- _config: {},
- _filter: 'iwbTab', //过滤器名
- _title: undefined,
- _content: undefined,
- _parentElem: undefined, //要存放的容器
- //检查选项卡DOM是否存在
- tabDomExists: function () {
- var that = this;
- if (doc.find('div.iwb-tab').length > 0) {
- that._title = $('.iwb-tab ul.layui-tab-title');
- that._content = $('.iwb-tab div.layui-tab-content');
- return true;
- }
- return false;
- },
- /**
- * 创建选项卡DOM
- */
- createTabDom: function () {
- var that = this,
- config = that._config;
- that._parentElem = config.elem;
- if (that.tabDomExists())
- return;
- //模板
- var tpl = [
- '<div class="layui-tab layui-tab-card iwb-tab" lay-filter="' + that._filter + '">',
- '<ul class="layui-tab-title">',
- '<li class="layui-this" lay-id="-1" data-url="' +
- config.mainUrl +
- '"><i class="layui-icon layui-icon-home"></i></li>',
- '</ul>',
- '<i class="fa fa-chevron-down iwb-tab-tool"></i>',
- '<div class="iwb-tab-tool-body layui-anim layui-anim-upbit" id="kuaijie">',
- '<ul id="menudiv">',
- // '<li class="iwb-item"><a class="iwb-item" data-target="refresh">刷新当前标签页</a></li>',
- // '<li class="iwb-item"><a class="iwb-item" data-target="closeCurrent">关闭当前标签页</a></li>',
- '<li class="iwb-item"><a class="iwb-item" data-target="closeOther">关闭其他标签页</a></li>',
- '<li class="iwb-item"><a class="iwb-item" data-target="closeAll">关闭全部标签页</a></li>',
- '</ul>',
- '</div>',
- '<div class="layui-tab-content">',
- '<div class="layui-tab-item layui-show" lay-item-id="-1">{{content}}</div>',
- '</div>',
- '</div>'
- ];
- var html = tpl.join('');
- switch (config.renderType) {
- case renderType.page:
- html = html.replace('{{content}}', that.getBodyContent(config.mainUrl + '?v=' + new Date().getTime()));
- break;
- case renderType.iframe:
- html = html.replace('{{content}}', '<iframe src="' + config.mainUrl + '"></iframe>');
- break;
- }
- //渲染
- $(config.elem).html(html);
- that._title = $('.iwb-tab ul.layui-tab-title');
- that._content = $('.iwb-tab div.layui-tab-content');
- var tool = $('.iwb-tab-tool'),
- toolBody = $('.iwb-tab-tool-body'),
- tooMenu = $('#kuaijie');
- //监听操作点击事件
- tool.on('click', function () {
- tooMenu.toggle();
- });
- //tooMenu.mouseout(function () {
- // tooMenu.hide();
- //});
- //监听操作项点击事件
- toolBody.find('a.iwb-item').each(function () {
- var $that = $(this);
- var target = $that.data('target');
- $that.off('click').on('click', function () {
- var layId = that._title.children('li[class=layui-this]').attr('lay-id');
- switch (target) {
- case 'refresh': //刷新
- switch (config.renderType) {
- case renderType.page:
- var loadIndex = that.load();
- var url = that._title.children('li[lay-id=' + layId + ']').data('url');
- that._content.children('div[lay-item-id=' + layId + ']')
- .html(that.getBodyContent(url + '?v=' + new Date().getTime(), function () {
- that.closeLoad(loadIndex);
- }));
- break;
- case renderType.iframe:
- var item = that._content.children('div[lay-item-id=' + layId + ']').children('iframe');
- item.attr('src', item.attr('src'));
- break;
- }
- break;
- case 'closeCurrent': //关闭当前
- if (layId !== "-1")
- that.tabDelete(layId);
- break;
- case 'closeOther': //关闭其他
- that._title.children('li[lay-id]').each(function () {
- var curId = $(this).attr('lay-id');
- if (curId !== layId && curId !== "-1")
- that.tabDelete(curId);
- });
- break;
- case 'closeAll': //关闭所有
- that._title.children('li[lay-id]').each(function () {
- var curId = $(this).attr('lay-id');
- if (curId !== "-1")
- that.tabDelete(curId);
- });
- that.tabChange(-1);
- break;
- }
- //_tool.click();
- tooMenu.hide();
- });
- });
- //监听浏览器窗口改变事件
- that.winResize();
- },
- load: function () {
- return layer.load(0, { shade: [0.3, '#333'] });
- },
- closeLoad: function (index) {
- setTimeout(function () {
- index && layer.close(index);
- }, 500);
- },
- getBodyContent: function (url, callback) {
- return utils.getBodyContent(utils.loadHtml(url, callback));
- },
- /**
- * 监听浏览器窗口改变事件
- */
- winResize: function () {
- var that = this,
- config = that._config;
- win.on('resize', function () {
- var currBoxHeight = $(that._parentElem).height(); //获取当前容器的高度
- switch (config.renderType) {
- case renderType.page:
- $('.iwb-tab .layui-tab-content').height(currBoxHeight - 43);
- break;
- case renderType.iframe:
- $('.iwb-tab .layui-tab-content iframe').height(currBoxHeight - 47);
- break;
- }
- }).resize();
- },
- /**
- * 检查选项卡是否存在
- */
- tabExists: function (layId) {
- var that = this;
- return that._title.find('li[lay-id=' + layId + ']').length > 0;
- },
- /**
- * 删除选项卡
- */
- tabDelete: function (layId) {
- element.tabDelete(this._filter, layId);
- },
- /**
- * 设置选中选项卡
- */
- tabChange: function (layId) {
- element.tabChange(this._filter, layId);
- },
- /**
- * 获取选项卡对象
- */
- getTab: function (layId) {
- return this._title.find('li[lay-id=' + layId + ']');
- },
- /**
- * 添加一个选项卡,已存在则获取焦点
- */
- tabAdd: function (options) {
- var that = this,
- config = that._config,
- loadIndex = undefined;
- options = options || {
- id: new Date().getTime(),
- title: '新标签页',
- icon: 'fa-file',
- url: '404.html'
- };
- var title = options.title,
- icon = options.icon,
- url = options.url,
- id = options.id;
- if (that.tabExists(id)) {
- that.tabChange(id);
- $('#refresh').click();
- return;
- }
- NProgress.start();
- if (config.openWait)
- loadIndex = that.load();
- var titleHtml = ['<li class="layui-this" lay-id="' + id + '" data-url="' + url + '">'];
- if (icon) {
- if (icon.indexOf('fa-') !== -1) {
- titleHtml.push('<i class="fa ' + icon + '" aria-hidden="true"></i>');
- } else {
- titleHtml.push('<i class="layui-icon">' + icon + '</i>');
- }
- }
- titleHtml.push(' ' + title);
- titleHtml.push('<i class="layui-icon layui-unselect layui-tab-close">ဆ</i>');
- titleHtml.push('</li>');
- var contentHtm = '<div class="layui-tab-item layui-show" lay-item-id="' + id + '">{{content}}</div>';
- switch (config.renderType) {
- case renderType.page:
- contentHtm = contentHtm.replace('{{content}}', that.getBodyContent(url + '?v=' + new Date().getTime(), function () {
- setTimeout(function () {
- NProgress.done();
- config.openWait && loadIndex && that.closeLoad(loadIndex);
- }, 500);
- }));
- break;
- case renderType.iframe:
- contentHtm = contentHtm.replace('{{content}}', '<iframe src="' + url + '"></iframe>');
- break;
- }
- //追加html
- that._title.append(titleHtml.join(''));
- that._content.append(contentHtm);
- if (config.renderType === renderType.iframe) {
- that._content.find('div[lay-item-id=' + id + ']').find('iframe').on('load', function () {
- NProgress.done();
- config.openWait && loadIndex && that.closeLoad(loadIndex);
- });
- }
- //监听选项卡关闭事件
- that.getTab(id).find('i.layui-tab-close').off('click').on('click', function () {
- //关闭之前
- if (config.closeBefore) {
- if (config.closeBefore(options)) {
- that.tabDelete(id);
- }
- } else {
- that.tabDelete(id);
- }
- });
- that.tabChange(id);
- that.winResize();
- if (config.onSwitch) {
- element.on('tab(' + that._filter + ')', function (data) {
- config.onSwitch({
- index: data.index,
- elem: data.elem,
- layId: that._title.children('li').eq(data.index).attr('lay-id')
- });
- });
- }
- },
- /**
- * 获取当前选项卡的id
- */
- getCurrLayId: function () {
- return this._title.find('li.layui-this').attr('lay-id');
- }
- };
- // ReSharper disable once InconsistentNaming
- var Tab = function () {
- this.config = {
- elem: undefined,
- mainUrl: "/Main/Home",
- renderType: 'iframe',
- openWait: false
- };
- this.v = '1.0.5';
- };
- Tab.fn = Tab.prototype;
- Tab.fn.set = function (options) {
- var that = this;
- $.extend(true, that.config, options);
- return that;
- };
- /**
- * 渲染选项卡
- */
- Tab.fn.render = function () {
- var that = this,
- config = that.config;
- if (config.elem === undefined) {
- layui.hint().error('Tab error:请配置选择卡容器.');
- return that;
- }
- tab._config = config;
- tab.createTabDom();
- return that;
- };
- /**
- * 添加一个选项卡
- */
- Tab.fn.tabAdd = function (params) {
- tab.tabAdd(params);
- };
- /**
- * 关闭一个选项卡
- */
- Tab.fn.close = function (layId) {
- tab.tabDelete(layId);
- };
- Tab.fn.getId = function () {
- return tab.getCurrLayId();
- };
-
- var t = new Tab();
- exports('tab', t);
- });
- //layui.define(['jquery', 'element', 'nprogress', 'utils'], function (exports) {
- // var $ = layui.jquery,
- // //modName = 'tab',
- // element = layui.element,
- // utils = layui.utils,
- // doc = $(document),
- // win = $(window),
- // renderType = {
- // page: 'page',
- // iframe: 'iframe'
- // };
- // var Tab = function () {
- // this.config = {
- // elem: undefined,
- // mainUrl: "/Home/index",
- // renderType: 'iframe',
- // openWait: false
- // };
- // this.v = '1.0.5';
- // };
- // Tab.fn = Tab.prototype;
- // Tab.fn.set = function (options) {
- // var that = this;
- // $.extend(true, that.config, options);
- // return that;
- // };
- // //私用对象
- // var tab = {
- // _config: {},
- // _filter: 'iwbTab', //过滤器名
- // _title: undefined,
- // _content: undefined,
- // _parentElem: undefined, //要存放的容器
- // //检查选项卡DOM是否存在
- // tabDomExists: function () {
- // var that = this;
- // if (doc.find('div.iwb-tab').length > 0) {
- // that._title = $('.iwb-tab ul.layui-tab-title');
- // that._content = $('.iwb-tab div.layui-tab-content');
- // return true;
- // }
- // return false;
- // },
- // /**
- // * 创建选项卡DOM
- // */
- // createTabDom: function () {
- // var that = this,
- // config = that._config;
- // that._parentElem = config.elem;
- // if (that.tabDomExists())
- // return;
- // //模板
- // var tpl = [
- // '<div class="layui-tab layui-tab-card iwb-tab" lay-filter="' + that._filter + '">',
- // '<ul class="layui-tab-title">',
- // '<li class="layui-this" lay-id="-1" data-url="' +
- // config.mainUrl +
- // '"><i class="layui-icon layui-icon-home"></i></li>',
- // '</ul>',
- // '<i class="fa fa-chevron-down iwb-tab-tool"></i>',
- // '<div class="iwb-tab-tool-body layui-anim layui-anim-upbit" id="kuaijie">',
- // '<ul id="menudiv">',
- // // '<li class="iwb-item"><a class="iwb-item" data-target="refresh">刷新当前标签页</a></li>',
- // // '<li class="iwb-item"><a class="iwb-item" data-target="closeCurrent">关闭当前标签页</a></li>',
- // '<li class="iwb-item"><a class="iwb-item" data-target="closeOther">关闭其他标签页</a></li>',
- // '<li class="iwb-item"><a class="iwb-item" data-target="closeAll">关闭全部标签页</a></li>',
- // '</ul>',
- // '</div>',
- // '<div class="layui-tab-content">',
- // '<div class="layui-tab-item layui-show" lay-item-id="-1">{{content}}</div>',
- // '</div>',
- // '</div>'
- // ];
- // var html = tpl.join('');
- // switch (config.renderType) {
- // case renderType.page:
- // html = html.replace('{{content}}', that.getBodyContent(config.mainUrl + '?v=' + new Date().getTime()));
- // break;
- // case renderType.iframe:
- // html = html.replace('{{content}}', '<iframe src="' + config.mainUrl + '"></iframe>');
- // break;
- // }
- // //渲染
- // $(config.elem).html(html);
- // that._title = $('.iwb-tab ul.layui-tab-title');
- // that._content = $('.iwb-tab div.layui-tab-content');
- // var tool = $('.iwb-tab-tool'),
- // toolBody = $('.iwb-tab-tool-body'),
- // tooMenu = $('#kuaijie');
- // //监听操作点击事件
- // tool.on('click', function () {
- // tooMenu.toggle();
- // });
- // tooMenu.mouseout(function() {
- // tooMenu.hide();
- // });
- // //监听操作项点击事件
- // toolBody.find('a.iwb-item').each(function () {
- // var $that = $(this);
- // var target = $that.data('target');
- // $that.off('click').on('click', function () {
- // var layId = that._title.children('li[class=layui-this]').attr('lay-id');
- // switch (target) {
- // case 'refresh': //刷新
- // switch (config.renderType) {
- // case renderType.page:
- // var loadIndex = that.load();
- // var url = that._title.children('li[lay-id=' + layId + ']').data('url');
- // that._content.children('div[lay-item-id=' + layId + ']')
- // .html(that.getBodyContent(url + '?v=' + new Date().getTime(), function () {
- // that.closeLoad(loadIndex);
- // }));
- // break;
- // case renderType.iframe:
- // var item = that._content.children('div[lay-item-id=' + layId + ']').children('iframe');
- // item.attr('src', item.attr('src'));
- // break;
- // }
- // break;
- // case 'closeCurrent': //关闭当前
- // if (layId !== -1)
- // that.tabDelete(layId);
- // break;
- // case 'closeOther': //关闭其他
- // that._title.children('li[lay-id]').each(function () {
- // var curId = $(this).attr('lay-id');
- // if (curId !== layId && curId !== -1)
- // that.tabDelete(curId);
- // });
- // break;
- // case 'closeAll': //关闭所有
- // that._title.children('li[lay-id]').each(function () {
- // var curId = $(this).attr('lay-id');
- // if (curId !== -1)
- // that.tabDelete(curId);
- // });
- // that.tabChange(-1);
- // break;
- // }
- // //_tool.click();
- // tooMenu.hide();
- // });
- // });
- // //监听浏览器窗口改变事件
- // that.winResize();
- // },
- // load: function () {
- // return layer.load(0, { shade: [0.3, '#333'] });
- // },
- // closeLoad: function (index) {
- // setTimeout(function () {
- // index && layer.close(index);
- // }, 500);
- // },
- // getBodyContent: function (url, callback) {
- // return utils.getBodyContent(utils.loadHtml(url, callback));
- // },
- // /**
- // * 监听浏览器窗口改变事件
- // */
- // winResize: function () {
- // var that = this,
- // config = that._config;
- // win.on('resize', function () {
- // var currBoxHeight = $(that._parentElem).height(); //获取当前容器的高度
- // switch (config.renderType) {
- // case renderType.page:
- // $('.iwb-tab .layui-tab-content').height(currBoxHeight - 43);
- // break;
- // case renderType.iframe:
- // $('.iwb-tab .layui-tab-content iframe').height(currBoxHeight - 47);
- // break;
- // }
- // }).resize();
- // },
- // /**
- // * 检查选项卡是否存在
- // */
- // tabExists: function (layId) {
- // var that = this;
- // return that._title.find('li[lay-id=' + layId + ']').length > 0;
- // },
- // /**
- // * 删除选项卡
- // */
- // tabDelete: function (layId) {
- // element.tabDelete(this._filter, layId);
- // },
- // /**
- // * 设置选中选项卡
- // */
- // tabChange: function (layId) {
- // element.tabChange(this._filter, layId);
- // },
- // /**
- // * 获取选项卡对象
- // */
- // getTab: function (layId) {
- // return this._title.find('li[lay-id=' + layId + ']');
- // },
- // /**
- // * 添加一个选项卡,已存在则获取焦点
- // */
- // tabAdd: function (options) {
- // var that = this,
- // config = that._config,
- // loadIndex = undefined;
- // options = options || {
- // id: new Date().getTime(),
- // title: '新标签页',
- // icon: 'fa-file',
- // url: '404.html'
- // };
- // var title = options.title,
- // icon = options.icon,
- // url = options.url,
- // id = options.id;
- // if (that.tabExists(id)) {
- // that.tabChange(id);
- // return;
- // }
- // NProgress.start();
- // if (config.openWait)
- // loadIndex = that.load();
- // var titleHtml = ['<li class="layui-this" lay-id="' + id + '" data-url="' + url + '">'];
- // if (icon) {
- // if (icon.indexOf('fa-') !== -1) {
- // titleHtml.push('<i class="fa ' + icon + '" aria-hidden="true"></i>');
- // } else {
- // titleHtml.push('<i class="layui-icon">' + icon + '</i>');
- // }
- // }
- // titleHtml.push(' ' + title);
- // titleHtml.push('<i class="layui-icon layui-unselect layui-tab-close">ဆ</i>');
- // titleHtml.push('</li>');
- // var contentHtm = '<div class="layui-tab-item layui-show" lay-item-id="' + id + '">{{content}}</div>';
- // switch (config.renderType) {
- // case renderType.page:
- // contentHtm = contentHtm.replace('{{content}}', that.getBodyContent(url + '?v=' + new Date().getTime(), function () {
- // setTimeout(function () {
- // NProgress.done();
- // config.openWait && loadIndex && that.closeLoad(loadIndex);
- // }, 500);
- // }));
- // break;
- // case renderType.iframe:
- // contentHtm = contentHtm.replace('{{content}}', '<iframe src="' + url + '"></iframe>');
- // break;
- // }
- // //追加html
- // that._title.append(titleHtml.join(''));
- // that._content.append(contentHtm);
- // if (config.renderType === renderType.iframe) {
- // that._content.find('div[lay-item-id=' + id + ']').find('iframe').on('load', function () {
- // NProgress.done();
- // config.openWait && loadIndex && that.closeLoad(loadIndex);
- // });
- // }
- // //监听选项卡关闭事件
- // that.getTab(id).find('i.layui-tab-close').off('click').on('click', function () {
- // //关闭之前
- // if (config.closeBefore) {
- // if (config.closeBefore(options)) {
- // that.tabDelete(id);
- // }
- // } else {
- // that.tabDelete(id);
- // }
- // });
- // that.tabChange(id);
- // that.winResize();
- // if (config.onSwitch) {
- // element.on('tab(' + that._filter + ')', function (data) {
- // config.onSwitch({
- // index: data.index,
- // elem: data.elem,
- // layId: that._title.children('li').eq(data.index).attr('lay-id')
- // });
- // });
- // }
- // },
- // /**
- // * 获取当前选项卡的id
- // */
- // getCurrLayId: function () {
- // return this._title.find('li.layui-this').attr('lay-id');
- // }
- // };
- // /**
- // * 渲染选项卡
- // */
- // Tab.fn.render = function () {
- // var that = this,
- // config = that.config;
- // if (config.elem === undefined) {
- // layui.hint().error('Tab error:请配置选择卡容器.');
- // return that;
- // }
- // tab._config = config;
- // tab.createTabDom();
- // return that;
- // };
- // /**
- // * 添加一个选项卡
- // */
- // Tab.fn.tabAdd = function (params) {
- // tab.tabAdd(params);
- // };
- // /**
- // * 关闭一个选项卡
- // */
- // Tab.fn.close = function (layId) {
- // tab.tabDelete(layId);
- // };
- // Tab.fn.getId = function () {
- // return tab.getCurrLayId();
- // };
-
- // var t = new Tab();
- // exports('tab', t);
- //});
|