PatternWorker.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. var _worker = self;
  2. var _action = {
  3. _checkRequest: function(s,f){
  4. if (this.request.readyState === XMLHttpRequest.DONE) {
  5. if (this.request.status === 200) {
  6. this._doneRequest(s);
  7. } else {
  8. this._errorRequest(f);
  9. }
  10. }
  11. },
  12. _createRequest: function(s, f){
  13. var request = new XMLHttpRequest();
  14. request.addEventListener("readystatechange",function(){
  15. if (this.readyState === XMLHttpRequest.DONE) {
  16. if (this.status === 200) {
  17. var json = JSON.parse(this.responseText);
  18. var xToken = this.getResponseHeader(_worker.findData.tokenName);
  19. if (xToken){
  20. json.xToken = xToken;
  21. }
  22. if (s) s(json);
  23. } else {
  24. if (f) f(this, this.responseText);
  25. }
  26. }
  27. });
  28. return request
  29. },
  30. sendRequest: function(data){
  31. return new Promise(function(s, f){
  32. var request = this._createRequest(s, f);
  33. var method = data.method;
  34. var noCache = !!data.noCache;
  35. var async = !!data.loadAsync;
  36. var withCredentials = !!data.credentials;
  37. var url = data.address;
  38. var body = data.body;
  39. var debug = data.debug;
  40. var token = data.token;
  41. if (noCache) url = url+(((url.indexOf("?")!==-1) ? "&" : "?")+(new Date()).getTime());
  42. request.open(method, url, async);
  43. request.withCredentials = withCredentials;
  44. request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  45. request.setRequestHeader("Accept", "text/html,application/json,*/*");
  46. if (debug) request.setRequestHeader("x-debugger", "true");
  47. if (token){
  48. request.setRequestHeader(_worker.findData.tokenName, token);
  49. request.setRequestHeader("authorization", token);
  50. }
  51. request.send(body);
  52. }.bind(this));
  53. },
  54. _doneRequest: function(s){
  55. var json = JSON.parse(this.request.responseText);
  56. var xToken = this.request.getResponseHeader(_worker.findData.tokenName);
  57. if (xToken){
  58. json.xToken = xToken;
  59. }
  60. if (s) s(json);
  61. // _worker.postMessage({"type": "done", "data": json});
  62. // _worker.close();
  63. },
  64. _errorRequest: function(f){
  65. if (s) s(this.request, this.request.responseText);
  66. // _worker.postMessage({"type":"error", "data": {"status": this.request.status, "statusText": this.request.statusText, "responseText":this.request.responseText}});
  67. // _worker.close();
  68. }
  69. };
  70. _worker.action = _action;
  71. _worker._receiveMessageReply = function(){
  72. _worker.postMessage({
  73. "type": "receive"
  74. });
  75. };
  76. _worker._readyMessageReply = function(){
  77. _worker.postMessage({
  78. "type": "ready",
  79. "count": this.filterOptionList.length
  80. });
  81. };
  82. _worker._getRequestOption = function(data, par){
  83. if (par){
  84. Object.keys(par).forEach(function(k){
  85. data.url = data.url.replace("{"+k+"}", par[k]);
  86. });
  87. }
  88. return {
  89. "method": data.method||"get",
  90. "noCache": false,
  91. "loadAsync": true,
  92. "credentials": true,
  93. "address": data.url,
  94. "body": data.body || "",
  95. "debug": data.debug || _worker.findData.debug,
  96. "token": data.token || _worker.findData.token
  97. };
  98. };
  99. _worker._createFilterOption = function(moduleType, appId, desingerType, desingerId){
  100. var filterOption = JSON.parse(_worker.filterOptionTemplete);
  101. filterOption.moduleList.push({
  102. "moduleType": moduleType,
  103. "moduleAppList": [{"appId": appId, "designerList": [{"desingerType": desingerType, "designerIdList": [desingerId]}]}]
  104. });
  105. this.filterOptionList.push(filterOption);
  106. return filterOption;
  107. };
  108. _worker._getDesingerModule = function(id, restful, par, moduleType, desingerType){
  109. var p = _worker.action.sendRequest(_worker._getRequestOption({"url": restful, "debug": _worker.findData.debug, "token": _worker.findData.token }, par));
  110. return p.then(function(json){
  111. list = json.data;
  112. list.forEach(function(designer){
  113. _worker._createFilterOption(moduleType, id, desingerType, designer.id)
  114. });
  115. }, function(){});
  116. };
  117. _worker._getDesinger_processPlatform = function(id){
  118. var promiseArr = [];
  119. if (_worker.findData.filterOption.designerTypes.indexOf("script")!=-1){ //所有脚本
  120. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listProcessScript, {"applicationId": id}, "processPlatform", "script"));
  121. }
  122. if (_worker.findData.filterOption.designerTypes.indexOf("form")!=-1){ //所有表单
  123. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listProcessForm, {"applicationId": id}, "processPlatform", "form"));
  124. }
  125. if (_worker.findData.filterOption.designerTypes.indexOf("process")!=-1){ //所有流程
  126. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listProcessProcess, {"applicationId": id}, "processPlatform", "process"));
  127. }
  128. return promiseArr;
  129. };
  130. _worker._getDesinger_cms = function(id){
  131. var promiseArr = [];
  132. if (_worker.findData.filterOption.designerTypes.indexOf("script")!=-1){ //所有脚本
  133. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listCmsScript, {"flag": id}, "cms", "script"));
  134. }
  135. if (_worker.findData.filterOption.designerTypes.indexOf("form")!=-1){ //所有表单
  136. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listCmsForm, {"appId": id}, "cms", "form"));
  137. }
  138. return promiseArr;
  139. };
  140. _worker._getDesinger_portal = function(id){
  141. var promiseArr = [];
  142. if (_worker.findData.filterOption.designerTypes.indexOf("script")!=-1){ //所有脚本
  143. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listPortalScript, {"portalId": id}, "portal", "script"));
  144. }
  145. if (_worker.findData.filterOption.designerTypes.indexOf("page")!=-1){ //所有表单
  146. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listPortalPage, {"portalId": id}, "portal", "page"));
  147. }
  148. if (_worker.findData.filterOption.designerTypes.indexOf("widget")!=-1){ //所有流程
  149. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listPortalWidget, {"portalId": id}, "portal", "widget"));
  150. }
  151. return promiseArr;
  152. };
  153. _worker._getDesinger_query = function(id){
  154. var promiseArr = [];
  155. if (_worker.findData.filterOption.designerTypes.indexOf("view")!=-1){ //所有脚本
  156. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listQueryView, {"flag": id}, "query", "view"));
  157. }
  158. if (_worker.findData.filterOption.designerTypes.indexOf("stat")!=-1){ //所有表单
  159. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listQueryStat, {"flag": id}, "query", "stat"));
  160. }
  161. if (_worker.findData.filterOption.designerTypes.indexOf("statement")!=-1){ //所有流程
  162. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listQueryStatement, {"flag": id}, "query", "statement"));
  163. }
  164. return promiseArr;
  165. };
  166. _worker._getDesinger_service = function(id){
  167. var promiseArr = [];
  168. if (_worker.findData.filterOption.designerTypes.indexOf("script")!=-1){ //所有脚本
  169. if (id=="invoke"){
  170. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listInvoke, null, "service", "script"));
  171. }else if (id=="script"){
  172. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listScript, null, "service", "script"));
  173. }else{
  174. promiseArr.push(_worker._getDesingerModule(id, _worker.findData.actions.listAgent, null, "service", "script"));
  175. }
  176. }
  177. return promiseArr;
  178. };
  179. _worker._listApplication = function(type){
  180. switch (type){
  181. case "processPlatform":
  182. return this.action.sendRequest(_worker._getRequestOption({"url": this.findData.actions.listProcess, "debug": this.findData.debug, "token": this.findData.token }));
  183. case "cms":
  184. return this.action.sendRequest(_worker._getRequestOption({"url": this.findData.actions.listCms, "debug": this.findData.debug, "token": this.findData.token }));
  185. case "portal":
  186. return this.action.sendRequest(_worker._getRequestOption({"url": this.findData.actions.listPortal, "debug": this.findData.debug, "token": this.findData.token }));
  187. case "query":
  188. return this.action.sendRequest(_worker._getRequestOption({"url": this.findData.actions.listQuery, "debug": this.findData.debug, "token": this.findData.token }));
  189. }
  190. };
  191. _worker._parseFindModule = function(moduleList){
  192. var promiseArr = [];
  193. moduleList.forEach(function(module){
  194. if (module.moduleType==="service"){
  195. if (!module.flagList || !module.flagList.length){
  196. var filterOption = JSON.parse(_worker.filterOptionTemplete);
  197. filterOption.moduleList.push({
  198. "moduleType": module.moduleType,
  199. "moduleAppList": [{"appId": "invoke"}]
  200. });
  201. this.filterOptionList.push(filterOption);
  202. filterOption = JSON.parse(_worker.filterOptionTemplete);
  203. filterOption.moduleList.push({
  204. "moduleType": module.moduleType,
  205. "moduleAppList": [{"appId": "agent"}]
  206. });
  207. this.filterOptionList.push(filterOption);
  208. filterOption = JSON.parse(_worker.filterOptionTemplete);
  209. filterOption.moduleList.push({
  210. "moduleType": module.moduleType,
  211. "moduleAppList": [{"appId": "script"}]
  212. });
  213. this.filterOptionList.push(filterOption);
  214. //promiseArr.push(Promise.resolve(""));
  215. promiseArr = promiseArr.concat(_worker["_getDesinger_"+module.moduleType]("invoke"));
  216. promiseArr = promiseArr.concat(_worker["_getDesinger_"+module.moduleType]("agent"));
  217. promiseArr = promiseArr.concat(_worker["_getDesinger_"+module.moduleType]("script"));
  218. }else{
  219. module.flagList.forEach(function(flag){
  220. if (!flag.designerList || !flag.designerList.length){
  221. promiseArr = promiseArr.concat(_worker["_getDesinger_"+module.moduleType](flag.id));
  222. }else{
  223. flag.designerList.forEach(function(designer){
  224. var filterOption = _worker._createFilterOption(module.moduleType, flag.id, designer.desingerType, designer.id)
  225. promiseArr = promiseArr.concat(Promise.resolve(filterOption));
  226. });
  227. }
  228. });
  229. }
  230. }else{
  231. if (!module.flagList || !module.flagList.length){
  232. var p = _worker._listApplication(module.moduleType);
  233. promiseArr.push(p.then(function(json){
  234. // json.data.forEach(function(app){
  235. // var filterOption = JSON.parse(_worker.filterOptionTemplete);
  236. // filterOption.moduleList.push({
  237. // "moduleType": module.moduleType,
  238. // "moduleAppList": [{"appId": app.id}]
  239. // });
  240. // this.filterOptionList.push(filterOption);
  241. // });
  242. // return Promise.resolve("");
  243. //临时处理
  244. var pArr = [];
  245. json.data.forEach(function(app){
  246. pArr = pArr.concat(_worker["_getDesinger_"+module.moduleType](app.id));
  247. });
  248. return Promise.all(pArr);
  249. }, function(){}));
  250. }else{
  251. module.flagList.forEach(function(flag){
  252. if (!flag.designerList || !flag.designerList.length){
  253. promiseArr = promiseArr.concat(_worker["_getDesinger_"+module.moduleType](flag.id));
  254. }else{
  255. flag.designerList.forEach(function(designer){
  256. var filterOption = _worker._createFilterOption(module.moduleType, flag.id, designer.desingerType, designer.id)
  257. promiseArr = promiseArr.concat(Promise.resolve(filterOption));
  258. });
  259. }
  260. });
  261. }
  262. }
  263. }.bind(this));
  264. return promiseArr;
  265. }
  266. _worker._findMessageReply = function(data, option){
  267. _worker.setTimeout(function(){
  268. _worker.postMessage(data);
  269. }, 100);
  270. };
  271. _worker._findOptionReply = function(){
  272. _worker.postMessage({
  273. "type": "done"
  274. });
  275. };
  276. _worker._createFindMessageReplyData = function(module, designer, aliase, pattern){
  277. return {
  278. "module": module,
  279. "appId": designer.appId,
  280. "appName": designer.appName,
  281. "designerId": designer.designerId,
  282. "designerName": designer.designerName,
  283. "designerType": designer.designerType,
  284. "designerAliase": aliase,
  285. "pattern": pattern
  286. };
  287. };
  288. _worker._setFilterOptionRegex = function(){
  289. var keyword = _worker.findData.option.keyword;
  290. if (_worker.findData.option.matchRegExp){
  291. var flag = (_worker.findData.option.caseSensitive) ? "g" : "gi";
  292. this.keywordRegexp = new RegExp(keyword, flag);
  293. }else{
  294. var flag = (_worker.findData.option.caseSensitive) ? "g" : "gi";
  295. keyword = (_worker.findData.option.matchWholeWord) ? "\\b"+keyword+"\\b" : keyword;
  296. this.keywordRegexp = new RegExp(keyword, flag);
  297. }
  298. };
  299. _worker._findProcessPlatformParse_script = function(designer){
  300. if (designer.patternList && designer.patternList.length){
  301. var action = this.findData.actions.getProcessScript;
  302. var p = _worker.action.sendRequest(_worker._getRequestOption({"url": action}, {"id": designer.designerId}));
  303. p.then(function(json){
  304. designer.patternList.forEach(function(pattern){
  305. if (pattern.property=="text"){
  306. var scriptLines = json.data.text.split(/\n/);
  307. pattern.lines.forEach(function(line){
  308. var scriptText = scriptLines[line-1];
  309. while ((arr = this.keywordRegexp.exec(scriptText)) !== null) {
  310. var col = arr.index;
  311. var key = arr[0];
  312. var value = arr.input;
  313. _worker._findMessageReply(_worker._createFindMessageReplyData("processPlatform", designer, json.data.aliase, {
  314. "property": pattern.property,
  315. "value": value,
  316. "line": line,
  317. "column": col,
  318. "key": key
  319. }));
  320. }
  321. });
  322. }else{
  323. _worker._findMessageReply(_worker._createFindMessageReplyData("processPlatform", designer, json.data.aliase, {
  324. "property": pattern.property,
  325. "value": json.data[pattern.property]
  326. }));
  327. }
  328. });
  329. }, function(){});
  330. }
  331. };
  332. _worker._findProcessPlatformParse_form = function(designer){
  333. };
  334. _worker._findProcessPlatformParse_process = function(designer){
  335. };
  336. _worker._findProcessPlatformParse = function(resultList){
  337. resultList.forEach(function(designer){
  338. switch (designer.designerType){
  339. case "script":
  340. _worker._findProcessPlatformParse_script(designer);
  341. break;
  342. case "form":
  343. _worker._findProcessPlatformParse_form(designer);
  344. break;
  345. case "process":
  346. _worker._findProcessPlatformParse_process(designer);
  347. break;
  348. }
  349. });
  350. };
  351. _worker._doFindDesigner = function(option){
  352. var res = _worker._getRequestOption({
  353. "method": "post",
  354. "url": this.findData.actions.findAction,
  355. "body": JSON.stringify(option),
  356. "debug": this.findData.debug,
  357. "token": this.findData.token
  358. });
  359. this.action.sendRequest(res).then(function(json){
  360. if (json.data.processPlatformList && json.data.processPlatformList.length){
  361. _worker._findProcessPlatformParse(json.data.processPlatformList);
  362. }
  363. if (json.data.cmsList && json.data.cmsList.length){
  364. }
  365. if (json.data.portalList && json.data.portalList.length){
  366. }
  367. if (json.data.queryList && json.data.queryList.length){
  368. }
  369. if (json.data.serviceList && json.data.serviceList.length){
  370. }
  371. _worker._findOptionReply();
  372. //_worker._findMessageReply(json.data, option);
  373. }, function(xhr){
  374. _worker._findOptionReply(null);
  375. _worker._findMessageReply(null);
  376. });
  377. },
  378. _worker._doFindDesignerFromFilterOption = function(){
  379. this.filterOptionList.forEach(function(option){
  380. _worker._doFindDesigner(option);
  381. });
  382. },
  383. onmessage = function(e) {
  384. this.findData = e.data;
  385. _worker._setFilterOptionRegex();
  386. // "parser": "_findProcessPlatformParse",
  387. // "actions": _worker.findData.actions,
  388. // "option": option,
  389. // "pattern": json.data.processPlatformList
  390. _worker[e.data.parser](e.data.pattern);
  391. }