2 var currentFormEl = false;
3 var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'};
7 url: wpListL10n.url, type: 'POST',
8 response: 'ajax-response',
11 alt: 'alternate', altOffset: 0,
12 addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
15 addBefore: null, addAfter: null,
16 delBefore: null, delAfter: null,
17 dimBefore: null, dimAfter: null
20 nonce: function(e,s) {
21 var url = wpAjax.unserialize(e.attr('href'));
22 return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name=_ajax_nonce]').val() || url._wpnonce || $('#' + s.element + ' input[name=_wpnonce]').val() || 0;
25 parseClass: function(e,t) {
28 cl = $(e).attr('class') || '';
29 cl = cl.match(new RegExp(t+':[A-Za-z0-9:_=-]+'));
30 if ( cl ) { c = cl[0].split(':'); }
35 pre: function(e,s,a) {
37 s = $.extend( {}, this.wpList.settings, {
42 if ( $.isFunction( s.confirm ) ) {
44 bg = $('#' + s.element).css('backgroundColor');
45 $('#' + s.element).css('backgroundColor', '#FF9966');
47 r = s.confirm.call(this,e,s,a,bg);
48 if ( 'add' != a ) { $('#' + s.element).css('backgroundColor', bg ); }
49 if ( !r ) { return false; }
54 ajaxAdd: function( e, s ) {
55 var list = this; e = $(e); s = s || {};
56 var cls = wpList.parseClass(e,'add');
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 var es = $('#' + s.element + ' :input').not('[name=_ajax_nonce], [name=_wpnonce], [name=action]');
74 var 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 var 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);
89 if ( !res || res.errors ) { return false; }
91 jQuery.each( res.responses, function() {
92 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
93 pos: this.position || 0,
95 oldId: this.oldId || null
99 if ( $.isFunction(s.addAfter) ) {
100 var o = this.complete;
101 this.complete = function(x,st) {
102 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
104 if ( $.isFunction(o) ) { o(x,st); }
107 list.wpList.recolor();
108 wpList.clear.call(list,'#' + s.element);
115 ajaxDel: function( e, s ) {
116 var list = this; e = $(e); s = s || {};
117 var cls = wpList.parseClass(e,'delete');
118 s = wpList.pre.call( list, e, s, 'delete' );
120 s.element = cls[2] || s.element || null;
121 if ( cls[3] ) { s.delColor = '#' + cls[3]; }
122 else { s.delColor = s.delColor || '#FF3333'; }
124 if ( !s || !s.element ) { return false; }
126 s.action = 'delete-' + s.what;
128 s.nonce = wpList.nonce(e,s);
131 { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
132 wpAjax.unserialize( cls[4] || '' )
135 if ( $.isFunction(s.delBefore) ) {
136 s = s.delBefore( s );
137 if ( !s ) { return true; }
139 if ( !s.data._ajax_nonce ) { return true; }
141 var element = $('#' + s.element);
143 if ( 'none' != s.delColor ) {
144 var anim = 'slideUp';
145 if ( element.css( 'display' ).match(/table/) )
146 anim = 'fadeOut'; // Can't slideup table rows and other table elements. Known jQuery bug
148 .animate( { backgroundColor: s.delColor }, 'fast' )[anim]( 'fast' )
149 .queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
151 list.wpList.recolor();
154 s.success = function(r) {
155 var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
156 if ( !res || res.errors ) {
157 element.stop().stop().css( 'backgroundColor', '#FF3333' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
160 if ( $.isFunction(s.delAfter) ) {
161 var o = this.complete;
162 this.complete = function(x,st) {
163 element.queue( function() {
164 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
166 if ( $.isFunction(o) ) { o(x,st); }
175 ajaxDim: function( e, s ) {
176 var list = this; e = $(e); s = s || {};
177 var cls = wpList.parseClass(e,'dim');
178 s = wpList.pre.call( list, e, s, 'dim' );
180 s.element = cls[2] || s.element || null;
181 s.dimClass = cls[3] || s.dimClass || null;
182 if ( cls[4] ) { s.dimAddColor = '#' + cls[4]; }
183 else { s.dimAddColor = s.dimAddColor || '#FFFF33'; }
184 if ( cls[5] ) { s.dimDelColor = '#' + cls[5]; }
185 else { s.dimDelColor = s.dimDelColor || '#FF3333'; }
187 if ( !s || !s.element || !s.dimClass ) { return true; }
189 s.action = 'dim-' + s.what;
191 s.nonce = wpList.nonce(e,s);
194 { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
195 wpAjax.unserialize( cls[6] || '' )
198 if ( $.isFunction(s.dimBefore) ) {
199 s = s.dimBefore( s );
200 if ( !s ) { return true; }
203 var element = $('#' + s.element);
204 var isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
205 var color = wpList.getColor( element );
206 element.toggleClass( s.dimClass )
207 var dimColor = isClass ? s.dimAddColor : s.dimDelColor;
208 if ( 'none' != dimColor ) {
210 .animate( { backgroundColor: dimColor }, 'fast' )
211 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
212 .animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
215 if ( !s.data._ajax_nonce ) { return true; }
217 s.success = function(r) {
218 var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
219 if ( !res || res.errors ) {
220 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
223 if ( $.isFunction(s.dimAfter) ) {
224 var o = this.complete;
225 this.complete = function(x,st) {
226 element.queue( function() {
227 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
229 if ( $.isFunction(o) ) { o(x,st); }
239 // From jquery.color.js: jQuery Color Animation by John Resig
240 getColor: function( el ) {
241 if ( el.constructor == Object )
243 var elem = el, color, rgbaTrans = new RegExp( "rgba\\(\\s*0,\\s*0,\\s*0,\\s*0\\s*\\)", "i" );
245 color = jQuery.curCSS(elem, 'backgroundColor');
246 if ( color != '' && color != 'transparent' && !color.match(rgbaTrans) || jQuery.nodeName(elem, "body") )
248 } while ( elem = elem.parentNode );
249 return color || '#ffffff';
252 add: function( e, s ) {
257 var _s = { pos: 0, id: 0, oldId: null };
258 if ( 'string' == typeof s ) { s = { what: s }; }
259 s = $.extend(_s, this.wpList.settings, s);
260 if ( !e.size() || !s.what ) { return false; }
261 if ( s.oldId ) { old = $('#' + s.what + '-' + s.oldId); }
262 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) ) { $('#' + s.what + '-' + s.id).remove(); }
264 if ( old && old.size() ) {
266 } else if ( isNaN(s.pos) ) {
268 if ( '-' == s.pos.substr(0,1) ) {
269 s.pos = s.pos.substr(1);
272 var ref = list.find( '#' + s.pos );
273 if ( 1 === ref.size() ) { ref[ba](e); }
274 else { list.append(e); }
275 } else if ( s.pos < 0 ) {
282 if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
283 else { e.addClass( s.alt ); }
286 if ( 'none' != s.addColor ) {
287 var color = wpList.getColor( e );
288 e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
290 list.each( function() { this.wpList.process( e ); } );
297 if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; }
298 e.find(':input').each( function() {
299 if ( $(this).parents('.form-no-clear').size() )
301 var t = this.type.toLowerCase(); var tag = this.tagName.toLowerCase();
302 if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; }
303 else if ( 'checkbox' == t || 'radio' == t ) { this.checked = false; }
304 else if ( 'select' == tag ) { this.selectedIndex = null; }
308 process: function(el) {
310 $("[class^=add:" + list.id + ":]", el || null)
311 .filter('form').submit( function() { return list.wpList.add(this); } ).end()
312 .not('form').click( function() { return list.wpList.add(this); } ).each( function() {
314 var c = wpList.parseClass(this,'add')[2] || addEl.id;
315 if ( !c ) { return; }
316 var forms = []; var ins = []; // this is all really inefficient
317 $('#' + c + ' :input').focus( function() { currentFormEl = this; } ).blur( function() { currentFormEl = false; } ).each( function() {
319 var f = $(this).parents('form:first').get(0);
320 if ( $.inArray(f,forms) < 0 ) { forms.push(f); }
322 $(forms).submit( function() {
323 if ( 0 <= $.inArray(currentFormEl,ins) ) {
324 $(addEl).trigger( 'click' );
325 $(currentFormEl).focus();
330 $("[class^=delete:" + list.id + ":]", el || null).click( function() { return list.wpList.del(this); } );
331 $("[class^=dim:" + list.id + ":]", el || null).click( function() { return list.wpList.dim(this); } );
334 recolor: function() {
336 if ( !list.wpList.settings.alt ) { return; }
337 var items = $('.list-item:visible', list);
338 if ( !items.size() ) { items = $(list).children(':visible'); }
339 var eo = [':even',':odd'];
340 if ( list.wpList.settings.altOffset % 2 ) { eo.reverse(); }
341 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
346 lists.wpList.process = function(a) {
347 lists.each( function() {
348 this.wpList.process(a);
351 lists.wpList.recolor = function() {
352 lists.each( function() {
353 this.wpList.recolor();
359 $.fn.wpList = function( settings ) {
360 this.each( function() {
362 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
363 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
365 wpList.init.call(this);
366 this.wpList.process();