/* * collapse plugin for jquery * -- * source: http://github.com/danielstocks/jquery-collapse/ * site: http://webcloud.se/jquery-collapse * * @author daniel stocks (http://webcloud.se) * copyright 2013, daniel stocks * released under the mit, bsd, and gpl licenses. */ /* �������������֮�� www.lanrenzhijia.com */ (function($) { // constructor function collapse (el, options) { options = options || {}; var _this = this, query = options.query || "> :even"; $.extend(_this, { $el: el, options : options, sections: [], isaccordion : options.accordion || false, db : options.persist ? jquerycollapsestorage(el[0].id) : false }); // figure out what sections are open if storage is used _this.states = _this.db ? _this.db.read() : []; // for every pair of elements in given // element, create a section _this.$el.find(query).each(function() { var section = new section($(this), _this); _this.sections.push(section); // check current state of section var state = _this.states[section._index()]; if(state === 0) { section.$summary.removeclass("open"); } if(state === 1) { section.$summary.addclass("open"); } // show or hide accordingly if(section.$summary.hasclass("open")) { section.open(true); } else { section.close(true); } }); // capute all the clicks! (function(scope) { _this.$el.on("click", "[data-collapse-summary]", $.proxy(_this.handleclick, scope)); }(_this)); } collapse.prototype = { handleclick: function(e) { e.preventdefault(); var sections = this.sections, l = sections.length; while(l--) { if($.contains(sections[l].$summary[0], e.target)) { sections[l].toggle(); break; } } }, open : function(eq) { if(isfinite(eq)) return this.sections[eq].open(); $.each(this.sections, function() { this.open(); }); }, close: function(eq) { if(isfinite(eq)) return this.sections[eq].close(); $.each(this.sections, function() { this.close(); }); } }; // section constructor function section($el, parent) { $.extend(this, { isopen : false, $summary : $el .attr("data-collapse-summary", "") .wrapinner(''), $details : $el.next(), options: parent.options, parent: parent }); } section.prototype = { toggle : function() { if(this.isopen) this.close(); else this.open(); }, close: function(bypass) { this._changestate("close", bypass); }, open: function(bypass) { var _this = this; if(_this.options.accordion && !bypass) { $.each(_this.parent.sections, function() { this.close(); }); } _this._changestate("open", bypass); }, _index: function() { return $.inarray(this, this.parent.sections); }, _changestate: function(state, bypass) { var _this = this; _this.isopen = state == "open"; if($.isfunction(_this.options[state]) && !bypass) { _this.options[state].apply(_this.$details); } else { if(_this.isopen) _this.$details.show(); else _this.$details.hide(); } _this.$summary.removeclass("open close").addclass(state); _this.$details.attr("aria-hidden", state == "close"); _this.parent.$el.trigger(state, _this); if(_this.parent.db) { _this.parent.db.write(_this._index(), _this.isopen); } } }; // expose in jquery api $.fn.extend({ collapse: function(options, scan) { var nodes = (scan) ? $("body").find("[data-collapse]") : $(this); return nodes.each(function() { var settings = (scan) ? {} : options, values = $(this).attr("data-collapse") || ""; $.each(values.split(" "), function(i,v) { if(v) settings[v] = true; }); new jquerycollapse($(this), settings); }); } }); //jquery dom ready $(function() { $.fn.collapse(false, true); }); // expose constructor to // global namespace jquerycollapse = collapse; })(window.jquery); /* �������������֮�� www.lanrenzhijia.com */