scriptWorker.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. window = self;
  2. window.addEvent = function(){};
  3. self.window = window;
  4. window.execScript = function(text){
  5. return eval(text);
  6. };
  7. if (!this.document) document = {
  8. "window": self,
  9. "head": "head",
  10. "location": "url",
  11. "documentElement": {
  12. style:{
  13. "color": ""
  14. }
  15. },
  16. "html": {
  17. style:{
  18. "color": ""
  19. }
  20. },
  21. "readyState": "loaded",
  22. "addEventListener": function(){},
  23. "removeEventListener": function(){},
  24. "createElement": function(){
  25. return {
  26. contains: null,
  27. getAttribute:function(){return null},
  28. cloneNode: function(){return {
  29. firstChild: {
  30. childNodes: {length: 1},
  31. value: "s"
  32. }
  33. }},
  34. firstChild: {
  35. childNodes: {length: 1},
  36. value: "s"
  37. },
  38. getAttributeNode: function(){return null},
  39. childNodes: [""],
  40. style: {},
  41. appendChild: function(){}
  42. }
  43. }
  44. };
  45. self.importScripts("../o2_core/o2.min.js");
  46. layout = window.layout || {};
  47. layout.desktop = layout;
  48. layout.desktop.type = "app";
  49. var locate = window.location;
  50. layout.protocol = locate.protocol;
  51. layout.inBrowser = true;
  52. layout.session = layout.session || {};
  53. layout.debugger = (locate.href.toString().indexOf("debugger") !== -1);
  54. layout.anonymous = (locate.href.toString().indexOf("anonymous") !== -1);
  55. o2.xApplication = o2.xApplication || {};
  56. o2.xDesktop = o2.xDesktop || {};
  57. o2.xDesktop.requireApp = function (module, clazz, callback, async) {
  58. o2.requireApp(module, clazz, callback, async);
  59. };
  60. layout.openApplication = function(){};
  61. layout.refreshApp = function(){};
  62. layout.load = function(){};
  63. layout.readys = [];
  64. layout.addReady = function () {
  65. for (var i = 0; i < arguments.length; i++) {
  66. if (o2.typeOf(arguments[i]) === "function") layout.readys.push(arguments[i]);
  67. }
  68. };
  69. o2.addReady(function () {
  70. var _setLayoutService = function(service, center){
  71. layout.serviceAddressList = service;
  72. layout.centerServer = center;
  73. layout.desktop.serviceAddressList = service;
  74. layout.desktop.centerServer = center;
  75. };
  76. var _getDistribute = function (callback) {
  77. if (layout.config.app_protocol === "auto") {
  78. layout.config.app_protocol = window.location.protocol;
  79. }
  80. if (layout.config.configMapping && (layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname])){
  81. var mapping = layout.config.configMapping[window.location.host] || layout.config.configMapping[window.location.hostname];
  82. if (mapping.servers){
  83. layout.serviceAddressList = mapping.servers;
  84. layout.desktop.serviceAddressList = mapping.servers;
  85. if (mapping.center) center = (o2.typeOf(mapping.center)==="array") ? mapping.center[0] : mapping.center;
  86. layout.centerServer = center;
  87. layout.desktop.centerServer = center;
  88. if (callback) callback();
  89. }else{
  90. if (mapping.center) layout.config.center = (o2.typeOf(mapping.center)==="array") ? mapping.center : [mapping.center];
  91. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  92. _setLayoutService(service, center);
  93. if (callback) callback();
  94. }.bind(this));
  95. }
  96. }else{
  97. o2.xDesktop.getServiceAddress(layout.config, function (service, center) {
  98. _setLayoutService(service, center);
  99. if (callback) callback();
  100. }.bind(this));
  101. }
  102. };
  103. var _load = function () {
  104. var _loadApp = function (data) {
  105. //用户已经登录
  106. if (data){
  107. layout.user = data;
  108. layout.session = layout.session || {};
  109. layout.session.user = data;
  110. layout.session.token = data.token;
  111. layout.desktop.session = layout.session;
  112. }
  113. while (layout.readys && layout.readys.length) {
  114. layout.readys.shift().apply(window);
  115. }
  116. };
  117. var data = { name: "anonymous", roleList: [] };
  118. o2.Actions.get("x_organization_assemble_authentication").getAuthentication(function (json) {
  119. data = json.data;
  120. }.bind(this), null, false);
  121. _loadApp(data);
  122. };
  123. //异步载入必要模块
  124. layout.config = null;
  125. var configLoaded = false;
  126. var lpLoaded = false;
  127. var commonLoaded = false;
  128. var lp = o2.session.path + "/lp/" + o2.language + ".js";
  129. var loadModuls = function () {
  130. lpLoaded = true;
  131. var modules = ["o2.xDesktop.$all"];
  132. o2.require(modules, {
  133. "onSuccess": function () {
  134. commonLoaded = true;
  135. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  136. }
  137. });
  138. }
  139. if (!o2.LP){
  140. o2.load(lp, loadModuls);
  141. }else{
  142. loadModuls();
  143. }
  144. o2.getJSON("../x_desktop/res/config/config.json", function (config) {
  145. layout.config = config;
  146. configLoaded = true;
  147. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  148. });
  149. });
  150. var _worker = self;
  151. layout.addReady(function(){
  152. window.isCompletionEnvironment = true;
  153. _worker.runtimeEnvironment = {};
  154. _worker.createEnvironment = function(runtime, url){
  155. return new Promise(function(s, f){
  156. o2.xhr_get(url, function (xhr) {
  157. if (xhr.responseText){
  158. _worker.createRuntime(runtime, xhr.responseText);
  159. if (s) s();
  160. }
  161. }, function (err) {
  162. f(err);
  163. });
  164. });
  165. };
  166. _worker.createRuntime = function(runtime, script){
  167. var code = "o2.Macro.swapSpace.tmpMacroCompletionFunction = function (){\n" + script + "\nreturn bind;" + "\n};";
  168. Browser.exec(code);
  169. var ev = o2.Macro.swapSpace.tmpMacroCompletionFunction() ;
  170. _worker.runtimeEnvironment[runtime] = {
  171. "environment": ev,
  172. exec: function(code){
  173. try{
  174. return o2.Macro.exec(code, this.environment);
  175. }catch(e){
  176. return null;
  177. }
  178. }
  179. }
  180. };
  181. _worker.getCompletionEnvironment = function(runtime) {
  182. if (!_worker.runtimeEnvironment[runtime]) {
  183. return new Promise(function(s){
  184. o2.require("o2.xScript.Macro", function() {
  185. switch (runtime) {
  186. case "service":
  187. s(_worker.getServiceCompletionEnvironment());
  188. break;
  189. case "server":
  190. s(_worker.getServerCompletionEnvironment());
  191. break;
  192. case "all":
  193. s(_worker.getAllCompletionEnvironment());
  194. break;
  195. default:
  196. s(_worker.getDefaultCompletionEnvironment());
  197. }
  198. });
  199. });
  200. } else {
  201. return Promise.resolve();
  202. }
  203. };
  204. _worker.getServiceCompletionEnvironment = function() {
  205. var runtime = "service";
  206. return _worker.createEnvironment(runtime, "../x_desktop/js/initalServiceScriptSubstitute.js");
  207. };
  208. _worker.getServerCompletionEnvironment = function() {
  209. var runtime = "server";
  210. return _worker.createEnvironment(runtime, "../x_desktop/js/initalScriptSubstitute.js");
  211. };
  212. _worker.getDefaultCompletionEnvironment = function(){
  213. var runtime = "web";
  214. return new Promise(function(s){
  215. o2.getJSON("../o2_core/o2/widget/$JavascriptEditor/environment.json", function (data) {
  216. json = data;
  217. _worker.runtimeEnvironment[runtime] = new o2.Macro.FormContext(json);
  218. if (s) s();
  219. });
  220. });
  221. }
  222. _worker.getAllCompletionEnvironment = function(){
  223. var runtime = "all";
  224. var arr = [
  225. _worker.getServiceCompletionEnvironment(),
  226. _worker.getServerCompletionEnvironment(),
  227. _worker.getDefaultCompletionEnvironment()
  228. ];
  229. return Promise.all(arr).then(function(){
  230. if (_worker.runtimeEnvironment["service"] && _worker.runtimeEnvironment["server"] && _worker.runtimeEnvironment["web"] ){
  231. var ev = Object.merge(_worker.runtimeEnvironment["service"].environment,
  232. _worker.runtimeEnvironment["server"].environment,
  233. _worker.runtimeEnvironment["web"].environment)
  234. _worker.runtimeEnvironment[runtime] = {
  235. "environment": ev,
  236. exec: function(code){
  237. try{
  238. return o2.Macro.exec(code, this.environment);
  239. }catch(e){
  240. return null;
  241. }
  242. }
  243. }
  244. }
  245. });
  246. };
  247. _worker.getMonacoCompletions = function(o, range, code){
  248. var arr = [];
  249. o = (o2.typeOf(o)=="array") ? o[0] : o;
  250. Object.keys(o).each(function (key) {
  251. if (key!="__type__"){
  252. var keyType = typeOf(o[key]);
  253. var oRange = Object.clone(range);
  254. switch (keyType){
  255. case "function":
  256. var count = o[key].length;
  257. var v = key + "(";
  258. for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
  259. v += ")";
  260. key = key+"()";
  261. arr.push({ label: key, kind: 1, insertText: v, detail: keyType, range: oRange });
  262. break;
  263. default:
  264. var insertText = key;
  265. var filterText = key;
  266. var kind = 3;
  267. if (o[key]){
  268. var text = (keyType=="array" && o[key][0]["__type__"]) ? o[key][0]["__type__"] : o[key].toString();
  269. var flagCount = text.indexOf(":");
  270. if (flagCount!=-1){
  271. keyType = text.substr(0,flagCount);
  272. text = text.substr(flagCount+1);
  273. }
  274. var oType = (o["__type__"]) ? o["__type__"].substr(0, o["__type__"].indexOf(":")) : o2.typeOf(o);
  275. if (oType=="object array"){
  276. if (code.substr(-1)!=="]"){
  277. insertText = "[0]."+key;
  278. oRange.startColumn = oRange.startColumn-1;
  279. }
  280. filterText="."+filterText;
  281. }
  282. }
  283. switch (keyType){
  284. case "array":
  285. kind = 15;
  286. key = key+"[]";
  287. break;
  288. case "object array":
  289. kind = 15;
  290. key = key+"[]";
  291. break;
  292. }
  293. arr.push({ label: key, filterText: filterText, key, kind: kind, insertText: insertText, detail: keyType, documentation: text, range: oRange});
  294. }
  295. }
  296. });
  297. return arr;
  298. };
  299. _worker.getAceCompletions = function(o, range, code){
  300. var arr = [];
  301. o = (o2.typeOf(o)=="array") ? o[0] : o;
  302. Object.keys(o).each(function(key){
  303. if (key!="__type__"){
  304. var keyType = typeOf(o[key]);
  305. var oRange = (range) ? Object.clone(range): null;
  306. var offset = 0;
  307. switch (keyType){
  308. case "function":
  309. var count = o[key].length;
  310. var v = key + "(";
  311. for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
  312. v += ")";
  313. key = key+"()";
  314. arr.push({ caption: key, value: v, score: 3, meta: keyType, type: keyType, docText: key });
  315. break;
  316. default:
  317. var insertText = key;
  318. var filterText = key;
  319. var kind = 3;
  320. if (o[key]){
  321. var text = (keyType=="array" && o[key][0]["__type__"]) ? o[key][0]["__type__"] : o[key].toString();
  322. var flagCount = text.indexOf(":");
  323. if (flagCount!=-1){
  324. keyType = text.substr(0,flagCount);
  325. text = text.substr(flagCount+1);
  326. }
  327. var oType = (o["__type__"]) ? o["__type__"].substr(0, o["__type__"].indexOf(":")) : o2.typeOf(o);
  328. if (oType=="object array"){
  329. if (code.substr(-1)!=="]"){
  330. insertText = "[0]."+key;
  331. offset = -1;
  332. }
  333. filterText="."+filterText;
  334. }
  335. }
  336. if (keyType=="array" || keyType=="object array") key = key+"[]";
  337. arr.push({ caption: key, value: insertText, score: 3, meta: keyType, type: keyType, docText: text, offset: offset });
  338. }
  339. }
  340. });
  341. return arr;
  342. };
  343. _worker.overwriteRequest = function(){
  344. if (!_worker.isOverwriteRequest){
  345. if (_worker.XMLHttpRequest){
  346. var send = XMLHttpRequest.prototype.send;
  347. var open = XMLHttpRequest.prototype.open;
  348. XMLHttpRequest.prototype.open = function(method, url){
  349. this.continueRequest = false;
  350. if (url){
  351. var tmpUrl = url;
  352. if (tmpUrl.indexOf(":")==-1) tmpUrl = "http://"+tmpUrl;
  353. var u = new URI(tmpUrl);
  354. var f = u.get("file");
  355. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  356. if (ext=="json" || ext=="js" || ext=="html" ){
  357. this.continueRequest = true;
  358. return open.apply(this, arguments);
  359. }
  360. }
  361. };
  362. XMLHttpRequest.prototype.send = function(){
  363. if (this.continueRequest){
  364. return send.apply(this, arguments);
  365. }
  366. };
  367. }
  368. if (_worker.fetch){
  369. var nativeFetch = _worker.fetch;
  370. _worker.fetch = function(o){
  371. var url = o;
  372. if (o2.typeOf(o)=="string"){
  373. url = o;
  374. }else if (Object.getPrototypeOf(o)==NativeRequest.prototype){
  375. url = o.url;
  376. }
  377. if (url.indexOf(":")==-1) url = "http://"+url;
  378. var u = new URI(url);
  379. var f = u.get("file");
  380. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  381. if (ext=="json" || ext=="js" || ext=="html" ){
  382. return nativeFetch.apply(_worker, arguments);
  383. }
  384. }
  385. }
  386. _worker.isOverwriteRequest = true;
  387. }
  388. };
  389. _worker.exec = function(id, uuid, code, preCode, range, runtime, type){
  390. var codeText = code;
  391. var promise = _worker.getCompletionEnvironment(runtime);
  392. promise.then(function(){
  393. _worker.overwriteRequest();
  394. if (_worker.runtimeEnvironment[runtime]){
  395. code = "return "+code+";";
  396. if (preCode){
  397. code = "try{\n"+preCode+"\n}catch(e){}\n"+"try{\n"+code+"\n}catch(e){return null;}";
  398. }
  399. //code = "try {\n"+code+"\n}catch(e){return null;}";
  400. var o = _worker.runtimeEnvironment[runtime].exec(code);
  401. if (o) {
  402. var completions = (type=="ace") ? _worker.getAceCompletions(o, range, codeText) : _worker.getMonacoCompletions(o ,range, codeText);
  403. _worker.postMessage({"o": completions, "id": id, "uuid": uuid});
  404. }
  405. }
  406. }, function(){});
  407. };
  408. _worker.postMessage({"type": "ready"});
  409. });
  410. onmessage = function(e) {
  411. if (e.data){
  412. var id = e.data.id;
  413. var uuid = e.data.uuid;
  414. var code = e.data.code;
  415. var runtime = e.data.runtime;
  416. var type = e.data.type;
  417. var preCode = e.data.preCode;
  418. var range = e.data.range;
  419. if (id && code && runtime && uuid && type){
  420. _worker.exec(id, uuid, code, preCode, range, runtime, type)
  421. }
  422. }
  423. }