2 var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'}, wpList;
6 url: ajaxurl, type: 'POST',
7 response: 'ajax-response',
10 alt: 'alternate', altOffset: 0,
11 addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
14 addBefore: null, addAfter: null,
15 delBefore: null, delAfter: null,
16 dimBefore: null, dimAfter: null
19 nonce: function(e,s) {
20 var url = wpAjax.unserialize(e.attr('href'));
21 return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name=_ajax_nonce]').val() || url._wpnonce || $('#' + s.element + ' input[name=_wpnonce]').val() || 0;
24 parseClass: function(e,t) {
27 cl = $(e).attr('class') || '';
28 cl = cl.match(new RegExp(t+':[\\S]+'));
29 if ( cl ) { c = cl[0].split(':'); }
34 pre: function(e,s,a) {
36 s = $.extend( {}, this.wpList.settings, {
41 if ( $.isFunction( s.confirm ) ) {
43 bg = $('#' + s.element).css('backgroundColor');
44 $('#' + s.element).css('backgroundColor', '#FF9966');
46 r = s.confirm.call(this,e,s,a,bg);
47 if ( 'add' != a ) { $('#' + s.element).css('backgroundColor', bg ); }
48 if ( !r ) { return false; }
53 ajaxAdd: function( e, s ) {
56 var list = this, cls = wpList.parseClass(e,'add'), es, valid, formData;
57 s = wpList.pre.call( list, e, s, 'add' );
59 s.element = cls[2] || e.attr( 'id' ) || s.element || null;
60 if ( cls[3] ) { s.addColor = '#' + cls[3]; }
61 else { s.addColor = s.addColor || '#FFFF33'; }
63 if ( !s ) { return false; }
65 if ( !e.is("[class^=add:" + list.id + ":]") ) { return !wpList.add.call( list, e, s ); }
67 if ( !s.element ) { return true; }
69 s.action = 'add-' + s.what;
71 s.nonce = wpList.nonce(e,s);
73 es = $('#' + s.element + ' :input').not('[name=_ajax_nonce], [name=_wpnonce], [name=action]');
74 valid = wpAjax.validateForm( '#' + s.element );
75 if ( !valid ) { return false; }
77 s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( cls[4] || '' ) ) );
78 formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
79 if ( formData ) { s.data += '&' + formData; }
81 if ( $.isFunction(s.addBefore) ) {
83 if ( !s ) { return true; }
85 if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) ) { return true; }
87 s.success = function(r) {
88 var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
89 if ( !res || res.errors ) { return false; }
91 if ( true === res ) { return true; }
93 jQuery.each( res.responses, function() {
94 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
95 pos: this.position || 0,
97 oldId: this.oldId || null
101 if ( $.isFunction(s.addAfter) ) {
103 this.complete = function(x,st) {
104 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
106 if ( $.isFunction(o) ) { o(x,st); }
109 list.wpList.recolor();
110 $(list).trigger( 'wpListAddEnd', [ s, list.wpList ] );
111 wpList.clear.call(list,'#' + s.element);
118 ajaxDel: function( e, s ) {
119 e = $(e); s = s || {};
120 var list = this, cls = wpList.parseClass(e,'delete'), element;
121 s = wpList.pre.call( list, e, s, 'delete' );
123 s.element = cls[2] || s.element || null;
124 if ( cls[3] ) { s.delColor = '#' + cls[3]; }
125 else { s.delColor = s.delColor || '#faa'; }
127 if ( !s || !s.element ) { return false; }
129 s.action = 'delete-' + s.what;
131 s.nonce = wpList.nonce(e,s);
134 { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
135 wpAjax.unserialize( cls[4] || '' )
138 if ( $.isFunction(s.delBefore) ) {
139 s = s.delBefore( s, list );
140 if ( !s ) { return true; }
142 if ( !s.data._ajax_nonce ) { return true; }
144 element = $('#' + s.element);
146 if ( 'none' != s.delColor ) {
147 element.css( 'backgroundColor', s.delColor ).fadeOut( 350, function(){
148 list.wpList.recolor();
149 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
152 list.wpList.recolor();
153 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
156 s.success = function(r) {
157 var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
158 if ( !res || res.errors ) {
159 element.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
162 if ( $.isFunction(s.delAfter) ) {
164 this.complete = function(x,st) {
165 element.queue( function() {
166 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
168 if ( $.isFunction(o) ) { o(x,st); }
177 ajaxDim: function( e, s ) {
178 if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
180 e = $(e); s = s || {};
181 var list = this, cls = wpList.parseClass(e,'dim'), element, isClass, color, dimColor;
182 s = wpList.pre.call( list, e, s, 'dim' );
184 s.element = cls[2] || s.element || null;
185 s.dimClass = cls[3] || s.dimClass || null;
186 if ( cls[4] ) { s.dimAddColor = '#' + cls[4]; }
187 else { s.dimAddColor = s.dimAddColor || '#FFFF33'; }
188 if ( cls[5] ) { s.dimDelColor = '#' + cls[5]; }
189 else { s.dimDelColor = s.dimDelColor || '#FF3333'; }
191 if ( !s || !s.element || !s.dimClass ) { return true; }
193 s.action = 'dim-' + s.what;
195 s.nonce = wpList.nonce(e,s);
198 { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
199 wpAjax.unserialize( cls[6] || '' )
202 if ( $.isFunction(s.dimBefore) ) {
203 s = s.dimBefore( s );
204 if ( !s ) { return true; }
207 element = $('#' + s.element);
208 isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
209 color = wpList.getColor( element );
210 element.toggleClass( s.dimClass );
211 dimColor = isClass ? s.dimAddColor : s.dimDelColor;
212 if ( 'none' != dimColor ) {
214 .animate( { backgroundColor: dimColor }, 'fast' )
215 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
216 .animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] ); } } );
218 $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
221 if ( !s.data._ajax_nonce ) { return true; }
223 s.success = function(r) {
224 var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
225 if ( !res || res.errors ) {
226 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
229 if ( $.isFunction(s.dimAfter) ) {
231 this.complete = function(x,st) {
232 element.queue( function() {
233 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
235 if ( $.isFunction(o) ) { o(x,st); }
245 // From jquery.color.js: jQuery Color Animation by John Resig
246 getColor: function( el ) {
247 if ( el.constructor == Object )
249 var elem = el, color, rgbaTrans = new RegExp( "rgba\\(\\s*0,\\s*0,\\s*0,\\s*0\\s*\\)", "i" );
251 color = jQuery.curCSS(elem, 'backgroundColor');
252 if ( color != '' && color != 'transparent' && !color.match(rgbaTrans) || jQuery.nodeName(elem, "body") )
254 } while ( elem = elem.parentNode );
255 return color || '#ffffff';
258 add: function( e, s ) {
261 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
263 if ( 'string' == typeof s )
266 s = $.extend(_s, this.wpList.settings, s);
268 if ( !e.size() || !s.what )
272 old = $('#' + s.what + '-' + s.oldId);
274 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) )
275 $('#' + s.what + '-' + s.id).remove();
277 if ( old && old.size() ) {
280 } else if ( isNaN(s.pos) ) {
282 if ( '-' == s.pos.substr(0,1) ) {
283 s.pos = s.pos.substr(1);
286 ref = list.find( '#' + s.pos );
287 if ( 1 === ref.size() ) { ref[ba](e); }
288 else { list.append(e); }
289 } else if ( s.pos < 0 ) {
296 if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
297 else { e.addClass( s.alt ); }
300 if ( 'none' != s.addColor ) {
301 color = wpList.getColor( e );
302 e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
304 list.each( function() { this.wpList.process( e ); } );
309 var list = this, t, tag;
311 if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; }
312 e.find(':input').each( function() {
313 if ( $(this).parents('.form-no-clear').size() )
315 t = this.type.toLowerCase();
316 tag = this.tagName.toLowerCase();
317 if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; }
318 else if ( 'checkbox' == t || 'radio' == t ) { this.checked = false; }
319 else if ( 'select' == tag ) { this.selectedIndex = null; }
323 process: function(el) {
325 $el = $(el || document);
327 $el.delegate( "form[class^=add:" + list.id + ":]", 'submit', function(){
328 return list.wpList.add(this);
331 $el.delegate( "[class^=add:" + list.id + ":]:not(form)", 'click', function(){
332 return list.wpList.add(this);
335 $el.delegate( "[class^=delete:" + list.id + ":]", 'click', function(){
336 return list.wpList.del(this);
339 $el.delegate( "[class^=dim:" + list.id + ":]", 'click', function(){
340 return list.wpList.dim(this);
344 recolor: function() {
345 var list = this, items, eo;
346 if ( !list.wpList.settings.alt ) { return; }
347 items = $('.list-item:visible', list);
348 if ( !items.size() ) { items = $(list).children(':visible'); }
349 eo = [':even',':odd'];
350 if ( list.wpList.settings.altOffset % 2 ) { eo.reverse(); }
351 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
356 lists.wpList.process = function(a) {
357 lists.each( function() {
358 this.wpList.process(a);
361 lists.wpList.recolor = function() {
362 lists.each( function() {
363 this.wpList.recolor();
369 $.fn.wpList = function( settings ) {
370 this.each( function() {
372 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
373 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
375 wpList.init.call(this);
376 this.wpList.process();