$Module.js 15 KB

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