Widget.js 8.2 KB

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