PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/_examples/mvc2/assets/js/tabs.js

https://bitbucket.org/tumivn/phpexamples
JavaScript | 283 lines | 168 code | 68 blank | 47 comment | 31 complexity | 4ad1f53983efc5577711c39ad787264a MD5 | raw file
  1. /**
  2. * @license
  3. * jQuery Tools 1.2.5 Tabs- The basics of UI design.
  4. *
  5. * NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
  6. *
  7. * http://flowplayer.org/tools/tabs/
  8. *
  9. * Since: November 2008
  10. * Date: Wed Sep 22 06:02:10 2010 +0000
  11. */
  12. (function($) {
  13. // static constructs
  14. $.tools = $.tools || {version: '1.2.5'};
  15. $.tools.tabs = {
  16. conf: {
  17. tabs: 'a',
  18. current: 'current',
  19. onBeforeClick: null,
  20. onClick: null,
  21. effect: 'default',
  22. initialIndex: 0,
  23. event: 'click',
  24. rotate: false,
  25. // 1.2
  26. history: false
  27. },
  28. addEffect: function(name, fn) {
  29. effects[name] = fn;
  30. }
  31. };
  32. var effects = {
  33. // simple "toggle" effect
  34. 'default': function(i, done) {
  35. this.getPanes().hide().eq(i).show();
  36. done.call();
  37. },
  38. /*
  39. configuration:
  40. - fadeOutSpeed (positive value does "crossfading")
  41. - fadeInSpeed
  42. */
  43. fade: function(i, done) {
  44. var conf = this.getConf(),
  45. speed = conf.fadeOutSpeed,
  46. panes = this.getPanes();
  47. if (speed) {
  48. panes.fadeOut(speed);
  49. } else {
  50. panes.hide();
  51. }
  52. panes.eq(i).fadeIn(conf.fadeInSpeed, done);
  53. },
  54. // for basic accordions
  55. slide: function(i, done) {
  56. this.getPanes().slideUp(200);
  57. this.getPanes().eq(i).slideDown(400, done);
  58. },
  59. /**
  60. * AJAX effect
  61. */
  62. ajax: function(i, done) {
  63. this.getPanes().eq(0).load(this.getTabs().eq(i).attr("href"), done);
  64. }
  65. };
  66. var w;
  67. /**
  68. * Horizontal accordion
  69. *
  70. * @deprecated will be replaced with a more robust implementation
  71. */
  72. $.tools.tabs.addEffect("horizontal", function(i, done) {
  73. // store original width of a pane into memory
  74. if (!w) { w = this.getPanes().eq(0).width(); }
  75. // set current pane's width to zero
  76. this.getCurrentPane().animate({width: 0}, function() { $(this).hide(); });
  77. // grow opened pane to it's original width
  78. this.getPanes().eq(i).animate({width: w}, function() {
  79. $(this).show();
  80. done.call();
  81. });
  82. });
  83. function Tabs(root, paneSelector, conf) {
  84. var self = this,
  85. trigger = root.add(this),
  86. tabs = root.find(conf.tabs),
  87. panes = paneSelector.jquery ? paneSelector : root.children(paneSelector),
  88. current;
  89. // make sure tabs and panes are found
  90. if (!tabs.length) { tabs = root.children(); }
  91. if (!panes.length) { panes = root.parent().find(paneSelector); }
  92. if (!panes.length) { panes = $(paneSelector); }
  93. // public methods
  94. $.extend(this, {
  95. click: function(i, e) {
  96. var tab = tabs.eq(i);
  97. if (typeof i == 'string' && i.replace("#", "")) {
  98. tab = tabs.filter("[href*=" + i.replace("#", "") + "]");
  99. i = Math.max(tabs.index(tab), 0);
  100. }
  101. if (conf.rotate) {
  102. var last = tabs.length -1;
  103. if (i < 0) { return self.click(last, e); }
  104. if (i > last) { return self.click(0, e); }
  105. }
  106. if (!tab.length) {
  107. if (current >= 0) { return self; }
  108. i = conf.initialIndex;
  109. tab = tabs.eq(i);
  110. }
  111. // current tab is being clicked
  112. if (i === current) { return self; }
  113. // possibility to cancel click action
  114. e = e || $.Event();
  115. e.type = "onBeforeClick";
  116. trigger.trigger(e, [i]);
  117. if (e.isDefaultPrevented()) { return; }
  118. // call the effect
  119. effects[conf.effect].call(self, i, function() {
  120. // onClick callback
  121. e.type = "onClick";
  122. trigger.trigger(e, [i]);
  123. });
  124. // default behaviour
  125. current = i;
  126. tabs.removeClass(conf.current);
  127. tab.addClass(conf.current);
  128. return self;
  129. },
  130. getConf: function() {
  131. return conf;
  132. },
  133. getTabs: function() {
  134. return tabs;
  135. },
  136. getPanes: function() {
  137. return panes;
  138. },
  139. getCurrentPane: function() {
  140. return panes.eq(current);
  141. },
  142. getCurrentTab: function() {
  143. return tabs.eq(current);
  144. },
  145. getIndex: function() {
  146. return current;
  147. },
  148. next: function() {
  149. return self.click(current + 1);
  150. },
  151. prev: function() {
  152. return self.click(current - 1);
  153. },
  154. destroy: function() {
  155. tabs.unbind(conf.event).removeClass(conf.current);
  156. panes.find("a[href^=#]").unbind("click.T");
  157. return self;
  158. }
  159. });
  160. // callbacks
  161. $.each("onBeforeClick,onClick".split(","), function(i, name) {
  162. // configuration
  163. if ($.isFunction(conf[name])) {
  164. $(self).bind(name, conf[name]);
  165. }
  166. // API
  167. self[name] = function(fn) {
  168. if (fn) { $(self).bind(name, fn); }
  169. return self;
  170. };
  171. });
  172. if (conf.history && $.fn.history) {
  173. $.tools.history.init(tabs);
  174. conf.event = 'history';
  175. }
  176. // setup click actions for each tab
  177. tabs.each(function(i) {
  178. $(this).bind(conf.event, function(e) {
  179. self.click(i, e);
  180. return e.preventDefault();
  181. });
  182. });
  183. // cross tab anchor link
  184. panes.find("a[href^=#]").bind("click.T", function(e) {
  185. self.click($(this).attr("href"), e);
  186. });
  187. // open initial tab
  188. if (location.hash && conf.tabs == "a" && root.find("[href=" +location.hash+ "]").length) {
  189. self.click(location.hash);
  190. } else {
  191. if (conf.initialIndex === 0 || conf.initialIndex > 0) {
  192. self.click(conf.initialIndex);
  193. }
  194. }
  195. }
  196. // jQuery plugin implementation
  197. $.fn.tabs = function(paneSelector, conf) {
  198. // return existing instance
  199. var el = this.data("tabs");
  200. if (el) {
  201. el.destroy();
  202. this.removeData("tabs");
  203. }
  204. if ($.isFunction(conf)) {
  205. conf = {onBeforeClick: conf};
  206. }
  207. // setup conf
  208. conf = $.extend({}, $.tools.tabs.conf, conf);
  209. this.each(function() {
  210. el = new Tabs($(this), paneSelector, conf);
  211. $(this).data("tabs", el);
  212. });
  213. return conf.api ? el: this;
  214. };
  215. }) (jQuery);