Table.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. MWF.xApplication.process.FormDesigner.Module = MWF.xApplication.process.FormDesigner.Module || {};
  2. MWF.xDesktop.requireApp("process.FormDesigner", "Module.$Component", null, false);
  3. MWF.xDesktop.requireApp("process.FormDesigner", "Module.Table$Td", null, false);
  4. MWF.xApplication.process.FormDesigner.Module.Table = MWF.FCTable = new Class({
  5. Extends: MWF.FC$Component,
  6. Implements: [Options, Events],
  7. options: {
  8. "style": "default",
  9. "propertyPath": "../x_component_process_FormDesigner/Module/Table/table.html",
  10. "propertyMultiPath": "../x_component_process_FormDesigner/Module/Table$Td/table$tdMulti.html",
  11. "multiActions": [
  12. {
  13. "name": "mergerCell",
  14. "icon": "mergerCell.png",
  15. "event": "click",
  16. "action": "mergerCell",
  17. "title": MWF.APPFD.LP.formAction.mergerCell
  18. }
  19. ]
  20. },
  21. initialize: function(form, options){
  22. this.setOptions(options);
  23. this.path = "../x_component_process_FormDesigner/Module/Table/";
  24. this.cssPath = "../x_component_process_FormDesigner/Module/Table/"+this.options.style+"/css.wcss";
  25. this._loadCss();
  26. this.moduleType = "component";
  27. this.moduleName = "table";
  28. this.form = form;
  29. this.container = null;
  30. this.containerNode = null;
  31. this.containers = [];
  32. this.elements = [];
  33. this.selectedMultiTds = [];
  34. },
  35. clearTemplateStyles: function(styles){
  36. if (styles){
  37. if (styles.styles) this.removeStyles(styles.styles, "styles");
  38. if (styles.properties) this.removeStyles(styles.properties, "properties");
  39. if (styles.titleStyles) this.removeStyles(styles.titleStyles, "titleTdStyles");
  40. if (styles.contentStyles) this.removeStyles(styles.contentStyles, "contentTdStyles");
  41. if (styles.layoutStyles) this.removeStyles(styles.layoutStyles, "layoutTdStyles");
  42. }
  43. },
  44. setTemplateStyles: function(styles){
  45. if (styles.styles) this.copyStyles(styles.styles, "styles");
  46. if (styles.properties) this.copyStyles(styles.properties, "properties");
  47. if (styles.titleStyles) this.copyStyles(styles.titleStyles, "titleTdStyles");
  48. if (styles.contentStyles) this.copyStyles(styles.contentStyles, "contentTdStyles");
  49. if (styles.layoutStyles) this.copyStyles(styles.layoutStyles, "layoutTdStyles");
  50. },
  51. _createMoveNode: function(){
  52. var tableHTML = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"2\" width=\"100%\" align=\"center\">";
  53. tableHTML += "<tr><td></td><td></td><td></td></tr>";
  54. tableHTML += "<tr><td></td><td></td><td></td></tr>";
  55. tableHTML += "<tr><td></td><td></td><td></td></tr>";
  56. tableHTML += "</table>";
  57. this.moveNode = new Element("div", {
  58. "html": tableHTML
  59. }).inject(this.form.container);
  60. // this.moveNode = divNode.getFirst();
  61. // this.moveNode.inject(divNode, "after");
  62. // divNode.destroy();
  63. this.moveNode.setStyles(this.css.moduleNodeMove);
  64. this._setTableStyle();
  65. },
  66. _setTableStyle: function(){
  67. var tds = this.moveNode.getElements("td");
  68. //var tds = this._getTds(this.moveNode);
  69. tds.setStyles({
  70. "border": "1px dashed #999",
  71. "height": "20px"
  72. });
  73. var ths = this.moveNode.getElements("th");
  74. //var tds = this._getTds(this.moveNode);
  75. ths.setStyles({
  76. "border": "1px dashed #999",
  77. "height": "20px"
  78. });
  79. },
  80. _getTds: function(node){
  81. tds = [];
  82. var table = (node || this.node).getElement("table");
  83. var rows = table.rows;
  84. for (var i=0; i<rows.length; i++){
  85. var row = rows[i];
  86. for (var j=0; j<row.cells.length; j++){
  87. tds.push(row.cells[j]);
  88. }
  89. }
  90. return tds;
  91. },
  92. _checkSelectedTds: function(sp, ep){
  93. this.selectedMultiTds = [];
  94. //var tds = this.node.getElements("td");
  95. var tds = this._getTds();
  96. // var tmpSelectedTds = [];
  97. // var startXList = [];
  98. // var startYList = [];
  99. // var endXList = [];
  100. // var endYList = [];
  101. var sx = sp.x, sy = sp.y, ex = ep.x, ey = ep.y;
  102. while (true){
  103. var tmpsx = sp.x, tmpsy = sp.y, tmpex = ep.x, tmpey = ep.y;
  104. tds.each(function(td){
  105. if (td.isInPointInRect(sx, sy, ex, ey)){
  106. var position = td.getPosition();
  107. var size = td.getSize();
  108. if (!tmpsx || position.x<tmpsx) tmpsx = position.x;
  109. if (!tmpsy || position.y<tmpsy) tmpsy = position.y;
  110. if (!tmpex || position.x+size.x>tmpex) tmpex = position.x+size.x;
  111. if (!tmpey || position.y+size.y>tmpey) tmpey = position.y+size.y;
  112. }
  113. }.bind(this));
  114. if (sx==tmpsx && sy==tmpsy && ex==tmpex && ey==tmpey) break;
  115. sx = tmpsx, sy = tmpsy, ex = tmpex, ey = tmpey;
  116. }
  117. tds.each(function(td){
  118. var module = td.retrieve("module");
  119. if (td.isInPointInRect(sx, sy, ex, ey)){
  120. module.selectedMulti();
  121. }else{
  122. module.unSelectedMulti();
  123. }
  124. }.bind(this));
  125. },
  126. _setOtherNodeEvent: function(){
  127. this.dragInfor = {};
  128. this.tdDragSelect = new Drag(this.node, {
  129. "stopPropagation": true,
  130. "preventDefault": true,
  131. "onStart": function(el, e){
  132. this.form._beginSelectMulti();
  133. var position = e.event.target.getPosition();
  134. this.dragInfor.start = {"x": e.event.offsetX+position.x, "y": e.event.offsetY+position.y};
  135. }.bind(this),
  136. "onDrag": function(el, e){
  137. var position = e.event.target.getPosition();
  138. var p = {"x": e.event.offsetX+position.x, "y": e.event.offsetY+position.y};
  139. this._checkSelectedTds(this.dragInfor.start, p);
  140. e.stopPropagation();
  141. }.bind(this),
  142. "onComplete": function(el, e){
  143. var position = e.event.target.getPosition();
  144. var p = {"x": e.event.offsetX+position.x, "y": e.event.offsetY+position.y};
  145. this._checkSelectedTds(this.dragInfor.start, p);
  146. this.form._completeSelectMulti();
  147. this._createMultiSelectedActions();
  148. this.showMultiProperty();
  149. e.stopPropagation();
  150. e.preventDefault();
  151. }.bind(this)
  152. });
  153. },
  154. showMultiProperty: function(){
  155. if (this.form.propertyMultiTd){
  156. this.form.propertyMultiTd.hide();
  157. this.form.propertyMultiTd = null;
  158. }
  159. this.form.propertyMultiTd = new MWF.xApplication.process.FormDesigner.PropertyMulti(this.form, this.form.selectedModules, this.form.designer.propertyContentArea, this.form.designer, {
  160. "path": this.options.propertyMultiPath,
  161. "onPostLoad": function(){
  162. this.show();
  163. }
  164. });
  165. this.form.propertyMultiTd.load();
  166. },
  167. _createMultiSelectedActions: function(){
  168. if (this.form.selectedModules.length>1){
  169. if (this.form.multimoduleActionsArea){
  170. this.form.multimoduleActionsArea.empty();
  171. this.options.multiActions.each(function(action){
  172. var actionNode = new Element("div", {
  173. "styles": this.options.actionNodeStyles,
  174. "title": action.title
  175. }).inject(this.form.multimoduleActionsArea);
  176. actionNode.setStyle("background", "url("+this.path+this.options.style+"/icon/"+action.icon+") no-repeat left center");
  177. actionNode.addEvent(action.event, function(e){
  178. this[action.action](e);
  179. }.bind(this));
  180. }.bind(this));
  181. this.form.multimoduleActionsArea.setStyle("width", 18*this.options.multiActions.length);
  182. }
  183. }else{
  184. this.form.multimoduleActionsArea.setStyle("display", "none");
  185. }
  186. },
  187. mergerCell: function(){
  188. if (this.form.selectedModules.length>1){
  189. var firstModuleObj = this.form._getFirstMultiSelectedModule();
  190. var firstModule = firstModuleObj.module;
  191. // var n=0;
  192. var td = firstModule.node;
  193. var colspan = 0;
  194. while (td && this.form.selectedModules.indexOf(td.retrieve("module"))!=-1 ){
  195. var tmpColspan = td.get("colspan").toInt() || 1;
  196. colspan = colspan+tmpColspan;
  197. td = td.getNext("td");
  198. }
  199. var maxRowIndex = Number.NEGATIVE_INFINITY;
  200. var minRowIndex = Number.POSITIVE_INFINITY;
  201. this.form.selectedModules.each(function(module, idx){
  202. var rIdx = module.node.getParent("tr").rowIndex;
  203. var tmpRowspan = module.node.get("rowspan").toInt() || 1;
  204. var rows = rIdx+tmpRowspan-1;
  205. maxRowIndex = Math.max(maxRowIndex, rows);
  206. minRowIndex = Math.min(minRowIndex, rows);
  207. }.bind(this));
  208. var rowspan = maxRowIndex-minRowIndex+1;
  209. if (colspan>1){
  210. firstModule.node.set("colspan", colspan);
  211. firstModule.json.properties.colspan = colspan;
  212. }else{
  213. firstModule.node.set("colspan", 1);
  214. delete firstModule.node.colspan;
  215. delete firstModule.json.properties.colspan;
  216. }
  217. if (rowspan>1){
  218. firstModule.node.set("rowspan", rowspan);
  219. firstModule.json.properties.rowspan = rowspan;
  220. }else{
  221. firstModule.node.set("rowspan", 1);
  222. delete firstModule.node.rowspan;
  223. delete firstModule.json.properties.rowspan;
  224. }
  225. while (this.form.selectedModules.length){
  226. var module = this.form.selectedModules[0];
  227. this.form.selectedModules.erase(module);
  228. if (module!==firstModule){
  229. var modules = module._getSubModule();
  230. modules.each(function(module){
  231. module._moveTo(firstModule);
  232. });
  233. this.containers.erase(module);
  234. module.destroy();
  235. }
  236. }
  237. firstModule.selected();
  238. }
  239. },
  240. _getContainers: function(){
  241. //var tds = this.node.getElements("td");
  242. var tds = this._getTds();
  243. this.form.getTemplateData("Table$Td", function(data){
  244. tds.each(function(td){
  245. var json = this.form.getDomjson(td);
  246. var tdContainer = null;
  247. if (!json){
  248. var moduleData = Object.clone(data);
  249. tdContainer = new MWF.FCTable$Td(this.form);
  250. tdContainer.table = this;
  251. tdContainer.load(moduleData, td, this);
  252. }else{
  253. var moduleData = Object.clone(data);
  254. Object.merge(moduleData, json);
  255. Object.merge(json, moduleData);
  256. tdContainer = new MWF.FCTable$Td(this.form);
  257. tdContainer.table = this;
  258. tdContainer.load(json, td, this);
  259. }
  260. this.containers.push(tdContainer);
  261. }.bind(this));
  262. }.bind(this));
  263. },
  264. _getElements: function(){
  265. // this.elements.push(this);
  266. var captions = this.node.getElements("caption");
  267. captions.each(function(caption){
  268. var json = this.form.getDomjson(caption);
  269. var el = null;
  270. if (!json){
  271. this.form.getTemplateData("Common", function(data){
  272. var moduleData = Object.clone(data);
  273. el = new MWF.FCCommon(this.form);
  274. el.table = this;
  275. el.load(moduleData, caption, this);
  276. }.bind(this));
  277. }else{
  278. el = new MWF.FCCommon(this.form);
  279. el.table = this;
  280. el.load(json, caption, this);
  281. }
  282. this.elements.push(el);
  283. }.bind(this));
  284. },
  285. _createNode: function(callback){
  286. var module = this;
  287. var url = this.path+"tableCreate.html";
  288. MWF.require("MWF.widget.Dialog", function(){
  289. var size = $(document.body).getSize();
  290. var x = size.x/2-180;
  291. var y = size.y/2-130;
  292. var dlg = new MWF.DL({
  293. "title": "Create Table",
  294. "style": "property",
  295. "top": y,
  296. "left": x-40,
  297. "fromTop":size.y/2-65,
  298. "fromLeft": size.x/2,
  299. "width": 360,
  300. "height": 260,
  301. "url": url,
  302. "buttonList": [
  303. {
  304. "text": MWF.APPFD.LP.button.ok,
  305. "action": function(){
  306. module._createTableNode();
  307. callback();
  308. this.close();
  309. }
  310. }
  311. ]
  312. });
  313. dlg.show();
  314. }.bind(this));
  315. },
  316. _createTableNode: function(){
  317. var rows = $("MWFNewTableLine").get("value");
  318. var cols = $("MWFNewTableColumn").get("value");
  319. var width = $("MWFNewTableWidth").get("value");
  320. var widthUnitNode = $("MWFNewTableWidthUnit");
  321. var widthUnit = widthUnitNode.options[widthUnitNode.selectedIndex].value;
  322. var border = $("MWFNewTableBorder").get("value");
  323. var cellpadding = $("MWFNewTableCellpadding").get("value");
  324. var cellspacing = $("MWFNewTableCellspacing").get("value");
  325. var w = "";
  326. if (widthUnit=="percent"){
  327. w = width+"%";
  328. }else{
  329. w = width+"px";
  330. }
  331. this.json.properties.width = w;
  332. this.json.properties.border = border;
  333. this.json.properties.cellpadding = cellpadding;
  334. this.json.properties.cellspacing = cellspacing;
  335. var tableHTML = "<table border=\""+border+"\" cellpadding=\""+cellpadding+"\" cellspacing=\""+cellspacing+"\" width=\""+w+"\" align=\"center\">";
  336. for (var i=0; i<rows.toInt(); i++){
  337. tableHTML += "<tr>";
  338. for (var j=0; j<cols.toInt(); j++){
  339. tableHTML += "<td></td>";
  340. }
  341. tableHTML += "</tr>";
  342. }
  343. tableHTML += "</table>";
  344. this.node = this.node = new Element("div", {
  345. "id": this.json.id,
  346. "MWFType": "table",
  347. "html": tableHTML,
  348. "styles": this.css.moduleNode,
  349. "events": {
  350. "selectstart": function(e){
  351. e.preventDefault();
  352. }
  353. }
  354. }).inject(this.form.node);
  355. //if (w!="100%"){
  356. // this.node.setStyle("width", w);
  357. //}
  358. },
  359. _dragComplete: function(){
  360. if (!this.node){
  361. this._createNode(function(){
  362. this._dragMoveComplete();
  363. }.bind(this));
  364. }else{
  365. this._dragMoveComplete();
  366. }
  367. },
  368. _dragMoveComplete: function(){
  369. this._resetTreeNode();
  370. this.node.inject(this.copyNode, "before");
  371. this._initModule();
  372. var thisDisplay = this.node.retrieve("thisDisplay");
  373. if (thisDisplay){
  374. this.node.setStyle("display", thisDisplay);
  375. }
  376. if (this.copyNode) this.copyNode.destroy();
  377. if (this.moveNode) this.moveNode.destroy();
  378. this.moveNode = null;
  379. this.copyNode = null;
  380. this.nextModule = null;
  381. this.form.moveModule = null;
  382. this.form.json.moduleList[this.json.id] = this.json;
  383. this.selected();
  384. },
  385. setPropertiesOrStyles: function(name){
  386. if (name=="styles"){
  387. try{
  388. this.setCustomStyles();
  389. }catch(e){}
  390. // var border = this.node.getStyle("border");
  391. // this.node.clearStyles();
  392. // this.node.setStyles(this.css.moduleNode);
  393. // this.node.setStyle("border", border);
  394. // Object.each(this.json.styles, function(value, key){
  395. // var reg = /^border\w*/ig;
  396. // if (!key.test(reg)){
  397. // this.node.setStyle(key, value);
  398. // }
  399. // }.bind(this));
  400. }
  401. if (name=="properties"){
  402. this.node.getFirst().setProperties(this.json.properties);
  403. if (this.json.properties.cellspacing==="0"){
  404. this.node.getFirst().setProperties({"cellspacing": "1"});
  405. }
  406. //if (this.json.properties.width){
  407. // if (this.json.properties.width!="100%"){
  408. // this.node.setStyle("width", this.json.properties.width);
  409. // }
  410. //}
  411. }
  412. },
  413. _setEditStyle_custom: function(name, obj, oldValue){
  414. if (name=="id"){
  415. if (name!=oldValue){
  416. var reg = new RegExp("^"+oldValue, "i");
  417. this.containers.each(function(container){
  418. var id = container.json.id;
  419. var newId = id.replace(reg, this.json.id);
  420. container.json.id = newId;
  421. delete this.form.json.moduleList[id];
  422. this.form.json.moduleList[newId] = container.json;
  423. container._setEditStyle("id");
  424. }.bind(this));
  425. }
  426. }
  427. if (name=="titleTdStyles") this.setTabStyles();
  428. if (name=="contentTdStyles") this.setTabStyles();
  429. if (name=="layoutTdStyles") this.setTabStyles();
  430. },
  431. setTabStyles: function(){
  432. this.containers.each(function(module){
  433. //if (module.json.cellType=="title"){
  434. //
  435. //}
  436. //if (module.json.cellType=="content"){
  437. //
  438. //}
  439. //if (module.json.cellType=="layout"){
  440. //
  441. //}
  442. module.setCustomStyles();
  443. }.bind(this));
  444. },
  445. setAllStyles: function(){
  446. this.setPropertiesOrStyles("styles");
  447. this.setPropertiesOrStyles("properties");
  448. this.setTabStyles();
  449. this.reloadMaplist();
  450. },
  451. copyComponentJsonData: function(newNode, pid){
  452. //var tds = newNode.getElements("td");
  453. var tds = this._getTds(newNode);
  454. // this.form.getTemplateData("Table$Td", function(data){
  455. tds.each(function(td, idx){
  456. var newContainerJson = Object.clone(this.containers[idx].json);
  457. newContainerJson.id = this.containers[idx]._getNewId(pid);
  458. this.form.json.moduleList[newContainerJson.id] = newContainerJson;
  459. td.set("id", newContainerJson.id);
  460. }.bind(this));
  461. // }.bind(this));
  462. },
  463. // setOtherNodeEvent: function(){
  464. //// var tds = this.node.getElements("td");
  465. //// tds.addEvent("click", function(e){
  466. //// this.selectedTd(e.target);
  467. //// }.bind(this));
  468. // },
  469. //
  470. // selectedTd: function(td){
  471. // if (this.currentSelectedTd){
  472. // if (this.currentSelectedTd==td){
  473. // return true;
  474. // }else{
  475. // this.unSelectedTd(this.currentSelectedTd);
  476. // }
  477. // }
  478. //
  479. // var top = td.getStyle("border-top");
  480. // var left = td.getStyle("border-left");
  481. // var bottom = td.getStyle("border-bottom");
  482. // var right = td.getStyle("border-right");
  483. // td.store("thisborder", {"top": top, "left": left, "bottom": bottom, "right": right});
  484. //
  485. // td.setStyles({
  486. // "border": "1px dashed #ff6b49"
  487. // });
  488. // this.currentSelectedTd = td;
  489. // },
  490. // unSelectedTd: function(td){
  491. // var border = td.retrieve("thisborder");
  492. // if (border) {
  493. // td.setStyles({
  494. // "border-top": border.top,
  495. // "border-left": border.left,
  496. // "border-bottom": border.bottom,
  497. // "border-right": border.right
  498. // });
  499. // }
  500. // this.currentSelectedTd = null;
  501. // },
  502. getContainerNodes: function(){
  503. //return this.node.getElements("td");
  504. return this._getTds();
  505. },
  506. _preprocessingModuleData: function(){
  507. this.node.clearStyles();
  508. //if (this.initialStyles) this.node.setStyles(this.initialStyles);
  509. this.json.recoveryStyles = Object.clone(this.json.styles);
  510. if (this.json.recoveryStyles) Object.each(this.json.recoveryStyles, function(value, key){
  511. if ((value.indexOf("x_processplatform_assemble_surface")!=-1 || value.indexOf("x_portal_assemble_surface")!=-1)){
  512. //需要运行时处理
  513. }else{
  514. var reg = /^border\w*/ig;
  515. if (!key.test(reg)){
  516. if (key){
  517. this.node.setStyle(key, value);
  518. delete this.json.styles[key];
  519. }
  520. }
  521. }
  522. }.bind(this));
  523. this.json.preprocessing = "y";
  524. }
  525. });