Statement.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.query = MWF.xApplication.query || {};
  3. MWF.xApplication.query.StatementDesigner = MWF.xApplication.query.StatementDesigner || {};
  4. MWF.APPDSMD = MWF.xApplication.query.StatementDesigner;
  5. MWF.xDesktop.requireApp("query.StatementDesigner", "lp."+MWF.language, null, false);
  6. MWF.xDesktop.requireApp("query.StatementDesigner", "Property", null, false);
  7. MWF.xDesktop.requireApp("query.ViewDesigner", "View", null, false);
  8. o2.require("o2.widget.JavascriptEditor", null, false);
  9. MWF.xApplication.query.StatementDesigner.Statement = new Class({
  10. Extends: MWF.widget.Common,
  11. Implements: [Options, Events],
  12. options: {
  13. "style": "default",
  14. "isView": false,
  15. "showTab": true,
  16. "propertyPath": "../x_component_query_StatementDesigner/$Statement/statement.html"
  17. },
  18. initialize: function(designer, data, options){
  19. this.setOptions(options);
  20. this.path = "../x_component_query_StatementDesigner/$Statement/";
  21. this.cssPath = "../x_component_query_StatementDesigner/$Statement/"+this.options.style+"/css.wcss";
  22. this._loadCss();
  23. this.designer = designer;
  24. this.data = data;
  25. this.parseData();
  26. this.node = this.designer.designNode;
  27. this.areaNode = new Element("div", {"styles": {"height": "100%", "overflow": "auto"}});
  28. //this.statementRunNode = this.designer.designerStatementArea;
  29. if(this.designer.application) this.data.applicationName = this.designer.application.name;
  30. if(this.designer.application) this.data.application = this.designer.application.id;
  31. this.isNewStatement = (this.data.id) ? false : true;
  32. this.view = this;
  33. this.autoSave();
  34. this.designer.addEvent("queryClose", function(){
  35. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  36. }.bind(this));
  37. },
  38. parseData: function(){
  39. this.json = this.data;
  40. if (!this.json.type) this.json.type = "select";
  41. if (!this.json.format) this.json.format = "jpql";
  42. if (!this.json.entityCategory) this.json.entityCategory = "official";
  43. if (!this.json.entityClassName) this.json.entityClassName = "com.x.processplatform.core.entity.content.Task";
  44. },
  45. autoSave: function(){
  46. this.autoSaveTimerID = window.setInterval(function(){
  47. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFAutoSaveCheck");
  48. if (this.autoSaveCheckNode){
  49. if (this.autoSaveCheckNode.get("checked")){
  50. this.save();
  51. }
  52. }
  53. }.bind(this), 60000);
  54. },
  55. load : function(){
  56. // this.setAreaNodeSize();
  57. // this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  58. this.areaNode.inject(this.node);
  59. this.designer.statementListAreaNode.getChildren().each(function(node){
  60. var statement = node.retrieve("statement");
  61. if (statement.id==this.data.id){
  62. if (this.designer.currentListStatementItem){
  63. this.designer.currentListStatementItem.setStyles(this.designer.css.listStatementItem);
  64. }
  65. node.setStyles(this.designer.css.listStatementItem_current);
  66. this.designer.currentListStatementItem = node;
  67. this.lisNode = node;
  68. }
  69. }.bind(this));
  70. this.loadStatement();
  71. this.showProperty();
  72. },
  73. showProperty: function(){
  74. if (!this.property){
  75. this.property = new MWF.xApplication.query.StatementDesigner.Property(this, this.designer.designerContentArea, this.designer, {
  76. "path": this.options.propertyPath,
  77. "onPostLoad": function(){
  78. this.property.show();
  79. }.bind(this)
  80. });
  81. this.property.load();
  82. }else{
  83. this.property.show();
  84. }
  85. },
  86. loadStatement: function(){
  87. //this.statementDesignerNode = new Element("div", {"styles": this.css.statementDesignerNode}).inject(this.areaNode);
  88. this.loadStatementHtml(function(){
  89. this.designerArea = this.areaNode.getElement(".o2_statement_statementDesignerNode");
  90. this.jpqlArea = this.areaNode.getElement(".o2_statement_statementDesignerJpql");
  91. this.scriptArea = this.areaNode.getElement(".o2_statement_statementDesignerScript");
  92. this.formatTypeArea = this.areaNode.getElement(".o2_statement_statementDesignerFormatContent");
  93. this.entityCategorySelect = this.areaNode.getElement(".o2_statement_statementDesignerCategoryContent").getElement("select");
  94. this.dynamicTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_dynamic");
  95. this.officialTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_official");
  96. this.customTableArea = this.areaNode.getElement(".o2_statement_statementDesignerTableArea_custom");
  97. this.dynamicTableSelect = this.areaNode.getElement(".o2_statement_statementDesignerSelectTable");
  98. this.officialTableSelect = this.officialTableArea.getElement("select");
  99. this.dynamicTableContent = this.areaNode.getElement(".o2_statement_statementDesignerTableContent");
  100. this.jpqlTypeSelect = this.areaNode.getElement(".o2_statement_statementDesignerTypeContent").getElement("select");
  101. // this.jpqlSelectEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_select");
  102. // this.jpqlUpdateEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_update");
  103. // this.jpqlDeleteEditor = this.areaNode.getElement(".o2_statement_statementDesignerJpql_sdelete");
  104. // this.jpqlSelectEditor_selectContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_selectContent");
  105. // this.jpqlSelectEditor_fromContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_fromContent");
  106. // this.jpqlSelectEditor_whereContent= this.jpqlSelectEditor.getElement(".o2_statement_statementDesignerJpql_jpql_whereContent");
  107. this.jpqlEditorNode = this.areaNode.getElement(".o2_statement_statementDesignerJpqlLine");
  108. // this.runArea = this.areaNode.getElement(".o2_statement_statementRunNode");
  109. // this.runTitleNode = this.areaNode.getElement(".o2_statement_statementRunTitleNode");
  110. // this.runContentNode = this.areaNode.getElement(".o2_statement_statementRunContentNode");
  111. // this.runJsonNode = this.runContentNode.getFirst();
  112. // this.runActionNode = this.runJsonNode.getNext();
  113. // this.runResultNode = this.runContentNode.getLast();
  114. //
  115. // this.setRunnerSize();
  116. // this.designer.addEvent("resize", this.setRunnerSize.bind(this));
  117. this.viewArea = this.areaNode.getElement(".o2_statement_viewNode");
  118. this.setViewSize();
  119. this.designer.addEvent("resize", this.setViewSize.bind(this));
  120. if (this.json.format=="script"){
  121. this.loadStatementScriptEditor();
  122. }else{
  123. this.loadStatementEditor();
  124. }
  125. // this.loadStatementRunner();
  126. this.loadView();
  127. this.setEvent();
  128. }.bind(this));
  129. },
  130. loadStatementScriptEditor: function(){
  131. if (! this.scriptEditor){
  132. debugger;
  133. o2.require("o2.widget.ScriptArea", function(){
  134. this.scriptEditor = new o2.widget.ScriptArea(this.scriptArea, {
  135. "isbind": false,
  136. "maxObj": this.designer.designNode,
  137. "title": this.designer.lp.scriptTitle,
  138. "onChange": function(){
  139. this.json.scriptText = this.scriptEditor.toJson().code;
  140. }.bind(this)
  141. });
  142. this.scriptEditor.load({"code": this.json.scriptText})
  143. }.bind(this), false);
  144. }
  145. },
  146. // setRunnerSize: function(){
  147. // debugger;
  148. // var size = this.areaNode.getSize();
  149. // var designerSize = this.designerArea.getComputedSize();
  150. // var y = size.y-designerSize.totalHeight;
  151. // var mTop = this.runArea.getStyle("margin-top").toInt();
  152. // var mBottom = this.runArea.getStyle("margin-bottom").toInt();
  153. // var pTop = this.runArea.getStyle("padding-top").toInt();
  154. // var pBottom = this.runArea.getStyle("padding-bottom").toInt();
  155. // y = y-mTop-mBottom-pTop-pBottom-1;
  156. //
  157. // this.runArea.setStyle("height", ""+y+"px");
  158. //
  159. // var titleSize = this.runTitleNode.getComputedSize();
  160. // y = y - titleSize.totalHeight;
  161. // this.runContentNode.setStyle("height", ""+y+"px");
  162. // },
  163. loadStatementEditor: function(){
  164. if (!this.editor){
  165. o2.require("o2.widget.JavascriptEditor", function(){
  166. this.editor = new o2.widget.JavascriptEditor(this.jpqlEditorNode, {"title": "JPQL", "option": {"mode": "sql"}});
  167. this.editor.load(function(){
  168. if (this.json.data){
  169. this.editor.editor.setValue(this.json.data);
  170. }else{
  171. var table = "table";
  172. switch (this.json.type) {
  173. case "update":
  174. this.editor.editor.setValue("UPDATE "+table+" o SET ");
  175. break;
  176. case "delete":
  177. this.editor.editor.setValue("DELETE "+table+" o WHERE ");
  178. break;
  179. default:
  180. this.editor.editor.setValue("SELECT * FROM "+table+" o");
  181. }
  182. }
  183. this.json.data = this.editor.editor.getValue();
  184. this.editor.addEditorEvent("change", function(){
  185. debugger;
  186. this.data.data = this.editor.getValue();
  187. this.checkJpqlType();
  188. }.bind(this));
  189. // this.editor.editor.on("change", function(){
  190. // this.data.data = this.editor.getValue();
  191. // this.checkJpqlType();
  192. // }.bind(this));
  193. }.bind(this));
  194. }.bind(this), false);
  195. }
  196. },
  197. setSatementTable: function(){
  198. if (!this.json.type) this.json.type = "select";
  199. this.changeType(this.json.type, true);
  200. if (this.json.data){
  201. this.editor.editor.setValue(this.json.data);
  202. }else{
  203. var table = (this.json.tableObj) ? this.json.tableObj.name : "table";
  204. switch (this.json.type) {
  205. case "update":
  206. this.editor.editor.setValue("UPDATE "+table+" o SET ");
  207. break;
  208. case "delete":
  209. this.editor.editor.setValue("DELETE "+table+" o WHERE ");
  210. break;
  211. default:
  212. this.editor.editor.setValue("SELECT * FROM "+table+" o");
  213. }
  214. }
  215. },
  216. checkJpqlType: function(){
  217. var str = this.json.data;
  218. this.json.data = str;
  219. var jpql_select = /^select/i;
  220. var jpql_update = /^update/i;
  221. var jpql_delete = /^delete/i;
  222. if (jpql_select.test(str)) return this.changeType("select");
  223. if (jpql_update.test(str)) return this.changeType("update");
  224. if (jpql_delete.test(str)) return this.changeType("delete");
  225. },
  226. changeType: function(type, force){
  227. if (this.json.type!=type) this.json.type=type;
  228. if (type != this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value || force){
  229. for (var i=0; i<this.jpqlTypeSelect.options.length; i++){
  230. if (this.jpqlTypeSelect.options[i].value==type){
  231. this.jpqlTypeSelect.options[i].set("selected", true);
  232. break;
  233. }
  234. }
  235. }
  236. },
  237. loadStatementHtml: function(callback){
  238. this.areaNode.loadAll({
  239. "css": this.path+this.options.style+"/statement.css",
  240. "html": this.path+"statementDesigner.html"
  241. }, {
  242. "bind": {"lp": this.designer.lp, "data": this.data}
  243. },function(){
  244. if (callback) callback();
  245. }.bind(this));
  246. },
  247. // loadStatementRunner: function(){
  248. // o2.require("o2.widget.JavascriptEditor", function(){
  249. // this.jsonEditor = new o2.widget.JavascriptEditor(this.runJsonNode, {"title": "JPQL", "option": {"mode": "json"}});
  250. // this.jsonEditor.load(function(){
  251. // this.jsonEditor.editor.setValue("{}");
  252. // }.bind(this));
  253. // }.bind(this), false);
  254. // },
  255. setEvent: function(){
  256. this.formatTypeArea.getElements("input").addEvent("click", function(e){
  257. if (e.target.checked){
  258. var v = e.target.get("value");
  259. if (v==="script"){
  260. this.scriptArea.show();
  261. this.jpqlArea.hide();
  262. this.loadStatementScriptEditor();
  263. }else{
  264. this.scriptArea.hide();
  265. this.jpqlArea.show();
  266. this.loadStatementEditor();
  267. }
  268. this.json.format = v;
  269. }
  270. }.bind(this));
  271. this.entityCategorySelect.addEvent("change", function(e){
  272. var entityCategory = e.target.options[e.target.selectedIndex].value;
  273. switch (entityCategory) {
  274. case "dynamic":
  275. this.officialTableArea.hide();
  276. this.dynamicTableArea.show();
  277. this.customTableArea.hide();
  278. break;
  279. case "custom":
  280. this.officialTableArea.hide();
  281. this.dynamicTableArea.hide();
  282. this.customTableArea.show();
  283. break;
  284. default:
  285. this.officialTableArea.show();
  286. this.dynamicTableArea.hide();
  287. this.customTableArea.hide();
  288. break;
  289. }
  290. this.json.entityCategory = entityCategory
  291. }.bind(this));
  292. //@todo change table
  293. this.officialTableSelect.addEvent("change", function(e){
  294. debugger;
  295. var entityClassName = e.target.options[e.target.selectedIndex].value;
  296. this.json.entityClassName = entityClassName;
  297. if (this.json.format=="jpql"){
  298. if (this.editor){
  299. var re = /(.*from\s*)/ig;
  300. if (this.json.type=="update") re = /(.*update\s*)/ig;
  301. //if (this.json.type=="select" && this.editor){
  302. var v = this.json.data;
  303. var re2 = /(\s+)/ig;
  304. var arr = re.exec(v);
  305. if (arr && arr[0]){
  306. var left = arr[0]
  307. v = v.substring(left.length, v.length);
  308. //var ar = re2.exec(v);
  309. var right = v.substring(v.indexOf(" "),v.length);
  310. this.json.data = left+entityClassName+right;
  311. this.editor.editor.setValue(this.json.data);
  312. }
  313. //}
  314. }
  315. }
  316. // var className = e.target.options[e.target.selectedIndex].value;
  317. // if (this.json.type=="select"){
  318. // this.json.data
  319. // /(select)*(where|)/g
  320. // }
  321. // }.bind(this));
  322. // this.jpqlTypeSelect.addEvent("change", function(){
  323. // var type = e.target.options[e.target.selectedIndex].value;
  324. // switch (entityCategory) {
  325. // case "update":
  326. // this.jpqlSelectEditor.hide();
  327. // this.jpqlUpdateEditor.show();
  328. // this.jpqlDeleteEditor.hide();
  329. // this.loadJpqlUpdateEditor();
  330. // break;
  331. // case "delete":
  332. // this.jpqlSelectEditor.hide();
  333. // this.jpqlUpdateEditor.hide();
  334. // this.jpqlDeleteEditor.show();
  335. // break;
  336. // default:
  337. // this.jpqlSelectEditor.show();
  338. // this.jpqlUpdateEditor.hide();
  339. // this.jpqlDeleteEditor.hide();
  340. // break;
  341. // }
  342. }.bind(this));
  343. // this.runActionNode.getFirst().addEvent("click", this.runStatement.bind(this));
  344. this.dynamicTableSelect.addEvent("click", this.selectTable.bind(this));
  345. this.jpqlTypeSelect.addEvent("change", function(){
  346. var t = this.jpqlTypeSelect.options[this.jpqlTypeSelect.selectedIndex].value;
  347. if (t!=this.json.type) this.json.type=t;
  348. }.bind(this));
  349. },
  350. selectTable: function(){
  351. new MWF.O2Selector(this.designer.content, {
  352. "type": "queryTable",
  353. "count": 1,
  354. "values": (this.json.table) ? [this.json.table] : [],
  355. "title": this.designer.lp.selectTable,
  356. "onComplete": function(items){
  357. if (items.length){
  358. var id = items[0].data.id;
  359. var name = items[0].data.name;
  360. this.dynamicTableContent.set("text", name);
  361. this.json.table = name;
  362. this.json.tableObj = items[0].data;
  363. }else{
  364. this.dynamicTableContent.set("text", "");
  365. this.json.table = "";
  366. }
  367. }.bind(this)
  368. });
  369. },
  370. runStatement:function(){
  371. debugger;
  372. // if (!this.json.data){
  373. // this.designer.notice(this.designer.lp.inputStatementData, "error");
  374. // return false;
  375. // }
  376. o2.require("o2.widget.Mask", null, false);
  377. this.runMask = new o2.widget.Mask();
  378. this.runMask.loadNode(this.node);
  379. this.saveSilence(function(){
  380. var json = this.jsonEditor.editor.getValue();
  381. var o = JSON.parse(json);
  382. o2.Actions.get("x_query_assemble_designer").executeStatement(this.json.id, 1, 50 , o, function(json){
  383. o2.require("o2.widget.JsonParse", function(){
  384. // this.runResultNode.empty();
  385. // var jsonResult = new o2.widget.JsonParse(json.data, this.runResultNode);
  386. // jsonResult.load();
  387. }.bind(this));
  388. this.runMask.hide();
  389. }.bind(this), function(xhr, text, error){
  390. debugger;
  391. if (this.runMask) this.runMask.hide();
  392. var errorText = error;
  393. if (xhr){
  394. var json = JSON.decode(xhr.responseText);
  395. if (json){
  396. errorText = json.message.trim() || "request json error";
  397. }else{
  398. errorText = "request json error: "+xhr.responseText;
  399. }
  400. }
  401. errorText = errorText.replace(/\</g, "&lt;");
  402. errorText = errorText.replace(/\</g, "&gt;");
  403. MWF.xDesktop.notice("error", {x: "right", y:"top"}, errorText);
  404. }.bind(this))
  405. }.bind(this));
  406. },
  407. save: function(callback){
  408. debugger;
  409. if (!this.data.name){
  410. this.designer.notice(this.designer.lp.inputStatementName, "error");
  411. return false;
  412. }
  413. //if( !this.data.tableType ){
  414. // this.data.tableType = "dynamic";
  415. //}
  416. if (this.editor) this.data.data = this.editor.editor.getValue();
  417. if (this.scriptEditor) this.data.scriptText = this.scriptEditor.toJson().code;
  418. this.designer.actions.saveStatement(this.data, function(json){
  419. this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  420. this.data.id = json.data.id;
  421. if (this.lisNode) {
  422. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  423. }
  424. if (callback) callback();
  425. }.bind(this));
  426. },
  427. _setEditStyle: function(){},
  428. saveSilence: function(callback){
  429. if (!this.data.name){
  430. this.designer.notice(this.designer.lp.inputStatementName, "error");
  431. return false;
  432. }
  433. if (this.editor) this.data.data = this.editor.editor.getValue();
  434. if (this.scriptEditor) this.data.scriptText = this.scriptEditor.toJson().code;
  435. this.designer.actions.saveStatement(this.data, function(json){
  436. //this.designer.notice(this.designer.lp.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  437. this.data.id = json.data.id;
  438. if (this.lisNode) {
  439. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  440. }
  441. if (callback) callback();
  442. }.bind(this));
  443. },
  444. loadView : function(){
  445. if( !this.data.view )this.data.view = {};
  446. this.view = new MWF.xApplication.query.StatementDesigner.View(this.designer, this, this.data.view, {});
  447. this.view.load();
  448. },
  449. setViewSize: function(){
  450. debugger;
  451. var size = this.areaNode.getSize();
  452. var designerSize = this.designerArea.getComputedSize();
  453. var y = size.y-designerSize.totalHeight;
  454. var mTop = this.viewArea.getStyle("margin-top").toInt();
  455. var mBottom = this.viewArea.getStyle("margin-bottom").toInt();
  456. var pTop = this.viewArea.getStyle("padding-top").toInt();
  457. var pBottom = this.viewArea.getStyle("padding-bottom").toInt();
  458. y = y-mTop-mBottom-pTop-pBottom-1;
  459. this.viewArea.setStyle("height", ""+y+"px");
  460. // var titleSize = this.runTitleNode.getComputedSize();
  461. // y = y - titleSize.totalHeight;
  462. // this.runContentNode.setStyle("height", ""+y+"px");
  463. }
  464. });
  465. MWF.xApplication.query.StatementDesigner.View = new Class({
  466. Extends: MWF.xApplication.query.ViewDesigner.View,
  467. Implements: [Options, Events],
  468. options: {
  469. "style": "default",
  470. "isView": false,
  471. "showTab": true,
  472. "propertyPath": "../x_component_query_StatementDesigner/$Statement/view.html"
  473. },
  474. initialize: function(designer, statement, data, options){
  475. this.setOptions(options);
  476. this.path = "../x_component_query_ViewDesigner/$View/";
  477. this.cssPath = "../x_component_query_ViewDesigner/$View/"+this.options.style+"/css.wcss";
  478. this._loadCss();
  479. this.statement = statement;
  480. this.designer = designer;
  481. this.data = data;
  482. // if (!this.data.data) this.data.data = {};
  483. this.parseData();
  484. this.node = this.statement.viewArea;
  485. //this.tab = this.designer.tab;
  486. this.areaNode = new Element("div", {"styles": {"height": "100%", "overflow": "auto"}});
  487. //MWF.require("MWF.widget.ScrollBar", function(){
  488. // new MWF.widget.ScrollBar(this.areaNode, {"distance": 100});
  489. //}.bind(this));
  490. // this.propertyListNode = this.designer.propertyDomArea;
  491. //this.propertyNode = this.designer.propertyContentArea;
  492. // if(this.designer.application) this.data.applicationName = this.designer.application.name;
  493. // if(this.designer.application) this.data.application = this.designer.application.id;
  494. // this.isNewView = (this.data.name) ? false : true;
  495. this.items = [];
  496. this.view = this;
  497. // this.autoSave();
  498. // this.designer.addEvent("queryClose", function(){
  499. // if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  500. // }.bind(this));
  501. },
  502. load : function(){
  503. this.setAreaNodeSize();
  504. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  505. this.areaNode.inject(this.node);
  506. this.domListNode = new Element("div", {"styles": {"overflow": "hidden"}}).inject(this.designer.propertyDomArea);
  507. this.loadTemplateStyle( function () {
  508. this.loadActionbar();
  509. this.loadView();
  510. this.loadPaging();
  511. this.selected();
  512. this.setEvent();
  513. //if (this.options.showTab) this.page.showTabIm();
  514. this.setViewWidth();
  515. this.designer.addEvent("resize", this.setViewWidth.bind(this));
  516. }.bind(this))
  517. },
  518. parseData: function(){
  519. this.json = this.data;
  520. if( !this.json.data || !this.json.data.events ){
  521. var url = "../x_component_query_StatementDesigner/$Statement/view.json";
  522. MWF.getJSON(url, {
  523. "onSuccess": function(obj){
  524. if(!this.json.data)this.json.data = obj.data;
  525. if(!this.json.data.events)this.json.data.events = obj.data.events;
  526. }.bind(this),
  527. "onerror": function(text){
  528. this.notice(text, "error");
  529. }.bind(this),
  530. "onRequestFailure": function(xhr){
  531. this.notice(xhr.responseText, "error");
  532. }.bind(this)
  533. },false);
  534. }
  535. },
  536. showProperty: function(){
  537. if (!this.property){
  538. this.property = new MWF.xApplication.query.StatementDesigner.Property(this, this.designer.propertyContentArea, this.designer, {
  539. "path": this.options.propertyPath,
  540. "onPostLoad": function(){
  541. this.property.show();
  542. }.bind(this)
  543. });
  544. this.property.load();
  545. }else{
  546. this.property.show();
  547. }
  548. },
  549. hideProperty: function(){
  550. if (this.property) this.property.hide();
  551. },
  552. loadViewData: function(){
  553. if (this.data.id){
  554. this.saveSilence(function(){
  555. this.viewContentBodyNode.empty();
  556. this.viewContentTableNode = new Element("table", {
  557. "styles": this.css.viewContentTableNode,
  558. "border": "0px",
  559. "cellPadding": "0",
  560. "cellSpacing": "0"
  561. }).inject(this.viewContentBodyNode);
  562. this.designer.actions.loadView(this.data.id, null,function(json){
  563. var entries = {};
  564. json.data.selectList.each(function(entry){entries[entry.column] = entry;}.bind(this));
  565. if (this.json.data.group.column){
  566. if (json.data.groupGrid.length){
  567. var groupColumn = null;
  568. for (var c = 0; c<json.data.selectList.length; c++){
  569. if (json.data.selectList[c].column === json.data.group.column){
  570. groupColumn = json.data.selectList[c];
  571. break;
  572. }
  573. }
  574. json.data.groupGrid.each(function(line, idx){
  575. var groupTr = new Element("tr", {
  576. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTr"] : this.css.viewContentTrNode,
  577. "data-is-group" : "yes"
  578. }).inject(this.viewContentTableNode);
  579. var colSpan = this.items.length ;
  580. var td = new Element("td", {
  581. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentGroupTd"] : this.css.viewContentGroupTdNode,
  582. "colSpan": colSpan
  583. }).inject(groupTr);
  584. var groupAreaNode;
  585. if( this.json.data.viewStyles ){
  586. groupAreaNode = new Element("div", {"styles": this.json.data.viewStyles["groupCollapseNode"]}).inject(td);
  587. groupAreaNode.set("text", line.group);
  588. }else{
  589. groupAreaNode = new Element("div", {"styles": this.css.viewContentTdGroupNode}).inject(td);
  590. var groupIconNode = new Element("div", {"styles": this.css.viewContentTdGroupIconNode}).inject(groupAreaNode);
  591. var groupTextNode = new Element("div", {"styles": this.css.viewContentTdGroupTextNode}).inject(groupAreaNode);
  592. if (groupColumn){
  593. //groupTextNode.set("text", (groupColumn.code) ? MWF.Macro.exec(groupColumn.code, {"value": line.group, "gridData": json.data.groupGrid, "data": json.data, "entry": line}) : line.group);
  594. groupTextNode.set("text", line.group);
  595. }else{
  596. groupTextNode.set("text", line.group);
  597. }
  598. }
  599. var subtrs = [];
  600. line.list.each(function(entry){
  601. var tr = new Element("tr", {
  602. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTr"] : this.css.viewContentTrNode
  603. }).inject(this.viewContentTableNode);
  604. tr.setStyle("display", "none");
  605. //this.createViewCheckboxTd( tr );
  606. var td = new Element("td", {
  607. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTd"] : this.css.viewContentTdNode
  608. }).inject(tr);
  609. Object.each(entries, function(c, k){
  610. var d = entry.data[k];
  611. if (d!=undefined){
  612. if (k!=this.json.data.group.column){
  613. var td = new Element("td", {
  614. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTd"] : this.css.viewContentTdNode
  615. }).inject(tr);
  616. //td.set("text", (entries[k].code) ? MWF.Macro.exec(entries[k].code, {"value": d, "gridData": json.data.groupGrid, "data": json.data, "entry": entry}) : d);
  617. if (c.isHtml){
  618. td.set("html", d);
  619. }else{
  620. td.set("text", d);
  621. }
  622. }
  623. }
  624. }.bind(this));
  625. // Object.each(entry.data, function(d, k){
  626. // if (k!=this.json.data.group.column){
  627. // var td = new Element("td", {"styles": this.css.viewContentTdNode}).inject(tr);
  628. // td.set("text", (entries[k].code) ? MWF.Macro.exec(entries[k].code, {"value": d, "gridData": json.data.groupGrid, "data": json.data, "entry": entry}) : d);
  629. // }
  630. // }.bind(this));
  631. subtrs.push(tr)
  632. }.bind(this));
  633. groupAreaNode.store("subtrs", subtrs);
  634. var _self = this;
  635. groupAreaNode.addEvent("click", function(){
  636. var subtrs = this.retrieve("subtrs");
  637. var iconNode = groupAreaNode.getFirst("div");
  638. if (subtrs[0]){
  639. if (subtrs[0].getStyle("display")=="none"){
  640. subtrs.each(function(subtr){ subtr.setStyle("display", "table-row"); });
  641. if( iconNode ) {
  642. iconNode.setStyle("background", "url(" + "../x_component_process_StatementDesigner/$View/default/icon/down.png) center center no-repeat");
  643. }else{
  644. this.setStyles( _self.json.data.viewStyles["groupExpandNode"] )
  645. }
  646. }else{
  647. subtrs.each(function(subtr){ subtr.setStyle("display", "none"); });
  648. if( iconNode ) {
  649. iconNode.setStyle("background", "url(" + "../x_component_process_StatementDesigner/$View/default/icon/right.png) center center no-repeat");
  650. }else{
  651. this.setStyles( _self.json.data.viewStyles["groupCollapseNode"] )
  652. }
  653. }
  654. }
  655. _self.setContentHeight();
  656. });
  657. }.bind(this));
  658. this.setContentColumnWidth();
  659. this.setContentHeight();
  660. }else if(this.json.data.noDataText){
  661. var noDataTextNodeStyle = this.css.noDataTextNode;
  662. if( this.json.data.viewStyles ){
  663. if( this.json.data.viewStyles["noDataTextNode"] ){
  664. noDataTextNodeStyle = this.json.data.viewStyles["noDataTextNode"]
  665. }else{
  666. this.json.data.viewStyles["noDataTextNode"] = this.css.noDataTextNode
  667. }
  668. }
  669. this.noDataTextNode = new Element( "div", {
  670. "styles": noDataTextNodeStyle,
  671. "text" : this.json.data.noDataText
  672. }).inject( this.viewContentBodyNode );
  673. }
  674. }else{
  675. if (json.data.grid.length){
  676. json.data.grid.each(function(line, idx){
  677. var tr = new Element("tr", {
  678. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTr"] : this.css.viewContentTrNode
  679. }).inject(this.viewContentTableNode);
  680. //this.createViewCheckboxTd( tr );
  681. Object.each(entries, function(c, k){
  682. var d = line.data[k];
  683. if (d!=undefined){
  684. var td = new Element("td", {
  685. "styles": this.json.data.viewStyles ? this.json.data.viewStyles["contentTd"] : this.css.viewContentTdNode
  686. }).inject(tr);
  687. //td.set("text", (entries[k].code) ? MWF.Macro.exec(entries[k].code, {"value": d, "gridData": json.data.grid, "data": json.data, "entry": line}) : d);
  688. if (c.isHtml){
  689. td.set("html", d);
  690. }else{
  691. td.set("text", d);
  692. }
  693. //td.set("text", d);
  694. }
  695. }.bind(this));
  696. // Object.each(line.data, function(d, k){
  697. // var td = new Element("td", {"styles": this.css.viewContentTdNode}).inject(tr);
  698. // td.set("text", (entries[k].code) ? MWF.Macro.exec(entries[k].code, {"value": d, "gridData": json.data.grid, "data": json.data, "entry": line}) : d);
  699. // }.bind(this));
  700. }.bind(this));
  701. this.setContentColumnWidth();
  702. this.setContentHeight();
  703. }else if(this.json.data.noDataText){
  704. var noDataTextNodeStyle = this.css.noDataTextNode;
  705. if( this.json.data.viewStyles ){
  706. if( this.json.data.viewStyles["noDataTextNode"] ){
  707. noDataTextNodeStyle = this.json.data.viewStyles["noDataTextNode"]
  708. }else{
  709. this.json.data.viewStyles["noDataTextNode"] = this.css.noDataTextNode
  710. }
  711. }
  712. this.noDataTextNode = new Element( "div", {
  713. "styles": noDataTextNodeStyle,
  714. "text" : this.json.data.noDataText
  715. }).inject( this.viewContentBodyNode );
  716. }
  717. }
  718. }.bind(this));
  719. }.bind(this));
  720. }
  721. },
  722. addColumn: function(){
  723. debugger;
  724. MWF.require("MWF.widget.UUID", function(){
  725. var id = (new MWF.widget.UUID).id;
  726. var json = {
  727. "id": id,
  728. "column": id,
  729. "displayName": this.designer.lp.unnamed,
  730. "orderType": "original"
  731. };
  732. if (!this.json.data.selectList) this.json.data.selectList = [];
  733. this.json.data.selectList.push(json);
  734. var column = new MWF.xApplication.query.StatementDesigner.View.Column(json, this);
  735. this.items.push(column);
  736. column.selected();
  737. if (this.viewContentTableNode){
  738. var trs = this.viewContentTableNode.getElements("tr");
  739. trs.each(function(tr){
  740. new Element("td", {"styles": this.css.viewContentTdNode}).inject(tr)
  741. }.bind(this));
  742. //this.setContentColumnWidth();
  743. }
  744. this.setViewWidth();
  745. this.addColumnNode.scrollIntoView(true);
  746. }.bind(this));
  747. //new Fx.Scroll(this.view.areaNode, {"wheelStops": false, "duration": 0}).toRight();
  748. },
  749. unSelected: function(){
  750. this.currentSelectedModule = null;
  751. this.hideProperty();
  752. },
  753. loadViewColumns: function(){
  754. // for (var i=0; i<10; i++){
  755. if (this.json.data.selectList) {
  756. this.json.data.selectList.each(function (json) {
  757. this.items.push(new MWF.xApplication.query.StatementDesigner.View.Column(json, this));
  758. }.bind(this));
  759. }
  760. // }
  761. },
  762. showActionbar : function( noSetHeight ){
  763. this.actionbarNode.show();
  764. if( !this.json.data.actionbarList )this.json.data.actionbarList = [];
  765. if( !this.actionbarList || this.actionbarList.length == 0 ){
  766. if( this.json.data.actionbarList.length ){
  767. this.json.data.actionbarList.each( function(json){
  768. this.actionbarList.push( new MWF.xApplication.query.StatementDesigner.View.Actionbar( json, this.json.data.actionbarList, this) )
  769. }.bind(this));
  770. }else{
  771. this.actionbarList.push( new MWF.xApplication.query.StatementDesigner.View.Actionbar( null, this.json.data.actionbarList, this) )
  772. }
  773. }
  774. if( !noSetHeight )this.setContentHeight();
  775. },
  776. loadPaging: function( noSetHeight ){
  777. this.pagingNode = new Element("div#pagingNode", {"styles": this.css.pagingNode}).inject(this.areaNode);
  778. this.pagingList = [];
  779. if( !this.json.data.pagingList )this.json.data.pagingList = [];
  780. if( !this.pagingList || this.pagingList.length == 0 ){
  781. if( this.json.data.pagingList.length ){
  782. this.json.data.pagingList.each( function(json){
  783. this.pagingList.push( new MWF.xApplication.query.StatementDesigner.View.Paging( json, this.json.data.pagingList, this) )
  784. }.bind(this));
  785. }else{
  786. this.pagingList.push( new MWF.xApplication.query.StatementDesigner.View.Paging( null, this.json.data.pagingList, this) )
  787. }
  788. }
  789. // if( !noSetHeight )this.setContentHeight();
  790. },
  791. setViewWidth: function(){
  792. if( !this.viewAreaNode )return;
  793. this.viewAreaNode.setStyle("width", "auto");
  794. this.viewTitleNode.setStyle("width", "auto");
  795. var s1 = this.viewTitleTableNode.getSize();
  796. var s2 = this.refreshNode.getSize();
  797. var s3 = this.addColumnNode.getSize();
  798. var width = s1.x+s2.x+s2.x;
  799. var size = this.areaNode.getSize();
  800. if (width>size.x){
  801. this.viewTitleNode.setStyle("width", ""+width+"px");
  802. this.viewAreaNode.setStyle("width", ""+width+"px");
  803. }else{
  804. this.viewTitleNode.setStyle("width", ""+size.x+"px");
  805. this.viewAreaNode.setStyle("width", ""+size.x+"px");
  806. }
  807. this.setContentColumnWidth();
  808. this.setContentHeight();
  809. },
  810. preview: function(){
  811. if( this.isNewView ){
  812. this.designer.notice( this.designer.lp.saveViewNotice, "error" );
  813. return;
  814. }
  815. this.saveSilence( function () {
  816. var url = "../x_desktop/app.html?app=query.Query&status=";
  817. url += JSON.stringify({
  818. id : this.data.application,
  819. viewId : this.data.id
  820. });
  821. window.open(o2.filterUrl(url),"_blank");
  822. }.bind(this));
  823. },
  824. saveSilence: function(callback){
  825. if (!this.data.name){
  826. this.designer.notice(this.designer.lp.notice.inputName, "error");
  827. return false;
  828. }
  829. this.designer.actions.saveView(this.data, function(json){
  830. this.data.id = json.data.id;
  831. this.isNewView = false;
  832. //this.page.textNode.set("text", this.data.name);
  833. if (this.lisNode) {
  834. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  835. }
  836. if (callback) callback();
  837. }.bind(this));
  838. },
  839. save: function(callback){
  840. //if (this.designer.tab.showPage==this.page){
  841. if (!this.data.name){
  842. this.designer.notice(this.designer.lp.notice.inputName, "error");
  843. return false;
  844. }
  845. //}
  846. this.designer.actions.saveView(this.data, function(json){
  847. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  848. this.isNewView = false;
  849. this.data.id = json.data.id;
  850. //this.page.textNode.set("text", this.data.name);
  851. if (this.lisNode) {
  852. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  853. }
  854. if (callback) callback();
  855. }.bind(this));
  856. },
  857. _setEditStyle: function(name, input, oldValue){
  858. if( name=="data.actionbarHidden" ){
  859. if( this.json.data.actionbarHidden ){
  860. this.hideActionbar()
  861. }else{
  862. this.showActionbar()
  863. }
  864. }
  865. if( name=="data.selectAllEnable" ){
  866. if( this.json.data.selectAllEnable ){
  867. this.viewTitleTrNode.getElement(".viewTitleCheckboxTd").setStyle("display","table-cell");
  868. this.viewContentTableNode.getElements(".viewContentCheckboxTd").setStyle("display","table-cell");
  869. }else{
  870. this.viewTitleTrNode.getElement(".viewTitleCheckboxTd").setStyle("display","none");
  871. this.viewContentTableNode.getElements(".viewContentCheckboxTd").setStyle("display","none");
  872. }
  873. }
  874. if (name=="data.viewStyleType"){
  875. var file = (this.stylesList && this.json.data.viewStyleType) ? this.stylesList[this.json.data.viewStyleType].file : null;
  876. var extendFile = (this.stylesList && this.json.data.viewStyleType) ? this.stylesList[this.json.data.viewStyleType].extendFile : null;
  877. this.loadTemplateStyles( file, extendFile, function( templateStyles ){
  878. this.templateStyles = templateStyles;
  879. var oldFile, oldExtendFile;
  880. if( oldValue && this.stylesList[oldValue] ){
  881. oldFile = this.stylesList[oldValue].file;
  882. oldExtendFile = this.stylesList[oldValue].extendFile;
  883. }
  884. this.loadTemplateStyles( oldFile, oldExtendFile, function( oldTemplateStyles ){
  885. this.json.data.styleConfig = (this.stylesList && this.json.data.viewStyleType) ? this.stylesList[this.json.data.viewStyleType] : null;
  886. if (oldTemplateStyles["view"]) this.clearTemplateStyles(oldTemplateStyles["view"]);
  887. if (this.templateStyles["view"]) this.setTemplateStyles(this.templateStyles["view"]);
  888. this.setAllStyles();
  889. this.actionbarList.each( function (module) {
  890. if (oldTemplateStyles["actionbar"]){
  891. module.clearTemplateStyles(oldTemplateStyles["actionbar"]);
  892. }
  893. module.setStyleTemplate();
  894. module.setAllStyles();
  895. })
  896. this.pagingList.each( function (module) {
  897. if (oldTemplateStyles["paging"]){
  898. module.clearTemplateStyles(oldTemplateStyles["paging"]);
  899. }
  900. module.setStyleTemplate();
  901. module.setAllStyles();
  902. });
  903. // this.moduleList.each(function(module){
  904. // if (oldTemplateStyles[module.moduleName]){
  905. // module.clearTemplateStyles(oldTemplateStyles[module.moduleName]);
  906. // }
  907. // module.setStyleTemplate();
  908. // module.setAllStyles();
  909. // }.bind(this));
  910. }.bind(this))
  911. }.bind(this))
  912. }
  913. if (name=="data.viewStyles"){
  914. this.setCustomStyles();
  915. }
  916. },
  917. removeStyles: function(from, to){
  918. if (this.json.data.viewStyles[to]){
  919. Object.each(from, function(style, key){
  920. if (this.json.data.viewStyles[to][key] && this.json.data.viewStyles[to][key]==style){
  921. delete this.json.data.viewStyles[to][key];
  922. }
  923. }.bind(this));
  924. }
  925. },
  926. copyStyles: function(from, to){
  927. if (!this.json.data.viewStyles[to]) this.json.data.viewStyles[to] = {};
  928. Object.each(from, function(style, key){
  929. if (!this.json.data.viewStyles[to][key]) this.json.data.viewStyles[to][key] = style;
  930. }.bind(this));
  931. },
  932. saveAs: function(){
  933. var form = new MWF.xApplication.query.StatementDesigner.View.NewNameForm(this, {
  934. name : this.data.name + "_" + MWF.xApplication.query.StatementDesigner.LP.copy,
  935. query : this.data.query || this.data.application,
  936. queryName : this.data.queryName || this.data.applicationName
  937. }, {
  938. onSave : function( data, callback ){
  939. this._saveAs( data, callback );
  940. }.bind(this)
  941. }, {
  942. app: this.designer
  943. });
  944. form.edit()
  945. },
  946. _saveAs : function( data , callback){
  947. var _self = this;
  948. var d = this.cloneObject( this.data );
  949. d.isNewView = true;
  950. d.id = this.designer.actions.getUUID();
  951. d.name = data.name;
  952. d.alias = "";
  953. d.query = data.query;
  954. d.queryName = data.queryName;
  955. d.application = data.query;
  956. d.applicationName = data.queryName;
  957. d.pid = d.id + d.id;
  958. delete d[this.data.id+"viewFilterType"];
  959. d[d.id+"viewFilterType"]="custom";
  960. d.data.selectList.each( function( entry ){
  961. entry.id = (new MWF.widget.UUID).id;
  962. }.bind(this));
  963. this.designer.actions.saveView(d, function(json){
  964. this.designer.notice(this.designer.lp.notice.saveAs_success, "success", this.node, {"x": "left", "y": "bottom"});
  965. if (callback) callback();
  966. }.bind(this));
  967. }
  968. });
  969. MWF.xApplication.query.StatementDesigner.View.Column = new Class({
  970. Extends: MWF.xApplication.query.ViewDesigner.View.Column
  971. });
  972. MWF.xApplication.query.StatementDesigner.View.Actionbar = new Class({
  973. Extends: MWF.xApplication.query.ViewDesigner.View.Actionbar
  974. });
  975. MWF.xApplication.query.StatementDesigner.View.Paging = new Class({
  976. Extends: MWF.xApplication.query.ViewDesigner.View.Paging
  977. });