PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/priv/public/js/tools.tabs.js

http://github.com/membase/ns_server
JavaScript | 285 lines | 172 code | 70 blank | 43 comment | 27 complexity | a54d536acf5861bf57733d019a2e5575 MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. /**
  2. * tools.tabs 1.0.4 - Tabs done right.
  3. *
  4. * Copyright (c) 2009 Tero Piirainen
  5. * http://flowplayer.org/tools/tabs.html
  6. *
  7. * Dual licensed under MIT and GPL 2+ licenses
  8. * http://www.opensource.org/licenses
  9. *
  10. * Launch : November 2008
  11. * Date: ${date}
  12. * Revision: ${revision}
  13. */
  14. (function($) {
  15. // static constructs
  16. $.tools = $.tools || {};
  17. $.tools.tabs = {
  18. version: '1.0.4',
  19. conf: {
  20. tabs: 'a',
  21. current: 'current',
  22. onBeforeClick: null,
  23. onClick: null,
  24. effect: 'default',
  25. initialIndex: 0,
  26. event: 'click',
  27. api:false,
  28. rotate: false
  29. },
  30. addEffect: function(name, fn) {
  31. effects[name] = fn;
  32. }
  33. };
  34. var effects = {
  35. // simple "toggle" effect
  36. 'default': function(i, done) {
  37. this.getPanes().hide().eq(i).show();
  38. done.call();
  39. },
  40. /*
  41. configuration:
  42. - fadeOutSpeed (positive value does "crossfading")
  43. - fadeInSpeed
  44. */
  45. fade: function(i, done) {
  46. var conf = this.getConf(),
  47. speed = conf.fadeOutSpeed,
  48. panes = this.getPanes();
  49. if (speed) {
  50. panes.fadeOut(speed);
  51. } else {
  52. panes.hide();
  53. }
  54. panes.eq(i).fadeIn(conf.fadeInSpeed, done);
  55. },
  56. // for basic accordions
  57. slide: function(i, done) {
  58. this.getPanes().slideUp(200);
  59. this.getPanes().eq(i).slideDown(400, done);
  60. },
  61. // simple AJAX effect
  62. ajax: function(i, done) {
  63. this.getPanes().eq(0).load(this.getTabs().eq(i).attr("href"), done);
  64. }
  65. };
  66. var w;
  67. // this is how you add effects
  68. $.tools.tabs.addEffect("horizontal", function(i, done) {
  69. // store original width of a pane into memory
  70. if (!w) { w = this.getPanes().eq(0).width(); }
  71. // set current pane's width to zero
  72. this.getCurrentPane().animate({width: 0}, function() { $(this).hide(); });
  73. // grow opened pane to it's original width
  74. this.getPanes().eq(i).animate({width: w}, function() {
  75. $(this).show();
  76. done.call();
  77. });
  78. });
  79. function Tabs(tabs, panes, conf) {
  80. var self = this, $self = $(this), current;
  81. // bind all callbacks from configuration
  82. $.each(conf, function(name, fn) {
  83. if ($.isFunction(fn)) { $self.bind(name, fn); }
  84. });
  85. // public methods
  86. $.extend(this, {
  87. click: function(i, e) {
  88. var pane = self.getCurrentPane();
  89. var tab = tabs.eq(i);
  90. if (typeof i == 'string' && i.replace("#", "")) {
  91. tab = tabs.filter("[href*=\"" + i.replace("#", "") + "\"]");
  92. i = Math.max(tabs.index(tab), 0);
  93. }
  94. if (conf.rotate) {
  95. var last = tabs.length -1;
  96. if (i < 0) { return self.click(last, e); }
  97. if (i > last) { return self.click(0, e); }
  98. }
  99. if (!tab.length) {
  100. if (current >= 0) { return self; }
  101. i = conf.initialIndex;
  102. tab = tabs.eq(i);
  103. }
  104. // current tab is being clicked
  105. if (i === current) { return self; }
  106. // possibility to cancel click action
  107. e = e || $.Event();
  108. e.type = "onBeforeClick";
  109. $self.trigger(e, [i]);
  110. if (e.isDefaultPrevented()) { return; }
  111. // call the effect
  112. effects[conf.effect].call(self, i, function() {
  113. // onClick callback
  114. e.type = "onClick";
  115. $self.trigger(e, [i]);
  116. });
  117. // onStart
  118. e.type = "onStart";
  119. $self.trigger(e, [i]);
  120. if (e.isDefaultPrevented()) { return; }
  121. // default behaviour
  122. current = i;
  123. tabs.removeClass(conf.current);
  124. tab.addClass(conf.current);
  125. return self;
  126. },
  127. getConf: function() {
  128. return conf;
  129. },
  130. getTabs: function() {
  131. return tabs;
  132. },
  133. getPanes: function() {
  134. return panes;
  135. },
  136. getCurrentPane: function() {
  137. return panes.eq(current);
  138. },
  139. getCurrentTab: function() {
  140. return tabs.eq(current);
  141. },
  142. getIndex: function() {
  143. return current;
  144. },
  145. next: function() {
  146. return self.click(current + 1);
  147. },
  148. prev: function() {
  149. return self.click(current - 1);
  150. },
  151. bind: function(name, fn) {
  152. $self.bind(name, fn);
  153. return self;
  154. },
  155. onBeforeClick: function(fn) {
  156. return this.bind("onBeforeClick", fn);
  157. },
  158. onClick: function(fn) {
  159. return this.bind("onClick", fn);
  160. },
  161. unbind: function(name) {
  162. $self.unbind(name);
  163. return self;
  164. }
  165. });
  166. // setup click actions for each tab
  167. tabs.each(function(i) {
  168. $(this).bind(conf.event, function(e) {
  169. self.click(i, e);
  170. return false;
  171. });
  172. });
  173. // if no pane is visible --> click on the first tab
  174. if (location.hash) {
  175. self.click(location.hash);
  176. } else {
  177. if (conf.initialIndex === 0 || conf.initialIndex > 0) {
  178. self.click(conf.initialIndex);
  179. }
  180. }
  181. // cross tab anchor link
  182. panes.find("a[href^=#]").click(function(e) {
  183. self.click($(this).attr("href"), e);
  184. });
  185. }
  186. // jQuery plugin implementation
  187. $.fn.tabs = function(query, conf) {
  188. // return existing instance
  189. var el = this.eq(typeof conf == 'number' ? conf : 0).data("tabs");
  190. if (el) { return el; }
  191. if ($.isFunction(conf)) {
  192. conf = {onBeforeClick: conf};
  193. }
  194. // setup options
  195. var globals = $.extend({}, $.tools.tabs.conf), len = this.length;
  196. conf = $.extend(globals, conf);
  197. // install tabs for each items in jQuery
  198. this.each(function(i) {
  199. var root = $(this);
  200. // find tabs
  201. var els = root.find(conf.tabs);
  202. if (!els.length) {
  203. els = root.children();
  204. }
  205. // find panes
  206. var panes = query.jquery ? query : root.children(query);
  207. if (!panes.length) {
  208. panes = len == 1 ? $(query) : root.parent().find(query);
  209. }
  210. el = new Tabs(els, panes, conf);
  211. root.data("tabs", el);
  212. });
  213. return conf.api ? el: this;
  214. };
  215. }) (jQuery);