]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.js
WordPress 4.2.5-scripts
[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                 $('a.cancel', qeRow).click(function(){
26                         return inlineEditPost.revert();
27                 });
28                 $('a.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                 $('a.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(){
52                         inlineEditPost.edit(this);
53                         return false;
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', $('.widefat:first thead th:visible').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;
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', $('.widefat:first thead th:visible').length);
135
136                 $(t.what+id).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                 if ( 'private' === status ) {
213                         $('input[name="keep_private"]', editRow).prop('checked', true);
214                         $('input.inline-edit-password-input').val('').prop('disabled', true);
215                 }
216
217                 // remove the current page and children from the parent dropdown
218                 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
219                 if ( pageOpt.length > 0 ) {
220                         pageLevel = pageOpt[0].className.split('-')[1];
221                         nextPage = pageOpt;
222                         while ( pageLoop ) {
223                                 nextPage = nextPage.next('option');
224                                 if ( nextPage.length === 0 ) {
225                                         break;
226                                 }
227
228                                 nextLevel = nextPage[0].className.split('-')[1];
229
230                                 if ( nextLevel <= pageLevel ) {
231                                         pageLoop = false;
232                                 } else {
233                                         nextPage.remove();
234                                         nextPage = pageOpt;
235                                 }
236                         }
237                         pageOpt.remove();
238                 }
239
240                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
241                 $('.ptitle', editRow).focus();
242
243                 return false;
244         },
245
246         save : function(id) {
247                 var params, fields, page = $('.post_status_page').val() || '';
248
249                 if ( typeof(id) === 'object' ) {
250                         id = this.getId(id);
251                 }
252
253                 $( 'table.widefat .spinner' ).addClass( 'is-active' );
254
255                 params = {
256                         action: 'inline-save',
257                         post_type: typenow,
258                         post_ID: id,
259                         edit_date: 'true',
260                         post_status: page
261                 };
262
263                 fields = $('#edit-'+id).find(':input').serialize();
264                 params = fields + '&' + $.param(params);
265
266                 // make ajax request
267                 $.post( ajaxurl, params,
268                         function(r) {
269                                 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
270
271                                 if (r) {
272                                         if ( -1 !== r.indexOf( '<tr' ) ) {
273                                                 $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
274                                                 $('#edit-'+id).before(r).remove();
275                                                 $(inlineEditPost.what+id).hide().fadeIn();
276                                         } else {
277                                                 r = r.replace( /<.[^<>]*?>/g, '' );
278                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
279                                         }
280                                 } else {
281                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
282                                 }
283                         },
284                 'html');
285                 return false;
286         },
287
288         revert : function(){
289                 var id = $('table.widefat tr.inline-editor').attr('id');
290
291                 if ( id ) {
292                         $( 'table.widefat .spinner' ).removeClass( 'is-active' );
293
294                         if ( 'bulk-edit' === id ) {
295                                 $('table.widefat #bulk-edit').removeClass('inline-editor').hide().siblings('tr.hidden').remove();
296                                 $('#bulk-titles').empty();
297                                 $('#inlineedit').append( $('#bulk-edit') );
298                         } else {
299                                 $('#'+id).siblings('tr.hidden').addBack().remove();
300                                 id = id.substr( id.lastIndexOf('-') + 1 );
301                                 $(this.what+id).show();
302                         }
303                 }
304
305                 return false;
306         },
307
308         getId : function(o) {
309                 var id = $(o).closest('tr').attr('id'),
310                         parts = id.split('-');
311                 return parts[parts.length - 1];
312         }
313 };
314
315 $( document ).ready( function(){ inlineEditPost.init(); } );
316
317 // Show/hide locks on posts
318 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
319         var locked = data['wp-check-locked-posts'] || {};
320
321         $('#the-list tr').each( function(i, el) {
322                 var key = el.id, row = $(el), lock_data, avatar;
323
324                 if ( locked.hasOwnProperty( key ) ) {
325                         if ( ! row.hasClass('wp-locked') ) {
326                                 lock_data = locked[key];
327                                 row.find('.column-title .locked-text').text( lock_data.text );
328                                 row.find('.check-column checkbox').prop('checked', false);
329
330                                 if ( lock_data.avatar_src ) {
331                                         avatar = $('<img class="avatar avatar-18 photo" width="18" height="18" />').attr( 'src', lock_data.avatar_src.replace(/&amp;/g, '&') );
332                                         row.find('.column-title .locked-avatar').empty().append( avatar );
333                                 }
334                                 row.addClass('wp-locked');
335                         }
336                 } else if ( row.hasClass('wp-locked') ) {
337                         // Make room for the CSS animation
338                         row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
339                 }
340         });
341 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
342         var check = [];
343
344         $('#the-list tr').each( function(i, el) {
345                 if ( el.id ) {
346                         check.push( el.id );
347                 }
348         });
349
350         if ( check.length ) {
351                 data['wp-check-locked-posts'] = check;
352         }
353 }).ready( function() {
354         // Set the heartbeat interval to 15 sec.
355         if ( typeof wp !== 'undefined' && wp.heartbeat ) {
356                 wp.heartbeat.interval( 15 );
357         }
358 });
359
360 }(jQuery));