code_manager.js
6 days ago
code_manager_dashboard.js
6 days ago
code_manager_listmode.js
6 days ago
code_manager_message.js
6 days ago
code_manager_tabmode.js
6 days ago
notify.min.js
6 days ago
code_manager.js
164 lines
| 1 | /** |
| 2 | * JavaScript code to add interactive features to Code Manager data entry page |
| 3 | * |
| 4 | * @author Peter Schulz |
| 5 | * @since 1.0.0 |
| 6 | */ |
| 7 | |
| 8 | const PHP_DEFAULT = '<?php\n\n?>'; |
| 9 | |
| 10 | var user_has_edited = false; |
| 11 | |
| 12 | var href = window.location.href; |
| 13 | var pathname = href.substring(0, href.lastIndexOf('/')) + '/admin-ajax.php'; |
| 14 | |
| 15 | var cm = null; |
| 16 | |
| 17 | jQuery(function () { |
| 18 | var editorSettings = cm_settings; |
| 19 | var code_type = jQuery('#code_type').val()===null ? '' : jQuery('#code_type').val(); |
| 20 | if (code_type.includes('html')) { |
| 21 | // Load HTML settings |
| 22 | editorSettings = wp.codeEditor.defaultSettings ? _.clone( wp.codeEditor.defaultSettings ) : {}; |
| 23 | editorSettings.codemirror = _.extend( |
| 24 | {}, |
| 25 | editorSettings.codemirror, |
| 26 | { |
| 27 | mode: 'html', |
| 28 | } |
| 29 | ); |
| 30 | } else if (code_type.includes('css')) { |
| 31 | // Load CSS settings |
| 32 | editorSettings = wp.codeEditor.defaultSettings ? _.clone( wp.codeEditor.defaultSettings ) : {}; |
| 33 | editorSettings.codemirror = _.extend( |
| 34 | {}, |
| 35 | editorSettings.codemirror, |
| 36 | { |
| 37 | mode: 'css', |
| 38 | } |
| 39 | ); |
| 40 | } if (code_type.includes('javascript')) { |
| 41 | // Load JavaScript settings |
| 42 | editorSettings = wp.codeEditor.defaultSettings ? _.clone( wp.codeEditor.defaultSettings ) : {}; |
| 43 | editorSettings.codemirror = _.extend( |
| 44 | {}, |
| 45 | editorSettings.codemirror, |
| 46 | { |
| 47 | mode: 'javascript', |
| 48 | } |
| 49 | ); |
| 50 | } else { |
| 51 | // Load PHP settings (default = cm_settings) |
| 52 | if (code==='') { |
| 53 | code = PHP_DEFAULT; |
| 54 | } |
| 55 | } |
| 56 | cm = wp.codeEditor.initialize(jQuery('#code'), editorSettings); |
| 57 | |
| 58 | jQuery('#code').parent().css('display','grid').css('width','100%'); |
| 59 | |
| 60 | jQuery('#code_name').on('keydown', function() { |
| 61 | user_has_edited = true; |
| 62 | }); |
| 63 | jQuery('#code_type').on('focus', function(event) { |
| 64 | jQuery(this).data({current_value:jQuery(this).val()}); |
| 65 | }); |
| 66 | jQuery('#code_type').on('change', function(event) { |
| 67 | if (jQuery('#code_id').val()=='') { |
| 68 | if (jQuery('#code_type option:selected').text().toLowerCase().includes('php')) { |
| 69 | // Add php tags |
| 70 | if (cm.codemirror.getValue()==='') { |
| 71 | cm.codemirror.setValue(PHP_DEFAULT); |
| 72 | } |
| 73 | } else { |
| 74 | // Remove php tags (clear field) |
| 75 | if (cm.codemirror.getValue()===PHP_DEFAULT) { |
| 76 | cm.codemirror.setValue(''); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | user_has_edited = true; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | html = '<div>Are you sure you want to change the code type? To prevent errors this code will be disabled!</div>'; |
| 85 | var dialog = |
| 86 | jQuery(html) |
| 87 | .data('current_element',jQuery(this)) |
| 88 | .data('current_value',jQuery.data(this, 'current_value')) |
| 89 | .dialog({ |
| 90 | dialogClass: 'no-close', |
| 91 | title: 'Change code type?', |
| 92 | buttons: { |
| 93 | 'Yes': function() { |
| 94 | user_has_edited = true; |
| 95 | dialog.dialog('destroy'); |
| 96 | }, |
| 97 | 'No': function() { |
| 98 | jQuery(jQuery.data(this, 'current_element')).val(jQuery.data(this, 'current_value')); |
| 99 | dialog.dialog('destroy'); |
| 100 | }, |
| 101 | 'Cancel': function() { |
| 102 | jQuery(jQuery.data(this, 'current_element')).val(jQuery.data(this, 'current_value')); |
| 103 | dialog.dialog('destroy'); |
| 104 | } |
| 105 | } |
| 106 | }); |
| 107 | }); |
| 108 | cm.codemirror.on('change',function(){ |
| 109 | user_has_edited = true; |
| 110 | }); |
| 111 | jQuery('#submit_button').on('click', function() { |
| 112 | user_has_edited = false; |
| 113 | }); |
| 114 | jQuery(window).on('beforeunload', function(){ |
| 115 | if ( user_has_edited === true ) { |
| 116 | return 'Your changes will not be saved! Are you sure you want to leave this page?'; |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | jQuery('.cm_menu_title').tooltip(); |
| 121 | jQuery('td.label label').tooltip(); |
| 122 | jQuery('#code_manager_preview').tooltip(); |
| 123 | }); |
| 124 | |
| 125 | function activate_code() { |
| 126 | jQuery.ajax({ |
| 127 | method: 'POST', |
| 128 | url: pathname + '?action=code_manager_activate_code_preview', |
| 129 | data: { |
| 130 | wpnonce: wpnonce, |
| 131 | code_id: jQuery('#code_id').val(), |
| 132 | page: 'code_manager_post' |
| 133 | } |
| 134 | }).done( |
| 135 | function(msg) { |
| 136 | if (msg==='OK') { |
| 137 | jQuery.notify('Preview activated', 'success'); |
| 138 | } else { |
| 139 | jQuery.notify(msg, 'error'); |
| 140 | } |
| 141 | } |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | function deactivate_code() { |
| 146 | jQuery.ajax({ |
| 147 | method: 'POST', |
| 148 | url: pathname + '?action=code_manager_deactivate_code_preview', |
| 149 | data: { |
| 150 | wpnonce: wpnonce, |
| 151 | code_id: jQuery('#code_id').val(), |
| 152 | page: 'code_manager_post' |
| 153 | } |
| 154 | }).done( |
| 155 | function(msg) { |
| 156 | if ( msg === 'OK' ) { |
| 157 | jQuery.notify('Preview deactivated', 'success'); |
| 158 | } else { |
| 159 | jQuery.notify(msg, 'error'); |
| 160 | } |
| 161 | } |
| 162 | ); |
| 163 | } |
| 164 |