add options for code mirror to be read only

This commit is contained in:
John Mitchell
2016-07-14 15:38:51 -04:00
parent 0882d26ef8
commit 89a4f8f77c
3 changed files with 16 additions and 9 deletions

View File

@@ -30,7 +30,7 @@
angular.module('AngularCodeMirrorModule', []) angular.module('AngularCodeMirrorModule', [])
.factory('AngularCodeMirror', [ function() { .factory('AngularCodeMirror', [ function() {
return function() { return function(readOnly) {
var fn = function() { var fn = function() {
this.myCodeMirror = null; this.myCodeMirror = null;
@@ -47,9 +47,9 @@ angular.module('AngularCodeMirrorModule', [])
height = 0; height = 0;
self.element = $(element); self.element = $(element);
// We don't want to touch the original textarea. Angular likely has a model and other listeners // We don't want to touch the original textarea. Angular likely has a model and other listeners
// attached to it. In prior iterations attaching CodeMirror to it seemed to go bad, so we'll insert a // attached to it. In prior iterations attaching CodeMirror to it seemed to go bad, so we'll insert a
// <div> under it, hide the textarea and let CodeMirror attach to the <div>. // <div> under it, hide the textarea and let CodeMirror attach to the <div>.
if ($('#cm-' + model + '-container').length > 0) { if ($('#cm-' + model + '-container').length > 0) {
$('#cm-' + model + '-container').empty(); $('#cm-' + model + '-container').empty();
@@ -57,18 +57,25 @@ angular.module('AngularCodeMirrorModule', [])
else { else {
self.element.after("<div id=\"cm-" + model + "-container\"></div>"); self.element.after("<div id=\"cm-" + model + "-container\"></div>");
} }
// Calc the height of the text area- our CodeMirror should match. // Calc the height of the text area- our CodeMirror should match.
height += self.element.attr('rows') * parseInt($(self.element).css('line-height').replace(/px/,''),10); height += self.element.attr('rows') * parseInt($(self.element).css('line-height').replace(/px/,''),10);
height += parseInt(self.element.css('padding-top').replace(/px|%/,''),10) + height += parseInt(self.element.css('padding-top').replace(/px|%/,''),10) +
parseInt(self.element.css('padding-bottom').replace(/px|%/,''),10); parseInt(self.element.css('padding-bottom').replace(/px|%/,''),10);
height += 2; //for the border height += 2; //for the border
// hide // hide
self.element.hide(); self.element.hide();
// Initialize CodeMirror // Initialize CodeMirror
self.modes[mode].value = scope[model]; self.modes[mode].value = scope[model];
// if readOnly is passed to AngularCodeMirror, set the
// options for all modes to be readOnly
if (readOnly) {
Object.keys(self.modes).forEach(function(val) {
self.modes[val].readOnly = true;
});
}
self.myCodeMirror = CodeMirror(document.getElementById('cm-' + model + '-container'), self.modes[mode]); self.myCodeMirror = CodeMirror(document.getElementById('cm-' + model + '-container'), self.modes[mode]);
// Adjust the height // Adjust the height

View File

@@ -25,7 +25,8 @@ export default
fld = (params.variable) ? params.variable : 'variables', fld = (params.variable) ? params.variable : 'variables',
pfld = (params.parse_variable) ? params.parse_variable : 'parseType', pfld = (params.parse_variable) ? params.parse_variable : 'parseType',
onReady = params.onReady, onReady = params.onReady,
onChange = params.onChange; onChange = params.onChange,
readOnly = params.readOnly;
function removeField(fld) { function removeField(fld) {
//set our model to the last change in CodeMirror and then destroy CodeMirror //set our model to the last change in CodeMirror and then destroy CodeMirror
@@ -35,8 +36,7 @@ export default
function createField(onChange, onReady, fld) { function createField(onChange, onReady, fld) {
//hide the textarea and show a fresh CodeMirror with the current mode (json or yaml) //hide the textarea and show a fresh CodeMirror with the current mode (json or yaml)
scope[fld + 'codeMirror'] = AngularCodeMirror(readOnly);
scope[fld + 'codeMirror'] = AngularCodeMirror();
scope[fld + 'codeMirror'].addModes($AnsibleConfig.variable_edit_modes); scope[fld + 'codeMirror'].addModes($AnsibleConfig.variable_edit_modes);
scope[fld + 'codeMirror'].showTextArea({ scope[fld + 'codeMirror'].showTextArea({
scope: scope, scope: scope,

View File

@@ -641,7 +641,7 @@ export default
return true; return true;
}); });
//scope.setSearchAll('host'); //scope.setSearchAll('host');
ParseTypeChange({ scope: scope, field_id: 'pre-formatted-variables' }); ParseTypeChange({ scope: scope, field_id: 'pre-formatted-variables', readOnly: true });
scope.$emit('LoadPlays', data.related.job_events); scope.$emit('LoadPlays', data.related.job_events);
}) })
.error(function(data, status) { .error(function(data, status) {