Page.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. MWF.xApplication.portal.PageDesigner.Module = MWF.xApplication.portal.PageDesigner.Module || {};
  2. MWF.require("MWF.widget.Common", null, false);
  3. MWF.xApplication.portal.PageDesigner.Module.Page = MWF.PCPage = new Class({
  4. Extends: MWF.widget.Common,
  5. Implements: [Options, Events],
  6. options: {
  7. "style": "default",
  8. "propertyPath": "/x_component_portal_PageDesigner/Module/Page/page.html",
  9. "mode": "PC"
  10. },
  11. initializeBase: function(options){
  12. this.setOptions(options);
  13. this.path = "/x_component_portal_PageDesigner/Module/Page/";
  14. this.cssPath = "/x_component_portal_PageDesigner/Module/Page/"+this.options.style+"/css.wcss";
  15. this._loadCss();
  16. },
  17. initialize: function(designer, container, options){
  18. this.initializeBase(options);
  19. this.container = null;
  20. this.page = this;
  21. this.form = this;
  22. this.moduleType = "page";
  23. this.moduleList = [];
  24. this.moduleNodeList = [];
  25. this.moduleContainerNodeList = [];
  26. this.moduleElementNodeList = [];
  27. this.moduleComponentNodeList = [];
  28. // this.moduleContainerList = [];
  29. this.dataTemplate = {};
  30. this.designer = designer;
  31. this.container = container;
  32. this.selectedModules = [];
  33. },
  34. load : function(data){
  35. this.data = data;
  36. this.json = data.json;
  37. this.html = data.html;
  38. this.json.mode = this.options.mode;
  39. this.isNewPage = (this.json.id) ? false : true;
  40. if (this.isNewPage) this.checkUUID();
  41. if(this.designer.application) this.data.json.applicationName = this.designer.application.name;
  42. if(this.designer.application) this.data.json.application = this.designer.application.id;
  43. this.container.set("html", this.html);
  44. this.loadStylesList(function(){
  45. var oldStyleValue = "";
  46. if ((!this.json.pageStyleType) || !this.stylesList[this.json.pageStyleType]) this.json.pageStyleType="blue-simple";
  47. if (this.options.mode=="Mobile"){
  48. if (this.json.pageStyleType != "defaultMobile"){
  49. oldStyleValue = this.json.pageStyleType;
  50. this.json.pageStyleType = "defaultMobile";
  51. }
  52. }
  53. this.templateStyles = (this.stylesList && this.json.pageStyleType) ? this.stylesList[this.json.pageStyleType] : null;
  54. this.loadDomModules();
  55. if (this.json.pageStyleType){
  56. if (this.stylesList[this.json.pageStyleType]){
  57. if (this.stylesList[this.json.pageStyleType]["page"]){
  58. this.setTemplateStyles(this.stylesList[this.json.pageStyleType]["page"]);
  59. }
  60. }
  61. }
  62. this.setCustomStyles();
  63. this.node.setProperties(this.json.properties);
  64. this.setNodeEvents();
  65. if (this.options.mode=="Mobile"){
  66. if (oldStyleValue) this._setEditStyle("pageStyleType", null, oldStyleValue);
  67. }
  68. this.selected();
  69. this.autoSave();
  70. this.designer.addEvent("queryClose", function(){
  71. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  72. }.bind(this));
  73. }.bind(this));
  74. },
  75. removeStyles: function(from, to){
  76. if (this.json[to]){
  77. Object.each(from, function(style, key){
  78. if (this.json[to][key] && this.json[to][key]==style){
  79. delete this.json[to][key];
  80. }
  81. }.bind(this));
  82. }
  83. },
  84. copyStyles: function(from, to){
  85. if (!this.json[to]) this.json[to] = {};
  86. Object.each(from, function(style, key){
  87. if (!this.json[to][key]) this.json[to][key] = style;
  88. }.bind(this));
  89. },
  90. clearTemplateStyles: function(styles){
  91. if (styles){
  92. if (styles.styles) this.removeStyles(styles.styles, "styles");
  93. if (styles.properties) this.removeStyles(styles.properties, "properties");
  94. }
  95. },
  96. setTemplateStyles: function(styles){
  97. if (styles.styles) this.copyStyles(styles.styles, "styles");
  98. if (styles.properties) this.copyStyles(styles.properties, "properties");
  99. },
  100. loadStylesList: function(callback){
  101. var stylesUrl = "/x_component_portal_PageDesigner/Module/Page/template/"+((this.options.mode=="Mobile") ? "styles": "styles")+".json";
  102. MWF.getJSON(stylesUrl,{
  103. "onSuccess": function(responseJSON){
  104. this.stylesList= responseJSON;
  105. if (callback) callback(this.stylesList);
  106. }.bind(this),
  107. "onRequestFailure": function(){
  108. this.stylesList = {};
  109. if (callback) callback(this.stylesList);
  110. }.bind(this),
  111. "onError": function(){
  112. this.stylesList = {};
  113. if (callback) callback(this.stylesList);
  114. }.bind(this)
  115. }
  116. );
  117. },
  118. autoSave: function(){
  119. this.autoSaveCheckNode = this.designer.pageToolbarNode.getElement("#MWFPageAutoSaveCheck");
  120. if (this.autoSaveCheckNode){
  121. this.autoSaveTimerID = window.setInterval(function(){
  122. if (this.autoSaveCheckNode.get("checked")){
  123. this.save();
  124. }
  125. }.bind(this), 60000);
  126. }
  127. },
  128. checkUUID: function(){
  129. this.designer.actions.getUUID(function(id){
  130. this.json.id = id;
  131. }.bind(this));
  132. },
  133. loadDomModules: function(){
  134. this.node = this.container.getFirst();
  135. this.node.set("id", this.json.id);
  136. this.node.setStyles(this.css.pageNode);
  137. this.node.store("module", this);
  138. var y = this.container.getSize().y-2;
  139. this.node.setStyle("min-height", ""+y+"px");
  140. this.designer.addEvent("resize", function(){
  141. var y = this.container.getSize().y-2;
  142. this.node.setStyle("min-height", ""+y+"px");
  143. }.bind(this));
  144. this.loadDomTree();
  145. },
  146. loadDomTree: function(){
  147. MWF.require("MWF.widget.Tree", function(){
  148. this.domTree = new MWF.widget.Tree(this.designer.propertyDomArea, {"style": "domtree"});
  149. this.domTree.load();
  150. this.createPageTreeNode();
  151. this.parseModules(this, this.node);
  152. }.bind(this));
  153. },
  154. createPageTreeNode: function(){
  155. var o = {
  156. "expand": true,
  157. "title": this.json.id,
  158. "text": "<"+this.json.type+"> "+this.json.name+" ["+this.options.mode+"] ",
  159. "icon": (this.options.mode=="Mobile") ? "mobile.png": "pc.png"
  160. };
  161. o.action = function(){
  162. if (this.module) this.module.selected();
  163. };
  164. this.treeNode = this.domTree.appendChild(o);
  165. this.treeNode.module = this;
  166. },
  167. parseModules: function(parent, dom){
  168. var tmpDom = null;
  169. var subDom = dom.getFirst();
  170. while (subDom){
  171. if (subDom.get("MWFtype")){
  172. // var module = subDom.retrieve("module");
  173. // alert(subDom.get("id")+": "+module);
  174. // if (!module){
  175. var json = this.getDomjson(subDom);
  176. if (json) module = this.loadModule(json, subDom, parent);
  177. // }
  178. // if (module.moduleType=="container") this.parseModules(module, subDom);
  179. // }else{
  180. // this.parseModules(parent, subDom);
  181. }
  182. // else if (subDom.getFirst()){
  183. // subDom = subDom.getFirst();
  184. // this.parseModules(parent, subDom);
  185. // }else{
  186. // subDom = subDom.getNext();
  187. // }
  188. if (!json) tmpDom = subDom;
  189. subDom = subDom.getNext();
  190. if (tmpDom){
  191. tmpDom.destroy();
  192. tmpDom = null;
  193. }
  194. }
  195. },
  196. getDomjson: function(dom){
  197. var mwfType = dom.get("MWFtype");
  198. switch (mwfType) {
  199. case "page":
  200. return this.json;
  201. case "":
  202. return null;
  203. default:
  204. var id = dom.get("id");
  205. if (id){
  206. return this.json.moduleList[id];
  207. }else{
  208. return null;
  209. }
  210. }
  211. },
  212. loadModule: function(json, dom, parent){
  213. if (json) {
  214. var module = new MWF["PC" + json.type](this);
  215. module.load(json, dom, parent);
  216. return module;
  217. }
  218. },
  219. setNodeEvents: function(){
  220. this.node.addEvent("click", function(e){
  221. this.selected();
  222. }.bind(this));
  223. },
  224. createModule: function(className, e){
  225. this.getTemplateData(className, function(data){
  226. var moduleData = Object.clone(data);
  227. var newTool = new MWF["PC"+className](this);
  228. newTool.create(moduleData, e);
  229. }.bind(this));
  230. },
  231. getTemplateData: function(className, callback){
  232. if (this.dataTemplate[className]){
  233. if (callback) callback(this.dataTemplate[className]);
  234. }else{
  235. var templateUrl = "/x_component_portal_PageDesigner/Module/"+className+"/template.json";
  236. MWF.getJSON(templateUrl, function(responseJSON, responseText){
  237. this.dataTemplate[className] = responseJSON;
  238. if (callback) callback(responseJSON);
  239. }.bind(this));
  240. }
  241. },
  242. selected: function(){
  243. if (this.currentSelectedModule){
  244. if (this.currentSelectedModule==this){
  245. return true;
  246. }else{
  247. this.currentSelectedModule.unSelected();
  248. }
  249. }
  250. if (this.propertyMultiTd){
  251. this.propertyMultiTd.hide();
  252. this.propertyMultiTd = null;
  253. }
  254. this.unSelectedMulti();
  255. this.currentSelectedModule = this;
  256. if (this.treeNode){
  257. this.treeNode.selectNode();
  258. }
  259. this.showProperty();
  260. // this.isFocus = true;
  261. },
  262. unSelectedMulti: function(){
  263. while (this.selectedModules.length){
  264. this.selectedModules[0].unSelectedMulti();
  265. }
  266. if (this.multimoduleActionsArea) this.multimoduleActionsArea.setStyle("display", "none");
  267. },
  268. unSelectAll: function(){
  269. },
  270. _beginSelectMulti: function(){
  271. if (this.currentSelectedModule) this.currentSelectedModule.unSelected();
  272. this.unSelectedMulti();
  273. this.noSelected = true;
  274. },
  275. _completeSelectMulti: function(){
  276. if (this.selectedModules.length<2){
  277. this.selectedModules[0].selected();
  278. }else{
  279. this._showMultiActions();
  280. }
  281. },
  282. createMultimoduleActionsArea: function(){
  283. this.multimoduleActionsArea = new Element("div", {
  284. styles: {
  285. "display": "none",
  286. // "width": 18*this.options.actions.length,
  287. "position": "absolute",
  288. "background-color": "#F1F1F1",
  289. "padding": "1px",
  290. "padding-right": "0px",
  291. "border": "1px solid #AAA",
  292. "box-shadow": "0px 2px 5px #999",
  293. "z-index": 10001
  294. }
  295. }).inject(this.page.container, "after");
  296. },
  297. _showMultiActions: function(){
  298. if (!this.multimoduleActionsArea) this.createMultimoduleActionsArea();
  299. var firstModule = this._getFirstMultiSelectedModule();
  300. if (firstModule){
  301. // var module = firstModule.module;
  302. var y = firstModule.position.y-25;
  303. var x = firstModule.position.x;
  304. this.multimoduleActionsArea.setPosition({"x": x, "y": y});
  305. this.multimoduleActionsArea.setStyle("display", "block");
  306. }
  307. },
  308. _getFirstMultiSelectedModule: function(){
  309. var firstModule = null;
  310. this.selectedModules.each(function(module){
  311. var position = module.node.getPosition(module.page.node.getOffsetParent());
  312. if (!firstModule){
  313. firstModule = {"module": module, "position": position};
  314. }else{
  315. if (position.y<firstModule.position.y){
  316. firstModule = {"module": module, "position": position};
  317. }else if (position.y==firstModule.position.y){
  318. if (position.x<firstModule.position.x){
  319. firstModule = {"module": module, "position": position};
  320. }
  321. }
  322. }
  323. });
  324. return firstModule;
  325. },
  326. showProperty: function(){
  327. if (!this.property){
  328. this.property = new MWF.xApplication.process.FormDesigner.Property(this, this.designer.propertyContentArea, this.designer, {
  329. "path": this.options.propertyPath,
  330. "onPostLoad": function(){
  331. this.property.show();
  332. }.bind(this)
  333. });
  334. this.property.load();
  335. }else{
  336. this.property.show();
  337. }
  338. },
  339. hideProperty: function(){
  340. if (this.property) this.property.hide();
  341. },
  342. unSelected: function(){
  343. this.currentSelectedModule = null;
  344. this.hideProperty();
  345. },
  346. _dragIn: function(module){
  347. if (!this.Component) module.inContainer = this;
  348. module.parentContainer = this;
  349. this.node.setStyles({"border": "1px solid #ffa200"});
  350. var copyNode = module._getCopyNode();
  351. copyNode.inject(this.node);
  352. },
  353. _dragOut: function(module){
  354. module.inContainer = null;
  355. module.parentContainer = null;
  356. this.node.setStyles(this.css.pageNode);
  357. this.node.setStyles(this.json.styles);
  358. var copyNode = module._getCopyNode();
  359. copyNode.setStyle("display", "none");
  360. },
  361. _dragDrop: function(module){
  362. this.node.setStyles(this.css.pageNode);
  363. this.node.setStyles(this.json.styles);
  364. },
  365. _clearNoId: function(node){
  366. var subNode = node.getFirst();
  367. while (subNode){
  368. var nextNode = subNode.getNext();
  369. if (subNode.get("MWFType")){
  370. if (!subNode.get("id")){
  371. subNode.destroy();
  372. }else{
  373. if (subNode) this._clearNoId(subNode);
  374. }
  375. }else{
  376. if (subNode) this._clearNoId(subNode);
  377. }
  378. subNode = nextNode;
  379. }
  380. },
  381. _getPageData: function(){
  382. var copy = this.node.clone(true, true);
  383. copy.clearStyles(true);
  384. this._clearNoId(copy);
  385. var html = copy.outerHTML;
  386. copy.destroy();
  387. this.data.json.mode = this.options.mode;
  388. this.data.html = html;
  389. return this.data;
  390. },
  391. preview: function(){
  392. MWF.xDesktop.requireApp("process.FormDesigner", "Preview", function(){
  393. if (this.options.mode=="Mobile"){
  394. this.previewBox = new MWF.xApplication.process.FormDesigner.Preview(this, {"size": {"x": "340", "y": 580, "layout":"mobile"}});
  395. }else{
  396. this.previewBox = new MWF.xApplication.process.FormDesigner.Preview(this);
  397. }
  398. this.previewBox.load();
  399. }.bind(this));
  400. },
  401. save: function(callback){
  402. this.designer.savePage();
  403. },
  404. explode: function(){
  405. this._getPageData();
  406. MWF.require("MWF.widget.Base64", null, false);
  407. var data = MWF.widget.Base64.encode(JSON.encode(this.data));
  408. MWF.require("MWF.widget.Panel", function(){
  409. var node = new Element("div");
  410. var size = this.designer.pageNode.getSize();
  411. var position = this.designer.pageNode.getPosition(this.designer.pageNode.getOffsetParent());
  412. var textarea = new Element("textarea", {
  413. "styles": {
  414. "border": "1px solid #999",
  415. "width": "770px",
  416. "margin-left": "14px",
  417. "margin-top": "14px",
  418. "height": "580px"
  419. },
  420. "text": JSON.encode(this.data)
  421. }).inject(node);
  422. this.explodePanel = new MWF.widget.Panel(node, {
  423. "style": "page",
  424. "isResize": false,
  425. "isMax": false,
  426. "title": "",
  427. "width": 800,
  428. "height": 660,
  429. "top": position.y,
  430. "left": position.x+3,
  431. "isExpand": false,
  432. "target": this.designer.node
  433. });
  434. this.explodePanel.load();
  435. }.bind(this));
  436. },
  437. setPropertiesOrStyles: function(name){
  438. if (name=="styles"){
  439. this.setCustomStyles();
  440. }
  441. if (name=="properties"){
  442. this.node.setProperties(this.json.properties);
  443. }
  444. },
  445. setCustomStyles: function(){
  446. var border = this.node.getStyle("border");
  447. this.node.clearStyles();
  448. this.node.setStyles(this.css.pageNode);
  449. var y = this.container.getSize().y-2;
  450. this.node.setStyle("min-height", ""+y+"px");
  451. if (this.initialStyles) this.node.setStyles(this.initialStyles);
  452. this.node.setStyle("border", border);
  453. Object.each(this.json.styles, function(value, key){
  454. var reg = /^border\w*/ig;
  455. if (!key.test(reg)){
  456. this.node.setStyle(key, value);
  457. }
  458. }.bind(this));
  459. },
  460. _setEditStyle: function(name, obj, oldValue){
  461. if (name=="name"){
  462. var title = this.json.name || this.json.id;
  463. this.treeNode.setText("<"+this.json.type+"> "+title);
  464. }
  465. if (name=="id"){
  466. if (!this.json.name) this.treeNode.setText("<"+this.json.type+"> "+this.json.id);
  467. this.treeNode.setTitle(this.json.id);
  468. this.node.set("id", this.json.id);
  469. }
  470. if (name=="pageStyleType"){
  471. debugger;
  472. this.templateStyles = (this.stylesList && this.json.pageStyleType) ? this.stylesList[this.json.pageStyleType] : null;
  473. if (oldValue) {
  474. var oldTemplateStyles = this.stylesList[oldValue];
  475. if (oldTemplateStyles){
  476. if (oldTemplateStyles["page"]) this.clearTemplateStyles(oldTemplateStyles["page"]);
  477. }
  478. }
  479. if (this.templateStyles){
  480. if (this.templateStyles["page"]) this.setTemplateStyles(this.templateStyles["page"]);
  481. }
  482. this.setAllStyles();
  483. this.moduleList.each(function(module){
  484. if (oldTemplateStyles){
  485. module.clearTemplateStyles(oldTemplateStyles[module.moduleName]);
  486. }
  487. module.setStyleTemplate();
  488. module.setAllStyles();
  489. }.bind(this));
  490. }
  491. this._setEditStyle_custom(name, obj, oldValue);
  492. },
  493. setAllStyles: function(){
  494. this.setPropertiesOrStyles("styles");
  495. this.setPropertiesOrStyles("properties");
  496. this.reloadMaplist();
  497. },
  498. reloadMaplist: function(){
  499. if (this.property) Object.each(this.property.maplists, function(map, name){ map.reload(this.json[name]);}.bind(this));
  500. },
  501. _setEditStyle_custom: function(){
  502. },
  503. saveAsTemplete: function(){
  504. }
  505. });