scriptWorker.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. if (config.proxyCenterEnable){
  146. if (o2.typeOf(config.center)==="array"){
  147. config.center.forEach(function(c){
  148. c.port = window.location.port;
  149. });
  150. }else{
  151. config.port = window.location.port;
  152. }
  153. }
  154. layout.config = config;
  155. o2.tokenName = config.tokenName || "x-token";
  156. configLoaded = true;
  157. if (configLoaded && commonLoaded && lpLoaded) _getDistribute(function () { _load(); });
  158. });
  159. });
  160. var _worker = self;
  161. layout.addReady(function(){
  162. window.isCompletionEnvironment = true;
  163. _worker.runtimeEnvironment = {};
  164. _worker.createEnvironment = function(runtime, url){
  165. debugger;
  166. if (!_worker.runtimeEnvironment[runtime]){
  167. return new Promise(function(s, f){
  168. o2.xhr_get(url, function (xhr) {
  169. if (xhr.responseText){
  170. var r =_worker.createRuntime(runtime, xhr.responseText);
  171. if (s) s(r);
  172. }
  173. }, function (err) {
  174. f(err);
  175. });
  176. });
  177. }else{
  178. return Promise.resolve(_worker.runtimeEnvironment[runtime]);
  179. }
  180. };
  181. _worker.createRuntime = function(runtime, script){
  182. var code = "o2.Macro.swapSpace.tmpMacroCompletionFunction = function (){\n" + script + "\nreturn bind;" + "\n};";
  183. Browser.exec(code);
  184. var ev = o2.Macro.swapSpace.tmpMacroCompletionFunction() ;
  185. _worker.runtimeEnvironment[runtime] = {
  186. "environment": ev,
  187. exec: function(code){
  188. try{
  189. return o2.Macro.exec(code, this.environment);
  190. }catch(e){
  191. return null;
  192. }
  193. }
  194. }
  195. return _worker.runtimeEnvironment[runtime];
  196. };
  197. _worker.getCompletionEnvironment = function(runtime) {
  198. if (!_worker.runtimeEnvironment[runtime]) {
  199. return new Promise(function(s){
  200. switch (runtime) {
  201. case "service":
  202. //s(_worker.getServiceCompletionEnvironment());
  203. s(_worker.getEnvironment(runtime, ["_service", "web"]));
  204. break;
  205. case "server":
  206. //s(_worker.getServerCompletionEnvironment());
  207. s(_worker.getEnvironment(runtime, ["_server", "web"]));
  208. break;
  209. case "all":
  210. //s(_worker.getAllCompletionEnvironment());
  211. s(_worker.getEnvironment(runtime, ["_service", "_server", "web"]));
  212. break;
  213. default:
  214. s(_worker.getDefaultCompletionEnvironment());
  215. }
  216. });
  217. } else {
  218. return Promise.resolve(_worker.runtimeEnvironment[runtime]);
  219. }
  220. };
  221. _worker.getEnvironment = function(runtime, rArr) {
  222. var ev = {}
  223. rArr.forEach(function(r){
  224. ev = Object.assign(ev, _worker.runtimeEnvironment[r].environment);
  225. });
  226. _worker.runtimeEnvironment[runtime] = {
  227. "environment": ev,
  228. exec: function(code){
  229. try{
  230. return o2.Macro.exec(code, this.environment);
  231. }catch(e){
  232. return null;
  233. }
  234. }
  235. }
  236. return Promise.resolve(_worker.runtimeEnvironment[runtime]);
  237. // var arr = pArr;
  238. // return Promise.all(arr).then(function(evs){
  239. // var ev = {}
  240. // evs.forEach(function(e){
  241. // ev = Object.assign(ev, e.environment);
  242. // });
  243. // _worker.runtimeEnvironment[runtime] = {
  244. // "environment": ev,
  245. // exec: function(code){
  246. // try{
  247. // return o2.Macro.exec(code, this.environment);
  248. // }catch(e){
  249. // return null;
  250. // }
  251. // }
  252. // }
  253. // });
  254. };
  255. _worker.getServiceCompletionEnvironment = function() {
  256. var runtime = "_service";
  257. return _worker.createEnvironment(runtime, "../x_desktop/js/initialServiceScriptText.min.js");
  258. };
  259. _worker.getServerCompletionEnvironment = function() {
  260. var runtime = "_server";
  261. return _worker.createEnvironment(runtime, "../x_desktop/js/initialScriptText.min.js");
  262. };
  263. _worker.getDefaultCompletionEnvironment = function(){
  264. var runtime = "web";
  265. return new Promise(function(s){
  266. o2.getJSON("../o2_core/o2/widget/$JavascriptEditor/environment.json", function (data) {
  267. json = data;
  268. _worker.runtimeEnvironment[runtime] = new o2.Macro.FormContext(json);
  269. if (s) s(_worker.runtimeEnvironment[runtime]);
  270. });
  271. });
  272. }
  273. _worker.getAllCompletionEnvironment = function(){
  274. var runtime = "all";
  275. var arr = [
  276. _worker.getServiceCompletionEnvironment(),
  277. _worker.getServerCompletionEnvironment(),
  278. _worker.getDefaultCompletionEnvironment()
  279. ];
  280. return Promise.all(arr).then(function(){
  281. if (_worker.runtimeEnvironment["service"] && _worker.runtimeEnvironment["server"] && _worker.runtimeEnvironment["web"] ){
  282. var ev = Object.merge(_worker.runtimeEnvironment["service"].environment,
  283. _worker.runtimeEnvironment["server"].environment,
  284. _worker.runtimeEnvironment["web"].environment)
  285. _worker.runtimeEnvironment[runtime] = {
  286. "environment": ev,
  287. exec: function(code){
  288. try{
  289. return o2.Macro.exec(code, this.environment);
  290. }catch(e){
  291. return null;
  292. }
  293. }
  294. }
  295. }
  296. });
  297. };
  298. _worker.getMonacoCompletions = function(o, range, code){
  299. var arr = [];
  300. o = (o2.typeOf(o)=="array") ? o[0] : o;
  301. Object.keys(o).each(function (key) {
  302. if (key!="__type__"){
  303. var keyType = typeOf(o[key]);
  304. // var oRange = Object.clone(range);
  305. var oRange = {
  306. endColumn: range.endColumn,
  307. endLineNumber: range.endLineNumber,
  308. startColumn: range.startColumn,
  309. startLineNumber: range.startLineNumber
  310. }
  311. switch (keyType){
  312. case "function":
  313. var count = o[key].length;
  314. var v = key + "(";
  315. for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
  316. v += ")";
  317. key = key+"()";
  318. arr.push({ label: key, kind: 1, insertText: v, detail: keyType, range: oRange });
  319. break;
  320. default:
  321. var insertText = key;
  322. var filterText = key;
  323. var kind = 3;
  324. if (o[key]){
  325. var text = (keyType=="array" && o[key][0] && o[key][0]["__type__"]) ? o[key][0]["__type__"] : o[key].toString();
  326. var flagCount = text.indexOf(":");
  327. if (flagCount!=-1){
  328. keyType = text.substr(0,flagCount);
  329. text = text.substr(flagCount+1);
  330. }
  331. var oType = (o["__type__"]) ? o["__type__"].substr(0, o["__type__"].indexOf(":")) : o2.typeOf(o);
  332. if (oType=="object array"){
  333. if (code.substr(-1)!=="]"){
  334. insertText = "[0]."+key;
  335. oRange.startColumn = oRange.startColumn-1;
  336. }
  337. filterText="."+filterText;
  338. }
  339. }
  340. switch (keyType){
  341. case "array":
  342. kind = 15;
  343. key = key+"[]";
  344. break;
  345. case "object array":
  346. kind = 15;
  347. key = key+"[]";
  348. break;
  349. }
  350. arr.push({ label: key, filterText: filterText, key: key, kind: kind, insertText: insertText, detail: keyType, documentation: text, range: oRange});
  351. }
  352. }
  353. });
  354. return arr;
  355. };
  356. _worker.getAceCompletions = function(o, range, code){
  357. var arr = [];
  358. o = (o2.typeOf(o)=="array") ? o[0] : o;
  359. Object.keys(o).each(function(key){
  360. if (key!="__type__"){
  361. var keyType = typeOf(o[key]);
  362. var oRange = (range) ? (function(){
  363. var o = {};
  364. Object.keys(range).each(function(k){
  365. o[k] = range[k];
  366. });
  367. return o;
  368. })() : null;
  369. var offset = 0;
  370. switch (keyType){
  371. case "function":
  372. var count = o[key].length;
  373. var v = key + "(";
  374. for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
  375. v += ")";
  376. key = key+"()";
  377. arr.push({ caption: key, value: v, score: 3, meta: keyType, type: keyType, docText: key });
  378. break;
  379. default:
  380. var insertText = key;
  381. var filterText = key;
  382. var kind = 3;
  383. if (o[key]){
  384. var text = (keyType=="array" && o[key][0]["__type__"]) ? o[key][0]["__type__"] : o[key].toString();
  385. var flagCount = text.indexOf(":");
  386. if (flagCount!=-1){
  387. keyType = text.substr(0,flagCount);
  388. text = text.substr(flagCount+1);
  389. }
  390. var oType = (o["__type__"]) ? o["__type__"].substr(0, o["__type__"].indexOf(":")) : o2.typeOf(o);
  391. if (oType=="object array"){
  392. if (code.substr(-1)!=="]"){
  393. insertText = "[0]."+key;
  394. offset = -1;
  395. }
  396. filterText="."+filterText;
  397. }
  398. }
  399. if (keyType=="array" || keyType=="object array") key = key+"[]";
  400. arr.push({ caption: key, value: insertText, score: 3, meta: keyType, type: keyType, docText: text, offset: offset });
  401. }
  402. }
  403. });
  404. return arr;
  405. };
  406. _worker.overwriteRequest = function(){
  407. if (!_worker.isOverwriteRequest){
  408. if (_worker.XMLHttpRequest){
  409. var send = XMLHttpRequest.prototype.send;
  410. var open = XMLHttpRequest.prototype.open;
  411. XMLHttpRequest.prototype.open = function(method, url){
  412. this.continueRequest = false;
  413. if (url){
  414. var tmpUrl = url;
  415. if (tmpUrl.indexOf(":")==-1) tmpUrl = "http://"+tmpUrl;
  416. var u = new URI(tmpUrl);
  417. var f = u.get("file");
  418. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  419. if (ext=="json" || ext=="js" || ext=="html" ){
  420. this.continueRequest = true;
  421. return open.apply(this, arguments);
  422. }
  423. }
  424. };
  425. XMLHttpRequest.prototype.send = function(){
  426. if (this.continueRequest){
  427. return send.apply(this, arguments);
  428. }
  429. };
  430. }
  431. if (_worker.fetch){
  432. var nativeFetch = _worker.fetch;
  433. _worker.fetch = function(o){
  434. var url = o;
  435. if (o2.typeOf(o)=="string"){
  436. url = o;
  437. }else if (Object.getPrototypeOf(o)==NativeRequest.prototype){
  438. url = o.url;
  439. }
  440. if (url.indexOf(":")==-1) url = "http://"+url;
  441. var u = new URI(url);
  442. var f = u.get("file");
  443. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  444. if (ext=="json" || ext=="js" || ext=="html" ){
  445. return nativeFetch.apply(_worker, arguments);
  446. }
  447. }
  448. }
  449. _worker.isOverwriteRequest = true;
  450. }
  451. };
  452. _worker.exec = function(id, uuid, code, preCode, range, runtime, type){
  453. var codeText = code;
  454. var promise = _worker.getCompletionEnvironment(runtime);
  455. promise.then(function(){
  456. _worker.overwriteRequest();
  457. if (_worker.runtimeEnvironment[runtime]){
  458. code = "return "+code+";";
  459. if (preCode){
  460. code = "try{\n"+preCode+"\n}catch(e){}\n"+"try{\n"+code+"\n}catch(e){return null;}";
  461. }
  462. //code = "try {\n"+code+"\n}catch(e){return null;}";
  463. var o = _worker.runtimeEnvironment[runtime].exec(code);
  464. if (o) {
  465. var completions = (type=="ace") ? _worker.getAceCompletions(o, range, codeText) : _worker.getMonacoCompletions(o ,range, codeText);
  466. _worker.postMessage({"o": completions, "id": id, "uuid": uuid});
  467. }
  468. }
  469. }, function(){});
  470. };
  471. o2.require("o2.xScript.Macro", function() {
  472. var arr = [
  473. _worker.getServiceCompletionEnvironment(),
  474. _worker.getServerCompletionEnvironment(),
  475. _worker.getDefaultCompletionEnvironment()
  476. ];
  477. Promise.all(arr).then(function(){
  478. _worker.postMessage({"type": "ready"});
  479. });
  480. });
  481. });
  482. onmessage = function(e) {
  483. if (e.data){
  484. var id = e.data.id;
  485. var uuid = e.data.uuid;
  486. var code = e.data.code;
  487. var runtime = e.data.runtime;
  488. var type = e.data.type;
  489. var preCode = e.data.preCode;
  490. var range = e.data.range;
  491. if (id && code && runtime && uuid && type){
  492. _worker.exec(id, uuid, code, preCode, range, runtime, type)
  493. }
  494. }
  495. }