]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/wp-lists.js
Wordpress 4.6
[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 ( true === res ) {
301                                 return true;
302                         }
303
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(); } );
306                                 return false;
307                         }
308
309                         if ( 'undefined' !== typeof res.responses[0].supplemental.comment_link ) {
310                                 var submittedOn = element.find( '.submitted-on' ),
311                                         commentLink = submittedOn.find( 'a' );
312
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 ) );
316
317                                 // Comment is not approved; unlink the date field.
318                                 } else if ( commentLink.length ) {
319                                         submittedOn.text( commentLink.text() );
320                                 }
321                         }
322                 };
323
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 );
329                                 }).dequeue();
330                         }
331                 };
332
333                 $.ajax( s );
334                 return false;
335         },
336
337         getColor: function( el ) {
338                 var color = jQuery(el).css('backgroundColor');
339
340                 return color || '#ffffff';
341         },
342
343         add: function( e, s ) {
344                 if ( 'string' == typeof e ) {
345                         e = $( $.trim( e ) ); // Trim leading whitespaces
346                 } else {
347                         e = $( e );
348                 }
349
350                 var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
351
352                 if ( 'string' == typeof s )
353                         s = { what: s };
354
355                 s = $.extend(_s, this.wpList.settings, s);
356
357                 if ( !e.length || !s.what )
358                         return false;
359
360                 if ( s.oldId )
361                         old = $('#' + s.what + '-' + s.oldId);
362
363                 if ( s.id && ( s.id != s.oldId || !old || !old.length ) )
364                         $('#' + s.what + '-' + s.id).remove();
365
366                 if ( old && old.length ) {
367                         old.before(e);
368                         old.remove();
369                 } else if ( isNaN(s.pos) ) {
370                         ba = 'after';
371
372                         if ( '-' == s.pos.substr(0,1) ) {
373                                 s.pos = s.pos.substr(1);
374                                 ba = 'before';
375                         }
376
377                         ref = list.find( '#' + s.pos );
378
379                         if ( 1 === ref.length )
380                                 ref[ba](e);
381                         else
382                                 list.append(e);
383
384                 } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
385                         if ( s.pos < 0 ) {
386                                 list.prepend(e);
387                         } else {
388                                 list.append(e);
389                         }
390                 }
391
392                 if ( s.alt ) {
393                         if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
394                         else { e.addClass( s.alt ); }
395                 }
396
397                 if ( 'none' != s.addColor ) {
398                         color = wpList.getColor( e );
399                         e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
400                 }
401                 list.each( function() { this.wpList.process( e ); } );
402                 return e;
403         },
404
405         clear: function(e) {
406                 var list = this, t, tag;
407
408                 e = $(e);
409
410                 if ( list.wpList && e.parents( '#' + list.id ).length )
411                         return;
412
413                 e.find(':input').each( function() {
414                         if ( $(this).parents('.form-no-clear').length )
415                                 return;
416
417                         t = this.type.toLowerCase();
418                         tag = this.tagName.toLowerCase();
419
420                         if ( 'text' == t || 'password' == t || 'textarea' == tag )
421                                 this.value = '';
422                         else if ( 'checkbox' == t || 'radio' == t )
423                                 this.checked = false;
424                         else if ( 'select' == tag )
425                                 this.selectedIndex = null;
426                 });
427         },
428
429         process: function(el) {
430                 var list = this,
431                         $el = $(el || document);
432
433                 $el.delegate( 'form[data-wp-lists^="add:' + list.id + ':"]', 'submit', function(){
434                         return list.wpList.add(this);
435                 });
436
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);
439                 });
440
441                 $el.delegate( '[data-wp-lists^="delete:' + list.id + ':"]', 'click', function(){
442                         return list.wpList.del(this);
443                 });
444
445                 $el.delegate( '[data-wp-lists^="dim:' + list.id + ':"]', 'click', function(){
446                         return list.wpList.dim(this);
447                 });
448         },
449
450         recolor: function() {
451                 var list = this, items, eo;
452
453                 if ( !list.wpList.settings.alt )
454                         return;
455
456                 items = $('.list-item:visible', list);
457
458                 if ( !items.length )
459                         items = $(list).children(':visible');
460
461                 eo = [':even',':odd'];
462
463                 if ( list.wpList.settings.altOffset % 2 )
464                         eo.reverse();
465
466                 items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
467         },
468
469         init: function() {
470                 var lists = this;
471
472                 lists.wpList.process = function(a) {
473                         lists.each( function() {
474                                 this.wpList.process(a);
475                         } );
476                 };
477
478                 lists.wpList.recolor = function() {
479                         lists.each( function() {
480                                 this.wpList.recolor();
481                         } );
482                 };
483         }
484 };
485
486 $.fn.wpList = function( settings ) {
487         this.each( function() {
488                 var _this = this;
489
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 ); }; } );
492         } );
493
494         wpList.init.call(this);
495
496         this.wpList.process();
497
498         return this;
499 };
500
501 })(jQuery);