Datatemplate.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. debugger;
  181. MWF.getJSON(this.getModulesTemplateUrl(), function(responseJSON, responseText){
  182. var parentModule = this.parentContainer || this.inContainer || this.onDragModule;
  183. this.containerModule = this.form.createModuleImmediately(
  184. "Div", parentModule, relativeNode || this.copyNode, position || "before", true, false);
  185. var containerNode = this.containerModule.node;
  186. // var dataStr = null;
  187. // if (this.form.options.mode !== "Mobile"){
  188. // dataStr = responseJSON.data;
  189. // }else{
  190. // dataStr = responseJSON.mobileData;
  191. // }
  192. // var data = null;
  193. // if (dataStr){
  194. // // data = JSON.decode(MWF.decodeJsonString(dataStr));
  195. // data = JSON.decode(dataStr);
  196. // }
  197. var data;
  198. if (this.form.options.mode !== "Mobile"){
  199. data = responseJSON.data;
  200. }else{
  201. data = responseJSON.mobileData;
  202. }
  203. var tmpNode = new Element("div").inject( this.form.container );
  204. tmpNode.set("html", data.html);
  205. var html = tmpNode.get("html");
  206. tmpNode.destroy();
  207. containerNode.set("html", html );
  208. //替换重复id
  209. var changedIdMap = {};
  210. var dataTemplateModuleJson;
  211. Object.each(data.json.moduleList, function (moduleJson) {
  212. if( !dataTemplateModuleJson && moduleJson.type === "Datatemplate" ){
  213. dataTemplateModuleJson = moduleJson;
  214. }
  215. var oid = moduleJson.id;
  216. var id = moduleJson.id;
  217. var idx = 1;
  218. // while (this.form.json.moduleList[id]) {
  219. // id = oid + "_" + idx;
  220. // idx++;
  221. // }
  222. while (this.form.checkModuleId(id, moduleJson.type).elementConflict){
  223. id = oid + "_" + idx;
  224. idx++;
  225. }
  226. if (oid !== id) {
  227. changedIdMap[oid] = id;
  228. moduleJson.id = id;
  229. var moduleNode = containerNode.getElementById(oid);
  230. if (moduleNode) moduleNode.set("id", id);
  231. }
  232. this.form.json.moduleList[moduleJson.id] = moduleJson;
  233. }.bind(this));
  234. if( Object.keys(changedIdMap).length > 0 ){
  235. this.form.designer.checkDatatemplateRelativeId( dataTemplateModuleJson, changedIdMap );
  236. }
  237. var moduleList = [];
  238. if( wrapDiv !== "yes" ) {
  239. var moduleNodeList = [];
  240. containerNode.getChildren().each(function (el) {
  241. if (el.get("MWFType") && el.get("id")) {
  242. moduleNodeList.push(el);
  243. var id = el.get("id");
  244. el.inject(relativeNode || this.copyNode, position || "before");
  245. }
  246. }.bind(this));
  247. this.containerModule.destroy();
  248. this._clearDragComplete();
  249. moduleNodeList.each(function (el) {
  250. var json = this.form.getDomjson(el);
  251. var module = this.form.loadModule(json, el, parentModule);
  252. moduleList.push(module);
  253. }.bind(this));
  254. this.containerModule = null;
  255. }else{
  256. this._clearDragComplete();
  257. moduleList = [this.containerModule];
  258. this.form.parseModules(this.containerModule, containerNode);
  259. }
  260. // this.form.parseModules( parentModule, parentModule.node);
  261. //this.containerModule.delete();
  262. if(dataTemplateModuleJson){
  263. var node = parentModule.node.getElementById(dataTemplateModuleJson.id);
  264. if(node && node.retrieve("module")){
  265. node.retrieve("module").selected();
  266. }
  267. }
  268. if( this.operation && !this.historyAddDelay ){
  269. this.addHistoryLog( this.operation, moduleList, this.fromLog, this.json.id, "Datatemplate" );
  270. }
  271. if( !this.historyAddDelay ){
  272. this.operation = null;
  273. this.fromLog = null;
  274. }
  275. }.bind(this));
  276. },
  277. // checkRelativeId: function( json, idMap ){
  278. // ["outerAddActionId","outerDeleteActionId","outerSelectAllId",
  279. // "addActionId","deleteActionId","sequenceId","selectorId"].each(function(key){
  280. // var str = json[key];
  281. // if(str){
  282. // var strArr;
  283. // if( str.indexOf("/") > -1 ) {
  284. // strArr = str.split("/");
  285. // }else if(str.indexOf(".*.") > -1){
  286. // strArr = str.split(".*.");
  287. // }
  288. // if(strArr){
  289. // strArr = strArr.map(function (s) {
  290. // return idMap[s] || s;
  291. // });
  292. // json[key] = strArr.join("/");
  293. // }else{
  294. // if( str && idMap[str] ){
  295. // json[key] = idMap[str];
  296. // }
  297. // }
  298. // }
  299. //
  300. // }.bind(this));
  301. // },
  302. // _getDroppableNodes: function(){
  303. // var nodes = [this.form.node].concat(this.form.moduleElementNodeList, this.form.moduleContainerNodeList, this.form.moduleComponentNodeList);
  304. // this.form.moduleList.each( function(module){
  305. // //数据模板不能往数据模板里拖
  306. // if( module.moduleName === "datatemplate" ){
  307. // var subDoms = this.form.getModuleNodes(module.node);
  308. // nodes.erase( module.node );
  309. // subDoms.each(function (dom) {
  310. // nodes.erase( dom );
  311. // })
  312. // }
  313. // }.bind(this));
  314. // return nodes;
  315. // },
  316. clearTemplateStyles: function(styles){
  317. if (styles){
  318. if (styles.styles) this.removeStyles(styles.styles, "styles");
  319. if (styles.properties) this.removeStyles(styles.properties, "properties");
  320. }
  321. },
  322. setTemplateStyles: function(styles){
  323. if (styles.styles) this.copyStyles(styles.styles, "styles");
  324. if (styles.properties) this.copyStyles(styles.properties, "properties");
  325. },
  326. _createMoveNode: function(){
  327. this.moveNode = new Element("div", {
  328. "MWFType": "datatemplate",
  329. "id": this.json.id,
  330. "styles": this.css.moduleNodeMove,
  331. "events": {
  332. "selectstart": function(){
  333. return false;
  334. }
  335. }
  336. }).inject(this.form.container);
  337. },
  338. _initModule: function(){
  339. if (!this.initialized){
  340. if (this.json.initialized!=="yes")this.setStyleTemplate();
  341. // this._getElements();
  342. // this._getContainers();
  343. this.setPropertiesOrStyles("styles");
  344. this.setPropertiesOrStyles("properties");
  345. if( !this.json.impExpTableStyles ){
  346. this.setImpExpTableStyles()
  347. }
  348. this._setNodeProperty();
  349. if (!this.form.isSubform) this._createIconAction();
  350. // this.checkSequenceShow();
  351. this._setNodeEvent();
  352. this.initialized = true;
  353. this.json.initialized = "yes";
  354. }
  355. },
  356. setImpExpTableStyles: function(){
  357. //设置导入导出表格样式
  358. if(this.form.templateStyles && this.form.templateStyles.table){
  359. this.json.impExpTableStyles = Object.merge(this.json.impExpTableStyles||{}, this.form.templateStyles.table.styles||{});
  360. this.json.impExpTableTitleStyles = Object.merge(this.json.impExpTableTitleStyles||{}, this.form.templateStyles.table.titleStyles||{});
  361. this.json.impExpTableContentStyles = Object.merge(this.json.impExpTableContentStyles||{}, this.form.templateStyles.table.contentStyles||{});
  362. this.json.impExpTableProperties = Object.merge(this.json.impExpTableProperties||{}, this.form.templateStyles.table.properties||{});
  363. }
  364. },
  365. setPropertiesOrStyles: function(name){
  366. if (name=="styles"){
  367. try{
  368. this.setCustomStyles();
  369. }catch(e){}
  370. var border = this.node.getStyle("border");
  371. this.node.clearStyles();
  372. this.node.setStyles(this.css.moduleNode);
  373. this.node.setStyle("border", border);
  374. Object.each(this.json.styles, function(value, key){
  375. var reg = /^border\w*/ig;
  376. if (!key.test(reg)){
  377. this.node.setStyle(key, value);
  378. }
  379. }.bind(this));
  380. }
  381. if (name=="properties"){
  382. try{
  383. this.setCustomProperties();
  384. }catch(e){}
  385. this.node.setProperties(this.json.properties);
  386. }
  387. },
  388. _setEditStyle_custom: function(name, obj, oldValue){
  389. if (name=="id"){
  390. if (name!=oldValue){
  391. // var reg = new RegExp("^"+oldValue, "i");
  392. // this.containers.each(function(container){
  393. // var id = container.json.id;
  394. // var newId = id.replace(reg, this.json.id);
  395. // container.json.id = newId;
  396. //
  397. // delete this.form.json.moduleList[id];
  398. // this.form.json.moduleList[newId] = container.json;
  399. // container._setEditStyle("id");
  400. // }.bind(this));
  401. }
  402. }
  403. //if (name=="sequence") this.checkSequenceShow();
  404. },
  405. // _dragIn: function(module){
  406. // if (this.options.disallowModules.indexOf(module.moduleName)===-1){
  407. // module.onDragModule = this;
  408. // if (!this.Component) module.inContainer = this;
  409. // module.parentContainer = this;
  410. // module.nextModule = null;
  411. //
  412. // this.node.setStyles({"border": "1px solid #ffa200"});
  413. //
  414. // if (module.controlMode){
  415. // if (module.copyNode) module.copyNode.hide();
  416. // }else{
  417. // var copyNode = module._getCopyNode(this);
  418. // copyNode.show();
  419. // copyNode.inject(this.node);
  420. // }
  421. // }else{
  422. // this.parentContainer._dragIn(module);
  423. // }
  424. // },
  425. setAllStyles: function(){
  426. this.setPropertiesOrStyles("styles");
  427. this.setPropertiesOrStyles("properties");
  428. this.setImpExpTableStyles();
  429. this.reloadMaplist();
  430. },
  431. getContainerNodes: function(){
  432. return this.node.getElements("td");
  433. },
  434. copyComponentJsonData: function(newNode, pid){
  435. var tds = newNode.getElements("td");
  436. var ths = newNode.getElements("th");
  437. tds.each(function(td, idx){
  438. var newContainerJson = Object.clone(this.containers[idx].json);
  439. newContainerJson.id = this.containers[idx]._getNewId(pid);
  440. this.form.json.moduleList[newContainerJson.id] = newContainerJson;
  441. td.set("id", newContainerJson.id);
  442. }.bind(this));
  443. ths.each(function(th, idx){
  444. var newElementJson = Object.clone(this.elements[idx].json);
  445. newElementJson.id = this.elements[idx]._getNewId(pid);
  446. this.form.json.moduleList[newElementJson.id] = newElementJson;
  447. th.set("id", newElementJson.id);
  448. }.bind(this));
  449. },
  450. _getDirectSubModuleJson: function(node, moduleJsons){
  451. var subNode = node.getFirst();
  452. while (subNode){
  453. var module = subNode.retrieve("module");
  454. var flag = module && !["datatable","datagrid","datatemplate"].contains(module.moduleName);
  455. if (flag) {
  456. moduleJsons[module.json.id] = Object.clone(module.json);
  457. }
  458. if( !module || flag){
  459. this._getDirectSubModuleJson(subNode, moduleJsons);
  460. }
  461. subNode = subNode.getNext();
  462. }
  463. },
  464. getExpImpFieldJson: function () {
  465. var o = {};
  466. var list = [];
  467. this._getDirectSubModuleJson(this.node, o);
  468. for( var key in o ){
  469. var json = o[key];
  470. if(this.form.options.fields.contains(json.type) && !["Office"].contains(json.type)){
  471. list.push({
  472. "field": json.id,
  473. "title": json.name || ""
  474. });
  475. }
  476. }
  477. return list;
  478. },
  479. changeRelativeId: function (idMap) {
  480. this.checkRelativeId(this.json, idMap);
  481. }
  482. });