/** * yaml页面 * @author wangting * @date 2021-5-27 */ layui.define(['layer','element' ],function(exports){ var webUtil = $webUtil; var Class = function(){ this.MODELNAME = "platform/monitor/VciYAML"; this.moduleKey = "VciYAML"; this.backPath = configData.compatibility?path:configData.adminServicePath; this.id = "VciYAML"; this.getContent=function(){ var that = this; var html = ""; html = [ '
', '
', '
', '' + '', '
', '
', '
', '
', '
' ].join(""); return html; }; this.init = function(){ var that = this; webUtil.copyConfig(that,that.moduleKey); webUtil.createScript('jslib/yaml/js/codemirror.js', function () { webUtil.createScript('jslib/yaml/js/yaml.js', function () { webUtil.createScript('jslib/yaml/js/js-yaml.min.js', function () { var yamlSourceCodeMirror = CodeMirror(document.getElementById('yamlSource'), { value: '', mode: 'yaml', lineNumbers: true, // 显示行数 indentUnit: 1, // 缩进单位为2 styleActiveLine: true, // 当前行背景高亮 matchBrackets: true, // 括号匹配 lineWrapping: true, // 自动换行 tabSize: 2, }); var yamlResultCodeMirror = CodeMirror(document.getElementById('yamlResult'), { value: '', mode: 'yaml', lineNumbers: true, // 显示行数 indentUnit: 1, // 缩进单位为2 styleActiveLine: true, // 当前行背景高亮 matchBrackets: true, // 括号匹配 lineWrapping: true, // 自动换行 tabSize: 2, }); // 监听输入值变化 yamlSourceCodeMirror.on('change', (cm) => { var parseYaml=parseYamlFn(cm.doc.getValue()) if(!!parseYaml.isYaml){ var json = JSON.stringify(parseYaml.isYaml, null, 2); yamlResultCodeMirror.setValue(json); }else { console.log(parseYaml.errorMessage) yamlResultCodeMirror.setValue(parseYaml.errorMessage); } }) const parseYamlFn = (str) => { let isYaml = false; let errorMessage = ''; try { isYaml = jsyaml.load(str) } catch(e) { errorMessage = e && e.message; } return { isYaml, errorMessage }; } }) }) }) }; }; var cs = new Class(); exports(cs.MODELNAME,cs); });