PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.0
Code Manager v1.0.0
1.0.47 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
code-manager / assets / js / code_manager.js
code-manager / assets / js Last commit date
clipboard.min.js 5 years ago code_manager.js 5 years ago code_manager_listmode.js 5 years ago code_manager_tabmode.js 5 years ago notify.min.js 5 years ago
code_manager.js
166 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(document).ready(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(document).ready(function(){
121 jQuery('.cm_menu_title').tooltip();
122 jQuery('td.label label').tooltip();
123 jQuery('#code_manager_preview').tooltip();
124 });
125 });
126
127 function activate_code() {
128 jQuery.ajax({
129 method: 'POST',
130 url: pathname + '?action=code_manager_activate_code_preview',
131 data: {
132 wpnonce: wpnone_activate_code_preview,
133 code_id: jQuery('#code_id').val(),
134 page: 'code_manager_post'
135 }
136 }).success(
137 function(msg) {
138 if (msg==='OK') {
139 jQuery.notify('Preview activated', 'success');
140 } else {
141 jQuery.notify(msg, 'error');
142 }
143 }
144 );
145 }
146
147 function deactivate_code() {
148 jQuery.ajax({
149 method: 'POST',
150 url: pathname + '?action=code_manager_deactivate_code_preview',
151 data: {
152 wpnonce: wpnone_activate_code_preview,
153 code_id: jQuery('#code_id').val(),
154 page: 'code_manager_post'
155 }
156 }).success(
157 function(msg) {
158 if ( msg === 'OK' ) {
159 jQuery.notify('Preview deactivated', 'success');
160 } else {
161 jQuery.notify(msg, 'error');
162 }
163 }
164 );
165 }
166