o2.widget = o2.widget || {}; o2.require("o2.widget.codemirror", null, false); o2.require("o2.widget.ace", null, false); o2.require("o2.xDesktop.UserData", null, false); o2.widget.JavascriptEditor = new Class({ Implements: [Options, Events], options: { "type": "ace", "title": "JavascriptEditor", "style": "default", "option": { value: "", mode: "javascript", "lineNumbers": true } }, initialize: function(node, options){ this.setOptions(options); this.editorClass = o2.widget[this.options.type]; this.node = $(node); }, getEditorTheme: function(callback){ if (!o2.editorData){ o2.UD.getData("editor", function(json){ if (json.data){ o2.editorData = JSON.decode(json.data); }else{ o2.editorData = { "javascriptEditor": { "theme": "tomorrow" } }; } if (callback) callback(); }); }else{ if (callback) callback(); } }, load: function(callback){ this.getEditorTheme(function(json){ if (o2.editorData.javascriptEditor){ this.theme = o2.editorData.javascriptEditor.theme; }else{ o2.editorData.javascriptEditor = {"theme": "tomorrow"}; } if (!this.theme) this.theme = "tomorrow"; if (this.options.type.toLowerCase()=="ace"){ this.loadAce(callback); } if (this.options.type.toLowerCase()=="codeMirror"){ this.loadCodeMirror(callback); } }.bind(this)); }, focus: function(){ if (this.editor){ this.editor.focus(); this.goto(); } }, goto: function(){ var p = this.editor.getCursorPosition(); if (p.row==0){ p.row = this.editor.renderer.getScrollBottomRow(); } this.editor.gotoLine(p.row+1, p.column+1, true); }, loadAce: function(callback){ this.editorClass.load(function(){ var exports = ace.require("ace/ext/language_tools"); this.editor = ace.edit(this.node); this.editor.session.setMode("ace/mode/"+this.options.option.mode); this.editor.setTheme("ace/theme/"+this.theme); this.editor.setOptions({ enableBasicAutocompletion: true, enableSnippets: true, enableLiveAutocompletion: true }); if (this.options.option.value) this.editor.setValue(this.options.option.value); this.focus(); //this.editor.focus(); //this.editor.navigateFileStart(); //添加自动完成列表 o2.require("o2.xScript.Macro", function(){ var json = null; o2.getJSON("/o2_core/o2/widget/$JavascriptEditor/environment.json", function(data){ json = data; }, false); this.Macro = new o2.Macro.FormContext(json); exports.addCompleter({ identifierRegexps: [ /[a-zA-Z_0-9\$\-\u00A2-\uFFFF\.]/ ], getCompletions: function(editor, session, pos, prefix, callback){ var x = prefix.substr(0, prefix.lastIndexOf(".")); code = "try {return "+x+";}catch(e){return null;}"; var o = this.Macro.exec(code); if (o){ var arr1 = []; var arr2 = []; Object.keys(o).each(function(key){ var type = typeOf(o[key]); if (type==="function") { var count = o[key].length; var v = x+"."+key+"("; for (var i=1; i<=count; i++) v+= (i==count) ? "par"+i : "par"+i+", "; v+=");"; arr1.push({ caption: key, value: v, score: 3, meta: "function O2" }); }else{ arr2.push({ caption: key, value: x+"."+key, score: 3, meta: (type!="null") ? typeOf(o[key])+" O2" : "O2" }); } }); callback(null, arr1.concat(arr2)); } }.bind(this) }); }.bind(this)); // this.editor.on("change", function(e){ // if (e.start.row!==e.end.row){ // debugger; // var code = ""; // for (var i=e.start.row; i= 0; i--) { if (regex.test(text[i])) buf.push(text[i]); else break; } return buf.reverse().join(""); } });