PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/EspritApp/FrontBundle/Resources/public/js/jquery.accordion.js

https://gitlab.com/Daldoul/tft
JavaScript | 263 lines | 175 code | 35 blank | 53 comment | 39 complexity | cc526ab48ca4bff6f012c5409d7cc50d MD5 | raw file
  1. /**
  2. * Accordion, jQuery Plugin
  3. *
  4. * This plugin provides an accordion with cookie support.
  5. *
  6. * Copyright (c) 2011 John Snyder (snyderplace.com)
  7. * @license http://www.snyderplace.com/accordion/license.txt New BSD
  8. * @version 1.1
  9. */
  10. (function($) {
  11. $.fn.accordion = function(options) {
  12. //firewalling
  13. if (!this || this.length < 1) {
  14. return this;
  15. }
  16. initialize(this, options);
  17. };
  18. //create the initial accordion
  19. function initialize(obj, options) {
  20. //build main options before element iteration
  21. var opts = $.extend({}, $.fn.accordion.defaults, options);
  22. //store any opened default values to set cookie later
  23. var opened = '';
  24. //iterate each matched object, bind, and open/close
  25. obj.each(function() {
  26. var $this = $(this);
  27. saveOpts($this, opts);
  28. //bind it to the event
  29. if (opts.bind == 'mouseenter') {
  30. $this.bind('mouseenter', function(e) {
  31. e.preventDefault();
  32. toggle($this, opts);
  33. });
  34. }
  35. //bind it to the event
  36. if (opts.bind == 'mouseover') {
  37. $this.bind('mouseover',function(e) {
  38. e.preventDefault();
  39. toggle($this, opts);
  40. });
  41. }
  42. //bind it to the event
  43. if (opts.bind == 'click') {
  44. $this.bind('click', function(e) {
  45. e.preventDefault();
  46. toggle($this, opts);
  47. });
  48. }
  49. //bind it to the event
  50. if (opts.bind == 'dblclick') {
  51. $this.bind('dblclick', function(e) {
  52. e.preventDefault();
  53. toggle($this, opts);
  54. });
  55. }
  56. //initialize the panels
  57. //get the id for this element
  58. id = $this.attr('id');
  59. //if not using cookies, open defaults
  60. if (!useCookies(opts)) {
  61. //close it if not defaulted to open
  62. if (id != opts.defaultOpen) {
  63. $this.addClass(opts.cssClose);
  64. opts.loadClose($this, opts);
  65. } else { //its a default open, open it
  66. $this.addClass(opts.cssOpen);
  67. opts.loadOpen($this, opts);
  68. opened = id;
  69. }
  70. } else { //can use cookies, use them now
  71. //has a cookie been set, this overrides default open
  72. if (issetCookie(opts)) {
  73. if (inCookie(id, opts) === false) {
  74. $this.addClass(opts.cssClose);
  75. opts.loadClose($this, opts);
  76. } else {
  77. $this.addClass(opts.cssOpen);
  78. opts.loadOpen($this, opts);
  79. opened = id;
  80. }
  81. } else { //a cookie hasn't been set open defaults
  82. if (id != opts.defaultOpen) {
  83. $this.addClass(opts.cssClose);
  84. opts.loadClose($this, opts);
  85. } else { //its a default open, open it
  86. $this.addClass(opts.cssOpen);
  87. opts.loadOpen($this, opts);
  88. opened = id;
  89. }
  90. }
  91. }
  92. });
  93. //now that the loop is done, set the cookie
  94. if (opened.length > 0 && useCookies(opts)) {
  95. setCookie(opened, opts);
  96. } else { //there are none open, set cookie
  97. setCookie('', opts);
  98. }
  99. return obj;
  100. };
  101. //load opts from object
  102. function loadOpts($this) {
  103. return $this.data('accordion-opts');
  104. }
  105. //save opts into object
  106. function saveOpts($this, opts) {
  107. return $this.data('accordion-opts', opts);
  108. }
  109. //hides a accordion panel
  110. function close(opts) {
  111. opened = $(document).find('.' + opts.cssOpen);
  112. $.each(opened, function() {
  113. //give the proper class to the linked element
  114. $(this).addClass(opts.cssClose).removeClass(opts.cssOpen);
  115. opts.animateClose($(this), opts);
  116. });
  117. }
  118. //opens a accordion panel
  119. function open($this, opts) {
  120. close(opts);
  121. //give the proper class to the linked element
  122. $this.removeClass(opts.cssClose).addClass(opts.cssOpen);
  123. //open the element
  124. opts.animateOpen($this, opts);
  125. //do cookies if plugin available
  126. if (useCookies(opts)) {
  127. // split the cookieOpen string by ","
  128. id = $this.attr('id');
  129. setCookie(id, opts);
  130. }
  131. }
  132. //toggle a accordion on an event
  133. function toggle($this, opts) {
  134. // close the only open item
  135. if ($this.hasClass(opts.cssOpen))
  136. {
  137. close(opts);
  138. //do cookies if plugin available
  139. if (useCookies(opts)) {
  140. // split the cookieOpen string by ","
  141. setCookie('', opts);
  142. }
  143. return false;
  144. }
  145. close(opts);
  146. //open a closed element
  147. open($this, opts);
  148. return false;
  149. }
  150. //use cookies?
  151. function useCookies(opts) {
  152. //return false if cookie plugin not present or if a cookie name is not provided
  153. if (!$.cookie || opts.cookieName == '') {
  154. return false;
  155. }
  156. //we can use cookies
  157. return true;
  158. }
  159. //set a cookie
  160. function setCookie(value, opts)
  161. {
  162. //can use the cookie plugin
  163. if (!useCookies(opts)) { //no, quit here
  164. return false;
  165. }
  166. //cookie plugin is available, lets set the cookie
  167. $.cookie(opts.cookieName, value, opts.cookieOptions);
  168. }
  169. //check if a accordion is in the cookie
  170. function inCookie(value, opts)
  171. {
  172. //can use the cookie plugin
  173. if (!useCookies(opts)) {
  174. return false;
  175. }
  176. //if its not there we don't need to remove from it
  177. if (!issetCookie(opts)) { //quit here, don't have a cookie
  178. return false;
  179. }
  180. //unescape it
  181. cookie = unescape($.cookie(opts.cookieName));
  182. //is this value in the cookie arrray
  183. if (cookie != value) { //no, quit here
  184. return false;
  185. }
  186. return true;
  187. }
  188. //check if a cookie is set
  189. function issetCookie(opts)
  190. {
  191. //can we use the cookie plugin
  192. if (!useCookies(opts)) { //no, quit here
  193. return false;
  194. }
  195. //is the cookie set
  196. if ($.cookie(opts.cookieName) == null) { //no, quit here
  197. return false;
  198. }
  199. return true;
  200. }
  201. // settings
  202. $.fn.accordion.defaults = {
  203. cssClose: 'accordion-close', //class you want to assign to a closed accordion header
  204. cssOpen: 'accordion-open', //class you want to assign an opened accordion header
  205. cookieName: 'accordion', //name of the cookie you want to set for this accordion
  206. cookieOptions: { //cookie options, see cookie plugin for details
  207. path: '/',
  208. expires: 7,
  209. domain: '',
  210. secure: ''
  211. },
  212. defaultOpen: '', //id that you want opened by default
  213. speed: 'slow', //speed of the slide effect
  214. bind: 'click', //event to bind to, supports click, dblclick, mouseover and mouseenter
  215. animateOpen: function (elem, opts) { //replace the standard slideDown with custom function
  216. elem.next().stop(true, true).slideDown(opts.speed);
  217. },
  218. animateClose: function (elem, opts) { //replace the standard slideUp with custom function
  219. elem.next().stop(true, true).slideUp(opts.speed);
  220. },
  221. loadOpen: function (elem, opts) { //replace the default open state with custom function
  222. elem.next().show();
  223. },
  224. loadClose: function (elem, opts) { //replace the default close state with custom function
  225. elem.next().hide();
  226. }
  227. };
  228. })(jQuery);