Widget.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Widget 门户的部件组件。
  3. * @o2cn 部件
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var widget = this.form.get("fieldId"); //获取组件
  8. * //方法2
  9. * var widget = this.target; //在组件本身的脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Portal}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Widget = MWF.APPWidget = new Class(
  16. /** @lends MWF.xApplication.process.Xform.Widget# */
  17. {
  18. Extends: MWF.APP$Module,
  19. options: {
  20. /**
  21. * 部件的设计已经获取到,但还没有插入html及生成内部组件。
  22. * @event MWF.xApplication.process.Xform.Widget#beforeModulesLoad
  23. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  24. */
  25. /**
  26. * 部件的设计已经获取到,已经插入html,组件json已经获取到,但未生成内部组件。
  27. * * @example
  28. * //获取部件所有组件id
  29. * var moduleIdList = Object.keys(this.target.widgetData.json.moduleList);
  30. * @event MWF.xApplication.process.Xform.Widget#modulesLoad
  31. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  32. */
  33. /**
  34. * 部件内部组件加载完成。
  35. * @example
  36. * //获取部件所有组件id
  37. * var moduleIdList = Object.keys(this.target.widgetData.json.moduleList);
  38. * //获取部件所有组件
  39. * var moduleList = moduleIdList.map(function(id){
  40. * return this.form.get(id, widgetId); //widgetId为当前部件ID,布局组件有可能id冲突,通过widgetId来确定当前部件的组件
  41. * }.bind(this))
  42. * @event MWF.xApplication.process.Xform.Widget#afterModulesLoad
  43. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  44. */
  45. "moduleEvents": ["load", "queryLoad", "postLoad", "beforeModulesLoad", "modulesLoad", "afterModulesLoad"]
  46. },
  47. _loadUserInterface: function(){
  48. this.node.empty();
  49. this.modules = [];
  50. this.moduleList = {};
  51. this.getWidget(function(){
  52. this.loadWidget();
  53. }.bind(this));
  54. },
  55. /**
  56. * @summary 重新加载部件
  57. * @example
  58. * this.form.get("fieldId").reload()
  59. */
  60. reload: function(){
  61. this.clean();
  62. this.getWidget(function(){
  63. this.loadWidget();
  64. }.bind(this));
  65. },
  66. clean: function(){
  67. (this.modules || []).each(function(module){
  68. if (this.form.all[module.json.id]) delete this.form.all[module.json.id];
  69. if (this.form.forms[module.json.id])delete this.form.forms[module.json.id];
  70. this.form.modules.erase(module);
  71. }.bind(this));
  72. Object.each(this.moduleList || {}, function (module, formKey) {
  73. delete this.form.json.moduleList[formKey];
  74. }.bind(this));
  75. if( this.widgetData && this.widgetData.json.id ){
  76. var id = this.widgetData.json.id;
  77. // if( this.form.subformLoaded && this.form.subformLoaded.length ){
  78. // this.form.subformLoaded.erase(id);
  79. // }
  80. if( this.parentpageIdList && this.parentpageIdList.length){
  81. this.parentpageIdList.erase(id);
  82. }
  83. }
  84. if( this.json && this.json.id && this.form.widgetModules && this.form.widgetModules[ this.json.id ] ){
  85. this.form.widgetModules[ this.json.id ] = {};
  86. }
  87. this.modules = [];
  88. this.moduleList = {};
  89. this.node.empty();
  90. },
  91. loadCss: function(){
  92. if (this.widgetData.json.css && this.widgetData.json.css.code){
  93. var cssText = this.widgetData.json.css.code;
  94. //删除注释
  95. cssText = cssText.replace(/\/\*[\s\S]*?\*\/\n|([^:]|^)\/\/.*\n$/g, '').replace(/\\n/, '');
  96. cssText = this.form.parseCSS(cssText);
  97. var rex = new RegExp("(.+)(?=\\{)", "g");
  98. var match;
  99. var id = this.form.json.id.replace(/\-/g, "");
  100. var prefix = ".css" + id + " ";
  101. while ((match = rex.exec(cssText)) !== null) {
  102. var rulesStr = match[0];
  103. var startWith = rulesStr.substring(0, 1);
  104. if (startWith === "@" || startWith === ":" || rulesStr.indexOf("%") !== -1) {
  105. }else if (rulesStr.trim()==='from' || rulesStr.trim()==='to'){
  106. } else {
  107. if (rulesStr.indexOf(",") != -1) {
  108. //var rules = rulesStr.split(/\s*,\s*/g);
  109. var rules = rulesStr.split(/,/g);
  110. rules = rules.map(function (r) {
  111. return prefix + r;
  112. });
  113. var rule = rules.join(",");
  114. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  115. rex.lastIndex = rex.lastIndex + (prefix.length * rules.length);
  116. } else {
  117. var rule = prefix + match[0];
  118. cssText = cssText.substring(0, match.index) + rule + cssText.substring(rex.lastIndex, cssText.length);
  119. rex.lastIndex = rex.lastIndex + prefix.length;
  120. }
  121. }
  122. }
  123. var styleNode = $("style"+this.form.json.id);
  124. if (!styleNode){
  125. var styleNode = document.createElement("style");
  126. styleNode.setAttribute("type", "text/css");
  127. styleNode.id="style"+this.form.json.id;
  128. styleNode.inject(this.form.container, "before");
  129. }
  130. if(styleNode.styleSheet){
  131. var setFunc = function(){
  132. styleNode.styleSheet.cssText += cssText;
  133. };
  134. if(styleNode.styleSheet.disabled){
  135. setTimeout(setFunc, 10);
  136. }else{
  137. setFunc();
  138. }
  139. }else{
  140. var cssTextNode = document.createTextNode(cssText);
  141. styleNode.appendChild(cssTextNode);
  142. }
  143. }
  144. },
  145. checkWidgetNested : function( id ){
  146. if( this.parentpageIdList ){
  147. return !this.parentpageIdList.contains( id );
  148. }else{
  149. return ![ this.form.json.id ].contains( id );
  150. }
  151. },
  152. getParentpageIdList : function(){
  153. var parentpageIdList;
  154. if( this.parentpageIdList ){
  155. parentpageIdList = Array.clone( this.parentpageIdList );
  156. parentpageIdList.push( this.widgetData.json.id )
  157. }else{
  158. parentpageIdList = [ this.form.json.id, this.widgetData.json.id ];
  159. }
  160. return parentpageIdList;
  161. },
  162. loadWidget: function(){
  163. if (this.widgetData ){
  164. if( this.checkWidgetNested( this.widgetData.json.id ) ){
  165. //this.form.addEvent("postLoad", function(){
  166. this.fireEvent("beforeModulesLoad");
  167. this.loadCss();
  168. this.modules = [];
  169. this.moduleList = {};
  170. this.form.widgetModules = this.form.widgetModules || {};
  171. var widgetModules = this.form.widgetModules[ this.json.id ] = {};
  172. var params = this.getPageParamenters();
  173. if( typeOf(params) === "object" && this.form.Macro && this.form.Macro.environment ){
  174. var environment = this.form.Macro.environment;
  175. environment.widgetParameters = environment.widgetParameters || {};
  176. environment.widgetParameters[ this.json.id ] = params;
  177. }
  178. this.node.set("html", this.widgetData.html);
  179. if( this.widgetData.json.styles ){
  180. this.node.getFirst().setStyles(this.widgetData.json.styles);
  181. }
  182. Object.each(this.widgetData.json.moduleList, function(module, key){
  183. var formKey = key;
  184. if (this.form.json.moduleList[key]){
  185. formKey = this.json.id+"_"+key;
  186. var moduleNode = this.node.getElement("#"+key);
  187. if (moduleNode) moduleNode.set("id", formKey);
  188. module.orgiginalId = key;
  189. module.id = formKey;
  190. }
  191. this.form.json.moduleList[formKey] = module;
  192. this.moduleList[formKey] = module;
  193. }.bind(this));
  194. this.fireEvent("modulesLoad");
  195. var moduleNodes = this.form._getModuleNodes(this.node);
  196. moduleNodes.each(function(node){
  197. if (node.get("MWFtype")!=="form"){
  198. var _self = this;
  199. var json = this.form._getDomjson(node);
  200. var module = this.form._loadModule(json, node, function(){
  201. this.widget = _self;
  202. this.parentpageIdList = _self.getParentpageIdList();
  203. });
  204. this.form.modules.push(module);
  205. this.modules.push(module);
  206. widgetModules[ json.orgiginalId || json.id ] = module;
  207. }
  208. }.bind(this));
  209. this.fireEvent("afterModulesLoad");
  210. //}.bind(this));
  211. }else{
  212. this.form.notice(MWF.xApplication.process.Xform.LP.widgetNestedError, "error");
  213. }
  214. }
  215. if( this.form.widgetLoadedCount ){
  216. this.form.widgetLoadedCount++;
  217. }else{
  218. this.form.widgetLoadedCount = 1
  219. }
  220. this.form.checkSubformLoaded();
  221. },
  222. getWidget: function(callback){
  223. var method = (this.form.options.mode !== "Mobile" && !layout.mobile) ? "getWidgetByName" : "getWidgetByNameMobile";
  224. if (this.json.widgetType==="script"){
  225. if (this.json.widgetScript && this.json.widgetScript.code){
  226. var data = this.form.Macro.exec(this.json.widgetScript.code, this);
  227. if (data){
  228. var widgetName, app;
  229. if (typeOf(data) === "string") {
  230. widgetName = data;
  231. } else {
  232. if (data.application) app = data.application;
  233. if (data.widget) widgetName = data.widget;
  234. }
  235. if (widgetName) {
  236. if (!app) app = this.form.businessData.pageInfor.portal;
  237. o2.Actions.get("x_portal_assemble_surface")[method](widgetName, app, function(json){
  238. this.getWidgetData(json.data);
  239. if (callback) callback();
  240. }.bind(this));
  241. }else{
  242. if (callback) callback();
  243. }
  244. }else{
  245. if (callback) callback();
  246. }
  247. }
  248. }else{
  249. if (this.json.widgetSelected && this.json.widgetSelected!=="none"){
  250. var widgetData = (this.form.app.relatedFormMap) ? this.form.app.relatedFormMap[this.json.widgetSelected] : null;
  251. if (widgetData){
  252. this.getWidgetData({"data": widgetData.data});
  253. if (callback) callback();
  254. }else{
  255. var app;
  256. if (this.json.widgetAppSelected) {
  257. app = this.json.widgetAppSelected;
  258. } else {
  259. app = this.form.businessData.pageInfor.portal;
  260. }
  261. o2.Actions.get("x_portal_assemble_surface")[method](this.json.widgetSelected, app, function(json){
  262. this.getWidgetData(json.data);
  263. if (callback) callback();
  264. }.bind(this));
  265. }
  266. }else{
  267. if (callback) callback();
  268. }
  269. }
  270. },
  271. getWidgetData: function(data){
  272. var widgetDataStr = null;
  273. //if (this.form.options.mode !== "Mobile" && !layout.mobile){
  274. // widgetDataStr = data.data;
  275. //}else{
  276. // widgetDataStr = data.mobileData;
  277. //}
  278. widgetDataStr = data.data;
  279. this.widgetData = null;
  280. if (widgetDataStr){
  281. if( this.form.isParseLanguage ){
  282. var jsonStr = o2.bindJson(MWF.decodeJsonString(widgetDataStr), {"lp": MWF.xApplication.process.Xform.LP.form});
  283. this.widgetData = JSON.decode(jsonStr);
  284. }else{
  285. this.widgetData = JSON.decode(MWF.decodeJsonString(widgetDataStr));
  286. }
  287. this.widgetData.updateTime = data.updateTime;
  288. }
  289. },
  290. /**
  291. * @summary 获取设计部件时设置的参数
  292. * @return 设置的参数
  293. * @example
  294. * var param = this.form.get("fieldId").getPageParamenters()
  295. */
  296. getPageParamenters : function(){
  297. var params = null;
  298. if( this.json.parameterType === "map" ){
  299. params = this.json.parametersMapList;
  300. }else if( this.json.parameterType === "script" ){
  301. var code = (this.json.parametersScript) ? this.json.parametersScript.code : "";
  302. if (code){
  303. params = this.form.Macro.exec(code, this);
  304. }
  305. }
  306. return params;
  307. }
  308. });