jquery.ui.scrolltabs.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*! jquery-ui-scrolltab | * v1.0.0 | * https://davidsekar.github.io/jQuery-UI-ScrollTab/ | * @license MIT */
  2. /// <reference path="../../node_modules/@types/jquery/index.d.ts" />
  3. /// <reference path="../../node_modules/@types/jqueryui/index.d.ts" />
  4. /// <reference path="../ts/jquery.ui.scrolltabs.d.ts" />
  5. (function ($) {
  6. $.widget('ui.scrollTabs', $.ui.tabs, {
  7. $ul: null,
  8. $leftArrowWrapper: null,
  9. $rightArrowWrapper: null,
  10. $scrollDiv: null,
  11. $navPrev: null,
  12. $navNext: null,
  13. $navFirst: null,
  14. $navLast: null,
  15. $innerWrapper: null,
  16. debounceEnabled: false,
  17. eventDelay: 250,
  18. sbarWidth: null,
  19. options: {
  20. scrollOptions: {
  21. closable: true,
  22. customGotoFirstHTML: '<button class="stNavFirstArrow ui-state-active" title="First">' +
  23. '<span class="ui-icon ui-icon-seek-first">First tab</span></button>',
  24. customMoveNextHTML: '<button class="stNavNextArrow ui-state-active" title="Next">' +
  25. '<span class="ui-icon ui-icon-seek-next">Next tab</span></button>',
  26. customGoToLastHTML: '<button class="stNavLastArrow ui-state-active" title="Last">' +
  27. '<span class="ui-icon ui-icon-seek-end">Last tab</span></button>',
  28. customMovePreviousHTML: '<button class="stNavPrevArrow ui-state-active" title="Previous">' +
  29. '<span class="ui-icon ui-icon-seek-prev">Previous tab</span></button>',
  30. easing: 'swing',
  31. enableDebug: false,
  32. headerHTML: '<div class="ui-widget-header ui-corner-all"/>',
  33. headerScrollHTML: '<div class="ui-scroll-tabs-view"/>',
  34. hideDefaultArrows: false,
  35. leftArrowWrapperHTML: '<div class="stNavMain stNavMainLeft"/>',
  36. loadLastTab: false,
  37. onTabScroll: function () {
  38. // empty
  39. },
  40. rightArrowWrapperHTML: '<div class="stNavMain stNavMainRight"/>',
  41. scrollSpeed: 500,
  42. selectTabAfterScroll: true,
  43. selectTabOnAdd: true,
  44. showFirstLastArrows: false,
  45. showNavWhenNeeded: true,
  46. wrapperCssClass: ''
  47. }
  48. },
  49. navigateOptions: {
  50. previous: 1,
  51. next: 2,
  52. first: 3,
  53. last: 4,
  54. mouseScroll: 5
  55. },
  56. _create: function () {
  57. var _this = this;
  58. var options = this.options;
  59. var $elem = this.element;
  60. this.$ul = $elem.find('ol,ul').eq(0).detach();
  61. /* Add custom markup */
  62. var $headerWrapper = $(this.options.scrollOptions.headerHTML);
  63. $headerWrapper.addClass('ui-scroll-tabs-header');
  64. $elem.prepend($headerWrapper);
  65. this.$innerWrapper = $(this.options.scrollOptions.headerScrollHTML);
  66. this.$innerWrapper.addClass('ui-scroll-tabs-view');
  67. $headerWrapper.append(this.$innerWrapper);
  68. this.$innerWrapper.append(this.$ul);
  69. /* End */
  70. /**
  71. * jQuery UI widget automatically adds the widget name to the triggered
  72. * events. So instead of 'tabsactivate' event, bind to 'scrolltabsactivate'.
  73. */
  74. this._on(this.element, {
  75. scrolltabsactivate: function (event, ui) {
  76. _this._animateToActiveTab(event);
  77. }
  78. });
  79. // Call the base
  80. this._super();
  81. this._setupUserOptions();
  82. this._debug(this.eventNamespace);
  83. },
  84. _findScrollbarWidth: function () {
  85. var parent;
  86. var child;
  87. if (this.sbarWidth === null) {
  88. var style = document.createElement('style');
  89. style.innerHTML = '.__sb-test::-webkit-scrollbar { width: 0px; }';
  90. document.body.appendChild(style);
  91. parent = $('<div class="__sb-test" style="width:50px;height:50px;overflow:auto;">' +
  92. '<div/></div>')
  93. .appendTo('body');
  94. child = parent.children();
  95. this.sbarWidth = child.innerWidth() - child.height(99).innerWidth();
  96. // clean
  97. parent.remove();
  98. document.body.removeChild(style);
  99. }
  100. },
  101. _setOption: function (key, value) {
  102. this._super(key, value);
  103. },
  104. _setOptions: function (options) {
  105. this._super(options);
  106. },
  107. _activate: function (index) {
  108. this._super(index);
  109. },
  110. _setupUserOptions: function () {
  111. var options = this.options.scrollOptions;
  112. this.debounceEnabled = $.debounce ? true : false;
  113. this._debug('isDebounceEnabled : ' + this.debounceEnabled);
  114. var $elem = this.element;
  115. $elem.addClass(options.wrapperCssClass + ' ui-scroll-tabs');
  116. },
  117. /**
  118. * Centrally control all message to be logged to the console
  119. * @param message -message to be displayed
  120. */
  121. _debug: function (message, isError) {
  122. if (this.options.scrollOptions.enableDebug) {
  123. if (isError === true) {
  124. console.error(message);
  125. }
  126. else {
  127. console.log(message);
  128. }
  129. }
  130. },
  131. /**
  132. * If debounce/throttle plugin is found, it debounces the event handler function
  133. * @param dbFunc the event handler function
  134. */
  135. _debounceEvent: function (dbFunc) {
  136. return this.debounceEnabled ? $.debounce(this.eventDelay, dbFunc) : dbFunc;
  137. },
  138. /**
  139. * If debounce/throttle plugin is found, it uses it in the event handler function
  140. * @param dbFunc the event handler function
  141. */
  142. _throttleEvent: function (dbFunc) {
  143. return this.debounceEnabled ? $.throttle(this.eventDelay, dbFunc) : dbFunc;
  144. },
  145. _bindMouseScroll: function () {
  146. var _this = this;
  147. if ($.isFunction($.fn.mousewheel)) {
  148. var self_1 = this;
  149. this._on(this.$scrollDiv, {
  150. mousewheel: function (event) {
  151. event.preventDefault();
  152. self_1._scrollWithoutSelection(_this.navigateOptions.mouseScroll, event.deltaY * event.deltaFactor * 3.5);
  153. self_1._debug(event.deltaX + ',' + event.deltaY + ',' + event.deltaFactor);
  154. }
  155. });
  156. }
  157. },
  158. /**
  159. * Initializes the navigation controls based on user settings
  160. */
  161. _setupNavControls: function () {
  162. var _this = this;
  163. this.$scrollDiv = this.$ul.parent();
  164. // Set the height of the UL
  165. // this.$scrollDiv.height(this.tabs.first().outerHeight());
  166. this.$leftArrowWrapper = $(this.options.scrollOptions.leftArrowWrapperHTML);
  167. this.$rightArrowWrapper = $(this.options.scrollOptions.rightArrowWrapperHTML);
  168. if (!this.options.scrollOptions.hideDefaultArrows) {
  169. this.$navPrev = $(this.options.scrollOptions.customMovePreviousHTML);
  170. this.$leftArrowWrapper.append(this.$navPrev);
  171. this.$navNext = $(this.options.scrollOptions.customMoveNextHTML);
  172. this.$rightArrowWrapper.append(this.$navNext);
  173. if (this.options.scrollOptions.showFirstLastArrows === true) {
  174. this.$navFirst = $(this.options.scrollOptions.customGotoFirstHTML);
  175. this.$leftArrowWrapper.prepend(this.$navFirst);
  176. this.$navLast = $(this.options.scrollOptions.customGoToLastHTML);
  177. this.$rightArrowWrapper.append(this.$navLast);
  178. }
  179. else {
  180. this.$navFirst = this.$navLast = $();
  181. }
  182. }
  183. this.$scrollDiv.before(this.$leftArrowWrapper);
  184. this.$scrollDiv.after(this.$rightArrowWrapper);
  185. this._addclosebutton();
  186. this._bindMouseScroll();
  187. // Triggers on the scroll end
  188. this._on(this.$scrollDiv, {
  189. scroll: this._debounceEvent(function () {
  190. _this._showNavsIfNeeded();
  191. })
  192. });
  193. },
  194. /**
  195. * Initializes all the controls and events required for scroll tabs
  196. */
  197. _init: function () {
  198. var _this = this;
  199. this._setupNavControls();
  200. this._showNavsIfNeeded();
  201. this._hideScrollBars();
  202. this._addNavEvents();
  203. this._on(window, {
  204. resize: this._debounceEvent(function () {
  205. _this._debug('resize: ' + _this.eventNamespace);
  206. _this._showNavsIfNeeded();
  207. })
  208. });
  209. },
  210. _hideScrollBars: function () {
  211. this._findScrollbarWidth();
  212. this.$innerWrapper.css('margin-bottom', -1 * this.sbarWidth);
  213. },
  214. /**
  215. * Check if navigation need then show; otherwise hide it
  216. */
  217. _showNavsIfNeeded: function () {
  218. if (this.options.scrollOptions.showNavWhenNeeded === false) {
  219. return; // do nothing
  220. }
  221. var showLeft = !(this.$scrollDiv.scrollLeft() <= 0);
  222. var showRight = !(Math.abs(this.$scrollDiv[0].scrollWidth - this.$scrollDiv.scrollLeft()
  223. - this.$scrollDiv.outerWidth()) < 1);
  224. if (this.options.scrollOptions.selectTabAfterScroll) {
  225. showLeft = !(this.options.active === 0);
  226. showRight = (this.options.active + 1 === this.tabs.length) ? false : true;
  227. }
  228. showLeft ? this.$leftArrowWrapper.addClass('stNavVisible')
  229. : this.$leftArrowWrapper.removeClass('stNavVisible');
  230. showRight ? this.$rightArrowWrapper.addClass('stNavVisible')
  231. : this.$rightArrowWrapper.removeClass('stNavVisible');
  232. this._debug('Validate showing nav controls');
  233. },
  234. _callBackFnc: function (fName, event, arg1) {
  235. if ($.isFunction(fName)) {
  236. fName(event, arg1);
  237. }
  238. },
  239. /**
  240. * returns the delta that should be added to current scroll to bring it into view
  241. * @param $tab tab that should be tested
  242. */
  243. _getScrollDeltaValue: function ($tab) {
  244. var leftPosition = $tab.position();
  245. var width = $tab.outerWidth();
  246. var currentScroll = this.$scrollDiv.scrollLeft();
  247. var currentVisibleWidth = this.$scrollDiv.width();
  248. var hiddenDirection = 0;
  249. var arrowsWidth = this.$leftArrowWrapper.width();
  250. // Check if the new tab is in view
  251. if (leftPosition.left < (currentScroll + arrowsWidth)) {
  252. hiddenDirection = leftPosition.left - currentScroll - arrowsWidth;
  253. }
  254. else if ((leftPosition.left + width + arrowsWidth) > (currentScroll + currentVisibleWidth)) {
  255. hiddenDirection = (leftPosition.left + width + arrowsWidth)
  256. - (currentScroll + currentVisibleWidth);
  257. }
  258. return hiddenDirection;
  259. },
  260. _scrollWithoutSelection: function (navOpt, sLeft) {
  261. var scrollLeft = this.$scrollDiv.scrollLeft();
  262. switch (navOpt) {
  263. case this.navigateOptions.first:
  264. scrollLeft = 0;
  265. break;
  266. case this.navigateOptions.last:
  267. scrollLeft = this.$scrollDiv[0].scrollWidth;
  268. break;
  269. case this.navigateOptions.previous:
  270. scrollLeft -= this.$scrollDiv.outerWidth() / 2;
  271. break;
  272. case this.navigateOptions.next:
  273. scrollLeft += this.$scrollDiv.outerWidth() / 2;
  274. break;
  275. case this.navigateOptions.mouseScroll:
  276. scrollLeft += sLeft;
  277. break;
  278. }
  279. if (scrollLeft < 0) {
  280. scrollLeft = 0;
  281. }
  282. this.$scrollDiv.stop().animate({ scrollLeft: scrollLeft }, this.options.scrollOptions.scrollSpeed / 2, this.options.scrollOptions.easing);
  283. },
  284. _animateToActiveTab: function (e) {
  285. var calculatedDelta = this._getScrollDeltaValue(this.active);
  286. this.$scrollDiv.stop().animate({ scrollLeft: this.$scrollDiv.scrollLeft() + calculatedDelta }, this.options.scrollOptions.scrollSpeed, this.options.scrollOptions.easing);
  287. this._showNavsIfNeeded();
  288. // trigger callback if defined
  289. e = (typeof e === 'undefined') ? null : e;
  290. this._callBackFnc(this.options.scrollOptions.onTabScroll, e, this.active);
  291. },
  292. /**
  293. * Return a new jQuery object for user provided selectors or else use the default ones
  294. * @param col if selector is provided by user, then override the existing controls
  295. * @param nav Nav control selector option prop name suffix
  296. */
  297. _getCustomNavSelector: function (col, nav) {
  298. var sel = this.options.scrollOptions['customNav' + nav] || '';
  299. // Check for custom selector
  300. if (typeof sel === 'string' && $.trim(sel) !== '') {
  301. col = col.add(sel);
  302. }
  303. return col;
  304. },
  305. /**
  306. * This function add the navigation control and binds the required events
  307. */
  308. _addNavEvents: function () {
  309. var _this = this;
  310. // Handle next tab
  311. this.$navNext = this.$navNext || $();
  312. this.$navNext = this._getCustomNavSelector(this.$navNext, 'Next');
  313. this._on(this.$navNext, {
  314. click: this._debounceEvent(function (e) { _this._moveToNextTab(e); })
  315. });
  316. // Handle previous tab
  317. this.$navPrev = this.$navPrev || $();
  318. this.$navPrev = this._getCustomNavSelector(this.$navPrev, 'Prev');
  319. this._on(this.$navPrev, {
  320. click: this._debounceEvent(function (e) { _this._moveToPrevTab(e); })
  321. });
  322. // Handle First tab
  323. this.$navFirst = this.$navFirst || $();
  324. this.$navFirst = this._getCustomNavSelector(this.$navFirst, 'First');
  325. this._on(this.$navFirst, {
  326. click: this._debounceEvent(function (e) { _this._moveToFirstTab(e); })
  327. });
  328. // Handle last tab
  329. this.$navLast = this.$navLast || $();
  330. this.$navLast = this._getCustomNavSelector(this.$navLast, 'Last');
  331. this._on(this.$navLast, {
  332. click: this._debounceEvent(function (e) { _this._moveToLastTab(e); })
  333. });
  334. },
  335. /**
  336. * Handles move to next tab link click
  337. * @param e pass the link click event
  338. */
  339. _moveToNextTab: function (e) {
  340. if (e) {
  341. e.preventDefault();
  342. }
  343. if (!this.options.scrollOptions.selectTabAfterScroll) {
  344. this._scrollWithoutSelection(this.navigateOptions.next);
  345. return;
  346. }
  347. this._activate(this._findNextTab(Math.max(0, this.options.active + 1), true));
  348. },
  349. /**
  350. * Handles move to previous tab link click
  351. * @param e pass the link click event
  352. */
  353. _moveToPrevTab: function (e) {
  354. if (e) {
  355. e.preventDefault();
  356. }
  357. if (!this.options.scrollOptions.selectTabAfterScroll) {
  358. this._scrollWithoutSelection(this.navigateOptions.previous);
  359. return;
  360. }
  361. this._activate(this._findNextTab(Math.max(0, this.options.active - 1), false));
  362. },
  363. /**
  364. * Handles move to first tab link click
  365. * @param e pass the link click event
  366. */
  367. _moveToFirstTab: function (e) {
  368. if (e) {
  369. e.preventDefault();
  370. }
  371. if (!this.options.scrollOptions.selectTabAfterScroll) {
  372. this._scrollWithoutSelection(this.navigateOptions.first);
  373. return;
  374. }
  375. this._activate(this._findNextTab(0, false));
  376. },
  377. /**
  378. * Handles move to last tab link click
  379. * @param e pass the link click event
  380. */
  381. _moveToLastTab: function (e) {
  382. if (e) {
  383. e.preventDefault();
  384. }
  385. if (!this.options.scrollOptions.selectTabAfterScroll) {
  386. this._scrollWithoutSelection(this.navigateOptions.last);
  387. return;
  388. }
  389. this._activate(this._findNextTab(this.tabs.length - 1, true));
  390. },
  391. _addclosebutton: function ($li) {
  392. var _this = this;
  393. if (this.options.scrollOptions.closable === false) {
  394. return;
  395. }
  396. // If li is provide than just add to that, otherwise add to all
  397. var lis = $li || this.tabs;
  398. var self = this;
  399. lis.each(function (idx, obj) {
  400. var $thisLi = $(obj).addClass('stHasCloseBtn');
  401. $thisLi.append($('<span class="ui-state-default ui-corner-all stCloseBtn">' +
  402. '<span class="ui-icon ui-icon-circle-close" title="Close this tab">Close</span>' +
  403. '</span>'));
  404. var closeButton = $thisLi.find('.stCloseBtn').hover(function () {
  405. $(this).toggleClass('ui-state-hover');
  406. });
  407. _this._on(closeButton, {
  408. click: function (e) {
  409. var removeIdx = self.tabs.index($thisLi);
  410. var selectTabIdx;
  411. selectTabIdx = -1;
  412. if (self.options.active === removeIdx) {
  413. var tabcount = self.tabs.length;
  414. var mid = Math.ceil(tabcount / 2);
  415. if (removeIdx > mid) {
  416. selectTabIdx = removeIdx - 1;
  417. }
  418. else {
  419. selectTabIdx = removeIdx;
  420. }
  421. }
  422. self.removeTab($thisLi.find('a.ui-tabs-anchor'));
  423. if (selectTabIdx > -1 && selectTabIdx !== removeIdx) {
  424. self._activate(selectTabIdx);
  425. }
  426. }
  427. });
  428. });
  429. },
  430. addTab: function (header, panelContent) {
  431. var newId = $({}).uniqueId()[0].id;
  432. var tab = $('<li><a href="#' + newId + '" role="tab">' + header + '</a></li>');
  433. var panel = this._createPanel(newId);
  434. panel.html(panelContent);
  435. this.$ul.append(tab);
  436. panel.attr('aria-live', 'polite');
  437. if (panel.length) {
  438. $(this.panels[this.panels.length - 1]).after(panel);
  439. }
  440. panel.attr('role', 'tabpanel');
  441. this._addclosebutton(tab);
  442. this.refresh();
  443. if (this.options.scrollOptions.selectTabOnAdd) {
  444. this._moveToLastTab();
  445. }
  446. this._showNavsIfNeeded();
  447. },
  448. removeTab: function (anc) {
  449. var tabId = anc.attr('href');
  450. // Remove the panel
  451. $(tabId).remove();
  452. // Remove the tab
  453. anc.closest('li').remove();
  454. // Refresh the tabs widget
  455. this.refresh();
  456. },
  457. _destroy: function () {
  458. this._super();
  459. /* Remove navigation controls */
  460. this.$leftArrowWrapper.remove();
  461. this.$rightArrowWrapper.remove();
  462. /* undo the close button */
  463. this.tabs.each(function (idx, element) {
  464. var self = $(element);
  465. self.removeClass('stHasCloseBtn');
  466. self.find('.stCloseBtn').remove();
  467. });
  468. /* Undo enhanced tab headers */
  469. var $headerWrapper = this.$ul.closest('.ui-scroll-tabs-header');
  470. this.$ul = this.$ul.detach();
  471. $headerWrapper.remove();
  472. this.element.prepend(this.$ul);
  473. /* Remove class from the base wrapper */
  474. this.element.removeClass(this.options.wrapperCssClass)
  475. .removeClass('ui-scroll-tabs');
  476. /* unsubscribe all events in namespace */
  477. // this._off(window, 'resize');
  478. $(window).off('resize' + this.eventNamespace);
  479. }
  480. });
  481. return $.ui.scrollTabs;
  482. })(jQuery);
  483. //# sourceMappingURL=jquery.ui.scrolltabs.js.map