$Module.js 15 KB

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