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 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 { 159 close(opts); 160 //do cookies if plugin available 161 if (useCookies(opts)) { 162 // split the cookieOpen string by "," 163 setCookie('', opts); 164 } 165 return false; 166 } 167 close(opts); 168 //open a closed element 169 open($this, opts); 170 return false; 171 } 172 173 //use cookies? 174 function useCookies(opts) { 175 //return false if cookie plugin not present or if a cookie name is not provided 176 if (!$.cookie || opts.cookieName == '') { 177 return false; 178 } 179 180 //we can use cookies 181 return true; 182 } 183 184 //set a cookie 185 function setCookie(value, opts) 186 { 187 //can use the cookie plugin 188 if (!useCookies(opts)) { //no, quit here 189 return false; 190 } 191 192 //cookie plugin is available, lets set the cookie 193 $.cookie(opts.cookieName, value, opts.cookieOptions); 194 } 195 196 //check if a accordion is in the cookie 197 function inCookie(value, opts) 198 { 199 //can use the cookie plugin 200 if (!useCookies(opts)) { 201 return false; 202 } 203 204 //if its not there we don't need to remove from it 205 if (!issetCookie(opts)) { //quit here, don't have a cookie 206 return false; 207 } 208 209 //unescape it 210 cookie = unescape($.cookie(opts.cookieName)); 211 212 //is this value in the cookie arrray 213 if (cookie != value) { //no, quit here 214 return false; 215 } 216 217 return true; 218 } 219 220 //check if a cookie is set 221 function issetCookie(opts) 222 { 223 //can we use the cookie plugin 224 if (!useCookies(opts)) { //no, quit here 225 return false; 226 } 227 228 //is the cookie set 229 if ($.cookie(opts.cookieName) == null) { //no, quit here 230 return false; 231 } 232 233 return true; 234 } 235 236 // settings 237 $.fn.accordion.defaults = { 238 cssClose: 'accordion-close', //class you want to assign to a closed accordion header 239 cssOpen: 'accordion-open', //class you want to assign an opened accordion header 240 cookieName: 'accordion', //name of the cookie you want to set for this accordion 241 cookieOptions: { //cookie options, see cookie plugin for details 242 path: '/', 243 expires: 7, 244 domain: '', 245 secure: '' 246 }, 247 defaultOpen: '', //id that you want opened by default 248 speed: 'slow', //speed of the slide effect 249 bind: 'click', //event to bind to, supports click, dblclick, mouseover and mouseenter 250 animateOpen: function (elem, opts) { //replace the standard slideDown with custom function 251 elem.next().stop(true, true).slideDown(opts.speed); 252 }, 253 animateClose: function (elem, opts) { //replace the standard slideUp with custom function 254 elem.next().stop(true, true).slideUp(opts.speed); 255 }, 256 loadOpen: function (elem, opts) { //replace the default open state with custom function 257 elem.next().show(); 258 }, 259 loadClose: function (elem, opts) { //replace the default close state with custom function 260 elem.next().hide(); 261 } 262 }; 263})(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.