jquery.multipurpose_tabcontent.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. (function($) {
  2. //Attach this new method to jQuery
  3. $.fn.extend({
  4. //This is where you write your plugin's name
  5. champ: function(options) {
  6. //Iterate over the current set of matched elements
  7. var defaults = {
  8. selector: "tab_wrapper",
  9. plugin_type: "tab",
  10. side: "",
  11. //responsive: "true",
  12. active_tab: "1",
  13. controllers: "false",
  14. ajax: "false",
  15. multiple_tabs: "false",
  16. show_ajax_content_in_tab: "false",
  17. content_path: "false"
  18. }
  19. var obj = $.extend(defaults, options);
  20. var li_rel = 1,
  21. div_rel = 1;
  22. return this.each(function() {
  23. var plugin_type = obj.plugin_type;
  24. var side = obj.side;
  25. var active_tab = obj.active_tab;
  26. var controllers = obj.controllers;
  27. //var responsive = obj.responsive;
  28. var multiple_tabs = obj.multiple_tabs;
  29. var ajax = obj.ajax;
  30. var show_ajax_content_in_tab = obj.show_ajax_content_in_tab;
  31. var content_path = obj.content_path;
  32. var tab_content_selector = $(this).find(" > div > div.tab_content");
  33. var tab_selector = $(this).find(" >ul li");
  34. var parent = $(this);
  35. var controller_parent = $(".controller").closest(".tab_wrapper");
  36. var con_siblings;
  37. if (side != "") {
  38. parent.addClass(side + "_side");
  39. }
  40. if (controllers == "true") {
  41. parent.addClass("withControls");
  42. parent.append("<div class='controller'><span class='previous'>previous</span><span class='next'>next</span></div>");
  43. }
  44. if (plugin_type == "accordion") {
  45. //alert(34);
  46. parent.addClass("accordion");
  47. parent.removeClass(side + "_side");
  48. parent.removeClass("withControls");
  49. $(".controller", parent).remove();
  50. }
  51. if (ajax == "true") {
  52. $.ajax({
  53. url: content_path,
  54. success: function(result) {
  55. $(" .tab_content.tab_" + show_ajax_content_in_tab, parent).html(result);
  56. }
  57. });
  58. $(document).ajaxError(function(event, request, settings) {
  59. $(" .tab_content.tab_" + show_ajax_content_in_tab, parent).prepend("<h4 class='error'>Error requesting page " + settings.url + "</h2>");
  60. });
  61. }
  62. function hide_controls(parent) {
  63. if (parent.find(" >ul li:eq(0)").hasClass("active")) {
  64. $(".controller .previous", parent).hide();
  65. } else {
  66. $(".controller .previous", parent).show();
  67. }
  68. if (parent.find(" >ul li").last().hasClass("active")) {
  69. $(".controller .next", parent).hide();
  70. } else {
  71. $(".controller .next", parent).show();
  72. }
  73. }
  74. $(".controller .previous", $(this)).click(function() {
  75. con_siblings = $(this).closest(".controller");
  76. con_siblings.siblings("ul").find("li.active").prev().trigger("click");
  77. hide_controls(controller_parent);
  78. });
  79. $(".controller .next", $(this)).click(function() {
  80. con_siblings = $(this).closest(".controller");
  81. con_siblings.siblings("ul").find("li.active").next().trigger("click");
  82. hide_controls(controller_parent);
  83. });
  84. $(this).find(" >ul li").removeClass("active");
  85. $(this).find(" > div > div.tab_content").removeClass("active");
  86. if (active_tab == "") {
  87. $(this).find(" >ul li:eq(0)").addClass("active").show(); //set active tab on load
  88. $(this).find(" > div > div.tab_content:eq(0)").addClass("active").show(); //set active tab on load
  89. hide_controls(parent);
  90. } else {
  91. $(this).find(" >ul li:eq(" + (active_tab - 1) + ")").addClass("active").show(); //set active tab on load
  92. $(this).find(" > div > div.tab_content:eq(" + (active_tab - 1) + ")").addClass("active").show(); //set active tab on load
  93. hide_controls(parent);
  94. }
  95. tab_content_selector.first().addClass("first");
  96. tab_content_selector.last().addClass("last");
  97. // add class to content div
  98. tab_content_selector.each(function() {
  99. var tab_count = $(this).parents(".tab_wrapper").length;
  100. var add_relation = "tab_" + tab_count + "_" + div_rel;
  101. //var add_relation = "tab_" + div_rel;
  102. $(this).addClass(add_relation);
  103. $(this).attr("title", add_relation);
  104. div_rel++;
  105. });
  106. if (multiple_tabs == "true") {
  107. var get_parent = $(this).closest(".tab_wrapper");
  108. var active_tab_text = $(this).find(" >ul li:eq(0)").text();
  109. get_parent.addClass("show-as-dropdown");
  110. get_parent.prepend("<div class='active_tab'><span class='text'>" + active_tab_text + "</span><span class='arrow'></span></div>");
  111. }
  112. $(".active_tab").click(function() {
  113. $(this).next().stop(true, true).slideToggle();
  114. });
  115. // add relation attr to li and generate accordion header for mobile
  116. //if (responsive == "true") {
  117. tab_selector.each(function() {
  118. var tab_count = $(this).parents(".tab_wrapper").length;
  119. var add_relation = "tab_" + tab_count + "_" + li_rel;
  120. var accordian_header = $(this).text();
  121. //var add_relation = "tab_" + li_rel;
  122. var get_parent = $(this).closest(".tab_wrapper");
  123. $(this).attr("rel", add_relation);
  124. var current_tab_class = $(this).attr("class");
  125. tab_content_selector.each(function() {
  126. if ($(this).hasClass(add_relation)) {
  127. get_parent.find(" > div > div.tab_content." + add_relation).before("<div title='" + add_relation + "' class='accordian_header " + add_relation + ' ' + current_tab_class + "'>" + accordian_header + "<span class='arrow'></span></div>");
  128. }
  129. });
  130. li_rel++;
  131. });
  132. // }
  133. // on click of accordion header slideUp/SlideDown respective content
  134. $(".accordian_header").click(function() {
  135. var clicked_header = $(this).attr("title");
  136. var content_status = $(this).next(".tab_content").css("display");
  137. var get_closest_parent = $(this).closest(".tab_wrapper");
  138. if (content_status == "none") {
  139. get_closest_parent.find(">.content_wrapper >.accordian_header").removeClass("active");
  140. $(this).addClass("active");
  141. get_closest_parent.find(">ul >li").removeClass("active");
  142. get_closest_parent.find(">ul >li[rel='" + clicked_header + "']").addClass("active");
  143. tab_content_selector.removeClass("active").stop(true, true).slideUp();
  144. get_closest_parent.find(" > div > div.tab_content." + clicked_header).addClass("active").stop(true, true).slideDown();
  145. } else {
  146. get_closest_parent.find(">.content_wrapper >.accordian_header").removeClass("active");
  147. $(this).removeClass("active");
  148. get_closest_parent.find(">ul >li").removeClass("active");
  149. get_closest_parent.find(" > div > div.tab_content." + clicked_header).removeClass("active").stop(true, true).slideUp();
  150. }
  151. });
  152. // on click of tab hide/show respective content
  153. tab_selector.click(function() {
  154. var clicked_tab = $(this).attr("rel");
  155. var get_new_closest_parent = $(this).closest(".tab_wrapper");
  156. var get_closest_tab_list = $(this).closest(".tab_list");
  157. get_closest_tab_list.next(".content_wrapper").find(" >.accordian_header").removeClass("active");
  158. get_new_closest_parent.find(".accordian_header." + clicked_tab).addClass("active");
  159. tab_content_selector.removeClass("active").hide();
  160. get_new_closest_parent.find(" > div > div.tab_content." + clicked_tab).addClass("active").show();
  161. tab_selector.removeClass("active");
  162. $(this).addClass("active");
  163. hide_controls(get_new_closest_parent);
  164. var winWidth = $(window).width();
  165. if (multiple_tabs == "true") {
  166. if ($(this).parent(".tab_list").parent(".show-as-dropdown")) {
  167. var selected_tab_text = $(this).text();
  168. $(".active_tab .text").text(selected_tab_text);
  169. }
  170. if (winWidth <= 768) {
  171. $(this).closest(".tab_list").stop(true, true).slideUp();
  172. }
  173. }
  174. });
  175. });
  176. }
  177. });
  178. })(jQuery);