/* *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 live queries until restarted $.livequery.running = false; }, play: function() { // restart live queries $.livequery.running = true; // request a run of the live queries $.livequery.run(); }, registerplugin: function() { $.each( arguments, function(i,n) { // short-circuit if the method doesn't exist if (!$.fn[n]) return; // save a reference to the original method var old = $.fn[n]; // create a new method $.fn[n] = function() { // call the original method var r = old.apply(this, arguments); // request a run of the live queries $.livequery.run(); // return the original methods result return r; } }); }, run: function(id) { if (id != undefined) { // put the particular live query in the queue if it doesn't already exist if ( $.inarray(id, $.livequery.queue) < 0 ) $.livequery.queue.push( id ); } else // put each live query in the queue if it doesn't already exist $.each( $.livequery.queries, function(id) { if ( $.inarray(id, $.livequery.queue) < 0 ) $.livequery.queue.push( id ); }); // clear timeout if it already exists if ($.livequery.timeout) cleartimeout($.livequery.timeout); // create a timeout to check the queue and actually run the live queries $.livequery.timeout = settimeout($.livequery.checkqueue, 20); }, stop: function(id) { if (id != undefined) // stop are particular live query $.livequery.queries[ id ].stop(); else // stop all live queries $.each( $.livequery.queries, function(id) { $.livequery.queries[ id ].stop(); }); } }); // register core dom manipulation methods $.livequery.registerplugin('append', 'prepend', 'after', 'before', 'wrap', 'attr', 'removeattr', 'addclass', 'removeclass', 'toggleclass', 'empty', 'remove'); // run live queries when the document is ready $(function() { $.livequery.play(); }); // save a reference to the original init method var init = $.prototype.init; // create a new init method that exposes two new properties: selector and context $.prototype.init = function(a,c) { // call the original init and save the result var r = init.apply(this, arguments); // copy over properties if they exist already if (a && a.selector) r.context = a.context, r.selector = a.selector; // set properties if ( typeof a == 'string' ) r.context = c || document, r.selector = a; // return the result return r; }; // give the init function the jquery prototype for later instantiation (needed after rev 4091) $.prototype.init.prototype = $.prototype; })(jquery);