Widget.js 7.1 KB

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