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 ( true === res ) {
304 if ( ! res || res.errors ) {
305 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
309 if ( 'undefined' !== typeof res.responses[0].supplemental.comment_link ) {
310 var submittedOn = element.find( '.submitted-on' ),
311 commentLink = submittedOn.find( 'a' );
313 // Comment is approved; link the date field.
314 if ( '' !== res.responses[0].supplemental.comment_link ) {
315 submittedOn.html( $('<a></a>').text( submittedOn.text() ).prop( 'href', res.responses[0].supplemental.comment_link ) );
317 // Comment is not approved; unlink the date field.
318 } else if ( commentLink.length ) {
319 submittedOn.text( commentLink.text() );
324 s.complete = function(x, st) {
325 if ( $.isFunction(s.dimAfter) ) {
326 element.queue( function() {
327 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
328 s.dimAfter( rres, _s );
337 getColor: function( el ) {
338 var color = jQuery(el).css('backgroundColor');
340 return color || '#ffffff';
343 add: function( e, s ) {
344 if ( 'string' == typeof e ) {
345 e = $( $.trim( e ) ); // Trim leading whitespaces
350 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
352 if ( 'string' == typeof s )
355 s = $.extend(_s, this.wpList.settings, s);
357 if ( !e.length || !s.what )
361 old = $('#' + s.what + '-' + s.oldId);
363 if ( s.id && ( s.id != s.oldId || !old || !old.length ) )
364 $('#' + s.what + '-' + s.id).remove();
366 if ( old && old.length ) {
369 } else if ( isNaN(s.pos) ) {
372 if ( '-' == s.pos.substr(0,1) ) {
373 s.pos = s.pos.substr(1);
377 ref = list.find( '#' + s.pos );
379 if ( 1 === ref.length )
384 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
393 if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
394 else { e.addClass( s.alt ); }
397 if ( 'none' != s.addColor ) {
398 color = wpList.getColor( e );
399 e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
401 list.each( function() { this.wpList.process( e ); } );
406 var list = this, t, tag;
410 if ( list.wpList && e.parents( '#' + list.id ).length )
413 e.find(':input').each( function() {
414 if ( $(this).parents('.form-no-clear').length )
417 t = this.type.toLowerCase();
418 tag = this.tagName.toLowerCase();
420 if ( 'text' == t || 'password' == t || 'textarea' == tag )
422 else if ( 'checkbox' == t || 'radio' == t )
423 this.checked = false;
424 else if ( 'select' == tag )
425 this.selectedIndex = null;
429 process: function(el) {
431 $el = $(el || document);
433 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
434 return list.wpList.add(this);
437 $el.delegate( 'a[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', 'click', function(){
438 return list.wpList.add(this);
441 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
442 return list.wpList.del(this);
445 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
446 return list.wpList.dim(this);
450 recolor: function() {
451 var list = this, items, eo;
453 if ( !list.wpList.settings.alt )
456 items = $('.list-item:visible', list);
459 items = $(list).children(':visible');
461 eo = [':even',':odd'];
463 if ( list.wpList.settings.altOffset % 2 )
466 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
472 lists.wpList.process = function(a) {
473 lists.each( function() {
474 this.wpList.process(a);
478 lists.wpList.recolor = function() {
479 lists.each( function() {
480 this.wpList.recolor();
486 $.fn.wpList = function( settings ) {
487 this.each( function() {
490 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseData(this,'list')[1] || '' }, settings ) };
491 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
494 wpList.init.call(this);
496 this.wpList.process();