1 | "use strict";
|
2 |
|
3 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
4 |
|
5 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
6 |
|
7 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
8 |
|
9 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | ;
|
23 |
|
24 | (function ($, window, document, undefined) {
|
25 | "use strict";
|
26 |
|
27 | var defaults = {
|
28 | selected: 0,
|
29 |
|
30 | theme: 'default',
|
31 |
|
32 | orientation: 'horizontal',
|
33 |
|
34 | justified: true,
|
35 |
|
36 | autoAdjustHeight: true,
|
37 |
|
38 | backButtonSupport: true,
|
39 |
|
40 | enableURLhash: true,
|
41 |
|
42 | transition: {
|
43 | animation: 'none',
|
44 |
|
45 | speed: '400',
|
46 |
|
47 | easing: ''
|
48 |
|
49 | },
|
50 | autoProgress: {
|
51 |
|
52 | enabled: false,
|
53 |
|
54 | interval: 3500,
|
55 |
|
56 | stopOnFocus: true
|
57 |
|
58 | },
|
59 | keyboardSettings: {
|
60 | keyNavigation: true,
|
61 |
|
62 | keyLeft: [37],
|
63 |
|
64 | keyRight: [39]
|
65 |
|
66 | }
|
67 | };
|
68 |
|
69 | var SmartTab = function () {
|
70 | function SmartTab(element, options) {
|
71 | _classCallCheck(this, SmartTab);
|
72 |
|
73 |
|
74 | this.options = $.extend(true, {}, defaults, options);
|
75 |
|
76 | this.main = $(element);
|
77 |
|
78 | this.nav = this._getFirstDescendant('.nav');
|
79 |
|
80 | this.tabs = this.nav.find('.nav-link');
|
81 |
|
82 | this.container = this._getFirstDescendant('.tab-content');
|
83 |
|
84 | this.pages = this.container.children('.tab-pane');
|
85 |
|
86 | this.current_index = null;
|
87 |
|
88 | this.autoProgressId = null;
|
89 |
|
90 | this._initOptions();
|
91 |
|
92 |
|
93 | this._initLoad();
|
94 | }
|
95 |
|
96 |
|
97 | _createClass(SmartTab, [{
|
98 | key: "_initLoad",
|
99 | value: function _initLoad() {
|
100 |
|
101 | this.pages.hide();
|
102 | this.tabs.removeClass('active');
|
103 |
|
104 | var idx = this._getTabIndex();
|
105 |
|
106 |
|
107 | this._showTab(idx);
|
108 | }
|
109 |
|
110 | }, {
|
111 | key: "_initOptions",
|
112 | value: function _initOptions() {
|
113 |
|
114 | this._setElements();
|
115 |
|
116 |
|
117 | this._setEvents();
|
118 | }
|
119 | }, {
|
120 | key: "_getFirstDescendant",
|
121 | value: function _getFirstDescendant(selector) {
|
122 |
|
123 | var elm = this.main.children(selector);
|
124 |
|
125 | if (elm.length > 0) {
|
126 | return elm;
|
127 | }
|
128 |
|
129 |
|
130 | this.main.children().each(function (i, n) {
|
131 | var tmp = $(n).children(selector);
|
132 |
|
133 | if (tmp.length > 0) {
|
134 | elm = tmp;
|
135 | return false;
|
136 | }
|
137 | });
|
138 |
|
139 | if (elm.length > 0) {
|
140 | return elm;
|
141 | }
|
142 |
|
143 |
|
144 | this._showError("Element not found " + selector);
|
145 |
|
146 | return false;
|
147 | }
|
148 | }, {
|
149 | key: "_setElements",
|
150 | value: function _setElements() {
|
151 |
|
152 | this.main.addClass('st');
|
153 |
|
154 | if (this.options.justified === true) {
|
155 | this.main.addClass('st-justified');
|
156 | } else {
|
157 | this.main.removeClass('st-justified');
|
158 | }
|
159 |
|
160 | this._setTheme(this.options.theme);
|
161 |
|
162 | this._setOrientation(this.options.orientation);
|
163 | }
|
164 | }, {
|
165 | key: "_setEvents",
|
166 | value: function _setEvents() {
|
167 | var _this = this;
|
168 |
|
169 |
|
170 | if (this.main.data('click-init')) {
|
171 | return true;
|
172 | }
|
173 |
|
174 |
|
175 | this.main.data('click-init', true);
|
176 |
|
177 | $(this.tabs).on("click", function (e) {
|
178 | e.preventDefault();
|
179 |
|
180 | _this._showTab(_this.tabs.index(e.currentTarget));
|
181 | });
|
182 |
|
183 | if (this.options.keyboardSettings.keyNavigation) {
|
184 | $(document).keyup(function (e) {
|
185 | _this._keyNav(e);
|
186 | });
|
187 | }
|
188 |
|
189 |
|
190 | if (this.options.backButtonSupport) {
|
191 | $(window).on('hashchange', function (e) {
|
192 | var idx = _this._getURLHashIndex();
|
193 |
|
194 | if (idx !== false) {
|
195 | e.preventDefault();
|
196 |
|
197 | _this._showTab(idx);
|
198 | }
|
199 | });
|
200 | }
|
201 |
|
202 | if (this.options.autoProgress.enabled && this.options.autoProgress.stopOnFocus) {
|
203 | $(this.main).on("mouseover", function (e) {
|
204 | e.preventDefault();
|
205 |
|
206 | _this._stopAutoProgress();
|
207 | });
|
208 | $(this.main).on("mouseleave", function (e) {
|
209 | e.preventDefault();
|
210 |
|
211 | _this._startAutoProgress();
|
212 | });
|
213 | }
|
214 | }
|
215 | }, {
|
216 | key: "_showNext",
|
217 | value: function _showNext() {
|
218 | var si = 0;
|
219 |
|
220 | for (var i = this.current_index + 1; i < this.tabs.length; i++) {
|
221 | if (this._isShowable(i)) {
|
222 | si = i;
|
223 | break;
|
224 | }
|
225 | }
|
226 |
|
227 | this._showTab(si);
|
228 | }
|
229 | }, {
|
230 | key: "_showPrevious",
|
231 | value: function _showPrevious() {
|
232 | var si = this.tabs.length - 1;
|
233 |
|
234 | for (var i = this.current_index - 1; i >= 0; i--) {
|
235 | if (this._isShowable(i)) {
|
236 | si = i;
|
237 | break;
|
238 | }
|
239 | }
|
240 |
|
241 | this._showTab(si);
|
242 | }
|
243 | }, {
|
244 | key: "_isShowable",
|
245 | value: function _isShowable(idx) {
|
246 | if (this.tabs.eq(idx).hasClass('disabled') || this.tabs.eq(idx).hasClass('hidden')) {
|
247 | return false;
|
248 | }
|
249 |
|
250 | return true;
|
251 | }
|
252 | }, {
|
253 | key: "_showTab",
|
254 | value: function _showTab(idx) {
|
255 |
|
256 | if (idx == this.current_index) {
|
257 | return false;
|
258 | }
|
259 |
|
260 |
|
261 | if (!this.tabs.eq(idx)) {
|
262 | return false;
|
263 | }
|
264 |
|
265 |
|
266 | if (!this._isShowable(idx)) {
|
267 | return false;
|
268 | }
|
269 |
|
270 |
|
271 | this._loadTab(idx);
|
272 | }
|
273 | }, {
|
274 | key: "_loadTab",
|
275 | value: function _loadTab(idx) {
|
276 | var _this2 = this;
|
277 |
|
278 |
|
279 | var curTab = this._getAnchor(this.current_index);
|
280 |
|
281 | if (this.current_index !== null) {
|
282 |
|
283 | if (this._triggerEvent("leaveTab", [curTab, this.current_index]) === false) {
|
284 | return false;
|
285 | }
|
286 | }
|
287 |
|
288 |
|
289 | var selTab = this._getAnchor(idx);
|
290 |
|
291 |
|
292 | var getTabContent = this._triggerEvent("tabContent", [selTab, idx]);
|
293 |
|
294 | if (getTabContent) {
|
295 | if (_typeof(getTabContent) == "object") {
|
296 | getTabContent.then(function (res) {
|
297 | _this2._setTabContent(idx, res);
|
298 |
|
299 | _this2._transitTab(idx);
|
300 | })["catch"](function (err) {
|
301 | console.error(err);
|
302 |
|
303 | _this2._setTabContent(idx, err);
|
304 |
|
305 | _this2._transitTab(idx);
|
306 | });
|
307 | } else if (typeof getTabContent == "string") {
|
308 | this._setTabContent(idx, getTabContent);
|
309 |
|
310 | this._transitTab(idx);
|
311 | } else {
|
312 | this._transitTab(idx);
|
313 | }
|
314 | } else {
|
315 | this._transitTab(idx);
|
316 | }
|
317 | }
|
318 | }, {
|
319 | key: "_getAnchor",
|
320 | value: function _getAnchor(idx) {
|
321 | if (idx == null) {
|
322 | return null;
|
323 | }
|
324 |
|
325 | return this.tabs.eq(idx);
|
326 | }
|
327 | }, {
|
328 | key: "_getPage",
|
329 | value: function _getPage(idx) {
|
330 | if (idx == null) {
|
331 | return null;
|
332 | }
|
333 |
|
334 | var anchor = this._getAnchor(idx);
|
335 |
|
336 | return anchor.length > 0 ? this.main.find(anchor.attr("href")) : null;
|
337 | }
|
338 | }, {
|
339 | key: "_setTabContent",
|
340 | value: function _setTabContent(idx, html) {
|
341 | var page = this._getPage(idx);
|
342 |
|
343 | if (page) {
|
344 | page.html(html);
|
345 | }
|
346 | }
|
347 | }, {
|
348 | key: "_transitTab",
|
349 | value: function _transitTab(idx) {
|
350 | var _this3 = this;
|
351 |
|
352 |
|
353 | var selTab = this._getAnchor(idx);
|
354 |
|
355 |
|
356 | this._setURLHash(selTab.attr("href"));
|
357 |
|
358 |
|
359 | this._setAnchor(idx);
|
360 |
|
361 |
|
362 | this._doTabAnimation(idx, function () {
|
363 |
|
364 | _this3._fixHeight(idx);
|
365 |
|
366 |
|
367 | _this3._triggerEvent("showTab", [selTab, _this3.current_index]);
|
368 |
|
369 |
|
370 | _this3._restartAutoProgress();
|
371 | });
|
372 |
|
373 |
|
374 | this.current_index = idx;
|
375 | }
|
376 | }, {
|
377 | key: "_doTabAnimation",
|
378 | value: function _doTabAnimation(idx, callback) {
|
379 | var _this4 = this;
|
380 |
|
381 |
|
382 | var curPage = this._getPage(this.current_index);
|
383 |
|
384 |
|
385 | var selPage = this._getPage(idx);
|
386 |
|
387 |
|
388 | var transitionEffect = this.options.transition.animation.toLowerCase();
|
389 |
|
390 | this._stopAnimations();
|
391 |
|
392 | switch (transitionEffect) {
|
393 | case 'slide-horizontal':
|
394 | case 'slide-h':
|
395 |
|
396 | var containerWidth = this.container.width();
|
397 | var curLastLeft = containerWidth;
|
398 | var nextFirstLeft = containerWidth * -2;
|
399 |
|
400 | if (idx > this.current_index) {
|
401 | curLastLeft = containerWidth * -1;
|
402 | nextFirstLeft = containerWidth;
|
403 | }
|
404 |
|
405 |
|
406 | if (this.current_index == null) {
|
407 | this.container.height(selPage.outerHeight());
|
408 | }
|
409 |
|
410 | var css_pos, css_left;
|
411 |
|
412 | if (curPage) {
|
413 | css_pos = curPage.css("position");
|
414 | css_left = curPage.css("left");
|
415 | curPage.css("position", 'absolute').css("left", 0).animate({
|
416 | left: curLastLeft
|
417 | }, this.options.transition.speed, this.options.transition.easing, function () {
|
418 | $(this).hide();
|
419 | curPage.css("position", css_pos).css("left", css_left);
|
420 | });
|
421 | }
|
422 |
|
423 | css_pos = selPage.css("position");
|
424 | css_left = selPage.css("left");
|
425 | selPage.css("position", 'absolute').css("left", nextFirstLeft).outerWidth(containerWidth).show().animate({
|
426 | left: 0
|
427 | }, this.options.transition.speed, this.options.transition.easing, function () {
|
428 | selPage.css("position", css_pos).css("left", css_left);
|
429 | callback();
|
430 | });
|
431 | break;
|
432 |
|
433 | case 'slide-vertical':
|
434 | case 'slide-v':
|
435 |
|
436 | var containerHeight = this.container.height();
|
437 | var curLastTop = containerHeight;
|
438 | var nextFirstTop = containerHeight * -2;
|
439 |
|
440 | if (idx > this.current_index) {
|
441 | curLastTop = containerHeight * -1;
|
442 | nextFirstTop = containerHeight;
|
443 | }
|
444 |
|
445 | var css_vpos, css_vtop;
|
446 |
|
447 | if (curPage) {
|
448 | css_vpos = curPage.css("position");
|
449 | css_vtop = curPage.css("top");
|
450 | curPage.css("position", 'absolute').css("top", 0).animate({
|
451 | top: curLastTop
|
452 | }, this.options.transition.speed, this.options.transition.easing, function () {
|
453 | $(this).hide();
|
454 | curPage.css("position", css_vpos).css("top", css_vtop);
|
455 | });
|
456 | }
|
457 |
|
458 | css_vpos = selPage.css("position");
|
459 | css_vtop = selPage.css("top");
|
460 | selPage.css("position", 'absolute').css("top", nextFirstTop).show().animate({
|
461 | top: 0
|
462 | }, this.options.transition.speed, this.options.transition.easing, function () {
|
463 | selPage.css("position", css_vpos).css("top", css_vtop);
|
464 | callback();
|
465 | });
|
466 | break;
|
467 |
|
468 | case 'slide-swing':
|
469 | case 'slide-s':
|
470 |
|
471 | if (curPage) {
|
472 | curPage.slideUp('fast', this.options.transition.easing, function () {
|
473 | selPage.slideDown(_this4.options.transition.speed, _this4.options.transition.easing, function () {
|
474 | callback();
|
475 | });
|
476 | });
|
477 | } else {
|
478 | selPage.slideDown(this.options.transition.speed, this.options.transition.easing, function () {
|
479 | callback();
|
480 | });
|
481 | }
|
482 |
|
483 | break;
|
484 |
|
485 | case 'fade':
|
486 |
|
487 | if (curPage) {
|
488 | curPage.fadeOut('fast', this.options.transition.easing, function () {
|
489 | selPage.fadeIn('fast', _this4.options.transition.easing, function () {
|
490 | callback();
|
491 | });
|
492 | });
|
493 | } else {
|
494 | selPage.fadeIn(this.options.transition.speed, this.options.transition.easing, function () {
|
495 | callback();
|
496 | });
|
497 | }
|
498 |
|
499 | break;
|
500 |
|
501 | default:
|
502 | if (curPage) {
|
503 | curPage.hide();
|
504 | }
|
505 |
|
506 | selPage.show();
|
507 | callback();
|
508 | break;
|
509 | }
|
510 | }
|
511 | }, {
|
512 | key: "_stopAnimations",
|
513 | value: function _stopAnimations() {
|
514 | this.pages.finish();
|
515 | this.container.finish();
|
516 | }
|
517 | }, {
|
518 | key: "_setAnchor",
|
519 | value: function _setAnchor(idx) {
|
520 | this.tabs.eq(this.current_index).removeClass("active");
|
521 | this.tabs.eq(idx).addClass("active");
|
522 | }
|
523 | }, {
|
524 | key: "_getTabIndex",
|
525 | value: function _getTabIndex() {
|
526 |
|
527 | var idx = this._getURLHashIndex();
|
528 |
|
529 | return idx === false ? this.options.selected : idx;
|
530 | }
|
531 | }, {
|
532 | key: "_fixHeight",
|
533 | value: function _fixHeight(idx) {
|
534 |
|
535 | if (this.options.autoAdjustHeight) {
|
536 | var selPage = this._getPage(idx);
|
537 |
|
538 | this.container.finish().animate({
|
539 | height: selPage.outerHeight()
|
540 | }, this.options.transition.speed);
|
541 | }
|
542 | }
|
543 | }, {
|
544 | key: "_setTheme",
|
545 | value: function _setTheme(theme) {
|
546 | this.main.removeClass(function (index, className) {
|
547 | return (className.match(/(^|\s)st-theme-\S+/g) || []).join(' ');
|
548 | }).addClass('st-theme-' + theme);
|
549 | }
|
550 | }, {
|
551 | key: "_setOrientation",
|
552 | value: function _setOrientation(orientation) {
|
553 | this.main.removeClass('st-vertical st-horizontal').addClass('st-' + orientation);
|
554 | }
|
555 |
|
556 | }, {
|
557 | key: "_keyNav",
|
558 | value: function _keyNav(e) {
|
559 |
|
560 | if ($.inArray(e.which, this.options.keyboardSettings.keyLeft) > -1) {
|
561 |
|
562 | this._showPrevious();
|
563 |
|
564 | e.preventDefault();
|
565 | } else if ($.inArray(e.which, this.options.keyboardSettings.keyRight) > -1) {
|
566 |
|
567 | this._showNext();
|
568 |
|
569 | e.preventDefault();
|
570 | } else {
|
571 | return;
|
572 | }
|
573 | }
|
574 |
|
575 | }, {
|
576 | key: "_startAutoProgress",
|
577 | value: function _startAutoProgress() {
|
578 | var _this5 = this;
|
579 |
|
580 | if (this.options.autoProgress.enabled && !this.autoProgressId) {
|
581 | this.autoProgressId = setInterval(function () {
|
582 | return _this5._showNext();
|
583 | }, this.options.autoProgress.interval);
|
584 | }
|
585 | }
|
586 | }, {
|
587 | key: "_stopAutoProgress",
|
588 | value: function _stopAutoProgress() {
|
589 | if (this.autoProgressId) {
|
590 | clearInterval(this.autoProgressId);
|
591 | this.autoProgressId = null;
|
592 | }
|
593 | }
|
594 | }, {
|
595 | key: "_restartAutoProgress",
|
596 | value: function _restartAutoProgress() {
|
597 | this._stopAutoProgress();
|
598 |
|
599 | this._startAutoProgress();
|
600 | }
|
601 | }, {
|
602 | key: "_triggerEvent",
|
603 | value: function _triggerEvent(name, params) {
|
604 |
|
605 | var e = $.Event(name);
|
606 | this.main.trigger(e, params);
|
607 |
|
608 | if (e.isDefaultPrevented()) {
|
609 | return false;
|
610 | }
|
611 |
|
612 | return e.result;
|
613 | }
|
614 | }, {
|
615 | key: "_setURLHash",
|
616 | value: function _setURLHash(hash) {
|
617 | if (this.options.enableURLhash && window.location.hash !== hash) {
|
618 | history.pushState(null, null, hash);
|
619 | }
|
620 | }
|
621 | }, {
|
622 | key: "_getURLHashIndex",
|
623 | value: function _getURLHashIndex() {
|
624 | if (this.options.enableURLhash) {
|
625 |
|
626 | var hash = window.location.hash;
|
627 |
|
628 | if (hash.length > 0) {
|
629 | var elm = this.nav.find("a[href*='" + hash + "']");
|
630 |
|
631 | if (elm.length > 0) {
|
632 | return this.tabs.index(elm);
|
633 | }
|
634 | }
|
635 | }
|
636 |
|
637 | return false;
|
638 | }
|
639 | }, {
|
640 | key: "_loader",
|
641 | value: function _loader(action) {
|
642 | switch (action) {
|
643 | case 'show':
|
644 | this.main.addClass('st-loading');
|
645 | break;
|
646 |
|
647 | case 'hide':
|
648 | this.main.removeClass('st-loading');
|
649 | break;
|
650 |
|
651 | default:
|
652 | this.main.toggleClass('st-loading');
|
653 | }
|
654 | }
|
655 | }, {
|
656 | key: "_showError",
|
657 | value: function _showError(msg) {
|
658 | console.error(msg);
|
659 | }
|
660 |
|
661 | }, {
|
662 | key: "goToTab",
|
663 | value: function goToTab(tabIndex) {
|
664 | this._showTab(tabIndex);
|
665 | }
|
666 | }, {
|
667 | key: "setOptions",
|
668 | value: function setOptions(options) {
|
669 | this.options = $.extend(true, {}, this.options, options);
|
670 |
|
671 | this._initOptions();
|
672 | }
|
673 | }, {
|
674 | key: "loader",
|
675 | value: function loader(state) {
|
676 | if (state === "show") {
|
677 | this.main.addClass('st-loading');
|
678 | } else {
|
679 | this.main.removeClass('st-loading');
|
680 | }
|
681 | }
|
682 | }]);
|
683 |
|
684 | return SmartTab;
|
685 | }();
|
686 |
|
687 |
|
688 | $.fn.smartTab = function (options) {
|
689 | if (options === undefined || _typeof(options) === 'object') {
|
690 | return this.each(function () {
|
691 | if (!$.data(this, "smartTab")) {
|
692 | $.data(this, "smartTab", new SmartTab(this, options));
|
693 | }
|
694 | });
|
695 | } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
|
696 | var instance = $.data(this[0], 'smartTab');
|
697 |
|
698 | if (options === 'destroy') {
|
699 | $.data(this, 'smartTab', null);
|
700 | }
|
701 |
|
702 | if (instance instanceof SmartTab && typeof instance[options] === 'function') {
|
703 | return instance[options].apply(instance, Array.prototype.slice.call(arguments, 1));
|
704 | } else {
|
705 | return this;
|
706 | }
|
707 | }
|
708 | };
|
709 | })(jQuery, window, document); |
\ | No newline at end of file |