Widget.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. MWF.xApplication.portal.PageDesigner.Module = MWF.xApplication.portal.PageDesigner.Module || {};
  2. MWF.xDesktop.requireApp("process.FormDesigner", "Module.$Element", null, false);
  3. MWF.xApplication.portal.PageDesigner.Module.Widget = MWF.PCWidget = new Class({
  4. Extends: MWF.FC$Element,
  5. Implements: [Options, Events],
  6. options: {
  7. "style": "default",
  8. "propertyPath": "../x_component_portal_PageDesigner/Module/Widget/widget.html",
  9. "actions": [
  10. {
  11. "name": "move",
  12. "icon": "move1.png",
  13. "event": "mousedown",
  14. "action": "move",
  15. "title": MWF.APPPD.LP.formAction.move
  16. },
  17. {
  18. "name": "delete",
  19. "icon": "delete1.png",
  20. "event": "click",
  21. "action": "delete",
  22. "title": MWF.APPPD.LP.formAction["delete"]
  23. }
  24. // {
  25. // "name": "styleBrush",
  26. // "icon": "styleBrush.png",
  27. // "event": "click",
  28. // "action": "styleBrush",
  29. // "title": MWF.APPPD.LP.formAction["styleBrush"]
  30. // }
  31. ]
  32. },
  33. initialize: function(page, options){
  34. this.setOptions(options);
  35. this.path = "../x_component_portal_PageDesigner/Module/Widget/";
  36. this.cssPath = "../x_component_portal_PageDesigner/Module/Widget/"+this.options.style+"/css.wcss";
  37. this._loadCss();
  38. this.moduleType = "element";
  39. this.moduleName = "widget";
  40. this.page = page;
  41. this.form = page;
  42. this.container = null;
  43. this.containerNode = null;
  44. },
  45. load : function(json, node, parent){
  46. this.json = json;
  47. this.node= node;
  48. this.node.store("module", this);
  49. //this.node.empty();
  50. this.node.setStyles(this.css.moduleNode);
  51. //this._loadNodeStyles();
  52. this._initModule();
  53. if (this.json.widgetSelected && this.json.widgetSelected!=="none" && this.json.widgetType!=="script"){
  54. this.redoSelectedWidget(this.json.widgetSelected, null, "");
  55. }else{
  56. this.node.empty();
  57. this.loadIcon();
  58. }
  59. this._loadTreeNode(parent);
  60. this.setCustomStyles();
  61. this.parentContainer = this.treeNode.parentNode.module;
  62. this._setEditStyle_custom("id");
  63. this.parseModules();
  64. this.json.moduleName = this.moduleName;
  65. this.node.addEvent("click", function(){
  66. this.refreshWidget();
  67. }.bind(this));
  68. this.node.addEvent("dblclick", function(e){
  69. this.openWidget(e);
  70. }.bind(this));
  71. },
  72. _initModule: function(){
  73. if (!this.json.isSaved) this.setStyleTemplate();
  74. this.setPropertiesOrStyles("styles");
  75. this.setPropertiesOrStyles("inputStyles");
  76. this.setPropertiesOrStyles("properties");
  77. this._setNodeProperty();
  78. if (!this.page.isWidget) this._createIconAction();
  79. this._setNodeEvent();
  80. this.json.isSaved = true;
  81. this.queryGetPageDataFun = this.queryGetPageData.bind(this);
  82. this.postGetPageDataFun = this.postGetPageData.bind(this);
  83. this.page.addEvent("queryGetPageData", this.queryGetPageDataFun);
  84. this.page.addEvent("postGetPageData", this.postGetPageDataFun);
  85. },
  86. openWidget: function(e){
  87. if (this.json.widgetSelected && this.json.widgetSelected!=="none" && this.json.widgetType!=="script"){
  88. layout.desktop.openApplication(e, "portal.WidgetDesigner", {"id": this.json.widgetSelected, "appId": "WidgetDesigner"+this.json.widgetSelected});
  89. }
  90. },
  91. _createMoveNode: function(){
  92. this.moveNode = new Element("div", {
  93. "MWFType": "widget",
  94. "id": this.json.id,
  95. "styles": this.css.moduleNodeMove,
  96. "events": {
  97. "selectstart": function(){
  98. return false;
  99. }
  100. }
  101. }).inject(this.page.container);
  102. },
  103. _createNode: function(){
  104. this.node = this.moveNode.clone(true, true);
  105. this.node.setStyles(this.css.moduleNode);
  106. this.node.set("id", this.json.id);
  107. this.node.addEvent("selectstart", function(){
  108. return false;
  109. });
  110. // debugger;
  111. // if (this.json.widgetSelected && this.json.widgetSelected!="none" && this.json.widgetType!=="script"){
  112. // this.redoSelectedWidget(this.json.widgetSelected, $(this.property.data.pid+"selectWidget").getElement("select"), "");
  113. // }else{
  114. this.loadIcon();
  115. // }
  116. this.node.addEvent("click", function(){
  117. this.refreshWidget();
  118. }.bind(this));
  119. },
  120. postGetPageData: function(node){
  121. if (!node || node.contains(this.node)) this.show();
  122. },
  123. queryGetPageData: function(node){
  124. if (!node || node.contains(this.node)) this.hide();
  125. },
  126. hide: function(){
  127. this.node.empty();
  128. },
  129. show: function(){
  130. if (this.widgetData ){
  131. this.widgetModule = new MWF.PCWidget.Page(this.page, this.node, {
  132. parentpageIdList : this.getParentpageIdList(),
  133. level : this.getLevel()
  134. });
  135. this.widgetModule.widgetSelector = this.getWidgetSelector();
  136. this.widgetModule.widgetSelectedValue = this.getWidgetSelectedValue();
  137. this.widgetModule.level1Widget = this.getLevel1Widget();
  138. this.widgetModule.load(this.widgetData);
  139. }else{
  140. this.node.empty();
  141. this.loadIcon();
  142. }
  143. },
  144. "delete": function(e){
  145. var module = this;
  146. this.page.designer.shortcut = false;
  147. this.page.designer.confirm("warn", module.node, MWF.APPPD.LP.notice.deleteElementTitle, MWF.APPPD.LP.notice.deleteElement, 300, 120, function(){
  148. if (this.queryGetPageDataFun) module.page.removeEvent("queryGetPageData", this.queryGetPageDataFun);
  149. if (this.postGetPageDataFun) module.page.removeEvent("postGetPageData", this.postGetPageDataFun);
  150. module.destroy();
  151. module.page.selected();
  152. module.page.designer.shortcut = true;
  153. this.close();
  154. }, function(){
  155. module.page.designer.shortcut = true;
  156. this.close();
  157. }, null);
  158. },
  159. getLevel : function(){
  160. return ( this.page.options.level1 || 0 ) + 1;
  161. },
  162. getLevel1Widget : function(){
  163. return this.page.level1Widget || this;
  164. },
  165. getWidgetSelector : function(){
  166. return this.widgetSelector || this.page.widgetSelector;
  167. },
  168. getWidgetSelectedValue : function(){
  169. return this.widgetSelectedValue || this.page.widgetSelectedValue;
  170. },
  171. checkWidgetNested : function( id ){
  172. if( this.page.options.parentpageIdList ){
  173. return !this.page.options.parentpageIdList.contains( id );
  174. }
  175. return true;
  176. },
  177. getParentpageIdList : function(){
  178. var parentpageIdList;
  179. if( this.page.options.parentpageIdList ){
  180. parentpageIdList = Array.clone( this.page.options.parentpageIdList );
  181. parentpageIdList.push( this.page.json.id )
  182. }else{
  183. parentpageIdList = [ this.page.json.id ];
  184. }
  185. return parentpageIdList;
  186. },
  187. refreshWidget: function(){
  188. if (this.json.widgetSelected && this.json.widgetSelected!=="none" && this.json.widgetType!=="script"){
  189. MWF.Actions.get("x_portal_assemble_designer").getWidget(this.json.widgetSelected, function(json){
  190. if (this.widgetData.updateTime!==json.data.updateTime){
  191. var select = null;
  192. if (this.property){
  193. select = $(this.property.data.pid+"selectWidget").getElement("select");
  194. }
  195. this.clearWidgetList(this.json.widgetSelected);
  196. this.reloadWidget(json.data, select, "");
  197. }
  198. }.bind(this));
  199. }
  200. },
  201. loadIcon: function(){
  202. this.iconNode = new Element("div", {
  203. "styles": this.css.iconNode
  204. }).inject(this.node);
  205. new Element("div", {
  206. "styles": this.css.iconNodeIcon
  207. }).inject(this.iconNode);
  208. new Element("div", {
  209. "styles": this.css.iconNodeText,
  210. "text": "Widget"
  211. }).inject(this.iconNode);
  212. },
  213. _loadNodeStyles: function(){
  214. this.iconNode = this.node.getElement("div").setStyles(this.css.iconNode);
  215. this.iconNode.getFirst("div").setStyles(this.css.iconNodeIcon);
  216. this.iconNode.getLast("div").setStyles(this.css.iconNodeText);
  217. },
  218. _setEditStyle: function(name, input, oldValue){
  219. if (name==="widgetSelected"){
  220. if (this.json.widgetSelected!==oldValue){
  221. this.redoSelectedWidget(name, input, oldValue);
  222. }
  223. }
  224. if (name==="widgetType"){
  225. if (this.json.widgetType!==oldValue){
  226. if (this.json.widgetType !== "script"){
  227. this.redoSelectedWidget(name, $(this.property.data.pid+"selectWidget").getElement("select"), "");
  228. }
  229. if (this.json.widgetType === "script"){
  230. this.widgetData = null;
  231. this.clearWidgetList(this.json.widgetSelected);
  232. this.node.empty();
  233. this.loadIcon();
  234. }
  235. }
  236. }
  237. },
  238. redoSelectedWidget: function(name, input, oldValue){
  239. if (this.json.widgetSelected==="none") this.json.widgetSelected="";
  240. if (this.json.widgetSelected && this.json.widgetSelected!=="none"){
  241. if(input)this.widgetSelector = input;
  242. if( !input )input = this.getWidgetSelector();
  243. if( oldValue )this.widgetSelectedValue = oldValue;
  244. if( !oldValue )oldValue = this.getWidgetSelectedValue() || "";
  245. var level1Widget = this.getLevel1Widget();
  246. if( !this.checkWidgetNested(this.json.widgetSelected) ){
  247. //var p = this.node.getPosition(document.body);
  248. //this.page.designer.alert("error", {
  249. // "event": {
  250. // "x": p.x + 150,
  251. // "y": p.y + 80
  252. // }
  253. //}, this.page.designer.lp.widgetNestedTitle, this.page.designer.lp.widgetNestedInfor, 400, 120);
  254. this.page.designer.notice( this.page.designer.lp.widgetNestedInfor, "error", level1Widget.node );
  255. level1Widget.json.widgetSelected = oldValue;
  256. if (input) {
  257. for (var i = 0; i < input.options.length; i++) {
  258. if (input.options[i].value === oldValue || (input.options[i].value==="none" && !oldValue ) ) {
  259. input.options[i].set("selected", true);
  260. break;
  261. }
  262. }
  263. }
  264. if( !oldValue ){
  265. level1Widget.node.empty();
  266. level1Widget.loadIcon();
  267. }else{
  268. level1Widget.refreshWidget();
  269. }
  270. }else{
  271. MWF.Actions.get("x_portal_assemble_designer").getWidget(this.json.widgetSelected, function(json){
  272. this.reloadWidget(json.data, input, oldValue);
  273. }.bind(this));
  274. }
  275. }else{
  276. this.widgetData = null;
  277. this.clearWidgetList(oldValue);
  278. this.node.empty();
  279. this.loadIcon();
  280. }
  281. },
  282. clearWidgetList: function(pageName){
  283. if (!this.page.widgetList) this.page.widgetList = {};
  284. if (pageName) if (this.page.widgetList[pageName]) delete this.page.widgetList[pageName];
  285. },
  286. addWidgetList: function(){
  287. if (!this.page.widgetList) this.page.widgetList = {};
  288. this.page.widgetList[this.json.widgetSelected] = Object.clone(this.widgetData.json);
  289. },
  290. getWidgetData: function(data){
  291. var widgetDataStr = null;
  292. if (this.page.options.mode !== "Mobile"){
  293. widgetDataStr = data.data;
  294. }else{
  295. widgetDataStr = data.mobileData;
  296. }
  297. this.widgetData = null;
  298. if (widgetDataStr){
  299. this.widgetData = JSON.decode(MWF.decodeJsonString(widgetDataStr));
  300. this.widgetData.updateTime = data.updateTime;
  301. }
  302. },
  303. reloadWidget: function(data, input, oldValue){
  304. this.getWidgetData(data);
  305. if (this.widgetData){
  306. var oldWidgetData = (this.page.widgetList && oldValue) ? this.page.widgetList[oldValue] : null;
  307. this.clearWidgetList(oldValue);
  308. if (this.checkWidget(data, input)){
  309. this.node.empty();
  310. this.loadWidget();
  311. this.addWidgetList();
  312. }else{
  313. if (oldWidgetData){
  314. if (!this.page.widgetList) this.page.widgetList = {};
  315. this.page.widgetList[oldValue] = oldWidgetData;
  316. }else{
  317. this.clearWidgetList(oldValue);
  318. this.node.empty();
  319. this.loadIcon();
  320. }
  321. this.json.widgetSelected = oldValue;
  322. if (!oldValue){
  323. if (input) input.options[0].set("selected", true);
  324. }else{
  325. if (input){
  326. for (var i=0; i<input.options.length; i++){
  327. if (input.options[i].value===oldValue){
  328. input.options[i].set("selected", true);
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }else{
  336. this.json.widgetSelected = oldValue;
  337. if (input){
  338. if (!oldValue){
  339. input.options[0].set("selected", true);
  340. }else{
  341. for (var i=0; i<input.options.length; i++){
  342. if (input.options[i].value===oldValue){
  343. input.options[i].set("selected", true);
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. },
  351. regetWidgetData: function(){
  352. var flag = false;
  353. if (this.json.widgetSelected && this.json.widgetSelected!=="none" && this.json.widgetType!=="script"){
  354. MWF.Actions.get("x_portal_assemble_designer").getWidget(this.json.widgetSelected, function(json){
  355. if (!this.widgetData || this.widgetData.updateTime!==json.data.updateTime){
  356. this.getWidgetData(json.data);
  357. flag = true;
  358. }
  359. }.bind(this), null, false);
  360. }
  361. return flag;
  362. },
  363. getConflictFields: function(){
  364. var moduleNames = [];
  365. if (this.widgetData){
  366. Object.each(this.widgetData.json.moduleList, function(o, key){
  367. var check = this.page.checkModuleId(key, o.type, this.widgetData.json.id);
  368. if (check.fieldConflict){
  369. moduleNames.push(key)
  370. }else if (check.elementConflict){
  371. o.changeId = this.json.id+"_"+key;
  372. }
  373. }.bind(this));
  374. }
  375. return moduleNames;
  376. },
  377. checkWidget: function(data, input){
  378. return true;
  379. var moduleNames = this.getConflictFields();
  380. // Object.each(this.widgetData.json.moduleList, function(o, key){
  381. // var check = this.page.checkModuleId(key, o.type, this.widgetData.json.id);
  382. // if (check.fieldConflict){
  383. // moduleNames.push(key)
  384. // }else if (check.elementConflict){
  385. // o.changeId = this.json.id+"_"+key;
  386. // }
  387. // }.bind(this));
  388. if (moduleNames.length){
  389. var txt = this.page.designer.lp.widgetNameConflictInfor;
  390. txt = txt.replace("{name}", moduleNames.join(", "));
  391. //var p = (input) ? input.getPosition() : this.node.getPosition();
  392. // var p = this.node.getPosition(document.body);
  393. // this.page.designer.alert("error", {
  394. // "event": {
  395. // "x": p.x+150,
  396. // "y": p.y+80
  397. // }
  398. // }, this.page.designer.lp.widgetNameConflictTitle, txt, 400, 200);
  399. this.page.designer.notice(txt, "error", this.node);
  400. return false;
  401. }
  402. return true;
  403. },
  404. loadWidget: function(data) {
  405. this.widgetData.json.style = this.page.json.style;
  406. this.widgetData.json.properties = this.page.json.properties;
  407. this.widgetData.json.jsheader = {"code": "", "html": ""};
  408. this.widgetData.json.events = {};
  409. this.widgetData.json.pageStyleType = this.page.json.pageStyleType;
  410. //this.widgetData.json.id = this.json.id;
  411. this.widgetModule = new MWF.PCWidget.Page(this.page, this.node,{
  412. parentpageIdList : this.getParentpageIdList(),
  413. level : this.getLevel()
  414. });
  415. this.widgetModule.widgetSelector = this.getWidgetSelector();
  416. this.widgetModule.widgetSelectedValue = this.getWidgetSelectedValue();
  417. this.widgetModule.level1Widget = this.getLevel1Widget();
  418. this.widgetModule.load(this.widgetData);
  419. //this.createRefreshNode();
  420. },
  421. destroy: function(){
  422. this.page.moduleList.erase(this);
  423. this.page.moduleNodeList.erase(this.node);
  424. this.page.moduleElementNodeList.erase(this.node);
  425. this.clearWidgetList(this.json.widgetSelected);
  426. this.node.destroy();
  427. this.actionArea.destroy();
  428. delete this.page.json.moduleList[this.json.id];
  429. this.json = null;
  430. delete this.json;
  431. this.treeNode.destroy();
  432. }
  433. });
  434. MWF.xApplication.portal.PageDesigner.Module.Widget.Page = new Class({
  435. Extends: MWF.PCPage,
  436. initialize: function(page, container, options){
  437. this.setOptions(options);
  438. this.toppage = page.toppage || page;
  439. this.parentpage = page;
  440. this.parentform = page;
  441. this.css = this.parentpage.css;
  442. this.container = container;
  443. this.form = this;
  444. this.page = this;
  445. this.isWidget = true;
  446. this.isSubform = true;
  447. this.moduleType = "widget";
  448. this.moduleList = [];
  449. this.moduleNodeList = [];
  450. this.moduleContainerNodeList = [];
  451. this.moduleElementNodeList = [];
  452. this.moduleComponentNodeList = [];
  453. // this.moduleContainerList = [];
  454. this.dataTemplate = {};
  455. this.designer = this.parentpage.designer;
  456. this.selectedModules = [];
  457. },
  458. load : function(data){
  459. this.data = data;
  460. this.json = data.json;
  461. this.html = data.html;
  462. this.json.mode = this.options.mode;
  463. this.container.set("html", this.html);
  464. this.loadDomModules();
  465. //this.setCustomStyles();
  466. //this.node.setProperties(this.json.properties);
  467. //this.setNodeEvents();
  468. if (this.options.mode==="Mobile"){
  469. if (oldStyleValue) this._setEditStyle("pageStyleType", null, oldStyleValue);
  470. }
  471. },
  472. loadDomModules: function(){
  473. this.node = this.container.getFirst();
  474. this.node.set("id", this.json.id);
  475. this.node.setStyles((this.options.mode==="Mobile") ? this.css.pageMobileNode : this.css.pageNode);
  476. this.node.store("module", this);
  477. this.loadDomTree();
  478. },
  479. loadDomTree: function(){
  480. this.createPageTreeNode();
  481. this.parseModules(this, this.node);
  482. },
  483. createPageTreeNode: function(){
  484. this.treeNode = {
  485. "insertChild": function(){return this;},
  486. "appendChild": function(){return this;},
  487. "selectNode": function(){},
  488. "node": null,
  489. "parentNode": {}
  490. };
  491. this.treeNode.module = this;
  492. }
  493. });