PluginProbe
WP Docs / 2.3.0
WP Docs v2.3.0
trunk 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1
wp-docs / js / admin-scripts.js

admin-scripts.js in WP Docs 2.3.0, at js/admin-scripts.js

704 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // JavaScript Document
2 var wpdocs_move_copy = 'move';
3 jQuery(document).ready(function($){
4
5 var files_multi_select = false;
6
7 $('.new-folder').on('click', function(){
8 var data = {
9 'action': 'wpdocs_create_folder',
10 'parent_dir': $(this).data('id'),
11 'nonce': wpdocs_ajax_object.nonce
12 };
13 // We can also pass the url value separately from ajaxurl for front end AJAX implementations
14 $('.ab-new').removeClass('ab-new');
15 $.post(wpdocs_ajax_object.ajax_url, data, function(response) {
16
17 $('div.wpdocs_list > ul').prepend(response);
18
19 $('div.wpdocs_list ul li.ab-dir a.wpd-edit').eq(0).click();
20 });
21 });
22
23 $('body').on('click', '.wpdocs-reset', function(){
24 var reset_confirmation = confirm(wpdocs_ajax_object.reset_confirm);
25
26 if(!reset_confirmation){
27 return false;
28 }
29 });
30
31 $('body').on('click', '.wpdocs_list ul li .file', function(event){
32
33
34 if(files_multi_select){
35 event.preventDefault();
36 $(this).parent().toggleClass('selected-item');
37 }else{
38 return true;
39 }
40 });
41
42 $('body').on('click', '.wpdocs_toolbar .wp-docs-multi-select', function(event){
43
44 if(!wpdocs_ajax_object.wpdocs_pro){
45 alert(wpdocs_ajax_object.premium_feature);
46 return;
47 }
48
49 $(this).toggleClass('selection-enabled');
50 files_multi_select = $(this).hasClass('selection-enabled');
51
52 if(!files_multi_select){
53 $('.wpdocs_list ul li.selected-item').removeClass('selected-item');
54 }
55 });
56
57
58 $('body').on('click', '.wpdocs_list ul li.ab-dir > a.dtitle', function () {
59 const obj = $(this);
60 const id = obj.parent().data('id');
61 const resource_id = obj.parent().data('resource');
62
63 // Get plain folder name from HTML
64 let html_str = $.parseHTML(obj.html());
65 let folder_name = '';
66
67 $.each(html_str, function (i, el) {
68 if (el.wholeText) {
69 folder_name = el.wholeText;
70 }
71 });
72
73 let rename_to = prompt(wpdocs_ajax_object.rename_confirm, folder_name);
74
75 // Cancelled or empty input
76 if (!rename_to || $.trim(rename_to) === '') {
77 return;
78 }
79
80 // Strip HTML tags
81 rename_to = rename_to.replace(/(<([^>]+)>)/gi, "");
82
83 const data = {
84 action: 'wpdocs_update_folder',
85 dir_id: id,
86 resource_id: resource_id,
87 new_name: rename_to,
88 nonce: wpdocs_ajax_object.nonce
89 };
90
91 $.post(wpdocs_ajax_object.ajax_url, data, function (response) {
92 if (response.success && response.data && response.data.msg) {
93 // Update folder name in DOM
94 obj.text(rename_to);
95 alert(response.data.msg);
96 } else {
97 alert(response.data?.msg || 'Rename failed.');
98 }
99 });
100 });
101
102
103
104 $('body').on('click', '.wpdocs_list ul li.ab-dir a.wpd-edit', function(){
105 $(this).closest('li.ab-dir').find('a.dtitle').click();
106 });
107
108 $('body').on('click', '.wpdocs_list ul li.ab-dir a.wpd-trash', function(){
109 var delete_confirm = confirm(wpdocs_ajax_object.wpdocs_delete_msg);
110 var obj = $(this);
111 if(delete_confirm){
112 var data = {
113 'action': 'wpdocs_delete_folder',
114 'dir_id': $(this).closest('li.ab-dir').data('id'),
115 'resource_id': $(this).closest('li.ab-dir').data('resource'),
116 'nonce': wpdocs_ajax_object.nonce
117 };
118
119 $.post(wpdocs_ajax_object.ajax_url, data, function(response) {
120 obj.closest('li.ab-dir').fadeOut();
121 });
122
123
124 }
125 });
126
127 $('body').on('click', '.wpdocs_list ul li:not(.ab-dir):not(.ab-short) a.wpd-trash', function(event){
128 event.preventDefault();
129 var multi_delete = (files_multi_select && $('.wpdocs_list ul li.selected-item').length>1);
130 var delete_confirm = confirm(multi_delete?wpdocs_ajax_object.del_confirm_all:wpdocs_ajax_object.del_confirm);
131 var obj = $(this);
132 if(delete_confirm){
133 var id = obj.parents().eq(1).data('dir');
134
135 var attachment_id = obj.parents().eq(1).data('id');
136 var attachment_ids = {};
137
138 if($('.wpdocs_list ul li.selected-item').length>0){
139 $.each($('.wpdocs_list ul li.selected-item'), function(i,v){
140 attachment_ids[i] = $(this).data('id');
141 });
142 }else{
143 attachment_ids[0] = attachment_id;
144 }
145 //console.log(attachment_ids);
146 obj.parents().eq(1).fadeOut();
147 var data = {
148 'action': 'wpdocs_delete_files',
149 'dir_id': id,
150 'files': attachment_ids,
151 'nonce': wpdocs_ajax_object.nonce,
152 };
153 //alert(attachment.id);//return;
154 // We can also pass the url value separately from ajaxurl for front end AJAX implementations
155 $.post(wpdocs_ajax_object.ajax_url, data, function(response) {
156 //window.location.reload();
157 if(multi_delete){
158 $('.wpdocs_list ul li.selected-item').fadeOut();
159 }else{
160 obj.closest('li:not(.ab-dir)').fadeOut();
161 }
162 });
163 }
164 });
165
166 $('body').on('click', '.wpdocs_list ul li > a.folder', function(){
167 var is_shortcut = $(this).parent().hasClass('ab-short');
168 var linked_id = $(this).parent().data('linked');
169 var linked_url = (is_shortcut?$(this).parent().data('guid'):'');
170 var dir_id = (linked_id?linked_id:$(this).parent().data('id'));
171
172 if(linked_url && !linked_id){
173 window.open(linked_url, '_blank').focus();
174 return;
175 }else{
176 window.location.href = (linked_url?linked_url:'options-general.php?page=wpdocs&dir='+dir_id);
177 }
178 });
179 $('body').on('click', '.back-folder', function(){
180 window.location.href = 'options-general.php?page=wpdocs&dir='+$(this).data('parent');
181 });
182
183 setTimeout(function(){
184 if ($('.new-file:visible').length > 0) {
185 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
186 $('body').on('click', '.new-file:visible', function(e) {
187 var id = $(this).data('id');
188 var this_id = $(this).prop('id');
189
190 e.preventDefault();
191 //alert(id);alert(attachment.id);return;
192
193 var attachment_ids = [];
194 var add_file_status = true;
195
196 wp.media.editor.send.attachment = function(props, attachment) {
197
198
199 attachment_ids.push(attachment.id);
200 //alert(attachment.id);//return;
201 // We can also pass the url value separately from ajaxurl for front end AJAX implementations
202
203 if(add_file_status){
204
205 add_file_status = false;
206
207 setTimeout(function(){
208
209 var data = {
210 'action': 'wpdocs_add_files',
211 'dir_id': id,
212 'files': attachment_ids,
213 'nonce': wpdocs_ajax_object.nonce,
214 };
215 $.post(wpdocs_ajax_object.ajax_url, data, function(response) {
216 //window.location.reload();
217 //console.log(response);
218 if(response!=''){
219 $('div.wpdocs_list > ul > li:not(.ab-dir)').remove();
220 $('div.wpdocs_list > ul').append(response);
221 }
222 });
223
224 })
225
226 }
227
228
229 };
230
231 wp.media.editor.open(this_id);
232 //.open($(this));
233 //return false;
234 });
235
236 }
237 }
238 }, 1000);
239
240 //save selected file or directory globally every time on selection these will be replaced
241
242 var selected_move_dir = null;
243 var selected_move_is_file = false;
244 var selected_move_file_dir = null;
245
246 $('.wpdocs-specific-sections .save_changes').on('click', function(){
247 var dir_id = $('.wpdocs-specific-sections button.modal-btn').data('dir_id');
248
249 wp_docs_update_options(dir_id);
250 });
251
252 function wp_docs_update_options(dir_id = 0){
253 //console.log($(this));
254 //console.log($(this).parents().eq(1));
255
256 $conditional_class = (dir_id == 0 ? ':not(.wpdocs_dir_options)' : '.wpdocs_dir_options');
257
258 var wpdocs_option_ajax = $('input[name^="wpdocs_options"][value="ajax"]'+$conditional_class);
259 var wpdocs_option_ajax_url = $('input[name^="wpdocs_options"][value="ajax_url"]'+$conditional_class);
260
261 var wpdocs_option_file_upload = $('input[name^="wpdocs_options"][value="file_upload"]'+$conditional_class);
262 var wpdocs_option_current_user_files = $('input[name^="wpdocs_options"][value="current_user_files"]'+$conditional_class);
263 var wpdocs_option_del_from_front = $('input[name^="wpdocs_options"][value="del_from_front"]'+$conditional_class);
264
265
266 if(wpdocs_option_file_upload.prop('checked') == false){
267
268 wpdocs_option_current_user_files.prop('checked', false);
269 wpdocs_option_del_from_front.prop('checked', false);
270
271 }
272
273
274 if(wpdocs_option_ajax.prop('checked') == false){
275
276 wpdocs_option_ajax_url.prop('checked', false);
277
278 }
279
280 var wpdocs_option_checked = $('input[name^="wpdocs_options"][type="checkbox"]:checked'+$conditional_class);
281 var wpdocs_option_text = $('input[name^="wpdocs_options"][type="text"]'+$conditional_class);
282 var wpdocs_option_select = $('select[name^="wpdocs_options"]'+$conditional_class);
283
284 var wpdocs_options_post = {};
285
286 if(wpdocs_ajax_object.empty_settings){
287
288 wpdocs_options_post['wpdocs_options_update'] = true;
289
290 }
291
292
293 if(wpdocs_option_select.length > 0 ){
294 $.each(wpdocs_option_select, function () {
295
296 var name = $(this).data('name');
297
298
299 wpdocs_options_post[name] = $(this).val();
300
301 });
302 }
303
304
305 if(wpdocs_option_text.length > 0 ){
306 $.each(wpdocs_option_text, function () {
307
308 wpdocs_options_post[$(this).data('name')] = $(this).val();
309
310 });
311 }
312
313 if(wpdocs_option_checked.length > 0 ){
314 $.each(wpdocs_option_checked, function () {
315
316 wpdocs_options_post[$(this).val()] = true;
317
318 });
319 }
320
321 var wpdocs_option_colors = $('input[name^="wpdocs_options"][type="color"]'+$conditional_class);
322
323 if(wpdocs_option_colors.length > 0 && !wpdocs_ajax_object.empty_settings){
324 $.each(wpdocs_option_colors, function () {
325
326 wpdocs_options_post[$(this).attr('id')] = $(this).val();
327
328 });
329 }
330
331 if(!wpdocs_options_post.allowed_role){
332 wpdocs_options_post.allowed_role = 'empty';
333 }
334
335
336 var data = {
337
338 action : 'wpdocs_update_option',
339 wpdocs_update_option_nonce : wpdocs_ajax_object.nonce,
340 wpdocs_options : wpdocs_options_post,
341 wpdocs_dir_id : dir_id
342
343 }
344
345
346
347 $.post(ajaxurl, data, function(code, response){
348
349 //console.log(response);
350
351 if(response == 'success'){
352
353 var alerts = (dir_id == 0 ? $('.wpdocs-options .alert.alert_main'): $('.wpdocs-specific-sections .alert.alert_dir'));
354
355 //console.log(alert);
356 alerts.removeClass('d-none').addClass('show');
357 setTimeout(function(){
358 alerts.addClass('d-none');
359 }, 10000);
360
361 }
362
363 });
364
365
366 }
367
368 $('input[name^="wpdocs_options"]:checkbox').on('change', function(){
369
370 var this_obj = $(this);
371 if(this_obj.prop('checked')){
372 setTimeout(function(){
373 this_obj.parents().eq(1).find('ul').removeClass('d-none');
374 });
375
376 }else{
377 this_obj.parents().eq(1).find('ul').addClass('d-none');
378 }
379 });
380
381 $('input[name^="wpdocs_options"]:checkbox').change();
382
383 $('body').on('change', 'input[name^="wpdocs_options"]:not(.wpdocs_dir_options), select[name^="wpdocs_options"]:not(.wpdocs_dir_options)', function(){
384 wp_docs_update_options();
385 });
386
387 if(wpdocs_ajax_object.empty_settings){
388
389 $('input[name^="wpdocs_options"]').change();
390
391 }
392
393
394 function wpdocs_disable_child(dir_id){
395
396 var dir_list_select = $('li.wpdocs_move_folder_to select');
397 var dir_list_options = dir_list_select.find('option');
398 var current = dir_list_select.find('option[value="'+dir_id+'"]');
399 var current_parent = current.data('parent');
400
401 var childs = dir_list_select.find('option[data-parent="'+dir_id+'"]');
402
403 //check if the selected is not a file than disable all child directories
404 if (!selected_move_is_file) {
405
406 if (dir_list_options.length > 0) {
407
408 $.each(dir_list_options, function () {
409
410 var this_val = $(this).val();
411 var this_parent = $(this).data('parent');
412
413 if (this_val == current_parent) {
414
415 $(this).prop('disabled', true);
416 //$(this).hide();
417 $(this).prop('title', wpdocs_ajax_object.move_str);
418 }
419
420
421 if ($(this).val() == dir_id || $(this).data('parent') == dir_id) {
422
423 $(this).prop('disabled', true);
424 //$(this).hide();
425 $(this).prop('title', wpdocs_ajax_object.move_str);
426
427 if (childs.length > 0 && this_val != dir_id) {
428
429 wpdocs_disable_child(this_val);
430
431 }
432 }
433
434 });
435 }
436
437 } else {
438
439 //if selected is file than disable only its parent directory and root directory
440
441 current = dir_list_select.find('option[value="' + selected_move_file_dir + '"]');
442 var current_root = dir_list_select.find('option[value="0"]');
443 current.prop('disabled', true);
444 //current.hide();
445 current_root.prop('disabled', true);
446 //current_root.hide();
447 current_root.prop('title', wpdocs_ajax_object.move_str);
448
449 }
450
451 }
452
453 $('body').on('click', '.wpdocs_list ul li a.wpd-move, .wpdocs_list ul li a.wpd-copy', function(){
454
455 var dir_id = $(this).parents('li').data('id');
456 var file_dir = $(this).parents('li').data('dir');
457 var is_file = file_dir !== undefined;
458 selected_move_file_dir = is_file ? file_dir: null;
459 selected_move_is_file = is_file;
460
461 selected_move_dir = dir_id;
462 var dir_list_select = $('li.wpdocs_move_folder_to select');
463 var dir_list_options = dir_list_select.find('option');
464 dir_list_options.prop('disabled', false);
465 dir_list_options.show();
466
467
468 $('li.wpdocs_move_folder_to').show();
469
470 wpdocs_disable_child(selected_move_dir);
471
472
473
474 wpdocs_move_copy = ($(this).hasClass('wpd-copy')?'copy':'move');
475
476 $('.wpdocs_move_folder_to button').removeClass('copy move').addClass(wpdocs_move_copy).html(wpdocs_move_copy);
477
478
479 });
480
481 $('body').on('click', 'li.wpdocs_move_folder_to button', function(){
482
483 var selected_dir = $('li.wpdocs_move_folder_to select').val();
484
485
486
487 if(selected_dir != -1){
488
489 var wpdocs_move_selected_dir_obj = {
490
491 'dir_selected' : selected_move_dir,
492 'dir_id' : selected_dir,
493 'files' : selected_move_dir,
494 'is_file' : selected_move_is_file,
495 'file_dir': selected_move_file_dir,
496 'action_type': wpdocs_move_copy,
497 };
498
499 var data = {
500
501 action: 'wpdocs_update_option',
502 wpdocs_update_option_nonce : wpdocs_ajax_object.nonce,
503 wpdocs_move_selected_dir: wpdocs_move_selected_dir_obj
504
505 }
506
507 if(wpdocs_move_copy=='copy' && wpdocs_ajax_object.wpdocs_pro!='1'){
508 alert(wpdocs_ajax_object.premium_feature);return;
509 }
510
511 $.blockUI({message:(wpdocs_move_copy=='copy'?wpdocs_ajax_object.copied:wpdocs_ajax_object.moved)});
512
513 $.post(ajaxurl, data, function(response) {
514
515 response = JSON.parse(response);
516
517 if(response){
518
519
520
521 window.location.href= wpdocs_ajax_object.url+'&dir='+selected_dir ;
522
523 }else{
524
525 alert(wpdocs_ajax_object.move_error);
526 $(this).parents('li.wpdocs_move_folder_to').hide();
527
528 }
529
530 });
531
532
533 }else{
534
535 alert(wpdocs_ajax_object.target_dir_msg);
536 }
537 });
538
539 if($('.wpdocs_options_allowed_role:not(.wpdocs_dir_options)').length > 0){
540
541 new SlimSelect({
542
543 select:'.wpdocs_options_allowed_role:not(.wpdocs_dir_options)',
544 placeholder: wpdocs_ajax_object.select_role_str,
545 });
546 }
547
548
549 if($('.wpdocs_options_allowed_role.wpdocs_dir_options').length > 0){
550
551 new SlimSelect({
552
553 select:'.wpdocs_options_allowed_role.wpdocs_dir_options',
554 placeholder: wpdocs_ajax_object.select_role_str,
555 });
556
557 }
558
559
560 $('.wpdocs_meta_links .wpdocs_show_option').on('click', function(){
561
562 var control = $($(this).data('control'));
563 var show = $(this).attr('data-show');
564
565 if(show == 'true'){
566 $(this).attr('data-show', false);
567 control.slideUp();
568 }else{
569 $(this).attr('data-show', true);
570 control.slideDown();
571
572 }
573
574 });
575
576
577
578 $('.wp_docs_import_memphis').on('click', function(){
579
580 var import_confirm = confirm(wpdocs_ajax_object.import_confirm);
581 var progress_text = $(this).data('text');
582
583 if(!import_confirm) return;
584
585 var data = {
586
587 action : 'wp_docs_import_memphis_docs',
588 wp_docs_nonce:wpdocs_ajax_object.nonce,
589 };
590
591 $('.wp_docs_importing').show();
592 $('.wp_docs_importing .progress-bar').text(progress_text);
593 $('.wp_docs_importing_alert').hide();
594
595
596 $.post(ajaxurl, data, function(resp, code){
597 $('.wp_docs_importing').hide();
598 if(code == 'success' && resp.status){
599 window.location.href = wpdocs_ajax_object.url;
600 }else if(resp.remarks){
601 $('.wp_docs_importing_alert').show();
602 $('.wp_docs_importing_alert').text(resp.remarks);
603 }
604
605 setTimeout(function() {
606
607 $('.wp_docs_importing_alert').fadeOut();
608
609 }, 5000);
610
611 });
612
613 });
614
615 $('.wp_docs_import_memphis_rollback').on('click', function(){
616
617 var import_confirm = confirm(wpdocs_ajax_object.undo_import_confirm);
618 var progress_text = $(this).data('text');
619
620 if(!import_confirm) return;
621
622 var data = {
623
624 action : 'wp_docs_import_memphis_rollback',
625 wp_docs_nonce:wpdocs_ajax_object.nonce,
626 };
627
628 $('.wp_docs_importing .progress-bar').text(progress_text);
629 $('.wp_docs_importing').show();
630
631 $.post(ajaxurl, data, function(resp, code){
632 $('.wp_docs_importing').hide();
633 if(code == 'success' && resp.status){
634 window.location.href = wpdocs_ajax_object.url;
635 }
636
637 });
638
639 });
640
641
642 $('.wpdocs-wrapper').on('click', 'h2.nav-tab-wrapper a.nav-tab', function(){
643 wpdocs_ajax_object.wc_os_pg = parseInt(wpdocs_ajax_object.wc_os_pg);
644 $(this).siblings().removeClass('nav-tab-active');
645 $(this).addClass('nav-tab-active');
646 $('.nav-tab-content, form:not(.wrap.wpdocs-wrapper .nav-tab-content):not(.ignore)').hide();
647 //console.log($(this).index());
648 $('.nav-tab-content').eq($(this).index()).removeClass('hides').show();
649 var state_url = wpdocs_ajax_object.url+'&t='+$(this).index()+(wpdocs_ajax_object.wc_os_pg>0?'&pg='+wpdocs_ajax_object.wc_os_pg:'');
650 window.history.replaceState('', '', state_url);
651 $('form input[name="wos_tn"]').val($(this).index());
652 wpdocs_ajax_object.wc_os_tab = $(this).index();
653 $('.wrap.wpdocs-wrapper').attr('class', 'wrap wpdocs-wrapper tab-'+$(this).index());
654
655 $('.wpdocs-wrapper form').prop('action', state_url);
656
657 });
658 function parse_query_string(query) {
659 var vars = query.split("&");
660 var query_string = {};
661 for (var i = 0; i < vars.length; i++) {
662 var pair = vars[i].split("=");
663 // If first entry with this name
664 if (typeof query_string[pair[0]] === "undefined") {
665 query_string[pair[0]] = decodeURIComponent(pair[1]);
666 // If second entry with this name
667 } else if (typeof query_string[pair[0]] === "string") {
668 var arr = [query_string[pair[0]], decodeURIComponent(pair[1])];
669 query_string[pair[0]] = arr;
670 // If third or later entry with this name
671 } else {
672 query_string[pair[0]].push(decodeURIComponent(pair[1]));
673 }
674 }
675 return query_string;
676 }
677
678 if($('.wpdocs-wrapper').length>0){
679 var query = window.location.search.substring(1);
680 var qs = parse_query_string(query);
681
682 if(typeof(qs.t)!='undefined'){
683 $('.wpdocs-wrapper a.nav-tab').eq(qs.t).click();
684 }
685 }
686
687 $('#translate_wpdocs_btn').on('click', function(){
688 var val = $.trim($('#translate_wpdocs').val());
689 if(val){
690
691 var url = new URL(val);
692 var dir = url.searchParams.get("dir");
693 var updated_str = val;
694
695 if(typeof(dir)!='undefined' && (dir in wpdocs_ajax_object.all_dirs)){
696 updated_str = updated_str.replace('dir='+dir, wpdocs_ajax_object.all_dirs[dir]);
697 }
698 $('.translate_wpdocs').append(updated_str+'<br />');
699 $('#translate_wpdocs').val('');
700 }
701
702
703 });
704 });