Widget.js 8.2 KB

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