| 1 |
/** |
| 2 |
* Contains all dynamic functionality needed on post and term pages. |
| 3 |
* |
| 4 |
* @summary Control page and term functionality. |
| 5 |
*/ |
| 6 |
|
| 7 |
|
| 8 |
// Make sure the wp object exists. |
| 9 |
window.wp = window.wp || {}; |
| 10 |
|
| 11 |
let mainwp_post_newmeta_submit = function (action, me) { |
| 12 |
|
| 13 |
if (action != 'add' && action != 'delete' && action != 'update') |
| 14 |
return false; |
| 15 |
|
| 16 |
let data; |
| 17 |
let newkey; |
| 18 |
|
| 19 |
if (action == 'add') { |
| 20 |
|
| 21 |
let metakey = jQuery('#metakeyselect').val(); |
| 22 |
newkey = false; |
| 23 |
if (jQuery('#metakeyinput').is(':visible')) { |
| 24 |
metakey = '#NONE#'; |
| 25 |
newkey = true; |
| 26 |
} |
| 27 |
|
| 28 |
data = mainwp_secure_data({ |
| 29 |
action: 'mainwp_post_addmeta', |
| 30 |
post_id: jQuery('#post_ID').val(), |
| 31 |
metavalue: jQuery('#metavalue').val(), |
| 32 |
metakeyinput: jQuery('#metakeyinput').val(), |
| 33 |
metakeyselect: metakey |
| 34 |
}); |
| 35 |
|
| 36 |
} else { |
| 37 |
|
| 38 |
let row = jQuery(me).closest('.row'); |
| 39 |
let metaid = row.attr('meta-id'); |
| 40 |
|
| 41 |
if ((row.length == 0) || !metaid) |
| 42 |
return false; |
| 43 |
|
| 44 |
data = mainwp_secure_data({ |
| 45 |
action: 'mainwp_post_addmeta', |
| 46 |
post_id: jQuery('#post_ID').val() |
| 47 |
}); |
| 48 |
|
| 49 |
if (action == 'update') { |
| 50 |
row.addClass('yellow'); |
| 51 |
data['meta'] = {}; |
| 52 |
data['meta'][metaid] = { |
| 53 |
'key': row.find('#meta-' + metaid + '-key').val(), |
| 54 |
'value': row.find('#meta-' + metaid + '-value').val() |
| 55 |
}; |
| 56 |
} else { |
| 57 |
row.addClass('red'); |
| 58 |
data['delete_meta'] = 'yes'; |
| 59 |
data['meta_nonce'] = jQuery(me).attr('_ajax_nonce'); |
| 60 |
data['id'] = metaid; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
jQuery.post(ajaxurl, data, function (response) { |
| 65 |
if (response) { |
| 66 |
if (response.error) { |
| 67 |
feedback('mainwp-message-zone', response.error, 'red'); |
| 68 |
return; |
| 69 |
} |
| 70 |
if (action == 'update' && response.result) { |
| 71 |
row.replaceWith(response.result); |
| 72 |
} else if (action == 'add' && response.result) { |
| 73 |
jQuery(response.result).insertBefore('#mainwp-metaform-row'); |
| 74 |
|
| 75 |
if (newkey) { |
| 76 |
jQuery('#metakeyinput').val(''); |
| 77 |
} else { |
| 78 |
jQuery('#metakeyselect').val('#NONE#'); |
| 79 |
} |
| 80 |
|
| 81 |
} else if (action == 'delete' && response.ok) { |
| 82 |
row.remove(); |
| 83 |
} |
| 84 |
} |
| 85 |
}, 'json'); |
| 86 |
}; |
| 87 |
|
| 88 |
/** |
| 89 |
* All post and postbox controls and functionality. |
| 90 |
*/ |
| 91 |
jQuery(function ($) { |
| 92 |
let updateText, |
| 93 |
$textarea = $('#content'), |
| 94 |
$document = $(document), |
| 95 |
$timestampdiv = $('#timestampdiv'); |
| 96 |
|
| 97 |
/* |
| 98 |
* Clear the window name. Otherwise if this is a former preview window where the user navigated to edit another post, |
| 99 |
* and the first post is still being edited, clicking Preview there will use this window to show the preview. |
| 100 |
*/ |
| 101 |
window.name = ''; |
| 102 |
|
| 103 |
//Show/Hide visibility additional fields |
| 104 |
jQuery('input[name="visibility"').on('change', function () { |
| 105 |
if (jQuery(this).val() == 'public') { |
| 106 |
jQuery('#sticky-field').show(); |
| 107 |
jQuery('#post_password-field').hide(); |
| 108 |
} else if (jQuery(this).val() == 'password') { |
| 109 |
jQuery('#sticky-field').hide(); |
| 110 |
jQuery('#post_password-field').show(); |
| 111 |
} else { |
| 112 |
jQuery('#sticky-field').hide(); |
| 113 |
jQuery('#post_password-field').hide(); |
| 114 |
} |
| 115 |
}); |
| 116 |
|
| 117 |
//Show timestamp field |
| 118 |
jQuery('#post_timestamp').on('change', function () { |
| 119 |
if (jQuery(this).val() == 'schedule') { |
| 120 |
jQuery('#post_timestamp_value-field').show(); |
| 121 |
} else { |
| 122 |
jQuery('#post_timestamp_value-field').hide(); |
| 123 |
// cancel edit date time. |
| 124 |
$('#mm').val($('#hidden_mm').val()); |
| 125 |
$('#jj').val($('#hidden_jj').val()); |
| 126 |
$('#aa').val($('#hidden_aa').val()); |
| 127 |
$('#hh').val($('#hidden_hh').val()); |
| 128 |
$('#mn').val($('#hidden_mn').val()); |
| 129 |
} |
| 130 |
}); |
| 131 |
|
| 132 |
/** |
| 133 |
* Make sure all labels represent the current settings. |
| 134 |
* |
| 135 |
* @returns {boolean} False when an invalid timestamp has been selected, otherwise True. |
| 136 |
*/ |
| 137 |
updateText = function () { |
| 138 |
|
| 139 |
if (!$timestampdiv.length) |
| 140 |
return true; |
| 141 |
|
| 142 |
let attemptedDate, |
| 143 |
aa = $('#aa').val(), |
| 144 |
mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val(); |
| 145 |
|
| 146 |
attemptedDate = new Date(aa, mm - 1, jj, hh, mn); |
| 147 |
|
| 148 |
// Catch unexpected date problems. |
| 149 |
if (attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn) { |
| 150 |
$timestampdiv.find('.timestamp-wrap').addClass('form-invalid'); |
| 151 |
return false; |
| 152 |
} else { |
| 153 |
$timestampdiv.find('.timestamp-wrap').removeClass('form-invalid'); |
| 154 |
} |
| 155 |
return true; |
| 156 |
}; |
| 157 |
|
| 158 |
// init post datetime calendar |
| 159 |
jQuery('#schedule_post_datetime').calendar({ |
| 160 |
type: 'datetime', |
| 161 |
initialDate: function () { |
| 162 |
let aa = $('#aa').val(), mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val(); |
| 163 |
let ind = new Date(aa, mm - 1, jj, hh, mn); |
| 164 |
return ind; |
| 165 |
}(), |
| 166 |
monthFirst: false, |
| 167 |
formatter: { |
| 168 |
date: function (date) { |
| 169 |
if (!date) return ''; |
| 170 |
let jj = date.getDate(); |
| 171 |
let mm = date.getMonth() + 1; |
| 172 |
let aa = date.getFullYear(); |
| 173 |
return aa + '-' + mm + '-' + jj; |
| 174 |
} |
| 175 |
}, |
| 176 |
onChange: function (attemptedDate, textDate) { |
| 177 |
let aa = attemptedDate.getFullYear(), mm = attemptedDate.getMonth() + 1, jj = attemptedDate.getDate(), mn = attemptedDate.getMinutes(), hh = attemptedDate.getHours(); |
| 178 |
mm = ('0' + mm).slice(-2); // to format 01,02,03, ... 11,12 |
| 179 |
$('#aa').val(aa); |
| 180 |
$('#mm').val(mm).trigger("change"); // selector element |
| 181 |
$('#jj').val(jj); |
| 182 |
$('#hh').val(hh); |
| 183 |
$('#mn').val(mn); |
| 184 |
return updateText(); // not set if invalid date |
| 185 |
}, |
| 186 |
onSelect: function (attemptedDate, mode) { |
| 187 |
let aa = attemptedDate.getFullYear(), mm = attemptedDate.getMonth() + 1, jj = attemptedDate.getDate(), mn = attemptedDate.getMinutes(), hh = attemptedDate.getHours(); |
| 188 |
mm = ('0' + mm).slice(-2); // to format 01,02,03, ... 11,12 |
| 189 |
$('#aa').val(aa); |
| 190 |
$('#mm').val(mm).trigger("change"); // selector element |
| 191 |
$('#jj').val(jj); |
| 192 |
$('#hh').val(hh); |
| 193 |
$('#mn').val(mn); |
| 194 |
console.log(aa + ' ' + mm + ' ' + jj + ' ' + hh + ' ' + mn); |
| 195 |
|
| 196 |
return updateText(); // not set if invalid date |
| 197 |
}, |
| 198 |
}); |
| 199 |
|
| 200 |
|
| 201 |
// Post locks: contain focus inside the dialog. If the dialog is shown, focus the first item. |
| 202 |
$('#post-lock-dialog .notification-dialog').on('keydown', function (e) { |
| 203 |
// Don't do anything when [tab] is pressed. |
| 204 |
if (e.which != 9) |
| 205 |
return; |
| 206 |
|
| 207 |
let target = $(e.target); |
| 208 |
|
| 209 |
// [shift] + [tab] on first tab cycles back to last tab. |
| 210 |
if (target.hasClass('wp-tab-first') && e.shiftKey) { |
| 211 |
$(this).find('.wp-tab-last').trigger('focus'); |
| 212 |
e.preventDefault(); |
| 213 |
// [tab] on last tab cycles back to first tab. |
| 214 |
} else if (target.hasClass('wp-tab-last') && !e.shiftKey) { |
| 215 |
$(this).find('.wp-tab-first').trigger('focus'); |
| 216 |
e.preventDefault(); |
| 217 |
} |
| 218 |
}).filter(':visible').find('.wp-tab-first').trigger('focus'); |
| 219 |
|
| 220 |
// This code is meant to allow tabbing from Title to Post content. |
| 221 |
$('#title').on('keydown.editor-focus', function (event) { |
| 222 |
let editor; |
| 223 |
|
| 224 |
if (event.keyCode === 9 && !event.ctrlKey && !event.altKey && !event.shiftKey) { |
| 225 |
editor = typeof tinymce != 'undefined' && tinymce.get('content'); |
| 226 |
|
| 227 |
if (editor && !editor.isHidden()) { |
| 228 |
editor.focus(); |
| 229 |
} else if ($textarea.length) { |
| 230 |
$textarea.trigger('focus'); |
| 231 |
} else { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
event.preventDefault(); |
| 236 |
} |
| 237 |
}); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
// Resize the WYSIWYG and plain text editors. |
| 243 |
(function () { |
| 244 |
let editor, offset, mce, |
| 245 |
$handle = $('#post-status-info'), |
| 246 |
$postdivrich = $('#postdivrich'); |
| 247 |
|
| 248 |
// If there are no textareas or we are on a touch device, we can't do anything. |
| 249 |
if (!$textarea.length || 'ontouchstart' in window) { |
| 250 |
// Hide the resize handle. |
| 251 |
$('#content-resize-handle').hide(); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Handle drag event. |
| 257 |
* |
| 258 |
* @param {Object} event Event containing details about the drag. |
| 259 |
*/ |
| 260 |
function dragging(event) { |
| 261 |
if ($postdivrich.hasClass('wp-editor-expand')) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
if (mce) { |
| 266 |
editor.theme.resizeTo(null, offset + event.pageY); |
| 267 |
} else { |
| 268 |
$textarea.height(Math.max(50, offset + event.pageY)); |
| 269 |
} |
| 270 |
|
| 271 |
event.preventDefault(); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* When the dragging stopped make sure we return focus and do a sanity check on the height. |
| 276 |
*/ |
| 277 |
function endDrag() { |
| 278 |
let height, toolbarHeight; |
| 279 |
|
| 280 |
if ($postdivrich.hasClass('wp-editor-expand')) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
if (mce) { |
| 285 |
editor.focus(); |
| 286 |
toolbarHeight = Number.parseInt($('#wp-content-editor-container .mce-toolbar-grp').height(), 10); |
| 287 |
|
| 288 |
if (toolbarHeight < 10 || toolbarHeight > 200) { |
| 289 |
toolbarHeight = 30; |
| 290 |
} |
| 291 |
|
| 292 |
height = Number.parseInt($('#content_ifr').css('height'), 10) + toolbarHeight - 28; |
| 293 |
} else { |
| 294 |
$textarea.trigger('focus'); |
| 295 |
height = Number.parseInt($textarea.css('height'), 10); |
| 296 |
} |
| 297 |
|
| 298 |
$document.off('.wp-editor-resize'); |
| 299 |
|
| 300 |
// Sanity check: normalize height to stay within acceptable ranges. |
| 301 |
if (height && height > 50 && height < 5000) { |
| 302 |
setUserSetting('ed_size', height); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
$handle.on('mousedown.wp-editor-resize', function (event) { |
| 307 |
if (typeof tinymce !== 'undefined') { |
| 308 |
editor = tinymce.get('content'); |
| 309 |
} |
| 310 |
|
| 311 |
if (editor && !editor.isHidden()) { |
| 312 |
mce = true; |
| 313 |
offset = $('#content_ifr').height() - event.pageY; |
| 314 |
} else { |
| 315 |
mce = false; |
| 316 |
offset = $textarea.height() - event.pageY; |
| 317 |
$textarea.blur(); |
| 318 |
} |
| 319 |
|
| 320 |
$document.on('mousemove.wp-editor-resize', dragging) |
| 321 |
.on('mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag); |
| 322 |
|
| 323 |
event.preventDefault(); |
| 324 |
}).on('mouseup.wp-editor-resize', endDrag); |
| 325 |
})(); |
| 326 |
|
| 327 |
// TinyMCE specific handling of Post Format changes to reflect in the editor. |
| 328 |
if (typeof tinymce !== 'undefined') { |
| 329 |
// When changing post formats, change the editor body class. |
| 330 |
$('#post-formats-select input.post-format').on('change.set-editor-class', function () { |
| 331 |
let editor, body, format = this.id; |
| 332 |
|
| 333 |
if (format && $(this).prop('checked') && (editor = tinymce.get('content'))) { |
| 334 |
body = editor.getBody(); |
| 335 |
body.className = body.className.replace(/\bpost-format-[^ ]+/, ''); |
| 336 |
editor.dom.addClass(body, format == 'post-format-0' ? 'post-format-standard' : format); |
| 337 |
$(document).trigger('editor-classchange'); |
| 338 |
} |
| 339 |
}); |
| 340 |
|
| 341 |
// When changing page template, change the editor body class |
| 342 |
$('#page_template').on('change.set-editor-class', function () { |
| 343 |
let editor, body, pageTemplate = $(this).val() || ''; |
| 344 |
|
| 345 |
pageTemplate = pageTemplate.substring(pageTemplate.lastIndexOf('/') + 1) |
| 346 |
.replace(/\.php$/, '') |
| 347 |
.replace(/\./g, '-'); |
| 348 |
|
| 349 |
if (pageTemplate && (editor = tinymce.get('content'))) { |
| 350 |
body = editor.getBody(); |
| 351 |
body.className = body.className.replace(/\bpage-template-[^ ]+/, ''); |
| 352 |
editor.dom.addClass(body, 'page-template-' + pageTemplate); |
| 353 |
$(document).trigger('editor-classchange'); |
| 354 |
} |
| 355 |
}); |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
}); |
| 360 |
|
| 361 |
/** |
| 362 |
* TinyMCE word count display |
| 363 |
*/ |
| 364 |
(function ($, counter) { |
| 365 |
$(function () { |
| 366 |
let $content = $('#content'), |
| 367 |
$count = $('#wp-word-count').find('.word-count'), |
| 368 |
prevCount = 0, |
| 369 |
contentEditor; |
| 370 |
|
| 371 |
/** |
| 372 |
* Get the word count from TinyMCE and display it |
| 373 |
*/ |
| 374 |
function update() { |
| 375 |
let text, count; |
| 376 |
|
| 377 |
if (!contentEditor || contentEditor.isHidden()) { |
| 378 |
text = $content.val(); |
| 379 |
} else { |
| 380 |
text = contentEditor.getContent({ format: 'raw' }); |
| 381 |
} |
| 382 |
|
| 383 |
count = counter.count(text); |
| 384 |
|
| 385 |
if (count !== prevCount) { |
| 386 |
$count.text(count); |
| 387 |
} |
| 388 |
|
| 389 |
prevCount = count; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Bind the word count update triggers. |
| 394 |
* |
| 395 |
* When a node change in the main TinyMCE editor has been triggered. |
| 396 |
* When a key has been released in the plain text content editor. |
| 397 |
*/ |
| 398 |
$(document).on('tinymce-editor-init', function (event, editor) { |
| 399 |
if (editor.id !== 'content') { |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
contentEditor = editor; |
| 404 |
|
| 405 |
editor.on('nodechange keyup', _.debounce(update, 1000)); |
| 406 |
}); |
| 407 |
|
| 408 |
$content.on('input keyup', _.debounce(update, 1000)); |
| 409 |
|
| 410 |
update(); |
| 411 |
}); |
| 412 |
|
| 413 |
})(jQuery, new wp.utils.WordCounter()); |
| 414 |
|
| 415 |
|