1
1
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
2
2
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply */
5
* Contains all dynamic functionality needed on post and term pages.
7
* @summary Control page and term functionality.
4
10
var commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
5
// Back-compat: prevent fatal errors
11
// Backwards compatibility: prevent fatal errors.
6
12
makeSlugeditClickable = editPermalink = function(){};
14
// Make sure the wp object exists.
8
15
window.wp = window.wp || {};
11
18
var titleHasFocus = false;
16
get : function(total, num) {
17
var st = this.st, data;
23
$( '#commentsdiv .spinner' ).addClass( 'is-active' );
26
'action' : 'get-comments',
28
'_ajax_nonce' : $('#add_comment_nonce').val(),
29
'p' : $('#post_ID').val(),
36
r = wpAjax.parseAjaxResponse(r);
37
$('#commentsdiv .widefat').show();
38
$( '#commentsdiv .spinner' ).removeClass( 'is-active' );
40
if ( 'object' == typeof r && r.responses[0] ) {
41
$('#the-comment-list').append( r.responses[0].data );
43
theList = theExtraList = null;
44
$( 'a[className*=\':\']' ).unbind();
46
if ( commentsBox.st > commentsBox.total )
47
$('#show-comments').hide();
49
$('#show-comments').show().children('a').html(postL10n.showcomm);
52
} else if ( 1 == r ) {
53
$('#show-comments').html(postL10n.endcomm);
57
$('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
64
load: function(total){
65
this.st = jQuery('#the-comment-list tr.comment:visible').length;
70
WPSetThumbnailHTML = function(html){
71
$('.inside', '#postimagediv').html(html);
74
WPSetThumbnailID = function(id){
75
var field = $('input[value="_thumbnail_id"]', '#list-table');
76
if ( field.length > 0 ) {
77
$('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
81
WPRemoveThumbnail = function(nonce){
83
action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
86
alert( setPostThumbnailL10n.error );
88
WPSetThumbnailHTML(str);
94
$(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
95
var lock = $('#active_post_lock').val(),
96
post_id = $('#post_ID').val(),
99
if ( ! post_id || ! $('#post-lock-dialog').length )
102
send.post_id = post_id;
107
data['wp-refresh-post-lock'] = send;
109
}).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
110
// Post locks: update the lock string or show the dialog if somebody has taken over editing
111
var received, wrap, avatar;
113
if ( data['wp-refresh-post-lock'] ) {
114
received = data['wp-refresh-post-lock'];
116
if ( received.lock_error ) {
117
// show "editing taken over" message
118
wrap = $('#post-lock-dialog');
120
if ( wrap.length && ! wrap.is(':visible') ) {
122
// Save the latest changes and disable
123
$(document).one( 'heartbeat-tick', function() {
124
wp.autosave.server.suspend();
125
wrap.removeClass('saving').addClass('saved');
126
$(window).off( 'beforeunload.edit-post' );
129
wrap.addClass('saving');
130
wp.autosave.server.triggerSave();
133
if ( received.lock_error.avatar_src ) {
134
avatar = $( '<img class="avatar avatar-64 photo" width="64" height="64" alt="" />' ).attr( 'src', received.lock_error.avatar_src.replace( /&/g, '&' ) );
135
wrap.find('div.post-locked-avatar').empty().append( avatar );
138
wrap.show().find('.currently-editing').text( received.lock_error.text );
139
wrap.find('.wp-tab-first').focus();
141
} else if ( received.new_lock ) {
142
$('#active_post_lock').val( received.new_lock );
145
}).on( 'before-autosave.update-post-slug', function() {
146
titleHasFocus = document.activeElement && document.activeElement.id === 'title';
147
}).on( 'after-autosave.update-post-slug', function() {
148
// Create slug area only if not already there
149
// and the title field was not focused (user was not typing a title) when autosave ran
150
if ( ! $('#edit-slug-box > *').length && ! titleHasFocus ) {
152
action: 'sample-permalink',
153
post_id: $('#post_ID').val(),
154
new_title: $('#title').val(),
155
samplepermalinknonce: $('#samplepermalinknonce').val()
158
if ( data != '-1' ) {
159
$('#edit-slug-box').html(data);
21
* Control loading of comments on the post and term edit pages.
23
* @type {{st: number, get: commentsBox.get, load: commentsBox.load}}
25
* @namespace commentsBox
28
// Comment offset to use when fetching new comments.
32
* Fetch comments using AJAX and display them in the box.
34
* @param {int} total Total number of comments for this post.
35
* @param {int} num Optional. Number of comments to fetch, defaults to 20.
36
* @returns {boolean} Always returns false.
38
* @memberof commentsBox
40
get : function(total, num) {
41
var st = this.st, data;
47
$( '#commentsdiv .spinner' ).addClass( 'is-active' );
50
'action' : 'get-comments',
52
'_ajax_nonce' : $('#add_comment_nonce').val(),
53
'p' : $('#post_ID').val(),
62
r = wpAjax.parseAjaxResponse(r);
63
$('#commentsdiv .widefat').show();
64
$( '#commentsdiv .spinner' ).removeClass( 'is-active' );
66
if ( 'object' == typeof r && r.responses[0] ) {
67
$('#the-comment-list').append( r.responses[0].data );
69
theList = theExtraList = null;
70
$( 'a[className*=\':\']' ).unbind();
72
// If the offset is over the total number of comments we cannot fetch any more, so hide the button.
73
if ( commentsBox.st > commentsBox.total )
74
$('#show-comments').hide();
76
$('#show-comments').show().children('a').html(postL10n.showcomm);
79
} else if ( 1 == r ) {
80
$('#show-comments').html(postL10n.endcomm);
84
$('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
92
* Load the next batch of comments.
94
* @param {int} total Total number of comments to load.
96
* @memberof commentsBox
98
load: function(total){
99
this.st = jQuery('#the-comment-list tr.comment:visible').length;
105
* Overwrite the content of the Featured Image postbox
107
* @param {string} html New HTML to be displayed in the content area of the postbox.
111
WPSetThumbnailHTML = function(html){
112
$('.inside', '#postimagediv').html(html);
116
* Set the Image ID of the Featured Image
118
* @param {int} id The post_id of the image to use as Featured Image.
122
WPSetThumbnailID = function(id){
123
var field = $('input[value="_thumbnail_id"]', '#list-table');
124
if ( field.length > 0 ) {
125
$('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
130
* Remove the Featured Image
132
* @param {string} nonce Nonce to use in the request.
136
WPRemoveThumbnail = function(nonce){
138
action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
141
* Handle server response
143
* @param {string} str Response, will be '0' when an error occurred otherwise contains link to add Featured Image.
147
alert( setPostThumbnailL10n.error );
149
WPSetThumbnailHTML(str);
158
* Used to lock editing of an object by only one user at a time.
160
* When the user does not send a heartbeat in a heartbeat-time
161
* the user is no longer editing and another user can start editing.
163
$(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
164
var lock = $('#active_post_lock').val(),
165
post_id = $('#post_ID').val(),
168
if ( ! post_id || ! $('#post-lock-dialog').length )
171
send.post_id = post_id;
176
data['wp-refresh-post-lock'] = send;
178
}).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
179
// Post locks: update the lock string or show the dialog if somebody has taken over editing.
180
var received, wrap, avatar;
182
if ( data['wp-refresh-post-lock'] ) {
183
received = data['wp-refresh-post-lock'];
185
if ( received.lock_error ) {
186
// Show "editing taken over" message.
187
wrap = $('#post-lock-dialog');
189
if ( wrap.length && ! wrap.is(':visible') ) {
191
// Save the latest changes and disable.
192
$(document).one( 'heartbeat-tick', function() {
193
wp.autosave.server.suspend();
194
wrap.removeClass('saving').addClass('saved');
195
$(window).off( 'beforeunload.edit-post' );
198
wrap.addClass('saving');
199
wp.autosave.server.triggerSave();
202
if ( received.lock_error.avatar_src ) {
203
avatar = $( '<img class="avatar avatar-64 photo" width="64" height="64" alt="" />' ).attr( 'src', received.lock_error.avatar_src.replace( /&/g, '&' ) );
204
wrap.find('div.post-locked-avatar').empty().append( avatar );
207
wrap.show().find('.currently-editing').text( received.lock_error.text );
208
wrap.find('.wp-tab-first').focus();
210
} else if ( received.new_lock ) {
211
$('#active_post_lock').val( received.new_lock );
214
}).on( 'before-autosave.update-post-slug', function() {
215
titleHasFocus = document.activeElement && document.activeElement.id === 'title';
216
}).on( 'after-autosave.update-post-slug', function() {
219
* Create slug area only if not already there
220
* and the title field was not focused (user was not typing a title) when autosave ran.
222
if ( ! $('#edit-slug-box > *').length && ! titleHasFocus ) {
224
action: 'sample-permalink',
225
post_id: $('#post_ID').val(),
226
new_title: $('#title').val(),
227
samplepermalinknonce: $('#samplepermalinknonce').val()
230
if ( data != '-1' ) {
231
$('#edit-slug-box').html(data);
241
* Heartbeat refresh nonces.
169
244
var check, timeout;
247
* Only allow to check for nonce refresh every 30 seconds.
171
249
function schedule() {
173
251
window.clearTimeout( timeout );
450
566
$(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
451
567
$('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
453
if ( '#' + taxonomy + '-all' == t )
569
if ( '#' + taxonomy + '-all' == t ) {
454
570
deleteUserSetting( settingName );
456
572
setUserSetting( settingName, 'pop' );
459
576
if ( getUserSetting( settingName ) )
460
577
$('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
463
$( '#new' + taxonomy ).one( 'focus', function() { $( this ).val( '' ).removeClass( 'form-input-tip' ); } );
579
// Add category button controls.
580
$('#new' + taxonomy).one( 'focus', function() {
581
$( this ).val( '' ).removeClass( 'form-input-tip' );
584
// On [enter] submit the taxonomy.
465
585
$('#new' + taxonomy).keypress( function(event){
466
586
if( 13 === event.keyCode ) {
467
587
event.preventDefault();
468
588
$('#' + taxonomy + '-add-submit').click();
471
$('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
592
// After submitting a new taxonomy, re-focus the input field.
593
$('#' + taxonomy + '-add-submit').click( function() {
594
$('#new' + taxonomy).focus();
598
* Before adding a new taxonomy, disable submit button.
600
* @param {Object} s Taxonomy object which will be added.
473
604
catAddBefore = function( s ) {
474
if ( !$('#new'+taxonomy).val() )
605
if ( !$('#new'+taxonomy).val() ) {
476
609
s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
477
610
$( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
615
* Re-enable submit button after a taxonomy has been added.
617
* Re-enable submit button.
618
* If the taxonomy has a parent place the taxonomy underneath the parent.
620
* @param {Object} r Response.
621
* @param {Object} s Taxonomy data.
481
625
catAddAfter = function( r, s ) {
482
626
var sup, drop = $('#new'+taxonomy+'_parent');