wangzhibo
2021-09-06 2efb5d2a02ce47a9fd6f1cec138496aba72f0815
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
 * UI v1.1.0
 * Copyright 2017-2018 Muyao
 * Licensed under the Muyao License 1.0 
 */
(function (window, document, $) {
    'use strict';
 
    $.site.contentTabs = {
        $instance: $('#admui-navTabs .site-menu'),
        $content: $('#admui-pageContent'),
        storageKey: 'admui.base.contentTabs',
        tabId: 0,
        relative: 0,
        tabTimeout: 30,
        init: function () {
            this.bind();
            this._defaultTab();
        },
        bind: function () {
            var self = this,
                $navContabs = $("#admui-siteConTabs"),
                $navContent = $navContabs.find("ul.con-tabs");
 
            this.tabWidth = $navContent.find("li").width();
            this.view = $navContabs.find(".contabs-scroll").width();
 
            this.ifameTabs();
 
            // 标签页的左右移动  &&  关闭单个标签页  && 切换标签页
            $navContabs
                .on('click.site.contabs', 'button.pull-left', function () {
                    self.tabPosition($navContent, self.tabWidth, "right");
                })
                .on('click.site.contabs', '.pull-right>.btn-icon', function () {
                    var content = $navContent.width();
 
                    self.tabPosition($navContent, self.tabWidth, "left", self.view, content);
                })
                .on('click.site.contabs', 'ul.con-tabs>li', function (e) {
                    var $target = $(e.target), $item = $(this);
 
                    if ($target.is("i.wb-close-mini")) { // 关闭
                        self.closeTab($item);
                    } else if (!$item.is(".active")) { // 切换
                        $item.siblings("li").removeClass("active");
                        $item.addClass("active");
 
                        self._checkoutTab($item.find('a'));
                        self.enable($item);
                    }
                    e.preventDefault();
                });
 
            // 刷新当前页 && 关闭其他 && 所有标签页
            $navContabs
                .on('click.site.contabs', '.pull-right li.reload-page', function () {
                    var $navLi = $navContabs.find('ul.con-tabs>li.active>a'),
                        tabUrl = $navLi.attr('href');
 
                    // 刷新当前页
                    self.$content.children('[src="' + tabUrl + '"]').attr('src', tabUrl);
 
                })
                .on('click.site.contabs', '.pull-right li.close-other', function () {
                    var $navLis = $navContabs.find('ul.con-tabs>li');
 
                    $navLis.each(function () {
                        var $that = $(this), tabId;
 
                        if (!$that.is('.active') && $that.index() !== 0) {
                            tabId = $that.find('a').attr('target');
                            $that.remove();
                            self.$content.children('[name="' + tabId + '"]').remove();
                            self._updateSetting(tabId);
                        }
                    });
 
                    $navContent.animate({left: 0}, 100);
 
                    self.btnView('hide');
                })
                .on('click.site.contabs', '.pull-right li.close-all', function () {
                    var $tabs = $navContabs.find('ul.con-tabs>li'),
                        $checked = $tabs.eq(0);
 
                    $tabs.each(function () {
                        var $that = $(this), tabId;
 
                        if ($that.index() !== 0) {
                            tabId = $that.find('a').attr('target');
                            $that.remove();
                            self._updateSetting(tabId);
                        }
                    });
 
                    $navContent.animate({left: 0}, 100);
 
                    self.btnView('hide');
 
                    $checked.addClass('active');
                    self.enable($tabs.eq(0));
                    self._checkoutTab($checked.find('a'));
 
                    // 关闭所有
                    self.$content.children(':not(:first)').remove();
 
                    self.tabSize();
                });
 
            // 登出清楚标签页缓存
            $(document).on('click', '#admui-signOut', function () {
                $.sessionStorage.remove(self.storageKey);
            });
 
            // 浏览器窗口大小改变,标签页的对应状态
            $(window).on('resize', this.resize);
        },
        ifameTabs: function (el) { // 为当前doc对象中的a[data-iframe]标签绑定创建标签页功能
            var self = this,
                doc = el === undefined ? document : el;
 
            // 新建标签页
            $(doc).on('click', 'a[data-iframe]', function (e) {
                var $item = $(this), urlResult,
                    url = $item.attr('href'),
                    title = $.trim($item.text()) || $.trim($item.attr('title')),
                    iframeContent = $item.data('iframe') || '#admui-pageContent',
                    tabResult = $item.is('[target="_blank"]');
                
                urlResult = new RegExp(/^([a-zA-z]+:|#|javascript|www\.)/); // 不执行新建标签页操作的地址
 
                if (urlResult.test(url)) {
                    return;
                }
 
                if (tabResult && iframeContent === '#admui-pageContent') {
                    window.history.replaceState(null, '', '#!' + url);
                    self.tabType = true;
                    self.buildTab({name: title, url: url});
 
                    if (!self.$instance.find($item).length) {
                        self.enable($item.parent());
                    }
                } else if (!tabResult) {
                    $(iframeContent).find('iframe.active').attr('src', url);
                }
 
                e.preventDefault();
            });
        },
        _checkoutTab: function (tab) { // 标签页的切换
            var $content = this.$content,
                tabId = tab.attr('target'),
                tabTitle = $.trim(tab.attr('title')),
                tabUrl = tab.attr('href'),
                $checkedTab = $content.children('iframe[name="' + tabId + '"]');
 
            $('title').text(tabTitle);
 
            if (!this.tabType) {
                window.history.replaceState(null, '', '#!' + tabUrl);
            }
 
            if (!$checkedTab.attr('src')) {
                $checkedTab.attr('src', tabUrl);
            }
 
            $content.children('.active').removeClass('active');
            $checkedTab.addClass('active');
 
            $.site.iframeEvents($checkedTab);
            this._updateSetting('checked', tabId);
            this.tabType = false;
        },
        _defaultTab: function () { // 存储默认标签的信息
            var $defaultTab = $('#admui-siteConTabs').find('li:first > a'), settings;
 
            settings = this.settings = $.sessionStorage.get(this.storageKey);
 
            if (settings === null) {
                settings = $.extend(true, {}, {
                    'iframe-0': {
                        'url': $defaultTab.attr('href'),
                        'name': $defaultTab.text()
                    },
                    'checked': $defaultTab.attr('target'),
                    'tabId': this.tabId
                });
 
                this._updateSetting(settings);
            } else {
                this.tabId = settings.tabId;
            }
        },
        _updateSetting: function (item, value) {
            var settings = $.sessionStorage.get(this.storageKey);
            settings = settings ? settings : {};
 
            if (typeof item === 'object') {
                $.extend(true, settings, item);
            } else if (!value) {
                delete settings[item];
            } else {
                settings[item] = value;
            }
 
            $.sessionStorage.set(this.storageKey, settings, this.tabTimeout);
        },
        resize: function () {
            var self = $.site.contentTabs,
                $navContabs = $(".site-contabs"),
                $navContent = $navContabs.find("ul.con-tabs");
 
            self._throttle(function () {
                self.view = $navContabs.find(".contabs-scroll").width();
                self.tabEvent($navContent, 'media');
            }, 200)();
        },
        enable: function ($el) { // 左侧菜单定位
            var $instance = this.$instance,
                href = $.trim($el.find('a').attr('href')),
                _result = href.indexOf('#'),
                tabUrl = _result > 0 ? href.substring(0, _result) : href,
                $current = $instance.find('a[href="' + tabUrl + '"]'),
                $currentParents, $currentLi, $currentHasSub, $instanceLi, checkedId, currentId;
 
            if ($current.length === 0) {
                $.site.menu.refresh();
                return;
            }
 
            checkedId = $.trim($instance.closest('div.tab-pane.active').attr('id'));
            currentId = $.trim($current.closest('div.tab-pane').attr('id'));
 
            if (checkedId !== currentId) {
                $('#admui-navbar a[href="#' + currentId + '"]').tab('show');
            }
 
            $currentLi = $current.closest('li').siblings('li.open');
            $currentParents = $current.parents('li.has-sub');
            $currentHasSub = $current.closest('li.has-sub').siblings('li.open');
            $instanceLi = $instance.find('li.open');
 
            $instance.find('li.active').trigger('deactive.site.menu');
            $current.closest('li').trigger('active.site.menu');
 
            if ($currentLi.length) {
                $currentLi.trigger('close.site.menu');
            }
 
            if (!$current.closest('li.has-sub').hasClass('open')) {
                if ($currentHasSub.length) {
                    $currentHasSub.trigger('close.site.menu');
                }
 
                if ($instanceLi.length) {
                    $instanceLi.not($currentParents).trigger('close.site.menu');
                }
 
                $currentParents.trigger('open.site.menu');
            }
        },
        buildTab: function (opt) { // 新建标签页
            var $tabNav = $(".con-tabs"), tabName, obj = {}, tabId,
                _url = opt.url,
                _result = _url.indexOf('#'),
                tabUrl = _result > 0 ? _url.substring(0, _result) : _url;
 
            if (this._checkTabs($tabNav, tabUrl)) { // 标签查重
                return;
            }
 
            tabId = ++this.tabId;
            tabName = 'iframe-' + tabId;
 
            // 修改当前选中的标签页
            $tabNav.find("li.active").removeClass("active");
            $tabNav.append('<li  class="active"><a href="' + tabUrl + '" target="' + tabName +
                '" title="' + opt.name + '' + '" rel="contents"><span>' + opt.name + '</span><i class="icon' +
                ' wb-close-mini">' + '</i></a></li>');
 
            obj[tabName] = {
                'url': tabUrl,
                'name': opt.name
            };
 
            $.extend(obj, {
                'checked': tabName,
                'tabId': tabId
            });
 
            this._updateSetting(obj);
 
            // 修改页面标题
            opt.name = opt.name === '' ? '无标题' : opt.name;
            $('title').text($.trim(opt.name));
 
            this.buildIframe(_url);
            this.tabSize();
            this.tabEvent($tabNav, 'media');
            this.tabType = false;
        },
        _checkTabs: function (doc, url) { // 标签查重
            var x, prevAll, nextAll, contentW,
                _view = this.view, _tab = this.tabWidth,
                $currenttab = doc.find("a[href='" + url + "']").closest('li');
 
            if ($currenttab.hasClass('active')) { // 标签存在&已选中
                return true;
            }
 
            if ($currenttab.size() <= 0) { // 标签不存在
                return false;
            }
 
            // 标签存在未选中
            doc.find("li.active").removeClass("active");
            $currenttab.addClass("active");
 
            // 切换标签页
            this._checkoutTab($currenttab.find('a'));
 
            // 标签位移到可视界面显示
            x = doc.position().left;
            contentW = doc.width();
            prevAll = $currenttab.prevAll('li').size() * _tab;
            nextAll = $currenttab.nextAll('li').size() * _tab;
 
            if (-prevAll < x) {
                if (prevAll + x < _view) {
                    return true;
                }
 
                x = -(prevAll - _view + _tab);
            } else {
                if (-x < contentW - nextAll) {
                    return true;
                }
 
                x = -(contentW - nextAll - _tab);
            }
 
            doc.animate({
                left: x
            }, 100);
 
            return true;
        },
        buildIframe: function (url) { // 创建iframe窗口
            var $content = this.$content,
                iframeName = 'iframe-' + this.tabId, $checkedIframe;
 
            $content.children('.active').removeClass('active');
            $content.append('<iframe src="' + url + '" frameborder="0" name="' + iframeName + '" class="page-frame animation-fade active"></iframe>');
 
            $checkedIframe = $content.find('iframe[name="' + iframeName + '"]');
 
            $.site.iframeEvents($checkedIframe);
        },
        tabSize: function () { // 修改标签页盒子尺寸
            var content, $tabNav = $(".con-tabs"),
                _num = $tabNav.find("li").size();
 
            content = this.tabWidth * _num;
            $tabNav.css("width", content);
        },
        tabEvent: function (doc, media) { // 增删标签页的对应状态
            var content = $(".con-tabs").width(),
                _view = this.view, _tab = this.tabWidth;
 
            if (content > this.view) {
                this.tabPosition(doc, _tab, "left", _view, content, media);
                this.btnView('visible');
            } else {
                this.btnView('hide');
            }
 
            if (this.currentView < _view || this.currentContent > content) {
                this.tabPosition(doc, _tab, "right", _view, content, media);
            }
            this.currentView = _view;
            this.currentContent = content;
        },
        tabPosition: function (doc, width, dir, view, content, media) { // 标签页的位移
            var self = this,
                x = doc.position().left,
                callback = function (x) {
                    var flag = x + width;
 
                    if (flag > 0) {
                        self.relative = x;
                        return 0;
                    } else {
                        return x;
                    }
                };
 
            if (dir === "left") {
                if (x <= view - content) {
                    return false;
                }
                if (typeof media !== 'undefined') {
                    x = view - content;
                } else {
                    x = this.relative !== 0 ? x - width + this.relative : x - width;
                    this.relative = 0;
                }
            } else if (dir === "right") {
                if (x === 0) {
                    return false;
                }
 
                if (typeof media !== 'undefined') {
                    x = content <= view ? 0 : view - content;
                } else {
                    x = callback(x + width);
                }
            }
 
            doc.animate({
                left: x
            }, 100);
        },
        _throttle: function (fn, interval) { // 函数节流操作
            var _fn = fn,
                timer,
                firstTime = true;
            return function () {
                var args = arguments,
                    self = this;
 
                if (firstTime) {
                    _fn.apply(self, args);
                    firstTime = false;
                }
 
                if (timer) {
                    return false;
                }
 
                timer = setTimeout(function () {
                    clearTimeout(timer);
                    timer = null;
                    _fn.apply(self, args);
                }, interval || 500);
            };
        },
        closeTab: function ($item) {
            var checkedTabId = $item.children('a').attr('target'), $checked = '',
                $nextLi = $item.next('li'),
                $content = this.$content;
 
            if ($item.is('.active')) { // 关闭选中标签
                if ($nextLi.size() > 0) {
                    $checked = $nextLi;
                } else {
                    $checked = $item.prev('li');
                }
 
                $checked.addClass("active");
 
                this.enable($checked);
                this._checkoutTab($checked.find('a'));
            }
 
            $item.remove();
            $content.children('[name="' + checkedTabId + '"]').remove();
            this._updateSetting(checkedTabId);
 
            this.tabSize();
            this.tabEvent($(".con-tabs"), 'media');
 
        },
        btnView: function (status) { // 标签页左右移动按钮状态
            var $siteContabs = $('.site-contabs'),
                $contabsLeftBtn = $siteContabs.children('button.pull-left'),
                $contabsRightBtn = $siteContabs.find('.pull-right > button.btn-icon');
 
            if (status === 'visible') {
                $contabsLeftBtn.removeClass('hide');
                $contabsRightBtn.removeClass('hide');
            } else if (status === 'hide') {
                $contabsLeftBtn.addClass('hide');
                $contabsRightBtn.addClass('hide');
            }
        }
    };
 
})(window, document, jQuery);