1 /* global ajaxurl, wpAjax */
3 var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'}, wpList;
7 url: ajaxurl, 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;
26 * Extract list item data from a DOM element.
28 * @param {HTMLElement} e The DOM element.
32 parseData: function(e,t) {
33 var d = [], wpListsData;
36 wpListsData = $(e).attr('data-wp-lists') || '';
37 wpListsData = wpListsData.match(new RegExp(t+':[\\S]+'));
40 d = wpListsData[0].split(':');
46 pre: function(e,s,a) {
49 s = $.extend( {}, this.wpList.settings, {
55 if ( $.isFunction( s.confirm ) ) {
57 bg = $('#' + s.element).css('backgroundColor');
58 $('#' + s.element).css('backgroundColor', '#FF9966');
60 r = s.confirm.call(this, e, s, a, bg);
63 $('#' + s.element).css('backgroundColor', bg );
72 ajaxAdd: function( e, s ) {
75 var list = this, data = wpList.parseData(e,'add'), es, valid, formData, res, rres;
77 s = wpList.pre.call( list, e, s, 'add' );
79 s.element = data[2] || e.attr( 'id' ) || s.element || null;
82 s.addColor = '#' + data[3];
84 s.addColor = s.addColor || '#FFFF33';
89 if ( !e.is('[id="' + s.element + '-submit"]') )
90 return !wpList.add.call( list, e, s );
95 s.action = 'add-' + s.what;
97 s.nonce = wpList.nonce(e,s);
99 es = $('#' + s.element + ' :input').not('[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]');
100 valid = wpAjax.validateForm( '#' + s.element );
105 s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( data[4] || '' ) ) );
106 formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
109 s.data += '&' + formData;
111 if ( $.isFunction(s.addBefore) ) {
112 s = s.addBefore( s );
117 if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) )
120 s.success = function(r) {
121 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
125 if ( !res || res.errors )
131 jQuery.each( res.responses, function() {
132 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
133 pos: this.position || 0,
135 oldId: this.oldId || null
139 list.wpList.recolor();
140 $(list).trigger( 'wpListAddEnd', [ s, list.wpList ] );
141 wpList.clear.call(list,'#' + s.element);
144 s.complete = function(x, st) {
145 if ( $.isFunction(s.addAfter) ) {
146 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
147 s.addAfter( rres, _s );
156 * Delete an item in the list via AJAX.
158 * @param {HTMLElement} e A DOM element containing item data.
162 ajaxDel: function( e, s ) {
165 var list = this, data = wpList.parseData(e,'delete'), element, res, rres;
167 s = wpList.pre.call( list, e, s, 'delete' );
169 s.element = data[2] || s.element || null;
172 s.delColor = '#' + data[3];
174 s.delColor = s.delColor || '#faa';
176 if ( !s || !s.element )
179 s.action = 'delete-' + s.what;
181 s.nonce = wpList.nonce(e,s);
184 { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
185 wpAjax.unserialize( data[4] || '' )
188 if ( $.isFunction(s.delBefore) ) {
189 s = s.delBefore( s, list );
194 if ( !s.data._ajax_nonce )
197 element = $('#' + s.element);
199 if ( 'none' != s.delColor ) {
200 element.css( 'backgroundColor', s.delColor ).fadeOut( 350, function(){
201 list.wpList.recolor();
202 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
205 list.wpList.recolor();
206 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
209 s.success = function(r) {
210 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
213 if ( !res || res.errors ) {
214 element.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
219 s.complete = function(x, st) {
220 if ( $.isFunction(s.delAfter) ) {
221 element.queue( function() {
222 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
223 s.delAfter( rres, _s );
232 ajaxDim: function( e, s ) {
233 if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
239 var list = this, data = wpList.parseData(e,'dim'), element, isClass, color, dimColor, res, rres;
241 s = wpList.pre.call( list, e, s, 'dim' );
243 s.element = data[2] || s.element || null;
244 s.dimClass = data[3] || s.dimClass || null;
247 s.dimAddColor = '#' + data[4];
249 s.dimAddColor = s.dimAddColor || '#FFFF33';
252 s.dimDelColor = '#' + data[5];
254 s.dimDelColor = s.dimDelColor || '#FF3333';
256 if ( !s || !s.element || !s.dimClass )
259 s.action = 'dim-' + s.what;
261 s.nonce = wpList.nonce(e,s);
264 { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
265 wpAjax.unserialize( data[6] || '' )
268 if ( $.isFunction(s.dimBefore) ) {
269 s = s.dimBefore( s );
274 element = $('#' + s.element);
275 isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
276 color = wpList.getColor( element );
277 element.toggleClass( s.dimClass );
278 dimColor = isClass ? s.dimAddColor : s.dimDelColor;
280 if ( 'none' != dimColor ) {
282 .animate( { backgroundColor: dimColor }, 'fast' )
283 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
284 .animate( { backgroundColor: color }, { complete: function() {
285 $(this).css( 'backgroundColor', '' );
286 $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
290 $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
293 if ( !s.data._ajax_nonce )
296 s.success = function(r) {
297 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
300 if ( !res || res.errors ) {
301 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
306 s.complete = function(x, st) {
307 if ( $.isFunction(s.dimAfter) ) {
308 element.queue( function() {
309 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
310 s.dimAfter( rres, _s );
319 getColor: function( el ) {
320 var color = jQuery(el).css('backgroundColor');
322 return color || '#ffffff';
325 add: function( e, s ) {
326 if ( 'string' == typeof e ) {
327 e = $( $.trim( e ) ); // Trim leading whitespaces
332 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
334 if ( 'string' == typeof s )
337 s = $.extend(_s, this.wpList.settings, s);
339 if ( !e.length || !s.what )
343 old = $('#' + s.what + '-' + s.oldId);
345 if ( s.id && ( s.id != s.oldId || !old || !old.length ) )
346 $('#' + s.what + '-' + s.id).remove();
348 if ( old && old.length ) {
351 } else if ( isNaN(s.pos) ) {
354 if ( '-' == s.pos.substr(0,1) ) {
355 s.pos = s.pos.substr(1);
359 ref = list.find( '#' + s.pos );
361 if ( 1 === ref.length )
366 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
375 if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
376 else { e.addClass( s.alt ); }
379 if ( 'none' != s.addColor ) {
380 color = wpList.getColor( e );
381 e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
383 list.each( function() { this.wpList.process( e ); } );
388 var list = this, t, tag;
392 if ( list.wpList && e.parents( '#' + list.id ).length )
395 e.find(':input').each( function() {
396 if ( $(this).parents('.form-no-clear').length )
399 t = this.type.toLowerCase();
400 tag = this.tagName.toLowerCase();
402 if ( 'text' == t || 'password' == t || 'textarea' == tag )
404 else if ( 'checkbox' == t || 'radio' == t )
405 this.checked = false;
406 else if ( 'select' == tag )
407 this.selectedIndex = null;
411 process: function(el) {
413 $el = $(el || document);
415 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
416 return list.wpList.add(this);
419 $el.delegate( 'a[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', 'click', function(){
420 return list.wpList.add(this);
423 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
424 return list.wpList.del(this);
427 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
428 return list.wpList.dim(this);
432 recolor: function() {
433 var list = this, items, eo;
435 if ( !list.wpList.settings.alt )
438 items = $('.list-item:visible', list);
441 items = $(list).children(':visible');
443 eo = [':even',':odd'];
445 if ( list.wpList.settings.altOffset % 2 )
448 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
454 lists.wpList.process = function(a) {
455 lists.each( function() {
456 this.wpList.process(a);
460 lists.wpList.recolor = function() {
461 lists.each( function() {
462 this.wpList.recolor();
468 $.fn.wpList = function( settings ) {
469 this.each( function() {
472 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseData(this,'list')[1] || '' }, settings ) };
473 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
476 wpList.init.call(this);
478 this.wpList.process();