Subform.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. parentformIdList : this.getParentformIdList(),
  134. level : this.getLevel()
  135. });
  136. this.subformModule.subformSelector = this.getSubformSelector();
  137. this.subformModule.subformSelectedValue = this.getSubformSelectedValue();
  138. this.subformModule.level1Subform = this.getLevel1Subform();
  139. this.subformModule.load(this.subformData);
  140. }else{
  141. this.node.empty();
  142. this.loadIcon();
  143. }
  144. },
  145. "delete": function(e){
  146. var module = this;
  147. this.form.designer.shortcut = false;
  148. this.form.designer.confirm("warn", module.node, MWF.APPFD.LP.notice.deleteElementTitle, MWF.APPFD.LP.notice.deleteElement, 300, 120, function(){
  149. if (this.queryGetFormDataFun) module.form.removeEvent("queryGetFormData", this.queryGetFormDataFun);
  150. if (this.postGetFormDataFun) module.form.removeEvent("postGetFormData", this.postGetFormDataFun);
  151. module.destroy();
  152. module.form.selected();
  153. module.form.designer.shortcut = true;
  154. this.close();
  155. }, function(){
  156. module.form.designer.shortcut = true;
  157. this.close();
  158. }, null);
  159. },
  160. getLevel : function(){
  161. return ( this.form.options.level1 || 0 ) + 1;
  162. },
  163. getLevel1Subform : function(){
  164. return this.form.level1Subform || this;
  165. },
  166. getSubformSelector : function(){
  167. return this.subformSelector || this.form.subformSelector;
  168. },
  169. getSubformSelectedValue : function(){
  170. return this.subformSelectedValue || this.form.subformSelectedValue;
  171. },
  172. checkSubformNested : function( id ){
  173. if( this.form.options.parentformIdList ){
  174. return !this.form.options.parentformIdList.contains( id );
  175. }
  176. return true;
  177. },
  178. isSubformUnique : function( id , oldId ){
  179. if( !this.form.topform )return true;
  180. if( !this.getLevel1Subform() || !this.getLevel1Subform().json )return true;
  181. return this.form.topform.isSubformUnique( id, this.getLevel1Subform().json.id, oldId );
  182. },
  183. getParentformIdList : function(){
  184. var parentformIdList;
  185. if( this.form.options.parentformIdList ){
  186. parentformIdList = Array.clone( this.form.options.parentformIdList );
  187. parentformIdList.push( this.form.json.id )
  188. }else{
  189. parentformIdList = [ this.form.json.id ];
  190. }
  191. return parentformIdList;
  192. },
  193. refreshSubform: function(){
  194. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  195. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  196. if (this.subformData.updateTime!==json.data.updateTime){
  197. var select = null;
  198. if (this.property){
  199. select = $(this.property.data.pid+"selectSubform").getElement("select");
  200. }
  201. this.clearSubformList(this.json.subformSelected);
  202. this.reloadSubform(json.data, select, "");
  203. }
  204. }.bind(this));
  205. }
  206. },
  207. loadIcon: function(){
  208. this.iconNode = new Element("div", {
  209. "styles": this.css.iconNode
  210. }).inject(this.node);
  211. new Element("div", {
  212. "styles": this.css.iconNodeIcon
  213. }).inject(this.iconNode);
  214. new Element("div", {
  215. "styles": this.css.iconNodeText,
  216. "text": "Subform"
  217. }).inject(this.iconNode);
  218. },
  219. _loadNodeStyles: function(){
  220. this.iconNode = this.node.getElement("div").setStyles(this.css.iconNode);
  221. this.iconNode.getFirst("div").setStyles(this.css.iconNodeIcon);
  222. this.iconNode.getLast("div").setStyles(this.css.iconNodeText);
  223. },
  224. _setEditStyle: function(name, input, oldValue){
  225. if (name==="subformSelected"){
  226. if (this.json.subformSelected!==oldValue){
  227. this.redoSelectedSubform(name, input, oldValue);
  228. }
  229. }
  230. if (name==="subformType"){
  231. if (this.json.subformType!==oldValue){
  232. if (this.json.subformType !== "script"){
  233. this.redoSelectedSubform(name, $(this.property.data.pid+"selectSubform").getElement("select"), "");
  234. }
  235. if (this.json.subformType === "script"){
  236. this.subformData = null;
  237. this.clearSubformList(this.json.subformSelected);
  238. this.node.empty();
  239. this.loadIcon();
  240. }
  241. }
  242. }
  243. },
  244. redoSelectedSubform: function(name, input, oldValue){
  245. if (this.json.subformSelected==="none") this.json.subformSelected="";
  246. if (this.json.subformSelected && this.json.subformSelected!=="none"){
  247. if(input)this.subformSelector = input;
  248. if( !input )input = this.getSubformSelector();
  249. if( oldValue )this.subformSelectedValue = oldValue;
  250. if( !oldValue )oldValue = this.getSubformSelectedValue() || "";
  251. var level1Subform = this.getLevel1Subform();
  252. if( !this.checkSubformNested(this.json.subformSelected) ){
  253. //var p = level1Subform.node.getPosition(document.body);
  254. //this.form.designer.alert("error", {
  255. // "event": {
  256. // "x": p.x + 150,
  257. // "y": p.y + 80
  258. // }
  259. //}, this.form.designer.lp.subformNestedTitle, this.form.designer.lp.subformNestedInfor, 400, 120);
  260. this.form.designer.notice( this.form.designer.lp.subformNestedInfor, "error", level1Subform.node );
  261. level1Subform.json.subformSelected = oldValue;
  262. if (input) {
  263. for (var i = 0; i < input.options.length; i++) {
  264. if (input.options[i].value === oldValue || (input.options[i].value==="none" && !oldValue ) ) {
  265. input.options[i].set("selected", true);
  266. break;
  267. }
  268. }
  269. }
  270. if( !oldValue ){
  271. level1Subform.node.empty();
  272. level1Subform.loadIcon();
  273. }else{
  274. level1Subform.refreshSubform();
  275. }
  276. }else if( !this.isSubformUnique( this.json.subformSelected , oldValue ) ){
  277. //if (this.form.subformList && this.form.subformList[this.json.subformSelected] ){
  278. //var p = (input) ? input.getPosition() : this.node.getPosition();
  279. //var p = level1Subform.node.getPosition(document.body);
  280. //this.form.designer.alert("error", {
  281. // "event": {
  282. // "x": p.x+150,
  283. // "y": p.y+80
  284. // }
  285. //}, this.form.designer.lp.subformConflictTitle, this.form.designer.lp.subformConflictInfor, 400, 120);
  286. this.form.designer.notice( this.form.designer.lp.subformConflictInfor, "error", level1Subform.node );
  287. level1Subform.json.subformSelected = oldValue;
  288. if (input){
  289. for (var i=0; i<input.options.length; i++){
  290. if (input.options[i].value===oldValue || (input.options[i].value==="none" && !oldValue ) ){
  291. input.options[i].set("selected", true);
  292. break;
  293. }
  294. }
  295. }
  296. if( !oldValue ){
  297. level1Subform.node.empty();
  298. level1Subform.loadIcon();
  299. }else{
  300. level1Subform.refreshSubform();
  301. }
  302. }else{
  303. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  304. this.reloadSubform(json.data, input, oldValue);
  305. }.bind(this));
  306. }
  307. }else{
  308. this.subformData = null;
  309. this.clearSubformList(oldValue);
  310. this.node.empty();
  311. this.loadIcon();
  312. }
  313. },
  314. clearSubformList: function(formName){
  315. if (!this.form.subformList) this.form.subformList = {};
  316. if (formName) if (this.form.subformList[formName]) delete this.form.subformList[formName];
  317. if( !this.form.topform )this.form.topform = this.form;
  318. this.form.topform.clearSubformList( this.getLevel1Subform().json.id );
  319. },
  320. addSubformList: function(){
  321. if (!this.form.subformList) this.form.subformList = {};
  322. this.form.subformList[this.json.subformSelected] = Object.clone(this.subformData.json);
  323. if( !this.form.topform )this.form.topform = this.form;
  324. this.form.topform.addSubformList( this.getLevel1Subform().json.id, this.json.subformSelected );
  325. },
  326. getSubformData: function(data){
  327. var subformDataStr = null;
  328. if (this.form.options.mode !== "Mobile"){
  329. subformDataStr = data.data;
  330. }else{
  331. subformDataStr = data.mobileData;
  332. }
  333. this.subformData = null;
  334. if (subformDataStr){
  335. this.subformData = JSON.decode(MWF.decodeJsonString(subformDataStr));
  336. this.subformData.updateTime = data.updateTime;
  337. }
  338. },
  339. reloadSubform: function(data, input, oldValue){
  340. this.getSubformData(data);
  341. if (this.subformData){
  342. var oldSubformData = (this.form.subformList && oldValue) ? this.form.subformList[oldValue] : null;
  343. this.clearSubformList(oldValue);
  344. if (this.checkSubform(data, input)){
  345. this.node.empty();
  346. this.loadSubform( null );
  347. this.addSubformList();
  348. }else{
  349. if (oldSubformData){
  350. if (!this.form.subformList) this.form.subformList = {};
  351. this.form.subformList[oldValue] = oldSubformData;
  352. }else{
  353. this.clearSubformList(oldValue);
  354. this.node.empty();
  355. this.loadIcon();
  356. }
  357. this.json.subformSelected = oldValue;
  358. if (!oldValue){
  359. if (input) input.options[0].set("selected", true);
  360. }else{
  361. if (input){
  362. for (var i=0; i<input.options.length; i++){
  363. if (input.options[i].value===oldValue){
  364. input.options[i].set("selected", true);
  365. break;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }else{
  372. this.json.subformSelected = oldValue;
  373. if (input){
  374. if (!oldValue){
  375. input.options[0].set("selected", true);
  376. }else{
  377. for (var i=0; i<input.options.length; i++){
  378. if (input.options[i].value===oldValue){
  379. input.options[i].set("selected", true);
  380. break;
  381. }
  382. }
  383. }
  384. }
  385. }
  386. },
  387. regetSubformData: function(){
  388. var flag = false;
  389. if (this.json.subformSelected && this.json.subformSelected!=="none" && this.json.subformType!=="script"){
  390. MWF.Actions.get("x_processplatform_assemble_designer").getForm(this.json.subformSelected, function(json){
  391. if (!this.subformData || this.subformData.updateTime!==json.data.updateTime){
  392. this.getSubformData(json.data);
  393. flag = true;
  394. }
  395. }.bind(this), null, false);
  396. }
  397. return flag;
  398. },
  399. getConflictFields: function(){
  400. var moduleNames = [];
  401. if (this.subformData){
  402. Object.each(this.subformData.json.moduleList, function(o, key){
  403. var check = this.form.checkModuleId(key, o.type, this.subformData.json.id);
  404. if (check.fieldConflict){
  405. moduleNames.push(key)
  406. }else if (check.elementConflict){
  407. o.changeId = this.json.id+"_"+key;
  408. }
  409. }.bind(this));
  410. }
  411. return moduleNames;
  412. },
  413. checkSubform: function(data, input){
  414. var moduleNames = this.getConflictFields();
  415. // Object.each(this.subformData.json.moduleList, function(o, key){
  416. // var check = this.form.checkModuleId(key, o.type, this.subformData.json.id);
  417. // if (check.fieldConflict){
  418. // moduleNames.push(key)
  419. // }else if (check.elementConflict){
  420. // o.changeId = this.json.id+"_"+key;
  421. // }
  422. // }.bind(this));
  423. if (moduleNames.length){
  424. var txt = this.form.designer.lp.subformNameConflictInfor;
  425. txt = txt.replace("{name}", moduleNames.join(", "));
  426. //var p = (input) ? input.getPosition() : this.node.getPosition();
  427. // var p = this.node.getPosition(document.body);
  428. // this.form.designer.alert("error", {
  429. // "event": {
  430. // "x": p.x+150,
  431. // "y": p.y+80
  432. // }
  433. // }, this.form.designer.lp.subformNameConflictTitle, txt, 400, 200);
  434. this.form.designer.notice(txt, "error", this.node);
  435. return false;
  436. }
  437. return true;
  438. },
  439. loadSubform: function(data) {
  440. this.subformData.json.style = this.form.json.style;
  441. this.subformData.json.properties = this.form.json.properties;
  442. this.subformData.json.jsheader = {"code": "", "html": ""};
  443. this.subformData.json.events = {};
  444. this.subformData.json.formStyleType = this.form.json.formStyleType;
  445. //this.subformData.json.id = this.json.id;
  446. this.subformModule = new MWF.FCSubform.Form(this.form, this.node, {
  447. parentformIdList : this.getParentformIdList(),
  448. level : this.getLevel()
  449. });
  450. this.subformModule.subformSelector = this.getSubformSelector();
  451. this.subformModule.subformSelectedValue = this.getSubformSelectedValue();
  452. this.subformModule.level1Subform = this.getLevel1Subform();
  453. this.subformModule.load(this.subformData);
  454. //this.createRefreshNode();
  455. },
  456. destroy: function(){
  457. this.form.moduleList.erase(this);
  458. this.form.moduleNodeList.erase(this.node);
  459. this.form.moduleElementNodeList.erase(this.node);
  460. this.form.subformModuleList.erase(this);
  461. this.clearSubformList(this.json.subformSelected);
  462. this.node.destroy();
  463. this.actionArea.destroy();
  464. delete this.form.json.moduleList[this.json.id];
  465. this.json = null;
  466. delete this.json;
  467. this.treeNode.destroy();
  468. }
  469. });
  470. MWF.xApplication.process.FormDesigner.Module.Subform.Form = new Class({
  471. Extends: MWF.FCForm,
  472. initialize: function(form, container, options){
  473. this.setOptions(options);
  474. this.topform = form.topform || form;
  475. this.parentform = form;
  476. this.css = this.parentform.css;
  477. this.container = container;
  478. this.form = this;
  479. this.isSubform = true;
  480. this.moduleType = "subform";
  481. this.moduleList = [];
  482. this.moduleNodeList = [];
  483. this.moduleContainerNodeList = [];
  484. this.moduleElementNodeList = [];
  485. this.moduleComponentNodeList = [];
  486. // this.moduleContainerList = [];
  487. this.dataTemplate = {};
  488. this.designer = this.parentform.designer;
  489. this.selectedModules = [];
  490. },
  491. load : function(data){
  492. this.data = data;
  493. this.json = data.json;
  494. this.html = data.html;
  495. this.json.mode = this.options.mode;
  496. this.container.set("html", this.html);
  497. this.loadDomModules();
  498. //this.setCustomStyles();
  499. //this.node.setProperties(this.json.properties);
  500. //this.setNodeEvents();
  501. if (this.options.mode==="Mobile"){
  502. if (oldStyleValue) this._setEditStyle("formStyleType", null, oldStyleValue);
  503. }
  504. },
  505. loadDomModules: function(){
  506. this.node = this.container.getFirst();
  507. this.node.set("id", this.json.id);
  508. this.node.setStyles((this.options.mode==="Mobile") ? this.css.formMobileNode : this.css.formNode);
  509. this.node.store("module", this);
  510. this.loadDomTree();
  511. },
  512. loadDomTree: function(){
  513. this.createFormTreeNode();
  514. this.parseModules(this, this.node);
  515. },
  516. createFormTreeNode: function(){
  517. this.treeNode = {
  518. "insertChild": function(){return this;},
  519. "appendChild": function(){return this;},
  520. "selectNode": function(){},
  521. "node": null,
  522. "parentNode": {}
  523. };
  524. this.treeNode.module = this;
  525. }
  526. });