]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-lists.js
WordPress 3.8
[autoinstalls/wordpress.git] / wp-includes / js / wp-lists.js
1 /* global ajaxurl, wpAjax */
2 (function($) {
3 var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'}, wpList;
4
5 wpList = {
6         settings: {
7                 url: ajaxurl, type: 'POST',
8                 response: 'ajax-response',
9
10                 what: '',
11                 alt: 'alternate', altOffset: 0,
12                 addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
13
14                 confirm: null,
15                 addBefore: null, addAfter: null,
16                 delBefore: null, delAfter: null,
17                 dimBefore: null, dimAfter: null
18         },
19
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;
23         },
24
25         parseData: function(e,t) {
26                 var d = [], wpListsData;
27
28                 try {
29                         wpListsData = $(e).attr('data-wp-lists') || '';
30                         wpListsData = wpListsData.match(new RegExp(t+':[\\S]+'));
31
32                         if ( wpListsData )
33                                 d = wpListsData[0].split(':');
34                 } catch(r) {}
35
36                 return d;
37         },
38
39         pre: function(e,s,a) {
40                 var bg, r;
41
42                 s = $.extend( {}, this.wpList.settings, {
43                         element: null,
44                         nonce: 0,
45                         target: e.get(0)
46                 }, s || {} );
47
48                 if ( $.isFunction( s.confirm ) ) {
49                         if ( 'add' != a ) {
50                                 bg = $('#' + s.element).css('backgroundColor');
51                                 $('#' + s.element).css('backgroundColor', '#FF9966');
52                         }
53                         r = s.confirm.call(this, e, s, a, bg);
54
55                         if ( 'add' != a )
56                                 $('#' + s.element).css('backgroundColor', bg );
57
58                         if ( !r )
59                                 return false;
60                 }
61
62                 return s;
63         },
64
65         ajaxAdd: function( e, s ) {
66                 e = $(e);
67                 s = s || {};
68                 var list = this, data = wpList.parseData(e,'add'), es, valid, formData, res, rres;
69
70                 s = wpList.pre.call( list, e, s, 'add' );
71
72                 s.element = data[2] || e.attr( 'id' ) || s.element || null;
73
74                 if ( data[3] )
75                         s.addColor = '#' + data[3];
76                 else
77                         s.addColor = s.addColor || '#FFFF33';
78
79                 if ( !s )
80                         return false;
81
82                 if ( !e.is('[id="' + s.element + '-submit"]') )
83                         return !wpList.add.call( list, e, s );
84
85                 if ( !s.element )
86                         return true;
87
88                 s.action = 'add-' + s.what;
89
90                 s.nonce = wpList.nonce(e,s);
91
92                 es = $('#' + s.element + ' :input').not('[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]');
93                 valid = wpAjax.validateForm( '#' + s.element );
94
95                 if ( !valid )
96                         return false;
97
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();
100
101                 if ( formData )
102                         s.data += '&' + formData;
103
104                 if ( $.isFunction(s.addBefore) ) {
105                         s = s.addBefore( s );
106                         if ( !s )
107                                 return true;
108                 }
109
110                 if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) )
111                         return true;
112
113                 s.success = function(r) {
114                         res = wpAjax.parseAjaxResponse(r, s.response, s.element);
115
116                         rres = r;
117
118                         if ( !res || res.errors )
119                                 return false;
120
121                         if ( true === res )
122                                 return true;
123
124                         jQuery.each( res.responses, function() {
125                                 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
126                                         pos: this.position || 0,
127                                         id: this.id || 0,
128                                         oldId: this.oldId || null
129                                 } ) );
130                         } );
131
132                         list.wpList.recolor();
133                         $(list).trigger( 'wpListAddEnd', [ s, list.wpList ] );
134                         wpList.clear.call(list,'#' + s.element);
135                 };
136
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 );
141                         }
142                 };
143
144                 $.ajax( s );
145                 return false;
146         },
147
148         ajaxDel: function( e, s ) {
149                 e = $(e);
150                 s = s || {};
151                 var list = this, data = wpList.parseData(e,'delete'), element, res, rres;
152
153                 s = wpList.pre.call( list, e, s, 'delete' );
154
155                 s.element = data[2] || s.element || null;
156
157                 if ( data[3] )
158                         s.delColor = '#' + data[3];
159                 else
160                         s.delColor = s.delColor || '#faa';
161
162                 if ( !s || !s.element )
163                         return false;
164
165                 s.action = 'delete-' + s.what;
166
167                 s.nonce = wpList.nonce(e,s);
168
169                 s.data = $.extend(
170                         { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
171                         wpAjax.unserialize( data[4] || '' )
172                 );
173
174                 if ( $.isFunction(s.delBefore) ) {
175                         s = s.delBefore( s, list );
176                         if ( !s )
177                                 return true;
178                 }
179
180                 if ( !s.data._ajax_nonce )
181                         return true;
182
183                 element = $('#' + s.element);
184
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 ] );
189                         });
190                 } else {
191                         list.wpList.recolor();
192                         $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
193                 }
194
195                 s.success = function(r) {
196                         res = wpAjax.parseAjaxResponse(r, s.response, s.element);
197                         rres = r;
198
199                         if ( !res || res.errors ) {
200                                 element.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
201                                 return false;
202                         }
203                 };
204
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 );
210                                 }).dequeue();
211                         }
212                 };
213
214                 $.ajax( s );
215                 return false;
216         },
217
218         ajaxDim: function( e, s ) {
219                 if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
220                         return false;
221
222                 e = $(e);
223                 s = s || {};
224
225                 var list = this, data = wpList.parseData(e,'dim'), element, isClass, color, dimColor, res, rres;
226
227                 s = wpList.pre.call( list, e, s, 'dim' );
228
229                 s.element = data[2] || s.element || null;
230                 s.dimClass =  data[3] || s.dimClass || null;
231
232                 if ( data[4] )
233                         s.dimAddColor = '#' + data[4];
234                 else
235                         s.dimAddColor = s.dimAddColor || '#FFFF33';
236
237                 if ( data[5] )
238                         s.dimDelColor = '#' + data[5];
239                 else
240                         s.dimDelColor = s.dimDelColor || '#FF3333';
241
242                 if ( !s || !s.element || !s.dimClass )
243                         return true;
244
245                 s.action = 'dim-' + s.what;
246
247                 s.nonce = wpList.nonce(e,s);
248
249                 s.data = $.extend(
250                         { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
251                         wpAjax.unserialize( data[6] || '' )
252                 );
253
254                 if ( $.isFunction(s.dimBefore) ) {
255                         s = s.dimBefore( s );
256                         if ( !s )
257                                 return true;
258                 }
259
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;
265
266                 if ( 'none' != dimColor ) {
267                         element
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 ] );
273                                         }
274                                 });
275                 } else {
276                         $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
277                 }
278
279                 if ( !s.data._ajax_nonce )
280                         return true;
281
282                 s.success = function(r) {
283                         res = wpAjax.parseAjaxResponse(r, s.response, s.element);
284                         rres = r;
285
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(); } );
288                                 return false;
289                         }
290                 };
291
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 );
297                                 }).dequeue();
298                         }
299                 };
300
301                 $.ajax( s );
302                 return false;
303         },
304
305         getColor: function( el ) {
306                 var color = jQuery(el).css('backgroundColor');
307
308                 return color || '#ffffff';
309         },
310
311         add: function( e, s ) {
312                 e = $( $.trim(e) ); // Trim leading whitespaces
313
314                 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
315
316                 if ( 'string' == typeof s )
317                         s = { what: s };
318
319                 s = $.extend(_s, this.wpList.settings, s);
320
321                 if ( !e.size() || !s.what )
322                         return false;
323
324                 if ( s.oldId )
325                         old = $('#' + s.what + '-' + s.oldId);
326
327                 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) )
328                         $('#' + s.what + '-' + s.id).remove();
329
330                 if ( old && old.size() ) {
331                         old.before(e);
332                         old.remove();
333                 } else if ( isNaN(s.pos) ) {
334                         ba = 'after';
335
336                         if ( '-' == s.pos.substr(0,1) ) {
337                                 s.pos = s.pos.substr(1);
338                                 ba = 'before';
339                         }
340
341                         ref = list.find( '#' + s.pos );
342
343                         if ( 1 === ref.size() )
344                                 ref[ba](e);
345                         else
346                                 list.append(e);
347
348                 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
349                         if ( s.pos < 0 ) {
350                                 list.prepend(e);
351                         } else {
352                                 list.append(e);
353                         }
354                 }
355
356                 if ( s.alt ) {
357                         if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
358                         else { e.addClass( s.alt ); }
359                 }
360
361                 if ( 'none' != s.addColor ) {
362                         color = wpList.getColor( e );
363                         e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
364                 }
365                 list.each( function() { this.wpList.process( e ); } );
366                 return e;
367         },
368
369         clear: function(e) {
370                 var list = this, t, tag;
371
372                 e = $(e);
373
374                 if ( list.wpList && e.parents( '#' + list.id ).size() )
375                         return;
376
377                 e.find(':input').each( function() {
378                         if ( $(this).parents('.form-no-clear').size() )
379                                 return;
380
381                         t = this.type.toLowerCase();
382                         tag = this.tagName.toLowerCase();
383
384                         if ( 'text' == t || 'password' == t || 'textarea' == tag )
385                                 this.value = '';
386                         else if ( 'checkbox' == t || 'radio' == t )
387                                 this.checked = false;
388                         else if ( 'select' == tag )
389                                 this.selectedIndex = null;
390                 });
391         },
392
393         process: function(el) {
394                 var list = this,
395                         $el = $(el || document);
396
397                 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
398                         return list.wpList.add(this);
399                 });
400
401                 $el.delegate( 'a[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', 'click', function(){
402                         return list.wpList.add(this);
403                 });
404
405                 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
406                         return list.wpList.del(this);
407                 });
408
409                 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
410                         return list.wpList.dim(this);
411                 });
412         },
413
414         recolor: function() {
415                 var list = this, items, eo;
416
417                 if ( !list.wpList.settings.alt )
418                         return;
419
420                 items = $('.list-item:visible', list);
421
422                 if ( !items.size() )
423                         items = $(list).children(':visible');
424
425                 eo = [':even',':odd'];
426
427                 if ( list.wpList.settings.altOffset % 2 )
428                         eo.reverse();
429
430                 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
431         },
432
433         init: function() {
434                 var lists = this;
435
436                 lists.wpList.process = function(a) {
437                         lists.each( function() {
438                                 this.wpList.process(a);
439                         } );
440                 };
441
442                 lists.wpList.recolor = function() {
443                         lists.each( function() {
444                                 this.wpList.recolor();
445                         } );
446                 };
447         }
448 };
449
450 $.fn.wpList = function( settings ) {
451         this.each( function() {
452                 var _this = this;
453
454                 this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseData(this,'list')[1] || '' }, settings ) };
455                 $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
456         } );
457
458         wpList.init.call(this);
459
460         this.wpList.process();
461
462         return this;
463 };
464
465 })(jQuery);