JavascriptEditor.js 24.5 KB
Newer Older
NoSubject's avatar
NoSubject 已提交
1 2 3 4 5 6
o2.widget = o2.widget || {};
o2.require("o2.widget.codemirror", null, false);
o2.require("o2.xDesktop.UserData", null, false);
o2.widget.JavascriptEditor = new Class({
	Implements: [Options, Events],
	options: {
NoSubject's avatar
NoSubject 已提交
7 8
        //"type": "ace",
        "type": "monaco",
NoSubject's avatar
NoSubject 已提交
9 10 11 12 13 14
		"title": "JavascriptEditor",
		"style": "default",
		"option": {
			value: "",
			mode: "javascript",
			"lineNumbers": true
NoSubject's avatar
NoSubject 已提交
15
		},
NoSubject's avatar
NoSubject 已提交
16
		"runtime": "all"
NoSubject's avatar
NoSubject 已提交
17 18 19
	},
	initialize: function(node, options){
		this.setOptions(options);
NoSubject's avatar
NoSubject 已提交
20
		this.unbindEvents = [];
NoSubject's avatar
NoSubject 已提交
21 22
		this.node = $(node);
	},
NoSubject's avatar
NoSubject 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    getDefaultEditorData: function(){
	    switch (this.options.type) {
            case "ace":
                return {
                    "javascriptEditor": {
                        "theme": "tomorrow",
                        "fontSize" : "12px"
                    }
                };
            case "monaco":
                return {
                    "javascriptEditor": {
                        "monaco_theme": "vs",
                        "fontSize" : "12px"
                    }
                };
        }
    },
NoSubject's avatar
NoSubject 已提交
41 42 43 44 45 46
    getEditorTheme: function(callback){
        if (!o2.editorData){
            o2.UD.getData("editor", function(json){
                if (json.data){
                    o2.editorData = JSON.decode(json.data);
                }else{
NoSubject's avatar
NoSubject 已提交
47
                    o2.editorData = this.getDefaultEditorData();
NoSubject's avatar
NoSubject 已提交
48 49
                }
                if (callback) callback();
NoSubject's avatar
NoSubject 已提交
50
            }.bind(this));
NoSubject's avatar
NoSubject 已提交
51 52 53 54 55 56
        }else{
            if (callback) callback();
        }
    },
    load: function(callback){
        this.getEditorTheme(function(json){
NoSubject's avatar
NoSubject 已提交
57
            this.options.type = o2.editorData.javascriptEditor.editor || "monaco";
NoSubject's avatar
NoSubject 已提交
58 59 60
            if (this.options.type.toLowerCase()=="ace"){
                this.loadAce(callback);
            }
NoSubject's avatar
NoSubject 已提交
61 62 63
            if (this.options.type.toLowerCase()=="monaco"){
                this.loadMonaco(callback);
            }
NoSubject's avatar
NoSubject 已提交
64 65 66
            if (this.options.type.toLowerCase()=="codeMirror"){
                this.loadCodeMirror(callback);
            }
NoSubject's avatar
NoSubject 已提交
67 68 69 70 71

            while (this.unbindEvents.length){
                var ev = this.unbindEvents.shift();
                this.addEditorEvent(ev.name, ev.fun);
            }
NoSubject's avatar
NoSubject 已提交
72 73
        }.bind(this));
    },
NoSubject's avatar
NoSubject 已提交
74

NoSubject's avatar
NoSubject 已提交
75 76 77
    loadMonacoEditor: function(callback){
        if (!window.monaco){
            o2.load("monaco", {"sequence": true}, function(){
78
                require.config({ paths: { "vs": "../o2_lib/vs" }});
NoSubject's avatar
NoSubject 已提交
79 80 81 82 83 84 85 86
                require(["vs/editor/editor.main"], function() {
                    if (callback) callback();
                });
            }.bind(this));
        }else{
            if (callback) callback();
        }
    },
NoSubject's avatar
NoSubject 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
    loadMonaco: function(callback){
        if (o2.editorData.javascriptEditor){
            this.theme = o2.editorData.javascriptEditor.monaco_theme;
            this.fontSize = o2.editorData.javascriptEditor.fontSize;
        }else{
            o2.editorData.javascriptEditor = {
                "monaco_theme": "vs",
                "fontSize" : "12px"
            };
        }
        if (!this.theme) this.theme = "vs";
        if( !this.fontSize )this.fontSize = "12px";

NoSubject's avatar
NoSubject 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

        o2.require("o2.widget.monaco", function () {
            this.editorClass = o2.widget.monaco;

            this.editorClass.load(function(){

                this.editor = monaco.editor.create(this.node, {
                    value: this.options.option.value,
                    language: this.options.option.mode,
                    theme: this.theme,
                    fontSize: this.fontSize,
                    lineNumbersMinChars: 3,
                    lineNumbers: this.options.option.lineNumbers ? "on" : "off",
                    mouseWheelZoom: true,
                    automaticLayout: true
NoSubject's avatar
NoSubject 已提交
115
                });
NoSubject's avatar
NoSubject 已提交
116
                this.focus();
NoSubject's avatar
NoSubject 已提交
117

NoSubject's avatar
NoSubject 已提交
118 119 120
                this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, function(e){
                    this.fireEvent("save");
                }.bind(this));
NoSubject's avatar
NoSubject 已提交
121

NoSubject's avatar
NoSubject 已提交
122 123 124 125 126 127
                this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_I, function(e){
                    this.format();
                }.bind(this));
                this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_F, function(e){
                    this.format();
                }.bind(this));
NoSubject's avatar
NoSubject 已提交
128

NoSubject's avatar
NoSubject 已提交
129 130 131
                if( this.fontSize ){
                    this.editor.updateOptions( {"fontSize": this.fontSize} );
                }
NoSubject's avatar
NoSubject 已提交
132

NoSubject's avatar
NoSubject 已提交
133 134 135 136 137
                o2.widget.JavascriptEditor.getCompletionEnvironment(this.options.runtime, function(){
                    this.monacoModel = this.editor.getModel();
                    this.monacoModel.o2Editor = this;
                    this.registerCompletion();
                }.bind(this));
NoSubject's avatar
NoSubject 已提交
138

NoSubject's avatar
NoSubject 已提交
139 140
                this.fireEvent("postLoad");
                if (callback) callback();
NoSubject's avatar
NoSubject 已提交
141

NoSubject's avatar
NoSubject 已提交
142
            }.bind(this));
NoSubject's avatar
NoSubject 已提交
143 144 145
        }.bind(this));
    },

NoSubject's avatar
NoSubject 已提交
146
    loadAce: function(callback){
NoSubject's avatar
NoSubject 已提交
147 148 149 150 151 152 153 154 155 156 157 158
        if (o2.editorData.javascriptEditor){
            this.theme = o2.editorData.javascriptEditor.theme;
            this.fontSize = o2.editorData.javascriptEditor.fontSize;
        }else{
            o2.editorData.javascriptEditor = {
                "theme": "tomorrow",
                "fontSize" : "12px"
            };
        }
        if (!this.theme) this.theme = "tomorrow";
        if( !this.fontSize )this.fontSize = "12px";

NoSubject's avatar
NoSubject 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        o2.require("o2.widget.ace", function(){
            this.editorClass = o2.widget.ace;

            this.editorClass.load(function(){
                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,
                    showLineNumbers: this.options.option.lineNumbers
                });
                if (this.options.option.value) this.editor.setValue(this.options.option.value);
                this.editor.o2Editor = this;

                this.focus();

                this.editor.commands.addCommand({
                    name: 'save',
                    bindKey: {win: 'Ctrl-S',  mac: 'Command-S'},
                    exec: function(editor) {
                        this.fireEvent("save");
                    }.bind(this),
                    readOnly: false
                });
NoSubject's avatar
NoSubject 已提交
185

NoSubject's avatar
NoSubject 已提交
186 187 188 189 190 191 192 193
                this.editor.commands.addCommand({
                    name: 'format',
                    bindKey: {win: 'Ctrl-Alt-i|Ctrl-Alt-f',  mac: 'Command-i|Command-f'},
                    exec: function(editor, e, e1) {
                        this.format();
                    }.bind(this),
                    readOnly: false
                });
NoSubject's avatar
NoSubject 已提交
194

NoSubject's avatar
NoSubject 已提交
195 196 197 198 199 200 201 202
                this.editor.commands.addCommand({
                    name: "showKeyboardShortcuts",
                    bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
                    exec: function(editor) {
                        ace.config.loadModule("ace/ext/keybinding_menu", function(module) {
                            module.init(editor);
                            editor.showKeyboardShortcuts()
                        })
NoSubject's avatar
NoSubject 已提交
203 204 205
                    }.bind(this)
                });

NoSubject's avatar
NoSubject 已提交
206 207 208
                this.node.addEvent("keydown", function(e){
                    e.stopPropagation();
                });
NoSubject's avatar
NoSubject 已提交
209

NoSubject's avatar
NoSubject 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                if( this.fontSize ){
                    this.setFontSize( this.fontSize );
                }

                o2.widget.JavascriptEditor.getCompletionEnvironment(this.options.runtime, function(){
                    this.registerCompletion();
                }.bind(this));

                this.fireEvent("postLoad");
                if (callback) callback();
            }.bind(this));
        }.bind(this));
    },
    registerCompletion: function(){
        debugger;
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.registerCompletionAce(); break;
                case "monaco": this.registerCompletionMonaco(); break;
            }
        }
    },
NoSubject's avatar
NoSubject 已提交
232

NoSubject's avatar
NoSubject 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    getCompletionObject: function(textPrefix, runtime){
        var macro = o2.widget.JavascriptEditor.runtimeEnvironment[runtime];
        var o = null;
        if (macro){
            code = "try {return "+textPrefix+";}catch(e){return null;}";
            o = macro.exec(code);
        }
        return o;
    },
    registerCompletionMonaco: function(){
        if (!o2.widget.monaco.registeredCompletion){
            monaco.languages.registerCompletionItemProvider('javascript', {
                "triggerCharacters": ["."],
                provideCompletionItems: function (model, position, context, token) {
                    var textUntilPosition = model.getValueInRange({ startLineNumber: position.lineNumber, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column });
                    var textPrefix = textUntilPosition.substr(0, textUntilPosition.lastIndexOf("."));
                    var o = this.getCompletionObject(textPrefix, model.o2Editor.options.runtime);

                    var word = model.getWordUntilPosition(position);
                    var range = { startLineNumber: position.lineNumber, endLineNumber: position.lineNumber, startColumn: word.startColumn, endColumn: word.endColumn };

                    if (o) {
                        var arr = [];
                        Object.keys(o).each(function (key) {
                            var type = typeOf(o[key]);
                            if (type === "function") {
                                var count = o[key].length;
                                var v = key + "(";
                                for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
                                v += ")";
                                arr.push({ label: key, kind: monaco.languages.CompletionItemKind.Function, insertText: v, range: range, detail: type });
                            } else {
                                arr.push({ label: key, kind: monaco.languages.CompletionItemKind.Interface, insertText: key, range: range, detail: type });
                            }
                        });
                    }
                    return {suggestions: arr}
NoSubject's avatar
NoSubject 已提交
270 271
                }.bind(this)
            });
NoSubject's avatar
NoSubject 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
            o2.widget.monaco.registeredCompletion = true;
        }
    },
    registerCompletionAce: function(){
        if (!o2.widget.ace.registeredCompletion){
            //添加自动完成列表
            var exports = ace.require("ace/ext/language_tools");
            exports.addCompleter({
                identifierRegexps: [
                    /[a-zA-Z_0-9\$\-\u00A2-\uFFFF\.]/
                ],
                getCompletions: function(editor, session, pos, prefix, callback){
                    var x = prefix.substr(0, prefix.lastIndexOf("."));
                    var o = this.getCompletionObject(x, editor.o2Editor.options.runtime);

                    if (o){
                        var arr = [];
                        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+=");";
                                arr.push({ caption: key, value: v, score: 3, meta: type });
                            }else{
                                arr.push({ caption: key, value: x+"."+key, score: 3, meta: type });
                            }
                        });
                        callback(null, arr);
                    }
                }.bind(this)
NoSubject's avatar
NoSubject 已提交
304
            });
NoSubject's avatar
NoSubject 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            o2.widget.ace.registeredCompletion = true;
        }
    },
    changeEditor: function(type){
	    debugger;
        if (this.editor){
            var value = this.getValue();
            this.destroyEditor();
            this.options.type = type;
            this.load(function(){
                this.setValue(value);
            }.bind(this));
        }else{
            this.options.type = o2.editorData.javascriptEditor.editor;
            if (this.options.type.toLowerCase()=="ace"){
                this.loadAce(callback);
NoSubject's avatar
NoSubject 已提交
321
            }
NoSubject's avatar
NoSubject 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
            if (this.options.type.toLowerCase()=="monaco"){
                this.loadMonaco(callback);
            }
            if (this.options.type.toLowerCase()=="codeMirror"){
                this.loadCodeMirror(callback);
            }
        }
    },
    destroyEditor: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.destroy(); this.node.empty(); break;
                case "monaco": this.editor.dispose(); break;
            }
        }
    },
    setTheme: function(theme){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.setTheme( "ace/theme/"+theme); break;
                case "monaco": monaco.editor.setTheme(theme); break;
            }
        }
    },
    setFontSize: function(fontSize){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.setFontSize( fontSize ); break;
                case "monaco": this.editor.updateOptions({"fontSize": fontSize}); break;
            }
        }
NoSubject's avatar
NoSubject 已提交
353
    },
NoSubject's avatar
NoSubject 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    setValue: function(v){
        if (this.editor) this.editor.setValue(v);
    },
    getValue: function(){
        return (this.editor) ? this.editor.getValue() : "";
    },
    resize: function(y){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.resize(); break;
                case "monaco": this.editor.layout(); break;
            }
        }
    },
    addEditorEvent: function(name, fun){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.on(name, fun); break;
                case "monaco":
                    var ev = name;
                    switch (ev) {
                        case "change": ev = "onDidChangeModelContent"; break;
NoSubject's avatar
NoSubject 已提交
376 377
                        case "blue": ev = "onDidBlurEditorText"; break;

NoSubject's avatar
NoSubject 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
                    }
                    if (this.editor[ev]) this.editor[ev](fun);
                    break;
            }
        }else{
            this.unbindEvents.push({"name": name, "fun": fun});
        }
    },
    validatedAce: function(){
        var session = this.editor.getSession();
        var annotations = session.getAnnotations();
        for (var i=0; i<annotations.length; i++){
            if (annotations[i].type=="error") return false;
        }
        return true;
    },
    validatedMonaco: function(){
        var mod = this.editor.getModel();
        var ms = monaco.editor.getModelMarkers({"resource": mod.uri});
        for (var i=0; i<ms.length; i++){
            if (ms[i].severity==8) return false;
        }
        return true;
    },

    validated: function(){
        if (this.editor){
           switch (this.options.type.toLowerCase()) {
               case "ace": return this.validatedAce();
               case "monaco": return this.validatedMonaco();
           }
            return true;
        }
        return true
    },

    formatAce: function(){
        var mode = this.options.option.mode.toString().toLowerCase();
        if (mode==="javascript"){
            o2.load("JSBeautifier", function(){
                this.editor.setValue(js_beautify(editor.getValue()));
            }.bind(this));
        }else if (mode==="html"){
            o2.load("JSBeautifier_html", function(){
                this.editor.setValue(html_beautify(editor.getValue()));
            }.bind(this));
        }else if (mode==="css"){
            o2.load("JSBeautifier_css", function(){
                this.editor.setValue(css_beautify(editor.getValue()));
            }.bind(this));
        }else{
            o2.load("JSBeautifier", function(){
                this.editor.setValue(js_beautify(editor.getValue()));
            }.bind(this));
        }
    },
    formatMonaco: function(){
        this.editor.getAction("editor.action.formatDocument").run();
    },
    format: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.formatAce();
                case "monaco": this.formatMonaco();
            }
        }
    },

    focus: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.focus(); this.goto(); break;
                case "monaco": this.editor.focus();
            }
        }
    },
    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);
    },
NoSubject's avatar
NoSubject 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    showLineNumbers: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.setOption("showLineNumbers", true); break;
                case "codeMirror":  this.editor.setOption("lineNumbers", true); break;
                case "monaco": this.editor.updateOptions({"lineNumbers": "on"});
            }
        }
    },
    hideLineNumbers: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "ace": this.editor.setOption("showLineNumbers", false); break;
                case "codeMirror":  this.editor.setOption("lineNumbers", false); break;
                case "monaco": this.editor.updateOptions({"lineNumbers": "off"});
            }
        }
    },
    max: function(){
        if (this.editor){
            switch (this.options.type.toLowerCase()) {
                case "codeMirror": this.editor.setSize("100%", "100%"); break;
                case "ace": this.editor.resize(); break;
                case "monaco": this.editor.layout(); break;
            }
        }
    },
NoSubject's avatar
NoSubject 已提交
488

NoSubject's avatar
NoSubject 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	loadCodeMirror: function(callback){
		if (this.fireEvent("queryLoad")){
			this.editorClass.load(function(){
				this.editorClass.loadJavascript(function(editor){
					this.options.option.mode = "javascript";
					this.editor = CodeMirror(this.node, this.options.option);

					this.editor.setSize("100%", "100%");
					this.fireEvent("postLoad");

                    if (callback) callback();
				}.bind(this));
			}.bind(this));
		}
	},

    getCursorPixelPosition: function(){
        var session = this.editor.getSession();
        var pos = this.editor.getCursorPosition();
        var line = session.getLine(pos.row);
        var prefix = this.retrievePrecedingIdentifier(line, pos.column);

        var base = session.doc.createAnchor(pos.row, pos.column - prefix.length);
        base.$insertRight = true;

        var renderer = this.editor.renderer;
        var pos = renderer.$cursorLayer.getPixelPosition(base, true);

        var rect = this.editor.container.getBoundingClientRect();
        pos.top += rect.top - renderer.layerConfig.offset;
        pos.left += rect.left - this.editor.renderer.scrollLeft;
        pos.left += renderer.gutterWidth;

        return pos;
    },
    retrievePrecedingIdentifier: function(text, pos, regex) {
        regex = regex || /[a-zA-Z_0-9\$\-\u00A2-\uFFFF]/;
        var buf = [];
        for (var i = pos-1; i >= 0; i--) {
            if (regex.test(text[i]))
                buf.push(text[i]);
            else
                break;
        }
        return buf.reverse().join("");
    }
NoSubject's avatar
NoSubject 已提交
535 536 537 538
});

o2.widget.JavascriptEditor.runtimeEnvironment = {};
o2.widget.JavascriptEditor.getCompletionEnvironment = function(runtime, callback) {
NoSubject's avatar
NoSubject 已提交
539
    debugger;
NoSubject's avatar
NoSubject 已提交
540 541 542 543 544 545 546 547 548
    if (!o2.widget.JavascriptEditor.runtimeEnvironment[runtime]) {
        o2.require("o2.xScript.Macro", function() {
            switch (runtime) {
                case "service":
                    o2.widget.JavascriptEditor.getServiceCompletionEnvironment(runtime,callback);
                    break;
                case "server":
                    o2.widget.JavascriptEditor.getServerCompletionEnvironment(runtime,callback);
                    break;
NoSubject's avatar
NoSubject 已提交
549 550 551
                case "all":
                    o2.widget.JavascriptEditor.getAllCompletionEnvironment(runtime,callback);
                    break;
NoSubject's avatar
NoSubject 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
                default:
                    o2.widget.JavascriptEditor.getDefaultCompletionEnvironment(runtime,callback);
            }
        });
    } else {
        if (callback) callback();
    }
};

o2.widget.JavascriptEditor.getServiceCompletionEnvironment = function(runtime, callback) {
    var serviceScriptText = null;
    var serviceScriptSubstitute = null;
    var check = function () {
        if (o2.typeOf(serviceScriptText) !== "null" && o2.typeOf(serviceScriptSubstitute) !== "null") {
            var code = "o2.Macro.swapSpace.tmpMacroCompletionFunction = function (){\n" + serviceScriptSubstitute + "\n" + serviceScriptText + "\nreturn bind;" + "\n};";
            Browser.exec(code);
            var ev = o2.Macro.swapSpace.tmpMacroCompletionFunction();
            o2.widget.JavascriptEditor.runtimeEnvironment[runtime] = {
                "environment": ev,
                exec: function(code){
                    return o2.Macro.exec(code, this.environment);
                }
            }
            if (callback) callback();
        }
    }
NoSubject's avatar
NoSubject 已提交
578

NoSubject's avatar
NoSubject 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    o2.xhr_get("../x_desktop/js/initialServiceScriptText.js", function (xhr) {
        serviceScriptText = xhr.responseText;
        check();
    }, function () {
        serviceScriptText = "";
        check();
    });
    o2.xhr_get("../x_desktop/js/initalServiceScriptSubstitute.js", function (xhr) {
        serviceScriptSubstitute = xhr.responseText;
        check();
    }, function () {
        serviceScriptSubstitute = "";
        check();
    });
};

o2.widget.JavascriptEditor.getServerCompletionEnvironment = function(runtime, callback) {
NoSubject's avatar
NoSubject 已提交
596 597 598 599 600 601 602 603 604 605 606 607
    var serverScriptText = null;
    var serverScriptSubstitute = null;
    var check = function () {
        if (o2.typeOf(serverScriptText) !== "null" && o2.typeOf(serverScriptSubstitute) !== "null") {
            var code = "o2.Macro.swapSpace.tmpMacroCompletionFunction = function (){\n" + serverScriptSubstitute + "\n" + serverScriptText + "\nreturn bind;" + "\n};";
            Browser.exec(code);
            var ev = o2.Macro.swapSpace.tmpMacroCompletionFunction();
            o2.widget.JavascriptEditor.runtimeEnvironment[runtime] = {
                "environment": ev,
                exec: function(code){
                    return o2.Macro.exec(code, this.environment);
                }
NoSubject's avatar
NoSubject 已提交
608
            }
NoSubject's avatar
NoSubject 已提交
609
            if (callback) callback();
NoSubject's avatar
NoSubject 已提交
610
        }
NoSubject's avatar
NoSubject 已提交
611
    }
NoSubject's avatar
NoSubject 已提交
612

NoSubject's avatar
NoSubject 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626
    o2.xhr_get("../x_desktop/js/initialScriptText.js", function (xhr) {
        serverScriptText = xhr.responseText;
        check();
    }, function () {
        serverScriptText = "";
        check();
    });
    o2.xhr_get("../x_desktop/js/initalScriptSubstitute.js", function (xhr) {
        serverScriptSubstitute = xhr.responseText;
        check();
    }, function () {
        serverScriptSubstitute = "";
        check();
    });
NoSubject's avatar
NoSubject 已提交
627 628 629 630 631 632 633 634 635
};

o2.widget.JavascriptEditor.getDefaultCompletionEnvironment = function(runtime, callback){
    var json = null;
    o2.getJSON("../o2_core/o2/widget/$JavascriptEditor/environment.json", function (data) {
        json = data;
        o2.widget.JavascriptEditor.runtimeEnvironment[runtime] = new o2.Macro.FormContext(json);
        if (callback) callback();
    });
NoSubject's avatar
NoSubject 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
}

o2.widget.JavascriptEditor.getAllCompletionEnvironment = function(runtime, callback){
    var check = function(){
        if (o2.widget.JavascriptEditor.runtimeEnvironment["service"] && o2.widget.JavascriptEditor.runtimeEnvironment["server"] && o2.widget.JavascriptEditor.runtimeEnvironment["web"] ){
            var ev = Object.merge(o2.widget.JavascriptEditor.runtimeEnvironment["service"].environment,
                o2.widget.JavascriptEditor.runtimeEnvironment["server"].environment,
                o2.widget.JavascriptEditor.runtimeEnvironment["web"].environment)
            o2.widget.JavascriptEditor.runtimeEnvironment[runtime] = {
                "environment": ev,
                exec: function(code){
                    return o2.Macro.exec(code, this.environment);
                }
            }
            if (callback) callback();
        }
    }
    o2.widget.JavascriptEditor.getServiceCompletionEnvironment("service", check);
    o2.widget.JavascriptEditor.getServerCompletionEnvironment("server", check);
    o2.widget.JavascriptEditor.getDefaultCompletionEnvironment("web", check);

NoSubject's avatar
NoSubject 已提交
657
}