Widget.js 7.5 KB

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