scriptWorker.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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){
  248. var arr = [];
  249. Object.keys(o).each(function (key) {
  250. var keyType = typeOf(o[key]);
  251. if (keyType === "function") {
  252. var count = o[key].length;
  253. var v = key + "(";
  254. for (var i = 1; i <= count; i++) v += (i == count) ? "par" + i : "par" + i + ", ";
  255. v += ")";
  256. arr.push({ label: key, kind: 1, insertText: v, detail: keyType });
  257. } else {
  258. arr.push({ label: key, kind: 7, insertText: key, detail: keyType });
  259. }
  260. });
  261. return arr;
  262. };
  263. _worker.getAceCompletions = function(o){
  264. var arr = [];
  265. Object.keys(o).each(function(key){
  266. var keyType = typeOf(o[key]);
  267. if (keyType==="function") {
  268. var count = o[key].length;
  269. var v = x+"."+key+"(";
  270. for (var i=1; i<=count; i++) v+= (i==count) ? "par"+i : "par"+i+", ";
  271. v+=")";
  272. arr.push({ caption: x+"."+key, value: v, score: 3, meta: keyType, type: keyType, docText: o[key] });
  273. }else{
  274. arr.push({ caption: x+"."+key, value: x+"."+key, score: 3, meta: keyType, type: keyType, docText: o[key] });
  275. }
  276. });
  277. return arr;
  278. };
  279. _worker.overwriteRequest = function(){
  280. if (!_worker.isOverwriteRequest){
  281. if (_worker.XMLHttpRequest){
  282. var send = XMLHttpRequest.prototype.send;
  283. var open = XMLHttpRequest.prototype.open;
  284. XMLHttpRequest.prototype.open = function(method, url){
  285. this.continueRequest = false;
  286. if (url){
  287. var tmpUrl = url;
  288. if (tmpUrl.indexOf(":")==-1) tmpUrl = "http://"+tmpUrl;
  289. var u = new URI(tmpUrl);
  290. var f = u.get("file");
  291. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  292. if (ext=="json" || ext=="js" || ext=="html" ){
  293. this.continueRequest = true;
  294. return open.apply(this, arguments);
  295. }
  296. }
  297. };
  298. XMLHttpRequest.prototype.send = function(){
  299. if (this.continueRequest){
  300. return send.apply(this, arguments);
  301. }
  302. };
  303. }
  304. if (_worker.fetch){
  305. var nativeFetch = _worker.fetch;
  306. _worker.fetch = function(o){
  307. var url = o;
  308. if (o2.typeOf(o)=="string"){
  309. url = o;
  310. }else if (Object.getPrototypeOf(o)==NativeRequest.prototype){
  311. url = o.url;
  312. }
  313. if (url.indexOf(":")==-1) url = "http://"+url;
  314. var u = new URI(url);
  315. var f = u.get("file");
  316. var ext = f.substr(f.indexOf(".")+1).toLowerCase();
  317. if (ext=="json" || ext=="js" || ext=="html" ){
  318. return nativeFetch.apply(_worker, arguments);
  319. }
  320. }
  321. }
  322. _worker.isOverwriteRequest = true;
  323. }
  324. };
  325. _worker.exec = function(id, uuid, code, preCode, runtime, type){
  326. var promise = _worker.getCompletionEnvironment(runtime);
  327. promise.then(function(){
  328. _worker.overwriteRequest();
  329. if (_worker.runtimeEnvironment[runtime]){
  330. code = "return "+code+";";
  331. if (preCode){
  332. code = preCode+"\n"+code;
  333. }
  334. code = "try {\n"+code+"\n}catch(e){return null;}";
  335. var o = _worker.runtimeEnvironment[runtime].exec(code);
  336. if (o) {
  337. var completions = (type=="ace") ? _worker.getAceCompletions(o) : _worker.getMonacoCompletions(o);
  338. _worker.postMessage({"o": completions, "id": id, "uuid": uuid});
  339. }
  340. }
  341. }, function(){});
  342. };
  343. _worker.postMessage({"type": "ready"});
  344. });
  345. onmessage = function(e) {
  346. if (e.data){
  347. var id = e.data.id;
  348. var uuid = e.data.uuid;
  349. var code = e.data.code;
  350. var runtime = e.data.runtime;
  351. var type = e.data.type;
  352. var preCode = e.data.preCode;
  353. if (id && code && runtime && uuid && type){
  354. _worker.exec(id, uuid, code, preCode, runtime, type)
  355. }
  356. }
  357. }