$Module.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. MWF.require("MWF.widget.Common", null, false);
  2. /** @classdesc $Module 组件类,此类为所有组件的父类。
  3. * @class
  4. * @o2category FormComponents
  5. * @hideconstructor
  6. * */
  7. MWF.xApplication.process.Xform.$Module = MWF.APP$Module = new Class(
  8. /** @lends MWF.xApplication.process.Xform.$Module# */
  9. {
  10. Implements: [Events],
  11. options: {
  12. /**
  13. * 组件加载前触发。
  14. * @event MWF.xApplication.process.Xform.$Module#queryLoad
  15. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  16. */
  17. /**
  18. * 组件加载时触发.
  19. * @event MWF.xApplication.process.Xform.$Module#load
  20. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  21. */
  22. /**
  23. * 组件加载后触发.
  24. * @event MWF.xApplication.process.Xform.$Module#postLoad
  25. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  26. */
  27. "moduleEvents": ["load", "queryLoad", "postLoad"]
  28. },
  29. initialize: function(node, json, form, options){
  30. /**
  31. * @summary 组件的节点,mootools封装过的Dom对象,可以直接使用原生的js和moootools方法访问和操作该对象。
  32. * @see https://mootools.net/core/docs/1.6.0/Element/Element
  33. * @member {Element}
  34. * @example
  35. * //可以在脚本中获取该组件
  36. * var field = this.form.get("fieldId"); //获取组件对象
  37. * field.node.setStyle("font-size","12px"); //给节点设置样式
  38. */
  39. this.node = $(node);
  40. this.node.store("module", this);
  41. /**
  42. * @summary 组件的配置信息,比如id,类型,是否只读等等。可以在修改组件的queryLoad事件里修改该配置来对组件做一些改变。
  43. * @member {JsonObject}
  44. * @example
  45. * //可以在脚本中获取该组件
  46. * var json = this.form.get("fieldId").json; //获取组件对象
  47. * var id = json.id; //获取组件的id
  48. * var type = json.type; //获取组件的类型,如Textfield 为文本输入组件,Select为下拉组件
  49. * json.isReadonly = true; //设置组件为只读。
  50. */
  51. this.json = json;
  52. /**
  53. * @summary 组件的所在表单对象.
  54. * @member {MWF.xApplication.process.Xform.Form}
  55. * @example
  56. * var form = this.form.get("fieldId").form; //获取组件所在表单对象
  57. * var container = form.container; //获取表单容器
  58. */
  59. this.form = form;
  60. },
  61. _getSource: function(){
  62. var parent = this.node.getParent();
  63. while(parent && (
  64. parent.get("MWFtype")!="source" &&
  65. parent.get("MWFtype")!="subSource" &&
  66. parent.get("MWFtype")!="subSourceItem"
  67. )) parent = parent.getParent();
  68. return (parent) ? parent.retrieve("module") : null;
  69. },
  70. /**
  71. * @summary 隐藏组件.
  72. * @example
  73. * this.form.get("fieldId").hide(); //隐藏组件
  74. */
  75. hide: function(){
  76. var dsp = this.node.getStyle("display");
  77. if (dsp!=="none") this.node.store("mwf_display", dsp);
  78. this.node.setStyle("display", "none");
  79. if (this.iconNode) this.iconNode.setStyle("display", "none");
  80. },
  81. /**
  82. * @summary 显示组件.
  83. * @example
  84. * this.form.get("fieldId").show(); //显示组件
  85. */
  86. show: function(){
  87. var dsp = this.node.retrieve("mwf_display", dsp);
  88. this.node.setStyle("display", dsp);
  89. if (this.iconNode) this.iconNode.setStyle("display", "block");
  90. },
  91. load: function(){
  92. this._loadModuleEvents();
  93. if (this.fireEvent("queryLoad")){
  94. this._queryLoaded();
  95. this._loadUserInterface();
  96. this._loadStyles();
  97. this._loadDomEvents();
  98. //this._loadEvents();
  99. this._afterLoaded();
  100. this.fireEvent("postLoad");
  101. this.fireEvent("load");
  102. }
  103. },
  104. _loadUserInterface: function(){
  105. // this.node = this.node;
  106. },
  107. _loadStyles: function(){
  108. if (this.json.styles) Object.each(this.json.styles, function(value, key){
  109. if ((value.indexOf("x_processplatform_assemble_surface")!=-1 || value.indexOf("x_portal_assemble_surface")!=-1 || value.indexOf("x_cms_assemble_control")!=-1)){
  110. var host1 = MWF.Actions.getHost("x_processplatform_assemble_surface");
  111. var host2 = MWF.Actions.getHost("x_portal_assemble_surface");
  112. var host3 = MWF.Actions.getHost("x_cms_assemble_control");
  113. if (value.indexOf("/x_processplatform_assemble_surface")!==-1){
  114. value = value.replace("/x_processplatform_assemble_surface", host1+"/x_processplatform_assemble_surface");
  115. }else if (value.indexOf("x_processplatform_assemble_surface")!==-1){
  116. value = value.replace("x_processplatform_assemble_surface", host1+"/x_processplatform_assemble_surface");
  117. }
  118. if (value.indexOf("/x_portal_assemble_surface")!==-1){
  119. value = value.replace("/x_portal_assemble_surface", host2+"/x_portal_assemble_surface");
  120. }else if (value.indexOf("x_portal_assemble_surface")!==-1){
  121. value = value.replace("x_portal_assemble_surface", host2+"/x_portal_assemble_surface");
  122. }
  123. if (value.indexOf("/x_cms_assemble_control")!==-1){
  124. value = value.replace("/x_cms_assemble_control", host3+"/x_cms_assemble_control");
  125. }else if (value.indexOf("x_cms_assemble_control")!==-1){
  126. value = value.replace("x_cms_assemble_control", host3+"/x_cms_assemble_control");
  127. }
  128. value = o2.filterUrl(value);
  129. }
  130. this.node.setStyle(key, value);
  131. }.bind(this));
  132. // if (["x_processplatform_assemble_surface", "x_portal_assemble_surface"].indexOf(root.toLowerCase())!==-1){
  133. // var host = MWF.Actions.getHost(root);
  134. // return (flag==="/") ? host+this.json.template : host+"/"+this.json.template
  135. // }
  136. //if (this.json.styles) this.node.setStyles(this.json.styles);
  137. },
  138. _loadModuleEvents : function(){
  139. Object.each(this.json.events, function(e, key){
  140. if (e.code){
  141. if (this.options.moduleEvents.indexOf(key)!==-1){
  142. this.addEvent(key, function(event){
  143. return this.form.Macro.fire(e.code, this, event);
  144. }.bind(this));
  145. }
  146. }
  147. }.bind(this));
  148. },
  149. _loadDomEvents: function(){
  150. Object.each(this.json.events, function(e, key){
  151. if (e.code){
  152. if (this.options.moduleEvents.indexOf(key)===-1){
  153. this.node.addEvent(key, function(event){
  154. return this.form.Macro.fire(e.code, this, event);
  155. }.bind(this));
  156. }
  157. }
  158. }.bind(this));
  159. },
  160. _loadEvents: function(){
  161. Object.each(this.json.events, function(e, key){
  162. if (e.code){
  163. if (this.options.moduleEvents.indexOf(key)!==-1){
  164. this.addEvent(key, function(event){
  165. return this.form.Macro.fire(e.code, this, event);
  166. }.bind(this));
  167. }else{
  168. this.node.addEvent(key, function(event){
  169. return this.form.Macro.fire(e.code, this, event);
  170. }.bind(this));
  171. }
  172. }
  173. }.bind(this));
  174. },
  175. addModuleEvent: function(key, fun){
  176. if (this.options.moduleEvents.indexOf(key)!==-1){
  177. this.addEvent(key, function(event){
  178. return (fun) ? fun(this, event) : null;
  179. }.bind(this));
  180. }else{
  181. this.node.addEvent(key, function(event){
  182. return (fun) ? fun(this, event) : null;
  183. }.bind(this));
  184. }
  185. },
  186. _getBusinessData: function(){
  187. if (this.json.section=="yes"){
  188. return this._getBusinessSectionData();
  189. }else {
  190. if (this.json.type==="Opinion"){
  191. return this._getBusinessSectionDataByPerson();
  192. }else{
  193. return this.form.businessData.data[this.json.id] || "";
  194. }
  195. }
  196. },
  197. _getBusinessSectionData: function(){
  198. switch (this.json.sectionBy){
  199. case "person":
  200. return this._getBusinessSectionDataByPerson();
  201. case "unit":
  202. return this._getBusinessSectionDataByUnit();
  203. case "activity":
  204. return this._getBusinessSectionDataByActivity();
  205. case "splitValue":
  206. return this._getBusinessSectionDataBySplitValue();
  207. case "script":
  208. return this._getBusinessSectionDataByScript(((this.json.sectionByScript) ? this.json.sectionByScript.code : ""));
  209. default:
  210. return this.form.businessData.data[this.json.id] || "";
  211. }
  212. },
  213. _getBusinessSectionDataByPerson: function(){
  214. this.form.sectionListObj[this.json.id] = layout.desktop.session.user.id;
  215. var dataObj = this.form.businessData.data[this.json.id];
  216. return (dataObj) ? (dataObj[layout.desktop.session.user.id] || "") : "";
  217. },
  218. _getBusinessSectionDataByUnit: function(){
  219. this.form.sectionListObj[this.json.id] = "";
  220. var dataObj = this.form.businessData.data[this.json.id];
  221. if (!dataObj) return "";
  222. var key = (this.form.businessData.task) ? this.form.businessData.task.unit : "";
  223. if (key) this.form.sectionListObj[this.json.id] = key;
  224. return (key) ? (dataObj[key] || "") : "";
  225. },
  226. _getBusinessSectionDataByActivity: function(){
  227. this.form.sectionListObj[this.json.id] = "";
  228. var dataObj = this.form.businessData.data[this.json.id];
  229. if (!dataObj) return "";
  230. var key = (this.form.businessData.work) ? this.form.businessData.work.activity : "";
  231. if (key) this.form.sectionListObj[this.json.id] = key;
  232. return (key) ? (dataObj[key] || "") : "";
  233. },
  234. _getBusinessSectionDataBySplitValue: function(){
  235. this.form.sectionListObj[this.json.id] = "";
  236. var dataObj = this.form.businessData.data[this.json.id];
  237. if (!dataObj) return "";
  238. var key = (this.form.businessData.work) ? this.form.businessData.work.splitValue : "";
  239. if (key) this.form.sectionListObj[this.json.id] = key;
  240. return (key) ? (dataObj[key] || "") : "";
  241. },
  242. _getBusinessSectionDataByScript: function(code){
  243. this.form.sectionListObj[this.json.id] = "";
  244. var dataObj = this.form.businessData.data[this.json.id];
  245. if (!dataObj) return "";
  246. var key = this.form.Macro.exec(code, this);
  247. if (key) this.form.sectionListObj[this.json.id] = key;
  248. return (key) ? (dataObj[key] || "") : "";
  249. },
  250. _setBusinessData: function(v){
  251. if (this.json.section=="yes"){
  252. this._setBusinessSectionData(v);
  253. }else {
  254. if (this.json.type==="Opinion"){
  255. this._setBusinessSectionDataByPerson(v);
  256. }else{
  257. if (this.form.businessData.data[this.json.id]){
  258. this.form.businessData.data[this.json.id] = v;
  259. }else{
  260. this.form.businessData.data[this.json.id] = v;
  261. this.form.Macro.environment.setData(this.form.businessData.data);
  262. }
  263. if (this.json.isTitle) this.form.businessData.work.title = v;
  264. }
  265. }
  266. },
  267. _setBusinessSectionData: function(v){
  268. switch (this.json.sectionBy){
  269. case "person":
  270. this._setBusinessSectionDataByPerson(v);
  271. break;
  272. case "unit":
  273. this._setBusinessSectionDataByUnit(v);
  274. break;
  275. case "activity":
  276. this._setBusinessSectionDataByActivity(v);
  277. break;
  278. case "splitValue":
  279. this._setBusinessSectionDataBySplitValue(v);
  280. break;
  281. case "script":
  282. this._setBusinessSectionDataByScript(this.json.sectionByScript.code, v);
  283. break;
  284. default:
  285. if (this.form.businessData.data[this.json.id]){
  286. this.form.businessData.data[this.json.id] = v;
  287. }else{
  288. this.form.businessData.data[this.json.id] = v;
  289. this.form.Macro.environment.setData(this.form.businessData.data);
  290. }
  291. }
  292. },
  293. _setBusinessSectionDataByPerson: function(v){
  294. var resetData = false;
  295. var key = layout.desktop.session.user.id;
  296. var dataObj = this.form.businessData.data[this.json.id];
  297. if (!dataObj){
  298. dataObj = {};
  299. this.form.businessData.data[this.json.id] = dataObj;
  300. resetData = true;
  301. }
  302. if (!dataObj[key]) resetData = true;
  303. dataObj[key] = v;
  304. if (resetData) this.form.Macro.environment.setData(this.form.businessData.data);
  305. },
  306. _setBusinessSectionDataByUnit: function(v){
  307. var resetData = false;
  308. var key = (this.form.businessData.task) ? this.form.businessData.task.unit : "";
  309. if (key){
  310. var dataObj = this.form.businessData.data[this.json.id];
  311. if (!dataObj){
  312. dataObj = {};
  313. this.form.businessData.data[this.json.id] = dataObj;
  314. resetData = true;
  315. }
  316. if (!dataObj[key]) resetData = true;
  317. dataObj[key] = v;
  318. }
  319. if (resetData) this.form.Macro.environment.setData(this.form.businessData.data);
  320. },
  321. _setBusinessSectionDataByActivity: function(v){
  322. var resetData = false;
  323. var key = (this.form.businessData.work) ? this.form.businessData.work.activity : "";
  324. if (key){
  325. var dataObj = this.form.businessData.data[this.json.id];
  326. if (!dataObj){
  327. dataObj = {};
  328. this.form.businessData.data[this.json.id] = dataObj;
  329. resetData = true;
  330. }
  331. if (!dataObj[key]) resetData = true;
  332. dataObj[key] = v;
  333. }
  334. if (resetData) this.form.Macro.environment.setData(this.form.businessData.data);
  335. },
  336. _setBusinessSectionDataBySplitValue: function(v){
  337. var resetData = false;
  338. var key = (this.form.businessData.work) ? this.form.businessData.work.splitValue : "";
  339. if (key){
  340. var dataObj = this.form.businessData.data[this.json.id];
  341. if (!dataObj){
  342. dataObj = {};
  343. this.form.businessData.data[this.json.id] = dataObj;
  344. resetData = true;
  345. }
  346. if (!dataObj[key]) resetData = true;
  347. dataObj[key] = v;
  348. }
  349. if (resetData) this.form.Macro.environment.setData(this.form.businessData.data);
  350. },
  351. _setBusinessSectionDataByScript: function(code, v){
  352. var resetData = false;
  353. var key = this.form.Macro.exec(code, this);
  354. if (key){
  355. var dataObj = this.form.businessData.data[this.json.id];
  356. if (!dataObj){
  357. dataObj = {};
  358. this.form.businessData.data[this.json.id] = dataObj;
  359. resetData = true;
  360. }
  361. if (!dataObj[key]) resetData = true;
  362. dataObj[key] = v;
  363. }
  364. if (resetData) this.form.Macro.environment.setData(this.form.businessData.data);
  365. },
  366. _queryLoaded: function(){},
  367. _afterLoaded: function(){},
  368. setValue: function(){
  369. },
  370. focus: function(){
  371. this.node.focus();
  372. }
  373. });