foundation.tab.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*jslint unparam: true, browser: true, indent: 2 */
  2. ;(function ($, window, document, undefined) {
  3. 'use strict';
  4. Foundation.libs.tab = {
  5. name : 'tab',
  6. version : '5.1.1',
  7. settings : {
  8. active_class: 'active',
  9. callback : function () {}
  10. },
  11. init : function (scope, method, options) {
  12. this.bindings(method, options);
  13. },
  14. events : function () {
  15. var self = this,
  16. S = this.S;
  17. S(this.scope).off('.tab').on('click.fndtn.tab', '[' + this.attr_name() + '] > dd > a', function (e) {
  18. e.preventDefault();
  19. e.stopPropagation();
  20. var tab = S(this).parent(),
  21. tabs = tab.closest('[' + self.attr_name() + ']'),
  22. target = S('#' + this.href.split('#')[1]),
  23. siblings = tab.siblings(),
  24. settings = tabs.data(self.attr_name(true) + '-init');
  25. // allow usage of data-tab-content attribute instead of href
  26. if (S(this).data(self.data_attr('tab-content'))) {
  27. target = S('#' + S(this).data(self.data_attr('tab-content')).split('#')[1]);
  28. }
  29. tab.addClass(settings.active_class).triggerHandler('opened');
  30. siblings.removeClass(settings.active_class);
  31. target.siblings().removeClass(settings.active_class).end().addClass(settings.active_class);
  32. settings.callback(tab);
  33. tabs.triggerHandler('toggled', [tab]);
  34. });
  35. },
  36. data_attr: function (str) {
  37. if (this.namespace.length > 0) {
  38. return this.namespace + '-' + str;
  39. }
  40. return str;
  41. },
  42. off : function () {},
  43. reflow : function () {}
  44. };
  45. }(jQuery, this, this.document));