|
2 | 2 | /* global console */ |
3 | 3 | /* global vizSettingsHaveChanged */ |
4 | 4 | /* global vizHaveSettingsChanged */ |
| 5 | +/* global wp */ |
5 | 6 |
|
6 | 7 | (function($, v) { |
7 | 8 | var timeout; |
|
22 | 23 | clear: updateChart |
23 | 24 | }); |
24 | 25 | $('#settings-form textarea[name="manual"]').change(validateJSON).keyup(validateJSON); |
| 26 | + initManualConfigEditor(); |
25 | 27 | vizSettingsHaveChanged(false); |
26 | 28 | }; |
27 | 29 |
|
|
173 | 175 | function validateJSON() { |
174 | 176 | $('#visualizer-error-manual').remove(); |
175 | 177 | try{ |
176 | | - var options = JSON.parse($(this).val()); |
| 178 | + JSON.parse($(this).val()); |
177 | 179 | }catch(error){ |
178 | | - $('<div class="visualizer-error" id="visualizer-error-manual">Invalid JSON: ' + error + '</div>').insertAfter($(this)); |
| 180 | + var $after = $(this).next('.CodeMirror').length ? $(this).next('.CodeMirror') : $(this); |
| 181 | + $('<div class="visualizer-error" id="visualizer-error-manual">Invalid JSON: ' + error + '</div>').insertAfter($after); |
179 | 182 | } |
180 | 183 | } |
181 | 184 |
|
| 185 | + function initManualConfigEditor() { |
| 186 | + var $textarea = $('textarea[name="manual"]'); |
| 187 | + if (!$textarea.length || $textarea.data('cm-initialized')) return; |
| 188 | + |
| 189 | + var CodeMirrorLib = (typeof wp !== 'undefined' && wp.CodeMirror) ? wp.CodeMirror : null; |
| 190 | + if (!CodeMirrorLib) return; |
| 191 | + |
| 192 | + $textarea.data('cm-initialized', true); |
| 193 | + |
| 194 | + var cm = CodeMirrorLib.fromTextArea($textarea.get(0), { |
| 195 | + mode: 'application/json', |
| 196 | + lineNumbers: true, |
| 197 | + lineWrapping: true, |
| 198 | + matchBrackets: true, |
| 199 | + autoCloseBrackets: true |
| 200 | + }); |
| 201 | + |
| 202 | + // Refresh when any sidebar group is expanded so line numbers render correctly |
| 203 | + // (CodeMirror can't measure gutter width while the container is hidden). |
| 204 | + $(document).on('click.vizManualConfig', '.viz-group-title', function() { |
| 205 | + setTimeout(function() { cm.refresh(); }, 50); |
| 206 | + }); |
| 207 | + |
| 208 | + cm.on('change', function() { |
| 209 | + cm.save(); |
| 210 | + $textarea.trigger('change'); |
| 211 | + }); |
| 212 | + } |
| 213 | + |
182 | 214 | $('.control-text').change(updateChart).keyup(updateChart); |
183 | 215 | $('.control-select, .control-checkbox, .control-check').change(updateChart); |
184 | 216 | $('.color-picker-hex').wpColorPicker({ |
185 | 217 | change: updateChart, |
186 | 218 | clear: updateChart |
187 | 219 | }); |
188 | 220 | $('textarea[name="manual"]').change(validateJSON).keyup(validateJSON); |
| 221 | + initManualConfigEditor(); |
189 | 222 |
|
190 | 223 | }); |
191 | 224 | })(jQuery, visualizer); |
|
0 commit comments