PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / assets / js / mainwp-post-edit.js

mainwp-post-edit.js in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0.1, at assets/js/mainwp-post-edit.js

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