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;
25 parseData: function(e,t) {
26 var d = [], wpListsData;
29 wpListsData = $(e).attr('data-wp-lists') || '';
30 wpListsData = wpListsData.match(new RegExp(t+':[\\S]+'));
33 d = wpListsData[0].split(':');
39 pre: function(e,s,a) {
42 s = $.extend( {}, this.wpList.settings, {
48 if ( $.isFunction( s.confirm ) ) {
50 bg = $('#' + s.element).css('backgroundColor');
51 $('#' + s.element).css('backgroundColor', '#FF9966');
53 r = s.confirm.call(this, e, s, a, bg);
56 $('#' + s.element).css('backgroundColor', bg );
65 ajaxAdd: function( e, s ) {
68 var list = this, data = wpList.parseData(e,'add'), es, valid, formData, res, rres;
70 s = wpList.pre.call( list, e, s, 'add' );
72 s.element = data[2] || e.attr( 'id' ) || s.element || null;
75 s.addColor = '#' + data[3];
77 s.addColor = s.addColor || '#FFFF33';
82 if ( !e.is('[id="' + s.element + '-submit"]') )
83 return !wpList.add.call( list, e, s );
88 s.action = 'add-' + s.what;
90 s.nonce = wpList.nonce(e,s);
92 es = $('#' + s.element + ' :input').not('[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]');
93 valid = wpAjax.validateForm( '#' + s.element );
98 s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( data[4] || '' ) ) );
99 formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
102 s.data += '&' + formData;
104 if ( $.isFunction(s.addBefore) ) {
105 s = s.addBefore( s );
110 if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) )
113 s.success = function(r) {
114 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
118 if ( !res || res.errors )
124 jQuery.each( res.responses, function() {
125 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
126 pos: this.position || 0,
128 oldId: this.oldId || null
132 list.wpList.recolor();
133 $(list).trigger( 'wpListAddEnd', [ s, list.wpList ] );
134 wpList.clear.call(list,'#' + s.element);
137 s.complete = function(x, st) {
138 if ( $.isFunction(s.addAfter) ) {
139 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
140 s.addAfter( rres, _s );
148 ajaxDel: function( e, s ) {
151 var list = this, data = wpList.parseData(e,'delete'), element, res, rres;
153 s = wpList.pre.call( list, e, s, 'delete' );
155 s.element = data[2] || s.element || null;
158 s.delColor = '#' + data[3];
160 s.delColor = s.delColor || '#faa';
162 if ( !s || !s.element )
165 s.action = 'delete-' + s.what;
167 s.nonce = wpList.nonce(e,s);
170 { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
171 wpAjax.unserialize( data[4] || '' )
174 if ( $.isFunction(s.delBefore) ) {
175 s = s.delBefore( s, list );
180 if ( !s.data._ajax_nonce )
183 element = $('#' + s.element);
185 if ( 'none' != s.delColor ) {
186 element.css( 'backgroundColor', s.delColor ).fadeOut( 350, function(){
187 list.wpList.recolor();
188 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
191 list.wpList.recolor();
192 $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
195 s.success = function(r) {
196 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
199 if ( !res || res.errors ) {
200 element.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
205 s.complete = function(x, st) {
206 if ( $.isFunction(s.delAfter) ) {
207 element.queue( function() {
208 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
209 s.delAfter( rres, _s );
218 ajaxDim: function( e, s ) {
219 if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
225 var list = this, data = wpList.parseData(e,'dim'), element, isClass, color, dimColor, res, rres;
227 s = wpList.pre.call( list, e, s, 'dim' );
229 s.element = data[2] || s.element || null;
230 s.dimClass = data[3] || s.dimClass || null;
233 s.dimAddColor = '#' + data[4];
235 s.dimAddColor = s.dimAddColor || '#FFFF33';
238 s.dimDelColor = '#' + data[5];
240 s.dimDelColor = s.dimDelColor || '#FF3333';
242 if ( !s || !s.element || !s.dimClass )
245 s.action = 'dim-' + s.what;
247 s.nonce = wpList.nonce(e,s);
250 { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
251 wpAjax.unserialize( data[6] || '' )
254 if ( $.isFunction(s.dimBefore) ) {
255 s = s.dimBefore( s );
260 element = $('#' + s.element);
261 isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
262 color = wpList.getColor( element );
263 element.toggleClass( s.dimClass );
264 dimColor = isClass ? s.dimAddColor : s.dimDelColor;
266 if ( 'none' != dimColor ) {
268 .animate( { backgroundColor: dimColor }, 'fast' )
269 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
270 .animate( { backgroundColor: color }, { complete: function() {
271 $(this).css( 'backgroundColor', '' );
272 $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
276 $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
279 if ( !s.data._ajax_nonce )
282 s.success = function(r) {
283 res = wpAjax.parseAjaxResponse(r, s.response, s.element);
286 if ( !res || res.errors ) {
287 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
292 s.complete = function(x, st) {
293 if ( $.isFunction(s.dimAfter) ) {
294 element.queue( function() {
295 var _s = $.extend( { xml: x, status: st, parsed: res }, s );
296 s.dimAfter( rres, _s );
305 getColor: function( el ) {
306 var color = jQuery(el).css('backgroundColor');
308 return color || '#ffffff';
311 add: function( e, s ) {
312 if ( 'string' == typeof e ) {
313 e = $( $.trim( e ) ); // Trim leading whitespaces
318 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
320 if ( 'string' == typeof s )
323 s = $.extend(_s, this.wpList.settings, s);
325 if ( !e.size() || !s.what )
329 old = $('#' + s.what + '-' + s.oldId);
331 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) )
332 $('#' + s.what + '-' + s.id).remove();
334 if ( old && old.size() ) {
337 } else if ( isNaN(s.pos) ) {
340 if ( '-' == s.pos.substr(0,1) ) {
341 s.pos = s.pos.substr(1);
345 ref = list.find( '#' + s.pos );
347 if ( 1 === ref.size() )
352 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
361 if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
362 else { e.addClass( s.alt ); }
365 if ( 'none' != s.addColor ) {
366 color = wpList.getColor( e );
367 e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
369 list.each( function() { this.wpList.process( e ); } );
374 var list = this, t, tag;
378 if ( list.wpList && e.parents( '#' + list.id ).size() )
381 e.find(':input').each( function() {
382 if ( $(this).parents('.form-no-clear').size() )
385 t = this.type.toLowerCase();
386 tag = this.tagName.toLowerCase();
388 if ( 'text' == t || 'password' == t || 'textarea' == tag )
390 else if ( 'checkbox' == t || 'radio' == t )
391 this.checked = false;
392 else if ( 'select' == tag )
393 this.selectedIndex = null;
397 process: function(el) {
399 $el = $(el || document);
401 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
402 return list.wpList.add(this);
405 $el.delegate( 'a[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', 'click', function(){
406 return list.wpList.add(this);
409 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
410 return list.wpList.del(this);
413 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
414 return list.wpList.dim(this);
418 recolor: function() {
419 var list = this, items, eo;
421 if ( !list.wpList.settings.alt )
424 items = $('.list-item:visible', list);
427 items = $(list).children(':visible');
429 eo = [':even',':odd'];
431 if ( list.wpList.settings.altOffset % 2 )
434 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
440 lists.wpList.process = function(a) {
441 lists.each( function() {
442 this.wpList.process(a);
446 lists.wpList.recolor = function() {
447 lists.each( function() {
448 this.wpList.recolor();
454 $.fn.wpList = function( settings ) {
455 this.each( function() {
458 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseData(this,'list')[1] || '' }, settings ) };
459 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
462 wpList.init.call(this);
464 this.wpList.process();