123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- o2.widget = o2.widget || {};
- //o2.require("o2.widget.ScriptEditor", null, false);
- o2.require("o2.widget.JavascriptEditor", null, false);
- o2.widget.ScriptArea = new Class({
- Implements: [Options, Events],
- Extends: o2.widget.Common,
- options: {
- "title": "ScriptArea",
- "style": "default",
- "helpStyle" : "default",
- "maxObj": document.body,
- "isload": false,
- "isbind": true,
- "mode": "javascript",
- "runtime": "all",
- "key": "code",
- "forceType": null,
- "type": "web" //"service" or "web"
- },
- initialize: function(node, options){
- this.setOptions(options);
- this.node = $(node);
- this.container = new Element("div");
- this.path = o2.session.path+"/widget/$ScriptArea/";
- this.cssPath = o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/css.wcss";
- this._loadCss();
- },
- load: function(content){
- if (this.fireEvent("queryLoad")){
- this.container.set("styles", this.css.container);
- this.container.inject(this.node);
- this.createTitleNode();
- this.createContent(content);
- this.fireEvent("postLoad");
- }
- },
- createTitleNode: function(){
- this.titleNode = new Element("div", {
- "styles": this.css.titleNode,
- "events": {
- "dblclick": this.toggleSize.bind(this)
- }
- }).inject(this.container);
- this.titleActionNode = new Element("div", {
- "styles": this.css.titleActionNode,
- "events": {
- "click": this.toggleSize.bind(this)
- }
- }).inject(this.titleNode);
- this.referenceNode = new Element("div", {
- "styles": this.css.referenceNode,
- "title": "open API",
- "events": {
- "click": this.openApi.bind(this)
- }
- }).inject(this.titleNode);
- this.titleTextNode = new Element("div", {
- "styles": this.css.titleTextNode,
- "text": this.options.title
- }).inject(this.titleNode);
- },
- openApi: function(){
- o2.openWindow(this.options.api || "../api")
- },
- toggleSize: function(e){
- var status = this.titleActionNode.retrieve("status", "max");
- if (status=="max"){
- this.maxSize();
- }else{
- this.returnSize();
- }
- },
- maxSize: function(){
- var obj = this.options.maxObj;
- var coordinates = obj.getCoordinates(obj.getOffsetParent());
- this.container.store("size", {"height": this.container.getStyle("height"), "width": this.container.getStyle("width")});
- this.jsEditor.showLineNumbers();
- this.jsEditor.max();
- this.container.inject(obj, "top");
- this.container.setStyles({
- "position": "relative",
- // "top": coordinates.top,
- // "left": coordinates.left,
- //"top": coordinates.top+"px",
- //"left": coordinates.left+"px",
- //"width": coordinates.width,
- "width": "100%",
- "height": coordinates.height-2,
- "z-index": 20001
- });
- this.resizeContentNodeSize();
- this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/return.png) center center no-repeat");
- this.titleActionNode.store("status", "return");
- this.jsEditor.focus();
- this.fireEvent("maxSize");
- },
- returnSize: function(){
- var size = this.container.retrieve("size");
- //this.editor.setOption("lineNumbers", false);
- this.jsEditor.hideLineNumbers();
- this.container.inject(this.node);
- this.container.setStyles({
- "position": "static",
- "top": 0,
- "left": 0,
- "width": "auto",
- "height": size.height
- });
- this.resizeContentNodeSize();
- this.titleActionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/max.png) center center no-repeat");
- this.titleActionNode.store("status", "max");
- this.jsEditor.focus();
- this.fireEvent("returnSize");
- },
- resizeContentNodeSize: function(){
- var titleSize = this.titleNode.getSize();
- var size = this.container.getSize();
- var h = this.container.getStyle("height").toInt();
- var th = this.titleNode.getStyle("height").toInt();
- var height = (size.y || h)-(titleSize.y || th)-2-6;
- this.contentNode.setStyle("height", ""+height+"px");
- if (this.jsEditor) this.jsEditor.resize();
- },
- toJson: function(){
- return (this.editor) ? {"code": this.editor.getValue(), "html": this.editor.getValue()} : this.contentCode;
- },
- createContent: function(content){
- var codeIcon = (this.options.type!=="service") ? "web_code.png" : "code.png";
- var codeEmptyIcon = (this.options.type!=="service") ? "web_code_empty.png" : "code_empty.png";
- this.contentNode = new Element("div", {
- "styles": this.css.contentNode
- }).inject(this.container);
- this.resizeContentNodeSize();
- if (!content || !content.code){
- this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeEmptyIcon+") center center no-repeat");
- }else{
- this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeIcon+") center center no-repeat");
- }
- this.contentCode = content;
- if (this.options.isload){
- this.loadEditor(content);
- }else{
- var inforNode = new Element("div", {"styles": this.css.inforNode, "text": o2.LP.widget.scriptAreaEditNotice}).inject(this.contentNode);
- var _self = this;
- inforNode.addEvent("click", function(){
- this.destroy();
- _self.loadEditor(content);
- });
- }
- },
- bind: function(content){
- if( o2.typeOf(content) !== "object" )return;
- this.value = content.code;
- this.html = content.code;
- if (content.editors){
- content.editors.push(this.jsEditor);
- }else{
- Object.defineProperty(content, "editors", {
- configurable : false,
- enumerable : false,
- writable: true,
- "value": []
- });
- content.editors.push(this.jsEditor);
- Object.defineProperty(content, this.options.key, {
- configurable : true,
- enumerable : true,
- "get": function(){return this.value;}.bind(this),
- "set": function(v){
- content.editors.each(function(editor){
- if (editor.editor){
- if (v!==editor.editor.getValue()) editor.editor.setValue(v,1);
- }else{
- editor.reload();
- }
- });
- this.value = v;
- }.bind(this)
- });
- }
- },
- loadEditor: function(content){
- var value=(content) ? content.code : "";
- value = (value) ? value : "";
- this.jsEditor = new o2.widget.JavascriptEditor(this.contentNode,{
- "runtime": this.options.runtime || "all",
- "forceType": this.options.forceType,
- "option": {
- "value": value,
- "lineNumbers": false,
- "mode": this.options.mode
- },
- "onPostLoad": function(){
- this.editor = this.jsEditor.editor;
- //this.editor.id = "2";
- this.jsEditor.addEditorEvent("change", function() {
- this.fireEvent("change");
- }.bind(this));
- this.jsEditor.addEditorEvent("blur", function() {
- this.fireEvent("blur");
- }.bind(this));
- // this.editor.on("change", function() {
- // this.fireEvent("change");
- // }.bind(this));
- // this.editor.on("paste", function() {
- // o2.load("JSBeautifier", function(){
- // this.editor.setValue(js_beautify(this.editor.getValue()));
- // }.bind(this));
- // this.fireEvent("paste");
- // }.bind(this));
- this.jsEditor.resize();
- this.fireEvent("postLoad");
- }.bind(this),
- "onSave": function(){
- this.fireEvent("change");
- this.fireEvent("save");
- }.bind(this)
- });
- this.jsEditor.load();
- if (this.options.isbind) this.bind(content);
- // this.createScriptReferenceMenu();
- //
- //
- // this.jsEditor.addEvent("reference", function(editor, e, e1){
- // if (!this.scriptReferenceMenu){
- // this.createScriptReferenceMenu(this.showReferenceMenu.bind(this));
- // }else{
- // this.showReferenceMenu();
- // }
- // }.bind(this));
- var codeIcon = (this.options.type!=="service") ? "web_code.png" : "code.png";
- this.referenceNode.setStyle("background", "url("+o2.session.path+"/widget/$ScriptArea/"+this.options.style+"/icon/"+codeIcon+") center center no-repeat")
- },
- createScriptReferenceMenu: function(callback){
- o2.require("o2.widget.ScriptHelp", function(){
- this.scriptReferenceMenu = new o2.widget.ScriptHelp(this.referenceNode, this.jsEditor.editor, {
- "style" : this.options.helpStyle,
- "event": "click",
- "onPostLoad": function(){
- if (callback) callback();
- }.bind(this)
- });
- this.scriptReferenceMenu.getEditor = function(){return this.jsEditor.editor;}.bind(this)
- }.bind(this));
- },
- showReferenceMenu: function(){
- var pos = this.jsEditor.getCursorPixelPosition();
- var e = {"page": {}};
- e.page.x = pos.left;
- e.page.y = pos.top;
- this.scriptReferenceMenu.menu.showIm(e);
- },
- focus: function(){
- if (this.jsEditor) this.jsEditor.focus();
- },
- destroy: function(){
- this.fireEvent("destroy");
- this.container.destroy();
- o2.release(this);
- }
- });
|