Subpage.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Subpage = MWF.APPSubpage = new Class({
  3. Extends: MWF.APP$Module,
  4. _loadUserInterface: function(){
  5. this.node.empty();
  6. this.modules = [];
  7. this.moduleList = {};
  8. this.getSubpage(function(){
  9. this.loadSubpage();
  10. }.bind(this));
  11. },
  12. reload: function(){
  13. this.clean();
  14. this.getSubpage(function(){
  15. this.loadSubpage();
  16. }.bind(this));
  17. },
  18. clean: function(){
  19. (this.modules || []).each(function(module){
  20. this.form.modules.erase(module);
  21. }.bind(this));
  22. Object.each(this.moduleList || {}, function (module, formKey) {
  23. delete this.form.json.moduleList[formKey];
  24. }.bind(this));
  25. if( this.subpageData && this.subpageData.json.id ){
  26. var id = this.subpageData.json.id;
  27. // if( this.form.subformLoaded && this.form.subformLoaded.length ){
  28. // this.form.subformLoaded.erase(id);
  29. // }
  30. if( this.parentpageIdList && this.parentpageIdList.length){
  31. this.parentpageIdList.erase(id);
  32. }
  33. }
  34. if( this.json.id && this.form.subpageModules && this.form.subpageModules[ this.json.id ] ){
  35. this.form.subpageModules[ this.json.id ] = {}
  36. }
  37. this.modules = [];
  38. this.moduleList = {};
  39. this.node.empty();
  40. },
  41. loadCss: function(){
  42. if (this.subpageData.json.css && this.subpageData.json.css.code){
  43. var cssText = this.form.parseCSS(this.subpageData.json.css.code);
  44. var rex = new RegExp("(.+)(?=\\{)", "g");
  45. var match;
  46. var id = this.form.json.id.replace(/\-/g, "");
  47. while ((match = rex.exec(cssText)) !== null) {
  48. var prefix = ".css" + id + " ";
  49. var rule = prefix + match[0];
  50. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  51. rex.lastIndex = rex.lastIndex + prefix.length;
  52. }
  53. var styleNode = $("style"+this.form.json.id);
  54. if (!styleNode){
  55. var styleNode = document.createElement("style");
  56. styleNode.setAttribute("type", "text/css");
  57. styleNode.id="style"+this.form.json.id;
  58. styleNode.inject(this.form.container, "before");
  59. }
  60. if(styleNode.styleSheet){
  61. var setFunc = function(){
  62. styleNode.styleSheet.cssText += cssText;
  63. };
  64. if(styleNode.styleSheet.disabled){
  65. setTimeout(setFunc, 10);
  66. }else{
  67. setFunc();
  68. }
  69. }else{
  70. var cssTextNode = document.createTextNode(cssText);
  71. styleNode.appendChild(cssTextNode);
  72. }
  73. }
  74. },
  75. checkSubpageNested : function( id ){
  76. if( this.parentpageIdList ){
  77. return !this.parentpageIdList.contains( id );
  78. }else{
  79. return ![ this.form.json.id ].contains( id );
  80. }
  81. },
  82. getParentpageIdList : function(){
  83. var parentpageIdList;
  84. if( this.parentpageIdList ){
  85. parentpageIdList = Array.clone( this.parentpageIdList );
  86. parentpageIdList.push( this.subpageData.json.id )
  87. }else{
  88. parentpageIdList = [ this.form.json.id, this.subpageData.json.id ];
  89. }
  90. return parentpageIdList;
  91. },
  92. loadSubpage: function(){
  93. if (this.subpageData ){
  94. if( this.checkSubpageNested( this.subpageData.json.id ) ){
  95. //this.form.addEvent("postLoad", function(){
  96. this.loadCss();
  97. this.form.subpageModules = this.form.subpageModules || {};
  98. var subpageModules = this.form.subpageModules[ this.json.id ] = {};
  99. var params = this.getPageParamenters();
  100. if( typeOf(params) === "object" && this.form.Macro && this.form.Macro.environment ){
  101. var environment = this.form.Macro.environment;
  102. environment.subpageParameters = environment.subpageParameters || {};
  103. environment.subpageParameters[ this.json.id ] = params;
  104. }
  105. this.node.set("html", this.subpageData.html);
  106. Object.each(this.subpageData.json.moduleList, function(module, key){
  107. var formKey = key;
  108. if (this.form.json.moduleList[key]){
  109. formKey = this.json.id+"_"+key;
  110. var moduleNode = this.node.getElement("#"+key);
  111. if (moduleNode) moduleNode.set("id", formKey);
  112. module.orgiginalId = key;
  113. module.id = formKey;
  114. }
  115. this.form.json.moduleList[formKey] = module;
  116. this.moduleList[formKey] = module;
  117. }.bind(this));
  118. var moduleNodes = this.form._getModuleNodes(this.node);
  119. moduleNodes.each(function(node){
  120. if (node.get("MWFtype")!=="form"){
  121. var _self = this;
  122. var json = this.form._getDomjson(node);
  123. var module = this.form._loadModule(json, node, function(){
  124. this.subpage = _self;
  125. this.parentpageIdList = _self.getParentpageIdList();
  126. });
  127. this.form.modules.push(module);
  128. this.modules.push(module);
  129. subpageModules[ json.orgiginalId || json.id ] = module;
  130. }
  131. }.bind(this));
  132. //}.bind(this));
  133. }else{
  134. this.form.notice(MWF.xApplication.process.Xform.LP.subpageNestedError, "error");
  135. }
  136. }
  137. if( this.form.subpageLoadedCount ){
  138. this.form.subpageLoadedCount++;
  139. }else{
  140. this.form.subpageLoadedCount = 1
  141. }
  142. this.form.checkSubformLoaded();
  143. },
  144. getSubpage: function(callback){
  145. var method = (this.form.json.mode !== "Mobile" && !layout.mobile) ? "getPageByName": "getPageByNameMobile";
  146. if (this.json.subpageType==="script"){
  147. if (this.json.subpageScript && this.json.subpageScript.code){
  148. var formNome = this.form.Macro.exec(this.json.subpageScript.code, this);
  149. if (formNome){
  150. var app = this.form.businessData.pageInfor.portal;
  151. o2.Actions.get("x_portal_assemble_surface")[method](formNome, app, function(json){
  152. this.getSubpageData(json.data);
  153. if (callback) callback();
  154. }.bind(this));
  155. }else{
  156. if (callback) callback();
  157. }
  158. }
  159. }else{
  160. if (this.json.subpageSelected && this.json.subpageSelected!=="none"){
  161. var app = this.form.businessData.pageInfor.portal;
  162. o2.Actions.get("x_portal_assemble_surface")[method](this.json.subpageSelected, app, function(json){
  163. this.getSubpageData(json.data);
  164. if (callback) callback();
  165. }.bind(this));
  166. }else{
  167. if (callback) callback();
  168. }
  169. }
  170. },
  171. getSubpageData: function(data){
  172. var subpageDataStr = null;
  173. // if (this.form.json.mode !== "Mobile" && !layout.mobile){
  174. // subpageDataStr = data.data;
  175. // }else{
  176. // subpageDataStr = data.mobileData;
  177. // }
  178. subpageDataStr = data.data;
  179. this.subpageData = null;
  180. if (subpageDataStr){
  181. var jsonStr = o2.bindJson(MWF.decodeJsonString(subpageDataStr), {"lp": MWF.xApplication.process.Xform.LP.form});
  182. this.subpageData = JSON.decode(jsonStr);
  183. this.subpageData.updateTime = data.updateTime;
  184. }
  185. },
  186. getPageParamenters : function(){
  187. var params = null;
  188. if( this.json.parameterType === "map" ){
  189. params = this.json.parametersMapList;
  190. }else if( this.json.parameterType === "script" ){
  191. var code = (this.json.parametersScript) ? this.json.parametersScript.code : "";
  192. if (code){
  193. params = this.form.Macro.exec(code, this);
  194. }
  195. }
  196. return params;
  197. }
  198. });