1/* 2 * Collapse plugin for jQuery 3 * -- 4 * source: http://github.com/danielstocks/jQuery-Collapse/ 5 * site: http://webcloud.se/code/jQuery-Collapse 6 * 7 * @author Daniel Stocks (http://webcloud.se) 8 * Copyright 2012, Daniel Stocks 9 * Released under the MIT, BSD, and GPL Licenses. 10 */ 11 12!function($) { 13 14 // Constructor 15 function Collapse (el, options) { 16 var _this = this, 17 options = options || {}, 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 ? new jQueryCollapseStorage(el[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 var section = new Section($(this), _this); 35 _this.sections.push(section); 36 _this.states[section._index()] || section.$summary.hasClass("open") ? 37 section.open(true) : section.close(true); 38 }); 39 40 // Capute ALL the clicks! 41 (function(scope) { 42 _this.$el.on("click", "[data-collapse-summary]", 43 $.proxy(_this.handleClick, scope)); 44 }(_this)); 45 } 46 47 Collapse.prototype = { 48 handleClick: function(e) { 49 e.preventDefault(); 50 var sections = this.sections, 51 parent = $(e.target).parent(), 52 l = sections.length; 53 while(l--) { 54 if(sections[l].$summary.find("a").is(e.target)) { 55 sections[l].toggle(); 56 break; 57 } 58 } 59 }, 60 open : function(eq) { 61 if(isFinite(eq)) return this.sections[eq].open(); 62 $.each(this.sections, function() { 63 this.open(); 64 }); 65 }, 66 close: function(eq) { 67 if(isFinite(eq)) return this.sections[eq].close(); 68 $.each(this.sections, function() { 69 this.close(); 70 }); 71 } 72 }; 73 74 // Section constructor 75 function Section($el, parent) { 76 $.extend(this, { 77 isOpen : false, 78 $summary : $el 79 .attr("data-collapse-summary", "") 80 .wrapInner('<a href="#"/>'), 81 $details : $el.next(), 82 options: parent.options, 83 parent: parent 84 }); 85 } 86 87 Section.prototype = { 88 toggle : function() { 89 this.isOpen ? this.close() : this.open(); 90 }, 91 close: function(bypass) { 92 this._changeState("close", bypass) 93 }, 94 open: function(bypass) { 95 var _this = this; 96 if(_this.options.accordion && !bypass) { 97 $.each(_this.parent.sections, function() { 98 this.close(); 99 }); 100 } 101 _this._changeState("open", bypass) 102 }, 103 _index: function() { 104 return $.inArray(this, this.parent.sections); 105 }, 106 _changeState: function(state, bypass) { 107 108 var _this = this; 109 _this.isOpen = state == "open" 110 if($.isFunction(_this.options[state]) && !bypass) { 111 _this.options[state].apply(_this.$details) 112 } else { 113 _this.isOpen ? _this.$details.show() : _this.$details.hide(); 114 } 115 _this.$summary.removeClass("open close").addClass(state); 116 _this.$details.attr("aria-hidden", state == "close"); 117 _this.parent.$el.trigger(state, _this); 118 if(_this.parent.db) { 119 _this.parent.db.write(_this._index(), _this.isOpen); 120 } 121 } 122 } 123 124 // Expose in jQuery API 125 $.fn.extend({ 126 collapse: function(options, scan) { 127 var nodes = (scan) ? $("body").find("[data-collapse]") : $(this); 128 return nodes.each(function() { 129 var settings = (scan) ? {} : options, 130 values = $(this).attr("data-collapse") || ""; 131 $.each(values.split(" "), function(i,v) { 132 if(v) settings[v] = true; 133 }); 134 new jQueryCollapse($(this), settings).$el; 135 }); 136 } 137 }); 138 139 //jQuery DOM Ready 140 $(function() { 141 $.fn.collapse(false, true) 142 }); 143 144 // Expose constructor to 145 // global namespace 146 jQueryCollapse = Collapse; 147 148}(window.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.