Subform.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. MWF.xApplication.process.FormDesigner.Module = MWF.xApplication.process.FormDesigner.Module || {};
  2. MWF.xDesktop.requireApp("process.FormDesigner", "Module.$Element", null, false);
  3. MWF.xApplication.process.FormDesigner.Module.Subform = MWF.FCSubform = new Class({
  4. Extends: MWF.FC$Element,
  5. Implements: [Options, Events],
  6. options: {
  7. "style": "default",
  8. "propertyPath": "../x_component_process_FormDesigner/Module/Subform/subform.html",
  9. "actions": [
  10. {
  11. "name": "move",
  12. "icon": "move1.png",
  13. "event": "mousedown",
  14. "action": "move",
  15. "title": MWF.APPFD.LP.formAction.move
  16. },
  17. {
  18. "name": "delete",
  19. "icon": "delete1.png",
  20. "event": "click",
  21. "action": "delete",
  22. "title": MWF.APPFD.LP.formAction["delete"]
  23. }
  24. // {
  25. // "name": "styleBrush",
  26. // "icon": "styleBrush.png",
  27. // "event": "click",
  28. // "action": "styleBrush",
  29. // "title": MWF.APPFD.LP.formAction["styleBrush"]
  30. // }
  31. ]
  32. },
  33. initialize: function(form, options){
  34. this.setOptions(options);
  35. this.path = "../x_component_process_FormDesigner/Module/Subform/";
  36. this.cssPath = "../x_component_process_FormDesigner/Module/Subform/"+this.options.style+"/css.wcss";
  37. this._loadCss();
  38. this.moduleType = "element";
  39. this.moduleName = "subform";
  40. this.form = form;
  41. this.container = null;
  42. this.containerNode = null;
  43. },
  44. load : function(json, node, parent){
  45. this.json = json;
  46. this.node= node;
  47. this.node.store("module", this);
  48. //this.node.empty();
  49. this.node.setStyles(this.css.moduleNode);
  50. //this._loadNodeStyles();
  51. this._initModule();
  52. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  53. this.redoSelectedSubform(this.json.subformSelected, null, "");
  54. }else{
  55. this.node.empty();
  56. this.loadIcon();
  57. }
  58. this._loadTreeNode(parent);
  59. this.setCustomStyles();
  60. this.parentContainer = this.treeNode.parentNode.module;
  61. this._setEditStyle_custom("id");
  62. this.parseModules();
  63. this.json.moduleName = this.moduleName;
  64. this.node.addEvent("click", function(){
  65. this.refreshSubform();
  66. }.bind(this));
  67. this.node.addEvent("dblclick", function(e){
  68. this.openSubform(e);
  69. }.bind(this));
  70. },
  71. _initModule: function(){
  72. if (!this.json.isSaved) this.setStyleTemplate();
  73. this.setPropertiesOrStyles("styles");
  74. this.setPropertiesOrStyles("inputStyles");
  75. this.setPropertiesOrStyles("properties");
  76. this._setNodeProperty();
  77. if (!this.form.isSubform) this._createIconAction();
  78. this._setNodeEvent();
  79. this.json.isSaved = true;
  80. if( !this.form.subformModuleList )this.form.subformModuleList = [];
  81. this.form.subformModuleList.push( this );
  82. this.queryGetFormDataFun = this.queryGetFormData.bind(this);
  83. this.postGetFormDataFun = this.postGetFormData.bind(this);
  84. this.form.addEvent("queryGetFormData", this.queryGetFormDataFun);
  85. this.form.addEvent("postGetFormData", this.postGetFormDataFun);
  86. },
  87. openSubform: function(e){
  88. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  89. layout.desktop.openApplication(e, "process.FormDesigner", {"id": this.json.subformSelected, "appId": "FormDesigner"+this.json.subformSelected});
  90. }
  91. },
  92. _createMoveNode: function(){
  93. this.moveNode = new Element("div", {
  94. "MWFType": "subform",
  95. "id": this.json.id,
  96. "styles": this.css.moduleNodeMove,
  97. "events": {
  98. "selectstart": function(){
  99. return false;
  100. }
  101. }
  102. }).inject(this.form.container);
  103. },
  104. _createNode: function(){
  105. this.node = this.moveNode.clone(true, true);
  106. this.node.setStyles(this.css.moduleNode);
  107. this.node.set("id", this.json.id);
  108. this.node.addEvent("selectstart", function(){
  109. return false;
  110. });
  111. // debugger;
  112. // if (this.json.subformSelected && this.json.subformSelected!="none" && this.json.subformType!=="script"){
  113. // this.redoSelectedSubform(this.json.subformSelected, $(this.property.data.pid+"selectSubform").getElement("select"), "");
  114. // }else{
  115. this.loadIcon();
  116. // }
  117. this.node.addEvent("click", function(){
  118. this.refreshSubform();
  119. }.bind(this));
  120. },
  121. postGetFormData: function(node){
  122. if (!node || node.contains(this.node)) this.show();
  123. },
  124. queryGetFormData: function(node){
  125. if (!node || node.contains(this.node)) this.hide();
  126. },
  127. hide: function(){
  128. this.node.empty();
  129. },
  130. show: function(){
  131. if (this.subformData){
  132. this.subformModule = new MWF.FCSubform.Form(this.form, this.node, {
  133. mode : this.form.options.mode,
  134. parentformIdList : this.getParentformIdList(),
  135. level : this.getLevel()
  136. });
  137. this.subformModule.subformSelector = this.getSubformSelector();
  138. this.subformModule.subformSelectedValue = this.getSubformSelectedValue();
  139. this.subformModule.level1Subform = this.getLevel1Subform();
  140. this.subformModule.load(this.subformData);
  141. }else{
  142. this.node.empty();
  143. this.loadIcon();
  144. }
  145. },
  146. "delete": function(e){
  147. var module = this;
  148. this.form.designer.shortcut = false;
  149. this.form.designer.confirm("warn", module.node, MWF.APPFD.LP.notice.deleteElementTitle, MWF.APPFD.LP.notice.deleteElement, 300, 120, function(){
  150. if (this.queryGetFormDataFun) module.form.removeEvent("queryGetFormData", this.queryGetFormDataFun);
  151. if (this.postGetFormDataFun) module.form.removeEvent("postGetFormData", this.postGetFormDataFun);
  152. module.destroy();
  153. module.form.selected();
  154. module.form.designer.shortcut = true;
  155. this.close();
  156. }, function(){
  157. module.form.designer.shortcut = true;
  158. this.close();
  159. }, null);
  160. },
  161. getLevel : function(){
  162. return ( this.form.options.level1 || 0 ) + 1;
  163. },
  164. getLevel1Subform : function(){
  165. return this.form.level1Subform || this;
  166. },
  167. getSubformSelector : function(){
  168. return this.subformSelector || this.form.subformSelector;
  169. },
  170. getSubformSelectedValue : function(){
  171. return this.subformSelectedValue || this.form.subformSelectedValue;
  172. },
  173. checkSubformNested : function( id ){
  174. if( this.form.options.parentformIdList ){
  175. return !this.form.options.parentformIdList.contains( id );
  176. }
  177. return true;
  178. },
  179. isSubformUnique : function( id , oldId ){
  180. if( !this.form.topform )return true;
  181. if( !this.getLevel1Subform() || !this.getLevel1Subform().json )return true;
  182. return this.form.topform.isSubformUnique( id, this.getLevel1Subform().json.id, oldId );
  183. },
  184. getParentformIdList : function(){
  185. var parentformIdList;
  186. if( this.form.options.parentformIdList ){
  187. parentformIdList = Array.clone( this.form.options.parentformIdList );
  188. parentformIdList.push( this.form.json.id )
  189. }else{
  190. parentformIdList = [ this.form.json.id ];
  191. }
  192. return parentformIdList;
  193. },
  194. refreshSubform: function(){
  195. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  196. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  197. if (this.subformData.updateTime!==json.data.updateTime){
  198. var select = null;
  199. if (this.property){
  200. select = $(this.property.data.pid+"selectSubform").getElement("select");
  201. }
  202. this.clearSubformList(this.json.subformSelected);
  203. this.reloadSubform(json.data, select, "");
  204. }
  205. }.bind(this));
  206. }
  207. },
  208. loadIcon: function(){
  209. this.iconNode = new Element("div", {
  210. "styles": this.css.iconNode
  211. }).inject(this.node);
  212. new Element("div", {
  213. "styles": this.css.iconNodeIcon
  214. }).inject(this.iconNode);
  215. new Element("div", {
  216. "styles": this.css.iconNodeText,
  217. "text": "Subform"
  218. }).inject(this.iconNode);
  219. },
  220. _loadNodeStyles: function(){
  221. this.iconNode = this.node.getElement("div").setStyles(this.css.iconNode);
  222. this.iconNode.getFirst("div").setStyles(this.css.iconNodeIcon);
  223. this.iconNode.getLast("div").setStyles(this.css.iconNodeText);
  224. },
  225. _setEditStyle_custom : function(name, input, oldValue){
  226. if (name==="subformSelected"){
  227. if (this.json.subformSelected!==oldValue){
  228. this.redoSelectedSubform(name, input, oldValue);
  229. }
  230. }
  231. if (name==="subformType"){
  232. if (this.json.subformType!==oldValue){
  233. if (this.json.subformType !== "script"){
  234. this.redoSelectedSubform(name, $(this.property.data.pid+"selectSubform").getElement("select"), "");
  235. }
  236. if (this.json.subformType === "script"){
  237. this.subformData = null;
  238. this.clearSubformList(this.json.subformSelected);
  239. this.node.empty();
  240. this.loadIcon();
  241. }
  242. }
  243. }
  244. },
  245. redoSelectedSubform: function(name, input, oldValue){
  246. if (this.json.subformSelected==="none") this.json.subformSelected="";
  247. if (this.json.subformSelected && this.json.subformSelected!=="none"){
  248. if(input)this.subformSelector = input;
  249. if( !input )input = this.getSubformSelector();
  250. if( oldValue )this.subformSelectedValue = oldValue;
  251. if( !oldValue )oldValue = this.getSubformSelectedValue() || "";
  252. var level1Subform = this.getLevel1Subform();
  253. if( !this.checkSubformNested(this.json.subformSelected) ){
  254. //var p = level1Subform.node.getPosition(document.body);
  255. //this.form.designer.alert("error", {
  256. // "event": {
  257. // "x": p.x + 150,
  258. // "y": p.y + 80
  259. // }
  260. //}, this.form.designer.lp.subformNestedTitle, this.form.designer.lp.subformNestedInfor, 400, 120);
  261. this.form.designer.notice( this.form.designer.lp.subformNestedInfor, "error", level1Subform.node );
  262. level1Subform.json.subformSelected = oldValue;
  263. if (input) {
  264. for (var i = 0; i < input.options.length; i++) {
  265. if (input.options[i].value === oldValue || (input.options[i].value==="none" && !oldValue ) ) {
  266. input.options[i].set("selected", true);
  267. break;
  268. }
  269. }
  270. }
  271. if( !oldValue ){
  272. level1Subform.node.empty();
  273. level1Subform.loadIcon();
  274. }else{
  275. level1Subform.refreshSubform();
  276. }
  277. }else if( !this.isSubformUnique( this.json.subformSelected , oldValue ) ){
  278. //if (this.form.subformList && this.form.subformList[this.json.subformSelected] ){
  279. //var p = (input) ? input.getPosition() : this.node.getPosition();
  280. //var p = level1Subform.node.getPosition(document.body);
  281. //this.form.designer.alert("error", {
  282. // "event": {
  283. // "x": p.x+150,
  284. // "y": p.y+80
  285. // }
  286. //}, this.form.designer.lp.subformConflictTitle, this.form.designer.lp.subformConflictInfor, 400, 120);
  287. this.form.designer.notice( this.form.designer.lp.subformConflictInfor, "error", level1Subform.node );
  288. level1Subform.json.subformSelected = oldValue;
  289. if (input){
  290. for (var i=0; i<input.options.length; i++){
  291. if (input.options[i].value===oldValue || (input.options[i].value==="none" && !oldValue ) ){
  292. input.options[i].set("selected", true);
  293. break;
  294. }
  295. }
  296. }
  297. if( !oldValue ){
  298. level1Subform.node.empty();
  299. level1Subform.loadIcon();
  300. }else{
  301. level1Subform.refreshSubform();
  302. }
  303. }else{
  304. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  305. this.reloadSubform(json.data, input, oldValue);
  306. }.bind(this));
  307. }
  308. }else{
  309. this.subformData = null;
  310. this.clearSubformList(oldValue);
  311. this.node.empty();
  312. this.loadIcon();
  313. }
  314. },
  315. clearSubformList: function(formName){
  316. if (!this.form.subformList) this.form.subformList = {};
  317. if (formName) if (this.form.subformList[formName]) delete this.form.subformList[formName];
  318. if( !this.form.topform )this.form.topform = this.form;
  319. this.form.topform.clearSubformList( this.getLevel1Subform().json.id );
  320. },
  321. addSubformList: function(){
  322. if (!this.form.subformList) this.form.subformList = {};
  323. this.form.subformList[this.json.subformSelected] = Object.clone(this.subformData.json);
  324. if( !this.form.topform )this.form.topform = this.form;
  325. this.form.topform.addSubformList( this.getLevel1Subform().json.id, this.json.subformSelected );
  326. },
  327. getSubformData: function(data){
  328. var subformDataStr = null;
  329. if (this.form.options.mode !== "Mobile"){
  330. subformDataStr = data.data;
  331. }else{
  332. subformDataStr = data.mobileData;
  333. }
  334. this.subformData = null;
  335. if (subformDataStr){
  336. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  337. this.subformData.updateTime = data.updateTime;
  338. }
  339. },
  340. reloadSubform: function(data, input, oldValue){
  341. this.getSubformData(data);
  342. if (this.subformData){
  343. var oldSubformData = (this.form.subformList && oldValue) ? this.form.subformList[oldValue] : null;
  344. this.clearSubformList(oldValue);
  345. if (this.checkSubform(data, input)){
  346. this.node.empty();
  347. this.loadSubform( null );
  348. this.addSubformList();
  349. }else{
  350. if (oldSubformData){
  351. if (!this.form.subformList) this.form.subformList = {};
  352. this.form.subformList[oldValue] = oldSubformData;
  353. }else{
  354. this.clearSubformList(oldValue);
  355. this.node.empty();
  356. this.loadIcon();
  357. }
  358. this.json.subformSelected = oldValue;
  359. if (!oldValue){
  360. if (input) input.options[0].set("selected", true);
  361. }else{
  362. if (input){
  363. for (var i=0; i<input.options.length; i++){
  364. if (input.options[i].value===oldValue){
  365. input.options[i].set("selected", true);
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. }else{
  373. this.json.subformSelected = oldValue;
  374. if (input){
  375. if (!oldValue){
  376. input.options[0].set("selected", true);
  377. }else{
  378. for (var i=0; i<input.options.length; i++){
  379. if (input.options[i].value===oldValue){
  380. input.options[i].set("selected", true);
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. },
  388. regetSubformData: function(){
  389. var flag = false;
  390. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  391. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  392. if (!this.subformData || this.subformData.updateTime!==json.data.updateTime){
  393. this.getSubformData(json.data);
  394. flag = true;
  395. }
  396. }.bind(this), null, false);
  397. }
  398. return flag;
  399. },
  400. getConflictFields: function(){
  401. var moduleNames = [];
  402. if (this.subformData){
  403. Object.each(this.subformData.json.moduleList, function(o, key){
  404. var check = this.form.checkModuleId(key, o.type, this.subformData.json.id);
  405. if (check.fieldConflict){
  406. moduleNames.push(key)
  407. }else if (check.elementConflict){
  408. o.changeId = this.json.id+"_"+key;
  409. }
  410. }.bind(this));
  411. }
  412. return moduleNames;
  413. },
  414. checkSubform: function(data, input){
  415. var moduleNames = this.getConflictFields();
  416. // Object.each(this.subformData.json.moduleList, function(o, key){
  417. // var check = this.form.checkModuleId(key, o.type, this.subformData.json.id);
  418. // if (check.fieldConflict){
  419. // moduleNames.push(key)
  420. // }else if (check.elementConflict){
  421. // o.changeId = this.json.id+"_"+key;
  422. // }
  423. // }.bind(this));
  424. if (moduleNames.length){
  425. var txt = this.form.designer.lp.subformNameConflictInfor;
  426. txt = txt.replace("{name}", moduleNames.join(", "));
  427. //var p = (input) ? input.getPosition() : this.node.getPosition();
  428. // var p = this.node.getPosition(document.body);
  429. // this.form.designer.alert("error", {
  430. // "event": {
  431. // "x": p.x+150,
  432. // "y": p.y+80
  433. // }
  434. // }, this.form.designer.lp.subformNameConflictTitle, txt, 400, 200);
  435. this.form.designer.notice(txt, "error", this.node);
  436. return false;
  437. }
  438. return true;
  439. },
  440. loadSubform: function(data) {
  441. this.subformData.json.style = this.form.json.style;
  442. this.subformData.json.properties = this.form.json.properties;
  443. this.subformData.json.jsheader = {"code": "", "html": ""};
  444. this.subformData.json.events = {};
  445. this.subformData.json.formStyleType = this.form.json.formStyleType;
  446. //this.subformData.json.id = this.json.id;
  447. this.subformModule = new MWF.FCSubform.Form(this.form, this.node, {
  448. mode : this.form.options.mode,
  449. parentformIdList : this.getParentformIdList(),
  450. level : this.getLevel()
  451. });
  452. this.subformModule.subformSelector = this.getSubformSelector();
  453. this.subformModule.subformSelectedValue = this.getSubformSelectedValue();
  454. this.subformModule.level1Subform = this.getLevel1Subform();
  455. this.subformModule.load(this.subformData);
  456. //this.createRefreshNode();
  457. },
  458. destroy: function(){
  459. this.form.moduleList.erase(this);
  460. this.form.moduleNodeList.erase(this.node);
  461. this.form.moduleElementNodeList.erase(this.node);
  462. this.form.subformModuleList.erase(this);
  463. this.clearSubformList(this.json.subformSelected);
  464. this.node.destroy();
  465. this.actionArea.destroy();
  466. delete this.form.json.moduleList[this.json.id];
  467. this.json = null;
  468. delete this.json;
  469. this.treeNode.destroy();
  470. }
  471. });
  472. MWF.xApplication.process.FormDesigner.Module.Subform.Form = new Class({
  473. Extends: MWF.FCForm,
  474. initialize: function(form, container, options){
  475. this.setOptions(options);
  476. this.topform = form.topform || form;
  477. this.parentform = form;
  478. this.css = this.parentform.css;
  479. this.container = container;
  480. this.form = this;
  481. this.isSubform = true;
  482. this.moduleType = "subform";
  483. this.moduleList = [];
  484. this.moduleNodeList = [];
  485. this.moduleContainerNodeList = [];
  486. this.moduleElementNodeList = [];
  487. this.moduleComponentNodeList = [];
  488. // this.moduleContainerList = [];
  489. this.dataTemplate = {};
  490. this.designer = this.parentform.designer;
  491. this.selectedModules = [];
  492. },
  493. load : function(data){
  494. this.data = data;
  495. this.json = data.json;
  496. this.html = data.html;
  497. this.json.mode = this.options.mode;
  498. this.container.set("html", this.html);
  499. this.loadDomModules();
  500. //this.setCustomStyles();
  501. //this.node.setProperties(this.json.properties);
  502. //this.setNodeEvents();
  503. if (this.options.mode==="Mobile"){
  504. //if (oldStyleValue) this._setEditStyle("formStyleType", null, oldStyleValue);
  505. }
  506. },
  507. loadDomModules: function(){
  508. this.node = this.container.getFirst();
  509. this.node.set("id", this.json.id);
  510. this.node.setStyles((this.options.mode==="Mobile") ? this.css.formMobileNode : this.css.formNode);
  511. this.node.store("module", this);
  512. this.loadDomTree();
  513. },
  514. loadDomTree: function(){
  515. this.createFormTreeNode();
  516. this.parseModules(this, this.node);
  517. },
  518. createFormTreeNode: function(){
  519. this.treeNode = {
  520. "insertChild": function(){return this;},
  521. "appendChild": function(){return this;},
  522. "selectNode": function(){},
  523. "node": null,
  524. "parentNode": {}
  525. };
  526. this.treeNode.module = this;
  527. }
  528. });