1/* 2 * Collapse plugin for jQuery 3 * -- 4 * source: http://github.com/danielstocks/jQuery-Collapse/ 5 * site: http://webcloud.se/jQuery-Collapse 6 * 7 * @author Daniel Stocks (http://webcloud.se) 8 * Copyright 2013, Daniel Stocks 9 * Released under the MIT, BSD, and GPL Licenses. 10 */ 11 12(function($, exports) { 13 14 // Constructor 15 function Collapse (el, options) { 16 options = options || {}; 17 var _this = this, 18 query = options.query || "> :even"; 19 20 $.extend(_this, { 21 $el: el, 22 options : options, 23 sections: [], 24 isAccordion : options.accordion || false, 25 db : options.persist ? jQueryCollapseStorage(el.get(0).id) : false 26 }); 27 28 // Figure out what sections are open if storage is used 29 _this.states = _this.db ? _this.db.read() : []; 30 31 // For every pair of elements in given 32 // element, create a section 33 _this.$el.find(query).each(function() { 34 new jQueryCollapseSection($(this), _this); 35 }); 36 37 // Capute ALL the clicks! 38 (function(scope) { 39 _this.$el.on("click", "[data-collapse-summary] " + (scope.options.clickQuery || ""), 40 $.proxy(_this.handleClick, scope)); 41 42 _this.$el.bind("toggle close open", 43 $.proxy(_this.handleEvent, scope)); 44 45 }(_this)); 46 } 47 48 Collapse.prototype = { 49 handleClick: function(e, state) { 50 e.preventDefault(); 51 state = state || "toggle"; 52 var sections = this.sections, 53 l = sections.length; 54 while(l--) { 55 if($.contains(sections[l].$summary[0], e.target)) { 56 sections[l][state](); 57 break; 58 } 59 } 60 }, 61 handleEvent: function(e) { 62 if(e.target == this.$el.get(0)) return this[e.type](); 63 this.handleClick(e, e.type); 64 }, 65 open: function(eq) { 66 this._change("open", eq); 67 }, 68 close: function(eq) { 69 this._change("close", eq); 70 }, 71 toggle: function(eq) { 72 this._change("toggle", eq); 73 }, 74 _change: function(action, eq) { 75 if(isFinite(eq)) return this.sections[eq][action](); 76 $.each(this.sections, function(i, section) { 77 section[action](); 78 }); 79 } 80 }; 81 82 // Section constructor 83 function Section($el, parent) { 84 85 if(!parent.options.clickQuery) $el.wrapInner('<a href="#"/>'); 86 87 $.extend(this, { 88 isOpen : false, 89 $summary : $el.attr("data-collapse-summary",""), 90 $details : $el.next(), 91 options: parent.options, 92 parent: parent 93 }); 94 parent.sections.push(this); 95 96 // Check current state of section 97 var state = parent.states[this._index()]; 98 99 if(state === 0) { 100 this.close(true); 101 } 102 else if(this.$summary.is(".open") || state === 1) { 103 this.open(true); 104 } else { 105 this.close(true); 106 } 107 } 108 109 Section.prototype = { 110 toggle : function() { 111 this.isOpen ? this.close() : this.open(); 112 }, 113 close: function(bypass) { 114 this._changeState("close", bypass); 115 }, 116 open: function(bypass) { 117 var _this = this; 118 if(_this.options.accordion && !bypass) { 119 $.each(_this.parent.sections, function(i, section) { 120 section.close(); 121 }); 122 } 123 _this._changeState("open", bypass); 124 }, 125 _index: function() { 126 return $.inArray(this, this.parent.sections); 127 }, 128 _changeState: function(state, bypass) { 129 130 var _this = this; 131 _this.isOpen = state == "open"; 132 if($.isFunction(_this.options[state]) && !bypass) { 133 _this.options[state].apply(_this.$details); 134 } else { 135 _this.$details[_this.isOpen ? "show" : "hide"](); 136 } 137 138 _this.$summary.toggleClass("open", state !== "close"); 139 _this.$details.attr("aria-hidden", state === "close"); 140 _this.$summary.attr("aria-expanded", state === "open"); 141 _this.$summary.trigger(state === "open" ? "opened" : "closed", _this); 142 if(_this.parent.db) { 143 _this.parent.db.write(_this._index(), _this.isOpen); 144 } 145 } 146 }; 147 148 // Expose in jQuery API 149 $.fn.extend({ 150 collapse: function(options, scan) { 151 var nodes = (scan) ? $("body").find("[data-collapse]") : $(this); 152 return nodes.each(function() { 153 var settings = (scan) ? {}
153 : options, 154 values = $(this).attr("data-collapse") || ""; 155 $.each(values.split(" "), function(i,v) { 156 if(v) settings[v] = true; 157 }); 158 new Collapse($(this), settings); 159 }); 160 } 161 }); 162 163 // Expose constructor to 164 // global namespace 165 exports.jQueryCollapse = Collapse; 166 exports.jQueryCollapseSection = Section; 167 168 //jQuery DOM Ready 169 $(function() { 170 $.fn.collapse(false, true); 171 }); 172 173})(window.jQuery, window);
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.