]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.js
WordPress 4.4
[autoinstalls/wordpress.git] / wp-admin / js / inline-edit-post.js
1 /* global inlineEditL10n, ajaxurl, typenow */
2
3 var inlineEditPost;
4 (function($) {
5 inlineEditPost = {
6
7         init : function(){
8                 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
9
10                 t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
11                 t.what = '#post-';
12
13                 // prepare the edit rows
14                 qeRow.keyup(function(e){
15                         if ( e.which === 27 ) {
16                                 return inlineEditPost.revert();
17                         }
18                 });
19                 bulkRow.keyup(function(e){
20                         if ( e.which === 27 ) {
21                                 return inlineEditPost.revert();
22                         }
23                 });
24
25                 $( '.cancel', qeRow ).click( function() {
26                         return inlineEditPost.revert();
27                 });
28                 $( '.save', qeRow ).click( function() {
29                         return inlineEditPost.save(this);
30                 });
31                 $('td', qeRow).keydown(function(e){
32                         if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
33                                 return inlineEditPost.save(this);
34                         }
35                 });
36
37                 $( '.cancel', bulkRow ).click( function() {
38                         return inlineEditPost.revert();
39                 });
40
41                 $('#inline-edit .inline-edit-private input[value="private"]').click( function(){
42                         var pw = $('input.inline-edit-password-input');
43                         if ( $(this).prop('checked') ) {
44                                 pw.val('').prop('disabled', true);
45                         } else {
46                                 pw.prop('disabled', false);
47                         }
48                 });
49
50                 // add events
51                 $('#the-list').on( 'click', 'a.editinline', function( e ) {
52                         e.preventDefault();
53                         inlineEditPost.edit(this);
54                 });
55
56                 $('#bulk-edit').find('fieldset:first').after(
57                         $('#inline-edit fieldset.inline-edit-categories').clone()
58                 ).siblings( 'fieldset:last' ).prepend(
59                         $('#inline-edit label.inline-edit-tags').clone()
60                 );
61
62                 $('select[name="_status"] option[value="future"]', bulkRow).remove();
63
64                 $('#doaction, #doaction2').click(function(e){
65                         var n = $(this).attr('id').substr(2);
66                         if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
67                                 e.preventDefault();
68                                 t.setBulk();
69                         } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
70                                 t.revert();
71                         }
72                 });
73         },
74
75         toggle : function(el){
76                 var t = this;
77                 $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
78         },
79
80         setBulk : function(){
81                 var te = '', type = this.type, tax, c = true;
82                 this.revert();
83
84                 $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
85                 // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
86                 $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
87                 $('#bulk-edit').addClass('inline-editor').show();
88
89                 $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
90                         if ( $(this).prop('checked') ) {
91                                 c = false;
92                                 var id = $(this).val(), theTitle;
93                                 theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
94                                 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
95                         }
96                 });
97
98                 if ( c ) {
99                         return this.revert();
100                 }
101
102                 $('#bulk-titles').html(te);
103                 $('#bulk-titles a').click(function(){
104                         var id = $(this).attr('id').substr(1);
105
106                         $('table.widefat input[value="' + id + '"]').prop('checked', false);
107                         $('#ttle'+id).remove();
108                 });
109
110                 // enable autocomplete for tags
111                 if ( 'post' === type ) {
112                         // support multi taxonomies?
113                         tax = 'post_tag';
114                         $('tr.inline-editor textarea[name="tax_input['+tax+']"]').suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma } );
115                 }
116                 $('html, body').animate( { scrollTop: 0 }, 'fast' );
117         },
118
119         edit : function(id) {
120                 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f, val, pw;
121                 t.revert();
122
123                 if ( typeof(id) === 'object' ) {
124                         id = t.getId(id);
125                 }
126
127                 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order'];
128                 if ( t.type === 'page' ) {
129                         fields.push('post_parent', 'page_template');
130                 }
131
132                 // add the new edit row with an extra blank row underneath to maintain zebra striping.
133                 editRow = $('#inline-edit').clone(true);
134                 $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
135
136                 $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
137
138                 // populate the data
139                 rowData = $('#inline_'+id);
140                 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
141                         // author no longer has edit caps, so we need to add them to the list of authors
142                         $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
143                 }
144                 if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
145                         $('label.inline-edit-author', editRow).hide();
146                 }
147
148                 // hide unsupported formats, but leave the current format alone
149                 cur_format = $('.post_format', rowData).text();
150                 $('option.unsupported', editRow).each(function() {
151                         var $this = $(this);
152                         if ( $this.val() !== cur_format ) {
153                                 $this.remove();
154                         }
155                 });
156
157                 for ( f = 0; f < fields.length; f++ ) {
158                         val = $('.'+fields[f], rowData);
159                         // Deal with Twemoji
160                         val.find( 'img' ).replaceWith( function() { return this.alt; } );
161                         val = val.text();
162                         $(':input[name="' + fields[f] + '"]', editRow).val( val );
163                 }
164
165                 if ( $( '.comment_status', rowData ).text() === 'open' ) {
166                         $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
167                 }
168                 if ( $( '.ping_status', rowData ).text() === 'open' ) {
169                         $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
170                 }
171                 if ( $( '.sticky', rowData ).text() === 'sticky' ) {
172                         $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
173                 }
174
175                 // hierarchical taxonomies
176                 $('.post_category', rowData).each(function(){
177                         var taxname,
178                                 term_ids = $(this).text();
179
180                         if ( term_ids ) {
181                                 taxname = $(this).attr('id').replace('_'+id, '');
182                                 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
183                         }
184                 });
185
186                 //flat taxonomies
187                 $('.tags_input', rowData).each(function(){
188                         var terms = $(this),
189                                 taxname = $(this).attr('id').replace('_' + id, ''),
190                                 textarea = $('textarea.tax_input_' + taxname, editRow),
191                                 comma = inlineEditL10n.comma;
192
193                         terms.find( 'img' ).replaceWith( function() { return this.alt; } );
194                         terms = terms.text();
195
196                         if ( terms ) {
197                                 if ( ',' !== comma ) {
198                                         terms = terms.replace(/,/g, comma);
199                                 }
200                                 textarea.val(terms);
201                         }
202
203                         textarea.suggest( ajaxurl + '?action=ajax-tag-search&tax=' + taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma } );
204                 });
205
206                 // handle the post status
207                 status = $('._status', rowData).text();
208                 if ( 'future' !== status ) {
209                         $('select[name="_status"] option[value="future"]', editRow).remove();
210                 }
211
212                 pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
213                 if ( 'private' === status ) {
214                         $('input[name="keep_private"]', editRow).prop('checked', true);
215                         pw.val( '' ).prop( 'disabled', true );
216                 }
217
218                 // remove the current page and children from the parent dropdown
219                 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
220                 if ( pageOpt.length > 0 ) {
221                         pageLevel = pageOpt[0].className.split('-')[1];
222                         nextPage = pageOpt;
223                         while ( pageLoop ) {
224                                 nextPage = nextPage.next('option');
225                                 if ( nextPage.length === 0 ) {
226                                         break;
227                                 }
228
229                                 nextLevel = nextPage[0].className.split('-')[1];
230
231                                 if ( nextLevel <= pageLevel ) {
232                                         pageLoop = false;
233                                 } else {
234                                         nextPage.remove();
235                                         nextPage = pageOpt;
236                                 }
237                         }
238                         pageOpt.remove();
239                 }
240
241                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
242                 $('.ptitle', editRow).focus();
243
244                 return false;
245         },
246
247         save : function(id) {
248                 var params, fields, page = $('.post_status_page').val() || '';
249
250                 if ( typeof(id) === 'object' ) {
251                         id = this.getId(id);
252                 }
253
254                 $( 'table.widefat .spinner' ).addClass( 'is-active' );
255
256                 params = {
257                         action: 'inline-save',
258                         post_type: typenow,
259                         post_ID: id,
260                         edit_date: 'true',
261                         post_status: page
262                 };
263
264                 fields = $('#edit-'+id).find(':input').serialize();
265                 params = fields + '&' + $.param(params);
266
267                 // make ajax request
268                 $.post( ajaxurl, params,
269                         function(r) {
270                                 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
271                                 $( '.ac_results' ).hide();
272
273                                 if (r) {
274                                         if ( -1 !== r.indexOf( '<tr' ) ) {
275                                                 $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
276                                                 $('#edit-'+id).before(r).remove();
277                                                 $(inlineEditPost.what+id).hide().fadeIn();
278                                         } else {
279                                                 r = r.replace( /<.[^<>]*?>/g, '' );
280                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
281                                         }
282                                 } else {
283                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
284                                 }
285                         },
286                 'html');
287                 return false;
288         },
289
290         revert : function(){
291                 var $tableWideFat = $( '.widefat' ),
292                         id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
293
294                 if ( id ) {
295                         $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
296                         $( '.ac_results' ).hide();
297
298                         if ( 'bulk-edit' === id ) {
299                                 $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
300                                 $('#bulk-titles').empty();
301                                 $('#inlineedit').append( $('#bulk-edit') );
302                         } else {
303                                 $('#'+id).siblings('tr.hidden').addBack().remove();
304                                 id = id.substr( id.lastIndexOf('-') + 1 );
305                                 $(this.what+id).show();
306                         }
307                 }
308
309                 return false;
310         },
311
312         getId : function(o) {
313                 var id = $(o).closest('tr').attr('id'),
314                         parts = id.split('-');
315                 return parts[parts.length - 1];
316         }
317 };
318
319 $( document ).ready( function(){ inlineEditPost.init(); } );
320
321 // Show/hide locks on posts
322 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
323         var locked = data['wp-check-locked-posts'] || {};
324
325         $('#the-list tr').each( function(i, el) {
326                 var key = el.id, row = $(el), lock_data, avatar;
327
328                 if ( locked.hasOwnProperty( key ) ) {
329                         if ( ! row.hasClass('wp-locked') ) {
330                                 lock_data = locked[key];
331                                 row.find('.column-title .locked-text').text( lock_data.text );
332                                 row.find('.check-column checkbox').prop('checked', false);
333
334                                 if ( lock_data.avatar_src ) {
335                                         avatar = $( '<img class="avatar avatar-18 photo" width="18" height="18" alt="" />' ).attr( 'src', lock_data.avatar_src.replace( /&amp;/g, '&' ) );
336                                         row.find('.column-title .locked-avatar').empty().append( avatar );
337                                 }
338                                 row.addClass('wp-locked');
339                         }
340                 } else if ( row.hasClass('wp-locked') ) {
341                         // Make room for the CSS animation
342                         row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
343                 }
344         });
345 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
346         var check = [];
347
348         $('#the-list tr').each( function(i, el) {
349                 if ( el.id ) {
350                         check.push( el.id );
351                 }
352         });
353
354         if ( check.length ) {
355                 data['wp-check-locked-posts'] = check;
356         }
357 }).ready( function() {
358         // Set the heartbeat interval to 15 sec.
359         if ( typeof wp !== 'undefined' && wp.heartbeat ) {
360                 wp.heartbeat.interval( 15 );
361         }
362 });
363
364 }(jQuery));