$Module.js 16 KB

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