Datatemplate.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. MWF.xApplication.process.FormDesigner.Module = MWF.xApplication.process.FormDesigner.Module || {};
  2. MWF.xDesktop.requireApp("process.FormDesigner", "Module.$Container", null, false);
  3. MWF.xDesktop.requireApp("process.FormDesigner", "Module.$Component", null, false);
  4. MWF.xApplication.process.FormDesigner.Module.Datatemplate = MWF.FCDatatemplate = new Class({
  5. Extends: MWF.FC$Container,
  6. Implements: [Options, Events],
  7. options: {
  8. "style": "default",
  9. "propertyPath": "../x_component_process_FormDesigner/Module/Datatemplate/datatemplate.html"
  10. // "disallowModules": ["subform","subpage","widget"]
  11. },
  12. initialize: function(form, options){
  13. this.setOptions(options);
  14. this.path = "../x_component_process_FormDesigner/Module/Datatemplate/";
  15. this.cssPath = "../x_component_process_FormDesigner/Module/Datatemplate/"+this.options.style+"/css.wcss";
  16. this._loadCss();
  17. this.moduleType = "container";
  18. this.moduleName = "datatemplate";
  19. this.form = form;
  20. },
  21. _createNode: function(){
  22. this.node = this.moveNode.clone(true, true);
  23. this.node.clearStyles(false);
  24. this.node.setStyles(this.css.moduleNode);
  25. this.node.set("id", this.json.id);
  26. this.node.addEvent("selectstart", function(){
  27. return false;
  28. });
  29. this._createIcon();
  30. },
  31. _createIcon: function(){
  32. this.iconNode = new Element("div", {
  33. "styles": this.css.iconNode,
  34. "o2icon": "datatemplate"
  35. }).inject(this.node, "top");
  36. new Element("div", {
  37. "styles": this.css.iconNodeIcon
  38. }).inject(this.iconNode);
  39. new Element("div", {
  40. "styles": this.css.iconNodeText,
  41. "text": "Datatemplate"
  42. }).inject(this.iconNode);
  43. },
  44. _loadNodeStyles: function(){
  45. this.iconNode = this.node.getElement("div[o2icon='datatemplate']");
  46. if( this.iconNode ){
  47. this.iconNode.setStyles(this.css.iconNode);
  48. this.iconNode.getFirst("div").setStyles(this.css.iconNodeIcon);
  49. this.iconNode.getLast("div").setStyles(this.css.iconNodeText);
  50. }else{
  51. this._createIcon()
  52. }
  53. },
  54. createImmediately: function(data, relativeNode, position, selectDisabled){
  55. this.json = data;
  56. this.json.id = this._getNewId();
  57. this._createMoveNode();
  58. this._dragMoveComplete( relativeNode, position, selectDisabled );
  59. },
  60. _dragComplete: function(relativeNode, position, selectDisabled){
  61. if (!this.node){
  62. this.showCreateDialog(relativeNode, position, selectDisabled );
  63. }else{
  64. this._dragMoveComplete(relativeNode, position, selectDisabled);
  65. }
  66. },
  67. _dragMoveComplete: function( relativeNode, position, selectDisabled ){
  68. this.setStyleTemplate();
  69. if( this.injectNoticeNode )this.injectNoticeNode.destroy();
  70. var overflow = this.moveNode.retrieve("overflow");
  71. if( overflow ){
  72. this.moveNode.setStyle("overflow",overflow);
  73. this.moveNode.eliminate("overflow");
  74. }
  75. if (!this.node){
  76. this._createNode();
  77. }
  78. this._resetTreeNode();
  79. if( relativeNode && position ){
  80. this.node.inject( relativeNode, position );
  81. }else{
  82. this.node.inject(this.copyNode, "before");
  83. }
  84. this._initModule();
  85. var thisDisplay = this.node.retrieve("thisDisplay");
  86. if (thisDisplay){
  87. this.node.setStyle("display", thisDisplay);
  88. }
  89. if (this.copyNode) this.copyNode.destroy();
  90. if (this.moveNode) this.moveNode.destroy();
  91. this.moveNode = null;
  92. this.copyNode = null;
  93. this.nextModule = null;
  94. this.form.moveModule = null;
  95. this.form.json.moduleList[this.json.id] = this.json;
  96. if (this.form.scriptDesigner) this.form.scriptDesigner.createModuleScript(this.json);
  97. if( !selectDisabled )this.selected();
  98. if( this.operation && !this.historyAddDelay ){
  99. this.addHistoryLog( this.operation, null, this.fromLog );
  100. }
  101. if( !this.historyAddDelay ){
  102. this.operation = null;
  103. this.fromLog = null;
  104. }
  105. },
  106. _clearDragComplete : function(){
  107. if( this.dragTimeout ){
  108. window.clearTimeout( this.dragTimeout );
  109. this.dragTimeout = null;
  110. }
  111. if( this.injectNoticeNode )this.injectNoticeNode.destroy();
  112. if (this.copyNode) this.copyNode.destroy();
  113. if (this.moveNode) this.moveNode.destroy();
  114. this.moveNode = null;
  115. this.copyNode = null;
  116. this.nextModule = null;
  117. this.form.moveModule = null;
  118. // delete this;
  119. },
  120. showCreateDialog: function(relativeNode, position){
  121. var module = this;
  122. var url ="../x_component_process_FormDesigner/Module/Datatemplate/createDialog.html";
  123. MWF.require("MWF.widget.Dialog", function(){
  124. var size = $(document.body).getSize();
  125. var x = size.x/2-180;
  126. var y = size.y/2-100;
  127. var dlg = new MWF.DL({
  128. "title": "Insert",
  129. "style": "property",
  130. "top": y,
  131. "left": x-40,
  132. "fromTop":size.y/2-65,
  133. "fromLeft": size.x/2,
  134. "width": 360,
  135. "height": 200,
  136. "url": url,
  137. "lp": MWF.xApplication.process.FormDesigner.LP.propertyTemplate,
  138. "buttonList": [
  139. {
  140. "text": MWF.APPFD.LP.button.ok,
  141. "action": function(){
  142. var widthModules = "no";
  143. dlg.node.getElements(".widthModules").each( function (el) {
  144. if( el.get("checked") )widthModules = el.get("value");
  145. });
  146. if( widthModules === "yes" ){
  147. var wrapDiv = "yes";
  148. dlg.node.getElements(".wrapDiv").each( function (el) {
  149. if( el.get("checked") )wrapDiv = el.get("value");
  150. });
  151. module.appendModules(relativeNode, position, wrapDiv);
  152. }else{
  153. module._dragMoveComplete( relativeNode, position );
  154. }
  155. this.close();
  156. }
  157. },
  158. {
  159. "text": MWF.APPFD.LP.button.cancel,
  160. "action": function(){
  161. module._clearDragComplete();
  162. this.close();
  163. }
  164. }
  165. ],
  166. "onPostShow": function(){
  167. var tr = dlg.node.getElement(".wrapDivTr");
  168. dlg.node.getElements(".widthModules").addEvent("change", function (el) {
  169. tr.setStyle("display", el.target.get("value") === "yes" ? "" : "none")
  170. })
  171. }.bind(this)
  172. });
  173. dlg.show();
  174. }.bind(this));
  175. },
  176. getModulesTemplateUrl: function(){
  177. return "../x_component_process_FormDesigner/Module/Datatemplate/modulesTemplate.json";
  178. },
  179. appendModules: function( relativeNode, position, wrapDiv ){
  180. MWF.getJSON(this.getModulesTemplateUrl(), function(responseJSON, responseText){
  181. var parentModule = this.parentContainer || this.inContainer || this.onDragModule;
  182. this.containerModule = this.form.createModuleImmediately(
  183. "Div", parentModule, relativeNode || this.copyNode, position || "before", true, false);
  184. var containerNode = this.containerModule.node;
  185. // var dataStr = null;
  186. // if (this.form.options.mode !== "Mobile"){
  187. // dataStr = responseJSON.data;
  188. // }else{
  189. // dataStr = responseJSON.mobileData;
  190. // }
  191. // var data = null;
  192. // if (dataStr){
  193. // // data = JSON.decode(MWF.decodeJsonString(dataStr));
  194. // data = JSON.decode(dataStr);
  195. // }
  196. var data;
  197. if (this.form.options.mode !== "Mobile"){
  198. data = responseJSON.data;
  199. }else{
  200. data = responseJSON.mobileData;
  201. }
  202. var tmpNode = new Element("div").inject( this.form.container );
  203. tmpNode.set("html", data.html);
  204. var html = tmpNode.get("html");
  205. tmpNode.destroy();
  206. containerNode.set("html", html );
  207. //替换重复id
  208. var changedIdMap = {};
  209. var dataTemplateModuleJson;
  210. Object.each(data.json.moduleList, function (moduleJson) {
  211. if( !dataTemplateModuleJson && moduleJson.type === "Datatemplate" ){
  212. dataTemplateModuleJson = moduleJson;
  213. }
  214. var oid = moduleJson.id;
  215. var id = moduleJson.id;
  216. var idx = 1;
  217. // while (this.form.json.moduleList[id]) {
  218. // id = oid + "_" + idx;
  219. // idx++;
  220. // }
  221. while (this.form.checkModuleId(id, moduleJson.type).elementConflict){
  222. id = oid + "_" + idx;
  223. idx++;
  224. }
  225. if (oid !== id) {
  226. changedIdMap[oid] = id;
  227. moduleJson.id = id;
  228. var moduleNode = containerNode.getElementById(oid);
  229. if (moduleNode) moduleNode.set("id", id);
  230. }
  231. this.form.json.moduleList[moduleJson.id] = moduleJson;
  232. }.bind(this));
  233. if( Object.keys(changedIdMap).length > 0 ){
  234. this.form.designer.checkDatatemplateRelativeId( dataTemplateModuleJson, changedIdMap );
  235. }
  236. var moduleList = [];
  237. if( wrapDiv !== "yes" ) {
  238. var moduleNodeList = [];
  239. containerNode.getChildren().each(function (el) {
  240. if (el.get("MWFType") && el.get("id")) {
  241. moduleNodeList.push(el);
  242. var id = el.get("id");
  243. el.inject(relativeNode || this.copyNode, position || "before");
  244. }
  245. }.bind(this));
  246. this.containerModule.destroy();
  247. this._clearDragComplete();
  248. moduleNodeList.each(function (el) {
  249. var json = this.form.getDomjson(el);
  250. var module = this.form.loadModule(json, el, parentModule);
  251. moduleList.push(module);
  252. }.bind(this));
  253. this.containerModule = null;
  254. }else{
  255. this._clearDragComplete();
  256. moduleList = [this.containerModule];
  257. this.form.parseModules(this.containerModule, containerNode);
  258. }
  259. // this.form.parseModules( parentModule, parentModule.node);
  260. //this.containerModule.delete();
  261. if(dataTemplateModuleJson){
  262. var node = parentModule.node.getElementById(dataTemplateModuleJson.id);
  263. if(node && node.retrieve("module")){
  264. node.retrieve("module").selected();
  265. }
  266. }
  267. if( this.operation && !this.historyAddDelay ){
  268. this.addHistoryLog( this.operation, moduleList, this.fromLog, this.json.id, "Datatemplate" );
  269. }
  270. if( !this.historyAddDelay ){
  271. this.operation = null;
  272. this.fromLog = null;
  273. }
  274. }.bind(this));
  275. },
  276. // checkRelativeId: function( json, idMap ){
  277. // ["outerAddActionId","outerDeleteActionId","outerSelectAllId",
  278. // "addActionId","deleteActionId","sequenceId","selectorId"].each(function(key){
  279. // var str = json[key];
  280. // if(str){
  281. // var strArr;
  282. // if( str.indexOf("/") > -1 ) {
  283. // strArr = str.split("/");
  284. // }else if(str.indexOf(".*.") > -1){
  285. // strArr = str.split(".*.");
  286. // }
  287. // if(strArr){
  288. // strArr = strArr.map(function (s) {
  289. // return idMap[s] || s;
  290. // });
  291. // json[key] = strArr.join("/");
  292. // }else{
  293. // if( str && idMap[str] ){
  294. // json[key] = idMap[str];
  295. // }
  296. // }
  297. // }
  298. //
  299. // }.bind(this));
  300. // },
  301. // _getDroppableNodes: function(){
  302. // var nodes = [this.form.node].concat(this.form.moduleElementNodeList, this.form.moduleContainerNodeList, this.form.moduleComponentNodeList);
  303. // this.form.moduleList.each( function(module){
  304. // //数据模板不能往数据模板里拖
  305. // if( module.moduleName === "datatemplate" ){
  306. // var subDoms = this.form.getModuleNodes(module.node);
  307. // nodes.erase( module.node );
  308. // subDoms.each(function (dom) {
  309. // nodes.erase( dom );
  310. // })
  311. // }
  312. // }.bind(this));
  313. // return nodes;
  314. // },
  315. clearTemplateStyles: function(styles){
  316. if (styles){
  317. if (styles.styles) this.removeStyles(styles.styles, "styles");
  318. if (styles.properties) this.removeStyles(styles.properties, "properties");
  319. }
  320. },
  321. setTemplateStyles: function(styles){
  322. if (styles.styles) this.copyStyles(styles.styles, "styles");
  323. if (styles.properties) this.copyStyles(styles.properties, "properties");
  324. },
  325. _createMoveNode: function(){
  326. this.moveNode = new Element("div", {
  327. "MWFType": "datatemplate",
  328. "id": this.json.id,
  329. "styles": this.css.moduleNodeMove,
  330. "events": {
  331. "selectstart": function(){
  332. return false;
  333. }
  334. }
  335. }).inject(this.form.container);
  336. },
  337. _initModule: function(){
  338. if (!this.initialized){
  339. if (this.json.initialized!=="yes")this.setStyleTemplate();
  340. // this._getElements();
  341. // this._getContainers();
  342. this.setPropertiesOrStyles("styles");
  343. this.setPropertiesOrStyles("properties");
  344. if( !this.json.impExpTableStyles ){
  345. this.setImpExpTableStyles()
  346. }
  347. this._setNodeProperty();
  348. if (!this.form.isSubform) this._createIconAction();
  349. // this.checkSequenceShow();
  350. this._setNodeEvent();
  351. this.initialized = true;
  352. this.json.initialized = "yes";
  353. }
  354. },
  355. setImpExpTableStyles: function(){
  356. //设置导入导出表格样式
  357. if(this.form.templateStyles && this.form.templateStyles.table){
  358. this.json.impExpTableStyles = Object.merge(this.json.impExpTableStyles||{}, this.form.templateStyles.table.styles||{});
  359. this.json.impExpTableTitleStyles = Object.merge(this.json.impExpTableTitleStyles||{}, this.form.templateStyles.table.titleStyles||{});
  360. this.json.impExpTableContentStyles = Object.merge(this.json.impExpTableContentStyles||{}, this.form.templateStyles.table.contentStyles||{});
  361. this.json.impExpTableProperties = Object.merge(this.json.impExpTableProperties||{}, this.form.templateStyles.table.properties||{});
  362. }
  363. },
  364. setPropertiesOrStyles: function(name){
  365. if (name=="styles"){
  366. try{
  367. this.setCustomStyles();
  368. }catch(e){}
  369. var border = this.node.getStyle("border");
  370. this.node.clearStyles();
  371. this.node.setStyles(this.css.moduleNode);
  372. this.node.setStyle("border", border);
  373. Object.each(this.json.styles, function(value, key){
  374. var reg = /^border\w*/ig;
  375. if (!key.test(reg)){
  376. this.node.setStyle(key, value);
  377. }
  378. }.bind(this));
  379. }
  380. if (name=="properties"){
  381. try{
  382. this.setCustomProperties();
  383. }catch(e){}
  384. this.node.setProperties(this.json.properties);
  385. }
  386. },
  387. _setEditStyle_custom: function(name, obj, oldValue){
  388. if (name=="id"){
  389. if (name!=oldValue){
  390. // var reg = new RegExp("^"+oldValue, "i");
  391. // this.containers.each(function(container){
  392. // var id = container.json.id;
  393. // var newId = id.replace(reg, this.json.id);
  394. // container.json.id = newId;
  395. //
  396. // delete this.form.json.moduleList[id];
  397. // this.form.json.moduleList[newId] = container.json;
  398. // container._setEditStyle("id");
  399. // }.bind(this));
  400. }
  401. }
  402. //if (name=="sequence") this.checkSequenceShow();
  403. },
  404. // _dragIn: function(module){
  405. // if (this.options.disallowModules.indexOf(module.moduleName)===-1){
  406. // module.onDragModule = this;
  407. // if (!this.Component) module.inContainer = this;
  408. // module.parentContainer = this;
  409. // module.nextModule = null;
  410. //
  411. // this.node.setStyles({"border": "1px solid #ffa200"});
  412. //
  413. // if (module.controlMode){
  414. // if (module.copyNode) module.copyNode.hide();
  415. // }else{
  416. // var copyNode = module._getCopyNode(this);
  417. // copyNode.show();
  418. // copyNode.inject(this.node);
  419. // }
  420. // }else{
  421. // this.parentContainer._dragIn(module);
  422. // }
  423. // },
  424. setAllStyles: function(){
  425. this.setPropertiesOrStyles("styles");
  426. this.setPropertiesOrStyles("properties");
  427. this.setImpExpTableStyles();
  428. this.reloadMaplist();
  429. },
  430. getContainerNodes: function(){
  431. return this.node.getElements("td");
  432. },
  433. copyComponentJsonData: function(newNode, pid){
  434. var tds = newNode.getElements("td");
  435. var ths = newNode.getElements("th");
  436. tds.each(function(td, idx){
  437. var newContainerJson = Object.clone(this.containers[idx].json);
  438. newContainerJson.id = this.containers[idx]._getNewId(pid);
  439. this.form.json.moduleList[newContainerJson.id] = newContainerJson;
  440. td.set("id", newContainerJson.id);
  441. }.bind(this));
  442. ths.each(function(th, idx){
  443. var newElementJson = Object.clone(this.elements[idx].json);
  444. newElementJson.id = this.elements[idx]._getNewId(pid);
  445. this.form.json.moduleList[newElementJson.id] = newElementJson;
  446. th.set("id", newElementJson.id);
  447. }.bind(this));
  448. },
  449. _getDirectSubModuleJson: function(node, moduleJsons){
  450. var subNode = node.getFirst();
  451. while (subNode){
  452. var module = subNode.retrieve("module");
  453. var flag = module && !["datatable","datagrid","datatemplate"].contains(module.moduleName);
  454. if (flag) {
  455. moduleJsons[module.json.id] = Object.clone(module.json);
  456. }
  457. if( !module || flag){
  458. this._getDirectSubModuleJson(subNode, moduleJsons);
  459. }
  460. subNode = subNode.getNext();
  461. }
  462. },
  463. getExpImpFieldJson: function () {
  464. var o = {};
  465. var list = [];
  466. this._getDirectSubModuleJson(this.node, o);
  467. for( var key in o ){
  468. var json = o[key];
  469. if(this.form.options.fields.contains(json.type) && !["Office"].contains(json.type)){
  470. list.push({
  471. "field": json.id,
  472. "title": json.name || ""
  473. });
  474. }
  475. }
  476. return list;
  477. },
  478. changeRelativeId: function (idMap) {
  479. this.checkRelativeId(this.json, idMap);
  480. }
  481. });