Application.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class Application 门户中嵌入的系统component对象或网页iframe(模块部署中配置的网页URL)。
  3. * @o2cn 嵌入的系统应用
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var application = this.form.get("fieldId"); //获取组件
  8. * //方法2
  9. * var application = this.target; //在组件本身的脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Portal}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.Application = MWF.APPApplication = new Class(
  16. /** @lends MWF.xApplication.process.Xform.Application# */
  17. {
  18. Extends: MWF.APP$Module,
  19. options: {
  20. /**
  21. * component对象初始化后,加载之前触发,this.event可获取component对象。
  22. * @event MWF.xApplication.process.Xform.Application#queryLoadApplication
  23. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  24. */
  25. "moduleEvents": ["load", "queryLoad", "postLoad", "queryLoadApplication"]
  26. },
  27. _loadUserInterface: function(){
  28. /**
  29. * @ignore
  30. * @member parentLine
  31. * @memberOf MWF.xApplication.process.Xform.Application#
  32. */
  33. /**
  34. * @ignore
  35. * @member getSource
  36. * @memberOf MWF.xApplication.process.Xform.Application#
  37. */
  38. this.node.empty();
  39. if( this.json.activeType !== "delay" ){
  40. if( !o2.api ){
  41. MWF.require("MWF.framework", function () {
  42. this.loadApplication();
  43. }.bind(this));
  44. }else{
  45. this.loadApplication();
  46. }
  47. }
  48. },
  49. /**
  50. * @summary 当组件被设置为延迟激活,通过active方法激活
  51. * @param {Function} callback 激活后的回调方法(不保证组件加载完成),另外已经激活过该方法还会被执行。
  52. * @example
  53. * var app = this.form.get("fieldId");
  54. * app.active(function(){
  55. * //do someting
  56. * })
  57. */
  58. active: function(callback){
  59. if (!this.loaded) {
  60. this.reload(callback);
  61. } else {
  62. if (callback) callback();
  63. }
  64. },
  65. /**
  66. * @summary 重新加载嵌入对象
  67. * @param {Function} callback 重载后的回调
  68. * @example
  69. * this.form.get("fieldId").reload()
  70. */
  71. reload: function(callback){
  72. this.clean();
  73. this.loadApplication( callback );
  74. },
  75. /**
  76. * @summary 清除当前嵌入的对象
  77. * @example
  78. * this.form.get("fieldId").clean()
  79. */
  80. clean: function(){
  81. if(this.component){
  82. try{
  83. this.component.close();
  84. if(this.node)this.node.empty();
  85. }catch (e) {
  86. console.log(e);
  87. if(this.node)this.node.empty();
  88. }
  89. this.component = null;
  90. }
  91. if( this.iframe ){
  92. this.iframe.destroy();
  93. if(this.node)this.node.empty();
  94. this.iframe = null;
  95. }
  96. },
  97. loadApplication: function ( callback ) {
  98. this.clean();
  99. if(this.node){
  100. this.node.empty();
  101. this.node.setStyles({
  102. "position": "relative"
  103. });
  104. if( !this.json.styles || (!this.json.styles["background"] && !this.json.styles["background-color"] )){
  105. this.node.setStyle("background-color", "#eee");
  106. }
  107. }
  108. var status = this.getComponentStatus() || {};
  109. var options = this.getComponentOptions() || {};
  110. this.getComponentPath(function (componentPath) {
  111. if( componentPath && componentPath.indexOf("@url:") === 0 ){
  112. this.loadIframe( componentPath.substring(5, componentPath.length ), callback );
  113. }else{
  114. this.loadComponent( componentPath, status, options, callback );
  115. }
  116. }.bind(this))
  117. },
  118. /**
  119. * @summary 加载Iframe
  120. * @param {String} src iframe的src,如'https://www.baidu.com/'
  121. * @example
  122. * this.form.get("fieldId").clean(); //清除当前嵌入的对象
  123. * this.form.get("fieldId").loadIframe('https://www.baidu.com/'); //加载iframe
  124. */
  125. loadIframe: function( src, callback ){
  126. var attr = {
  127. "src": src,
  128. "width": "100%",
  129. "height": "100%",
  130. "frameborder": "0px",
  131. "scrolling": "auto",
  132. "seamless": "seamless"
  133. };
  134. /**
  135. * @summary 当模块部署中配置的是@url:开头的链接时,嵌入的iframe.
  136. * @member {Object}
  137. * @example
  138. * var iframe = this.form.get("fieldId").iframe; //获取iframe
  139. * iframe.src; //获取iframe的地址
  140. */
  141. this.iframe = new Element("iframe", attr).inject( this.node );
  142. this.loaded = true;
  143. if(callback)callback();
  144. },
  145. /**
  146. * @summary 加载系统组件
  147. * @param {String} path 组件的路径,如'Calendar'
  148. * @param {Object} [status] 组件的状态
  149. * @param {Object} [options] 组件的选项
  150. * @example
  151. * this.form.get("fieldId").clean(); //清除当前嵌入的对象
  152. * this.form.get("fieldId").loadComponent('Calendar'); //加载日程安排
  153. * @example
  154. * this.form.get("fieldId").clean(); //清除当前嵌入的对象
  155. * this.form.get("fieldId").loadComponent('cms.Module', {
  156. * "columnId":"25434995-45d2-4c9a-a344-55ad0deff071"
  157. * }); //加载id为25434995-45d2-4c9a-a344-55ad0deff071的内容管理栏目
  158. */
  159. loadComponent: function ( path, status, options, callback ) {
  160. var clazz = MWF.xApplication;
  161. if( o2.typeOf(path) !== "string" )return;
  162. path.split(".").each(function (a) {
  163. clazz[a] = clazz[a] || {};
  164. clazz = clazz[a];
  165. });
  166. clazz.options = clazz.options || {};
  167. var _load = function () {
  168. if( clazz.Main ){
  169. var opt = options || {};
  170. var stt = status || {};
  171. opt.embededParent = this.node;
  172. /**
  173. * @summary 嵌入的component对象.
  174. * @member {Object}
  175. * @example
  176. * var app = this.form.get("fieldId").component; //获取component对象
  177. * app.recordStatus(); //获取应用的当前状态
  178. * app.refresh(); //刷新应用
  179. * app.dialog(option); //弹出一个对话框(详见MWF.widget.Dialog)
  180. * app.notice(content, type, target, where, offset); //显示一个通知消息
  181. * app.confirm(type, e, title, text, width, height, ok, cancel); //显示一个确认框
  182. * app.alert(type, e, title, text, width, height); //弹出一个信息框
  183. * app.addEvent(type, fun); //为应用绑定一个事件
  184. */
  185. this.component = new clazz.Main(this.form.app.desktop, opt);
  186. this.component.status = stt;
  187. this.fireEvent("queryLoadApplication", this.component);
  188. this.component.load();
  189. this.component.setEventTarget(this.form.app);
  190. var _self = this;
  191. this.component.refresh = function () {
  192. if( layout.inBrowser ){
  193. window.location.reload();
  194. }else{
  195. _self.form.app.refresh();
  196. }
  197. };
  198. }else{
  199. if( MWF.xApplication.process && MWF.xApplication.process.Xform && MWF.xApplication.process.Xform.LP ){
  200. this.form.app.notice(MWF.xApplication.process.Xform.LP.applicationNotFound+":"+path, "error");
  201. }else{
  202. this.form.app.notice(this.form.app.lp.applicationNotFound+":"+path, "error");
  203. }
  204. }
  205. this.loaded = true;
  206. if(callback)callback();
  207. }.bind(this);
  208. try{
  209. MWF.xDesktop.requireApp(path, "lp."+o2.language, null, false);
  210. MWF.xDesktop.requireApp(path, "Main", null, false);
  211. if (clazz.loading && clazz.loading.then){
  212. clazz.loading.then(function(){
  213. _load();
  214. });
  215. }else{
  216. _load();
  217. }
  218. }catch (e) {
  219. this.form.app.notice( e.message, "error" );
  220. }
  221. },
  222. /**
  223. * @summary 获取获取表单设计配置的component对象的路径
  224. * @param {Function} callback 获取路径后的回调方法,参数为路径
  225. * @example
  226. * this.form.get("fieldId").getComponentPath(function(path){
  227. * //path为路径
  228. * })
  229. */
  230. getComponentPath: function(callback){
  231. var path;
  232. if (this.json.componentType==="script"){
  233. if (this.json.componentScript && this.json.componentScript.code){
  234. path = this.form.Macro.exec(this.json.componentScript.code, this);
  235. }
  236. }else{
  237. if (this.json.componentSelected && this.json.componentSelected!=="none"){
  238. path = this.json.componentSelected;
  239. }else{
  240. path = "";
  241. }
  242. }
  243. Promise.resolve(path).then(function (p) {
  244. callback(p || "");
  245. })
  246. },
  247. /**
  248. * @summary 获取表单设计配置的component对象的参数
  249. * @return 设置的参数
  250. * @example
  251. * var param = this.form.get("fieldId").getComponentOptions()
  252. */
  253. getComponentOptions : function(){
  254. var params = "";
  255. if( this.json.optionsType === "map" ){
  256. params = this.json.optionsMapList;
  257. }else if( this.json.optionsType === "script" ){
  258. var code = (this.json.optionsScript) ? this.json.optionsScript.code : "";
  259. if (code){
  260. params = this.form.Macro.exec(code, this);
  261. }
  262. }
  263. return params;
  264. },
  265. /**
  266. * @summary 获取表单设计配置的component对象的状态
  267. * @return 设置的状态
  268. * @example
  269. * var param = this.form.get("fieldId").getComponentStatus()
  270. */
  271. getComponentStatus: function(){
  272. var params = "";
  273. if( this.json.statusType === "map" ){
  274. params = this.json.statusMapList;
  275. }else if( this.json.statusType === "script" ){
  276. var code = (this.json.statusScript) ? this.json.statusScript.code : "";
  277. if (code){
  278. params = this.form.Macro.exec(code, this);
  279. }
  280. }
  281. return params;
  282. }
  283. });