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