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 13 //firewalling 14 if (!this || this.length < 1) { 15 return this; 16 } 17 18 initialize(this, options); 19 20 }; 21 22 //create the initial accordion 23 function initialize(obj, options) { 24 25 //build main options before element iteration 26 var opts = $.extend({}, $.fn.accordion.defaults, options); 27 28 //store any opened default values to set cookie later 29 var opened = ''; 30 31 //iterate each matched object, bind, and open/close 32 obj.each(function() { 33 var $this = $(this); 34 saveOpts($this, opts); 35 36 //bind it to the event 37 if (opts.bind == 'mouseenter') { 38 $this.bind('mouseenter', function(e) { 39 e.preventDefault(); 40 toggle($this, opts); 41 }); 42 } 43 44 //bind it to the event 45 if (opts.bind == 'mouseover') { 46 $this.bind('mouseover', function(e) { 47 e.preventDefault(); 48 toggle($this, opts); 49 }); 50 } 51 52 //bind it to the event 53 if (opts.bind == 'click') { 54 $this.bind('click', function(e) { 55 e.preventDefault(); 56 toggle($this, opts); 57 }); 58 } 59 60 //bind it to the event 61 if (opts.bind == 'dblclick') { 62 $this.bind('dblclick', function(e) { 63 e.preventDefault(); 64 toggle($this, opts); 65 }); 66 } 67 68 //initialize the panels 69 //get the id for this element 70 var id = $this.attr('id'); 71 72 //if not using cookies, open defaults 73 if (!useCookies(opts)) { 74 //close it if not defaulted to open 75 if (id != opts.defaultOpen) { 76 $this.addClass(opts.cssClose); 77 opts.loadClose($this, opts); 78 } else { //its a default open, open it 79 $this.addClass(opts.cssOpen); 80 opts.loadOpen($this, opts); 81 opened = id; 82 } 83 } else { //can use cookies, use them now 84 //has a cookie been set, this overrides default open 85 if (issetCookie(opts)) { 86 if (inCookie(id, opts) === false) { 87 $this.addClass(opts.cssClose); 88 opts.loadClose($this, opts); 89 } else { 90 $this.addClass(opts.cssOpen); 91 opts.loadOpen($this, opts); 92 opened = id; 93 } 94 } else { //a cookie hasn't been set open defaults 95 if (id != opts.defaultOpen) { 96 $this.addClass(opts.cssClose); 97 opts.loadClose($this, opts); 98 } else { //its a default open, open it 99 $this.addClass(opts.cssOpen); 100 opts.loadOpen($this, opts); 101 opened = id; 102 } 103 } 104 } 105 }); 106 107 //now that the loop is done, set the cookie 108 if (opened.length > 0 && useCookies(opts)) { 109 setCookie(opened, opts); 110 } else { //there are none open, set cookie 111 setCookie('', opts); 112 } 113 114 return obj; 115 }; 116 117 //load opts from object 118 function loadOpts($this) { 119 return $this.data('accordion-opts'); 120 } 121 122 //save opts into object 123 function saveOpts($this, opts) { 124 return $this.data('accordion-opts', opts); 125 } 126 127 //hides a accordion panel 128 function close(opts) { 129 opened = $(document).find('.' + opts.cssOpen); 130 $.each(opened, function() { 131 //give the proper class to the linked element 132 $(this).addClass(opts.cssClose).removeClass(opts.cssOpen); 133 opts.animateClose($(this), opts); 134 }); 135 } 136 137 //opens a accordion panel 138 function open($this, opts) { 139 close(opts); 140 //give the proper class to the linked element 141 $this.removeClass(opts.cssClose).addClass(opts.cssOpen); 142 143 //open the element 144 opts.animateOpen($this, opts); 145 146 //do cookies if plugin available 147 if (useCookies(opts)) { 148 // split the cookieOpen string by "," 149 id = $this.attr('id');
150 setCookie(id, opts); 151 } 152 } 153 154 //toggle a accordion on an event 155 function toggle($this, opts) { 156 // close the only open item 157 if ($this.hasClass(opts.cssOpen)) { 158 close(opts); 159 //do cookies if plugin available 160 if (useCookies(opts)) { 161 // split the cookieOpen string by "," 162 setCookie('', opts); 163 } 164 return false; 165 } 166 close(opts); 167 //open a closed element 168 open($this, opts); 169 return false; 170 } 171 172 //use cookies? 173 function useCookies(opts) { 174 //return false if cookie plugin not present or if a cookie name is not provided 175 if (!$.cookie || opts.cookieName == '') { 176 return false; 177 } 178 179 //we can use cookies 180 return true; 181 } 182 183 //set a cookie 184 function setCookie(value, opts) { 185 //can use the cookie plugin 186 if (!useCookies(opts)) { //no, quit here 187 return false; 188 } 189 190 //cookie plugin is available, lets set the cookie 191 $.cookie(opts.cookieName, value, opts.cookieOptions); 192 } 193 194 //check if a accordion is in the cookie 195 function inCookie(value, opts) { 196 //can use the cookie plugin 197 if (!useCookies(opts)) { 198 return false; 199 } 200 201 //if its not there we don't need to remove from it 202 if (!issetCookie(opts)) { //quit here, don't have a cookie 203 return false; 204 } 205 206 //unescape it 207 cookie = unescape($.cookie(opts.cookieName)); 208 209 //is this value in the cookie arrray 210 if (cookie != value) { //no, quit here 211 return false; 212 } 213 214 return true; 215 } 216 217 //check if a cookie is set 218 function issetCookie(opts) { 219 //can we use the cookie plugin 220 if (!useCookies(opts)) { //no, quit here 221 return false; 222 } 223 224 //is the cookie set 225 if ($.cookie(opts.cookieName) == null) { //no, quit here 226 return false; 227 } 228 229 return true; 230 } 231 232 // settings 233 $.fn.accordion.defaults = { 234 cssClose: 'accordion-close', //class you want to assign to a closed accordion header 235 cssOpen: 'accordion-open', //class you want to assign an opened accordion header 236 cookieName: 'accordion', //name of the cookie you want to set for this accordion 237 cookieOptions: { //cookie options, see cookie plugin for details 238 path: '/', 239 expires: 7, 240 domain: '', 241 secure: '' 242 }, 243 defaultOpen: '', //id that you want opened by default 244 speed: 'slow', //speed of the slide effect 245 bind: 'click', //event to bind to, supports click, dblclick, mouseover and mouseenter 246 animateOpen: function(elem, opts) { //replace the standard slideDown with custom function 247 elem.next().stop(true, true).slideDown(opts.speed); 248 }, 249 animateClose: function(elem, opts) { //replace the standard slideUp with custom function 250 elem.next().stop(true, true).slideUp(opts.speed); 251 }, 252 loadOpen: function(elem, opts) { //replace the default open state with custom function 253 elem.next().show(); 254 }, 255 loadClose: function(elem, opts) { //replace the default close state with custom function 256 elem.next().hide(); 257 } 258 }; 259})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.