/* *copyright by 动力无限 v4.0 www.btoe.cn *邮箱 btoe@btoe.cn *合作电话 400-0599-360 *版权所有违者必究 /*-----------------------------------------------------------*/ (function($) { $.extend($.fn, { livequery: function(type, fn, fn2) { var self = this, q; // handle different call patterns if ($.isfunction(type)) fn2 = fn, fn = type, type = undefined; // see if live query already exists $.each( $.livequery.queries, function(i, query) { if ( self.selector == query.selector && self.context == query.context && type == query.type && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) ) // found the query, exit the each loop return (q = query) && false; }); // create new live query if it wasn't found q = q || new $.livequery(this.selector, this.context, type, fn, fn2); // make sure it is running q.stopped = false; // run it $.livequery.run( q.id ); // contnue the chain return this; }, expire: function(type, fn, fn2) { var self = this; // handle different call patterns if ($.isfunction(type)) fn2 = fn, fn = type, type = undefined; // find the live query based on arguments and stop it $.each( $.livequery.queries, function(i, query) { if ( self.selector == query.selector && self.context == query.context && (!type || type == query.type) && (!fn || fn.$lqguid == query.fn.$lqguid) && (!fn2 || fn2.$lqguid == query.fn2.$lqguid) && !this.stopped ) $.livequery.stop(query.id); }); // continue the chain return this; } }); $.livequery = function(selector, context, type, fn, fn2) { this.selector = selector; this.context = context || document; this.type = type; this.fn = fn; this.fn2 = fn2; this.elements = []; this.stopped = false; // the id is the index of the live query in $.livequery.queries this.id = $.livequery.queries.push(this)-1; // mark the functions for matching later on fn.$lqguid = fn.$lqguid || $.livequery.guid++; if (fn2) fn2.$lqguid = fn2.$lqguid || $.livequery.guid++; // return the live query return this; }; $.livequery.prototype = { stop: function() { var query = this; if ( this.type ) // unbind all bound events this.elements.unbind(this.type, this.fn); else if (this.fn2) // call the second function for all matched elements this.elements.each(function(i, el) { query.fn2.apply(el); }); // clear out matched elements this.elements = []; // stop the live query from running until restarted this.stopped = true; }, run: function() { // short-circuit if stopped if ( this.stopped ) return; var query = this; var oels = this.elements, els = $(this.selector, this.context), nels = els.not(oels); // set elements to the latest set of matched elements this.elements = els; if (this.type) { // bind events to newly matched elements nels.bind(this.type, this.fn); // unbind events to elements no longer matched if (oels.length > 0) $.each(oels, function(i, el) { if ( $.inarray(el, els) < 0 ) $.event.remove(el, query.type, query.fn); }); } else { // call the first function for newly matched elements nels.each(function() { query.fn.apply(this); }); // call the second function for elements no longer matched if ( this.fn2 && oels.length > 0 ) $.each(oels, function(i, el) { if ( $.inarray(el, els) < 0 ) query.fn2.apply(el); }); } } }; $.extend($.livequery, { guid: 0, queries: [], queue: [], running: false, timeout: null, checkqueue: function() { if ( $.livequery.running && $.livequery.queue.length ) { var length = $.livequery.queue.length; // run each live query currently in the queue while ( length-- ) $.livequery.queries[ $.livequery.queue.shift() ].run(); } }, pause: function() { // don't run anymore l