1
1
/* global inlineEditL10n, ajaxurl, typenow */
3
* This file contains the functions needed for the inline editing of posts.
2
8
window.wp = window.wp || {};
11
* Manages the quick edit and bulk edit windows for editing posts or pages.
20
* @property {string} type The type of inline editor.
21
* @property {string} what The prefix before the post id.
5
25
( function( $, wp ) {
30
* @summary Initializes the inline and bulk post editor.
32
* Binds event handlers to the escape key to close the inline editor
33
* and to the save and close buttons. Changes DOM to be ready for inline
34
* editing. Adds event handler to bulk edit.
36
* @memberof inlineEditPost
9
42
var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
11
44
t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
14
// prepare the edit rows
49
* @summary Bind escape key to revert the changes and close the quick editor.
51
* @returns {boolean} The result of revert.
15
53
qeRow.keyup(function(e){
54
// Revert changes if escape key is pressed.
16
55
if ( e.which === 27 ) {
17
56
return inlineEditPost.revert();
61
* @summary Bind escape key to revert the changes and close the bulk editor.
63
* @returns {boolean} The result of revert.
20
65
bulkRow.keyup(function(e){
66
// Revert changes if escape key is pressed.
21
67
if ( e.which === 27 ) {
22
68
return inlineEditPost.revert();
73
* @summary Revert changes and close the quick editor if the cancel button is clicked.
75
* @returns {boolean} The result of revert.
26
77
$( '.cancel', qeRow ).click( function() {
27
78
return inlineEditPost.revert();
82
* @summary Save changes in the quick editor if the save(named: update) button is clicked.
84
* @returns {boolean} The result of save.
29
86
$( '.save', qeRow ).click( function() {
30
87
return inlineEditPost.save(this);
91
* @summary If enter is pressed, and the target is not the cancel button, save the post.
93
* @returns {boolean} The result of save.
32
95
$('td', qeRow).keydown(function(e){
33
96
if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
34
97
return inlineEditPost.save(this);
102
* @summary Revert changes and close the bulk editor if the cancel button is clicked.
104
* @returns {boolean} The result of revert.
38
106
$( '.cancel', bulkRow ).click( function() {
39
107
return inlineEditPost.revert();
111
* @summary Disables the password input field when the private post checkbox is checked.
42
113
$('#inline-edit .inline-edit-private input[value="private"]').click( function(){
43
114
var pw = $('input.inline-edit-password-input');
44
115
if ( $(this).prop('checked') ) {
157
* @summary Toggles the quick edit window.
159
* Hides the window when it's active and shows the window when inactive.
161
* @memberof inlineEditPost
164
* @param {Object} el Element within a post table row.
80
166
toggle : function(el){
82
168
$( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
172
* @summary Creates the bulk editor row to edit multiple posts at once.
174
* @memberof inlineEditPost
85
177
setBulk : function(){
86
178
var te = '', type = this.type, c = true;
89
181
$( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
90
183
// Insert the editor at the top of the table with an empty row above to maintain zebra striping.
91
184
$('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
92
185
$('#bulk-edit').addClass('inline-editor').show();
188
* @summary Create a HTML div with the title and a delete link(cross-icon) for each selected post.
190
* Get the selected posts based on the checked checkboxes in the post table.
191
* Create a HTML div with the title and a link(delete-icon) for each selected post.
94
193
$( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
195
// If the checkbox for a post is selected, add the post to the edit list.
95
196
if ( $(this).prop('checked') ) {
97
198
var id = $(this).val(), theTitle;
204
// If no checkboxes where checked, just hide the quick/bulk edit rows.
104
206
return this.revert();
209
// Add onclick events to the delete-icons in the bulk editors the post title list.
107
210
$('#bulk-titles').html(te);
212
* @summary Binds on click events to the checkboxes before the posts in the table.
108
216
$('#bulk-titles a').click(function(){
109
217
var id = $(this).attr('id').substr(1);
127
235
$( element ).wpTagsSuggest();
239
// Scrolls to the top of the table where the editor is rendered.
130
240
$('html, body').animate( { scrollTop: 0 }, 'fast' );
244
* @summary Creates a quick edit window for the post that has been clicked.
246
* @memberof inlineEditPost
249
* @param {number|Object} id The id of the clicked post or an element within a post
251
* @returns {boolean} Always returns false at the end of execution.
133
253
edit : function(id) {
134
254
var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
143
263
fields.push('post_parent');
146
// add the new edit row with an extra blank row underneath to maintain zebra striping.
266
// Add the new edit row with an extra blank row underneath to maintain zebra striping.
147
267
editRow = $('#inline-edit').clone(true);
148
268
$( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
150
270
$(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
272
// Populate fields in the quick edit window.
153
273
rowData = $('#inline_'+id);
154
274
if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
155
// author no longer has edit caps, so we need to add them to the list of authors
276
// The post author no longer has edit capabilities, so we need to add them to the list of authors.
156
277
$(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
158
279
if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
162
283
for ( f = 0; f < fields.length; f++ ) {
163
284
val = $('.'+fields[f], rowData);
287
* @summary Replaces the image for a Twemoji(Twitter emoji) with it's alternate text.
289
* @returns Alternate text from the image.
165
291
val.find( 'img' ).replaceWith( function() { return this.alt; } );
166
292
val = val.text();
167
293
$(':input[name="' + fields[f] + '"]', editRow).val( val );
208
339
textarea.wpTagsSuggest();
211
// handle the post status
342
// Handle the post status.
212
343
status = $('._status', rowData).text();
213
344
if ( 'future' !== status ) {
214
345
$('select[name="_status"] option[value="future"]', editRow).remove();
220
351
pw.val( '' ).prop( 'disabled', true );
223
// remove the current page and children from the parent dropdown
354
// Remove the current page and children from the parent dropdown.
224
355
pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
225
356
if ( pageOpt.length > 0 ) {
226
357
pageLevel = pageOpt[0].className.split('-')[1];
252
// Ajax saving is only for Quick Edit.
384
* @summary Saves the changes made in the quick edit window to the post.
385
* AJAX saving is only for Quick Edit and not for bulk edit.
389
* @param {int} id The id for the post that has been changed.
390
* @returns {boolean} false, so the form does not submit when pressing
391
* Enter on a focused field.
253
393
save : function(id) {
254
394
var params, fields, page = $('.post_status_page').val() || '';
301
442
// Prevent submitting the form when pressing Enter on a focused field.
305
// Revert is for both Quick Edit and Bulk Edit.
447
* @summary Hides and empties the Quick Edit and/or Bulk Edit windows.
449
* @memberof inlineEditPost
452
* @returns {boolean} Always returns false.
306
454
revert : function(){
307
455
var $tableWideFat = $( '.widefat' ),
308
456
id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
312
460
$( '.ac_results' ).hide();
314
462
if ( 'bulk-edit' === id ) {
464
// Hide the bulk editor.
315
465
$( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
316
466
$('#bulk-titles').empty();
468
// Store the empty bulk editor in a hidden element.
317
469
$('#inlineedit').append( $('#bulk-edit') );
318
471
// Move focus back to the Bulk Action button that was activated.
319
472
$( '#' + inlineEditPost.whichBulkButtonId ).focus();
475
// Remove both the inline-editor and its hidden tr siblings.
321
476
$('#'+id).siblings('tr.hidden').addBack().remove();
322
477
id = id.substr( id.lastIndexOf('-') + 1 );
323
479
// Show the post row and move focus back to the Quick Edit link.
324
480
$( this.what + id ).show().find( '.editinline' ).focus();
488
* @summary Gets the id for a the post that you want to quick edit from the row
489
* in the quick edit table.
491
* @memberof inlineEditPost
494
* @param {Object} o DOM row object to get the id for.
495
* @returns {string} The post id extracted from the table row in the object.
331
497
getId : function(o) {
332
498
var id = $(o).closest('tr').attr('id'),
333
499
parts = id.split('-');