]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-lists.js
WordPress 4.2
[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                 if ( 'string' == typeof e ) {
313                         e = $( $.trim( e ) ); // Trim leading whitespaces
314                 } else {
315                         e = $( e );
316                 }
317
318                 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
319
320                 if ( 'string' == typeof s )
321                         s = { what: s };
322
323                 s = $.extend(_s, this.wpList.settings, s);
324
325                 if ( !e.size() || !s.what )
326                         return false;
327
328                 if ( s.oldId )
329                         old = $('#' + s.what + '-' + s.oldId);
330
331                 if ( s.id && ( s.id != s.oldId || !old || !old.size() ) )
332                         $('#' + s.what + '-' + s.id).remove();
333
334                 if ( old && old.size() ) {
335                         old.before(e);
336                         old.remove();
337                 } else if ( isNaN(s.pos) ) {
338                         ba = 'after';
339
340                         if ( '-' == s.pos.substr(0,1) ) {
341                                 s.pos = s.pos.substr(1);
342                                 ba = 'before';
343                         }
344
345                         ref = list.find( '#' + s.pos );
346
347                         if ( 1 === ref.size() )
348                                 ref[ba](e);
349                         else
350                                 list.append(e);
351
352                 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
353                         if ( s.pos < 0 ) {
354                                 list.prepend(e);
355                         } else {
356                                 list.append(e);
357                         }
358                 }
359
360                 if ( s.alt ) {
361                         if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
362                         else { e.addClass( s.alt ); }
363                 }
364
365                 if ( 'none' != s.addColor ) {
366                         color = wpList.getColor( e );
367                         e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
368                 }
369                 list.each( function() { this.wpList.process( e ); } );
370                 return e;
371         },
372
373         clear: function(e) {
374                 var list = this, t, tag;
375
376                 e = $(e);
377
378                 if ( list.wpList && e.parents( '#' + list.id ).size() )
379                         return;
380
381                 e.find(':input').each( function() {
382                         if ( $(this).parents('.form-no-clear').size() )
383                                 return;
384
385                         t = this.type.toLowerCase();
386                         tag = this.tagName.toLowerCase();
387
388                         if ( 'text' == t || 'password' == t || 'textarea' == tag )
389                                 this.value = '';
390                         else if ( 'checkbox' == t || 'radio' == t )
391                                 this.checked = false;
392                         else if ( 'select' == tag )
393                                 this.selectedIndex = null;
394                 });
395         },
396
397         process: function(el) {
398                 var list = this,
399                         $el = $(el || document);
400
401                 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
402                         return list.wpList.add(this);
403                 });
404
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);
407                 });
408
409                 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
410                         return list.wpList.del(this);
411                 });
412
413                 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
414                         return list.wpList.dim(this);
415                 });
416         },
417
418         recolor: function() {
419                 var list = this, items, eo;
420
421                 if ( !list.wpList.settings.alt )
422                         return;
423
424                 items = $('.list-item:visible', list);
425
426                 if ( !items.size() )
427                         items = $(list).children(':visible');
428
429                 eo = [':even',':odd'];
430
431                 if ( list.wpList.settings.altOffset % 2 )
432                         eo.reverse();
433
434                 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
435         },
436
437         init: function() {
438                 var lists = this;
439
440                 lists.wpList.process = function(a) {
441                         lists.each( function() {
442                                 this.wpList.process(a);
443                         } );
444                 };
445
446                 lists.wpList.recolor = function() {
447                         lists.each( function() {
448                                 this.wpList.recolor();
449                         } );
450                 };
451         }
452 };
453
454 $.fn.wpList = function( settings ) {
455         this.each( function() {
456                 var _this = this;
457
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 ); }; } );
460         } );
461
462         wpList.init.call(this);
463
464         this.wpList.process();
465
466         return this;
467 };
468
469 })(jQuery);