UNPKG

20.8 kBJavaScriptView Raw
1"use strict";
2
3function _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
5function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7function _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
9function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
10
11/*!
12 * jQuery SmartTab v3.1.1
13 * The flexible jQuery tab control plugin
14 * http://www.techlaboratory.net/jquery-smarttab
15 *
16 * Created by Dipu Raj
17 * http://dipu.me
18 *
19 * @license Licensed under the terms of the MIT License
20 * https://github.com/techlab/jquery-smarttab/blob/master/LICENSE
21 */
22;
23
24(function ($, window, document, undefined) {
25 "use strict"; // Default options
26
27 var defaults = {
28 selected: 0,
29 // Initial selected tab, 0 = first tab
30 theme: 'default',
31 // theme for the tab, related css need to include for other than default theme
32 orientation: 'horizontal',
33 // Nav menu orientation. horizontal/vertical
34 justified: true,
35 // Nav menu justification. true/false
36 autoAdjustHeight: true,
37 // Automatically adjust content height
38 backButtonSupport: true,
39 // Enable the back button support
40 enableURLhash: true,
41 // Enable selection of the tab based on url hash
42 transition: {
43 animation: 'none',
44 // Effect on navigation, none/fade/slide-horizontal/slide-vertical/slide-swing
45 speed: '400',
46 // Transion animation speed
47 easing: '' // Transition animation easing. Not supported without a jQuery easing plugin
48
49 },
50 autoProgress: {
51 // Auto navigate tabs on interval
52 enabled: false,
53 // Enable/Disable Auto navigation
54 interval: 3500,
55 // Auto navigate Interval (used only if "autoProgress" is enabled)
56 stopOnFocus: true // Stop auto navigation on focus and resume on outfocus
57
58 },
59 keyboardSettings: {
60 keyNavigation: true,
61 // Enable/Disable keyboard navigation(left and right keys are used if enabled)
62 keyLeft: [37],
63 // Left key code
64 keyRight: [39] // Right key code
65
66 }
67 };
68
69 var SmartTab = /*#__PURE__*/function () {
70 function SmartTab(element, options) {
71 _classCallCheck(this, SmartTab);
72
73 // Merge user settings with default
74 this.options = $.extend(true, {}, defaults, options); // Main container element
75
76 this.main = $(element); // Navigation bar element
77
78 this.nav = this._getFirstDescendant('.nav'); // Tab anchor elements
79
80 this.tabs = this.nav.find('.nav-link'); // Content container
81
82 this.container = this._getFirstDescendant('.tab-content'); // Content pages
83
84 this.pages = this.container.children('.tab-pane'); // Active Tab index
85
86 this.current_index = null; // Autoprogress timer id
87
88 this.autoProgressId = null; // Assign options
89
90 this._initOptions(); // Initial load
91
92
93 this._initLoad();
94 } // Initial Load Method
95
96
97 _createClass(SmartTab, [{
98 key: "_initLoad",
99 value: function _initLoad() {
100 // Clean the elements
101 this.pages.hide();
102 this.tabs.removeClass('active'); // Get the initial tab index
103
104 var idx = this._getTabIndex(); // Show the initial tab
105
106
107 this._showTab(idx);
108 } // Initialize options
109
110 }, {
111 key: "_initOptions",
112 value: function _initOptions() {
113 // Set the elements
114 this._setElements(); // Assign plugin events
115
116
117 this._setEvents();
118 }
119 }, {
120 key: "_getFirstDescendant",
121 value: function _getFirstDescendant(selector) {
122 // Check for first level element
123 var elm = this.main.children(selector);
124
125 if (elm.length > 0) {
126 return elm;
127 } // Check for second level element
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 } // Element not found
142
143
144 this._showError("Element not found " + selector);
145
146 return false;
147 }
148 }, {
149 key: "_setElements",
150 value: function _setElements() {
151 // Set the main element
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 // Check if event handler already exists
170 if (this.main.data('click-init')) {
171 return true;
172 } // Flag item to prevent attaching handler again
173
174
175 this.main.data('click-init', true); // Anchor click event
176
177 $(this.tabs).on("click", function (e) {
178 e.preventDefault();
179
180 _this._showTab(_this.tabs.index(e.currentTarget));
181 }); // Keyboard navigation event
182
183 if (this.options.keyboardSettings.keyNavigation) {
184 $(document).keyup(function (e) {
185 _this._keyNav(e);
186 });
187 } // Back/forward browser button event
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; // Find the next showable step
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; // Find the previous showable step
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 // If current tab is requested again, skip
256 if (idx == this.current_index) {
257 return false;
258 } // If tab not found, skip
259
260
261 if (!this.tabs.eq(idx)) {
262 return false;
263 } // If it is a disabled tab, skip
264
265
266 if (!this._isShowable(idx)) {
267 return false;
268 } // Load tab content
269
270
271 this._loadTab(idx);
272 }
273 }, {
274 key: "_loadTab",
275 value: function _loadTab(idx) {
276 var _this2 = this;
277
278 // Get current tab element
279 var curTab = this._getAnchor(this.current_index);
280
281 if (this.current_index !== null) {
282 // Trigger "leaveTab" event
283 if (this._triggerEvent("leaveTab", [curTab, this.current_index]) === false) {
284 return false;
285 }
286 } // Get next tab element
287
288
289 var selTab = this._getAnchor(idx); // Get the content if used
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 // Get tab to show element
353 var selTab = this._getAnchor(idx); // Change the url hash to new tab
354
355
356 this._setURLHash(selTab.attr("href")); // Update controls
357
358
359 this._setAnchor(idx); // Animate the tab
360
361
362 this._doTabAnimation(idx, function () {
363 // Fix height with content
364 _this3._fixHeight(idx); // Trigger "showTab" event
365
366
367 _this3._triggerEvent("showTab", [selTab, _this3.current_index]); // Restart auto progress if enabled
368
369
370 _this3._restartAutoProgress();
371 }); // Update the current index
372
373
374 this.current_index = idx;
375 }
376 }, {
377 key: "_doTabAnimation",
378 value: function _doTabAnimation(idx, callback) {
379 var _this4 = this;
380
381 // Get current tab element
382 var curPage = this._getPage(this.current_index); // Get next tab element
383
384
385 var selPage = this._getPage(idx); // Get the transition effect
386
387
388 var transitionEffect = this.options.transition.animation.toLowerCase(); // Complete any ongoing animations
389
390 this._stopAnimations();
391
392 switch (transitionEffect) {
393 case 'slide-horizontal':
394 case 'slide-h':
395 // horizontal slide
396 var containerWidth = this.container.width();
397 var curLastLeft = containerWidth;
398 var nextFirstLeft = containerWidth * -2; // Forward direction
399
400 if (idx > this.current_index) {
401 curLastLeft = containerWidth * -1;
402 nextFirstLeft = containerWidth;
403 } // First load set the container width
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 // vertical slide
436 var containerHeight = this.container.height();
437 var curLastTop = containerHeight;
438 var nextFirstTop = containerHeight * -2; // Forward direction
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 // normal slide
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 // normal fade
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 // Get selected tab from the url
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 // Auto adjust height of the container
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 } // HELPER FUNCTIONS
555
556 }, {
557 key: "_keyNav",
558 value: function _keyNav(e) {
559 // Keyboard navigation
560 if ($.inArray(e.which, this.options.keyboardSettings.keyLeft) > -1) {
561 // left
562 this._showPrevious();
563
564 e.preventDefault();
565 } else if ($.inArray(e.which, this.options.keyboardSettings.keyRight) > -1) {
566 // right
567 this._showNext();
568
569 e.preventDefault();
570 } else {
571 return; // exit this handler for other keys
572 }
573 } // Auto progress
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 // Trigger an event
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 // Get tab number from url hash if available
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 } // PUBLIC FUNCTIONS
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 }(); // Wrapper for the plugin
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