$Module.js 15 KB

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