Widget.js 20 KB

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