Select.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class Select 下拉选择组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var field = this.form.get("fieldId"); //获取组件对象
  7. * //方法2
  8. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  9. *
  10. * var data = field.getData(); //获取值
  11. * field.setData("字符串值"); //设置值
  12. * field.hide(); //隐藏字段
  13. * var id = field.json.id; //获取字段标识
  14. * var flag = field.isEmpty(); //字段是否为空
  15. * field.resetData(); //重置字段的值为默认值或置空
  16. * @extends MWF.xApplication.process.Xform.$Input
  17. * @category FormComponents
  18. * @hideconstructor
  19. */
  20. MWF.xApplication.process.Xform.Select = MWF.APPSelect = new Class(
  21. /** @lends MWF.xApplication.process.Xform.Select# */
  22. {
  23. Implements: [Events],
  24. Extends: MWF.APP$Input,
  25. iconStyle: "selectIcon",
  26. /**
  27. * @ignore
  28. * @member {Element} descriptionNode
  29. * @memberOf MWF.xApplication.process.Xform.Select#
  30. */
  31. initialize: function(node, json, form, options){
  32. this.node = $(node);
  33. this.node.store("module", this);
  34. this.json = json;
  35. this.form = form;
  36. this.field = true;
  37. },
  38. _loadNode: function(){
  39. if (this.readonly|| this.json.isReadonly){
  40. this._loadNodeRead();
  41. }else{
  42. this._loadNodeEdit();
  43. }
  44. },
  45. _loadNodeRead: function(){
  46. this.node.empty();
  47. this.node.set({
  48. "nodeId": this.json.id,
  49. "MWFType": this.json.type
  50. });
  51. var optionItems = this.getOptions();
  52. var value = this.getValue();
  53. if (value){
  54. if (typeOf(value)!=="array") value = [value];
  55. var texts = [];
  56. optionItems.each(function(item){
  57. var tmps = item.split("|");
  58. var t = tmps[0];
  59. var v = tmps[1] || t;
  60. if (v){
  61. if (value.indexOf(v)!=-1){
  62. texts.push(t);
  63. }
  64. }
  65. });
  66. this.node.set("text", texts.join(", "));
  67. }
  68. },
  69. _loadDomEvents: function(){
  70. Object.each(this.json.events, function(e, key){
  71. if (e.code){
  72. if (this.options.moduleEvents.indexOf(key)===-1){
  73. this.node.addEvent(key, function(event){
  74. return this.form.Macro.fire(e.code, this, event);
  75. }.bind(this));
  76. }
  77. }
  78. }.bind(this));
  79. },
  80. _loadEvents: function(){
  81. Object.each(this.json.events, function(e, key){
  82. if (e.code){
  83. if (this.options.moduleEvents.indexOf(key)!=-1){
  84. this.addEvent(key, function(event){
  85. return this.form.Macro.fire(e.code, this, event);
  86. }.bind(this));
  87. }else{
  88. this.node.addEvent(key, function(event){
  89. return this.form.Macro.fire(e.code, this, event);
  90. }.bind(this));
  91. }
  92. }
  93. }.bind(this));
  94. },
  95. addModuleEvent: function(key, fun){
  96. if (this.options.moduleEvents.indexOf(key)!==-1){
  97. this.addEvent(key, function(event){
  98. return (fun) ? fun(this, event) : null;
  99. }.bind(this));
  100. }else{
  101. this.node.addEvent(key, function(event){
  102. return (fun) ? fun(this, event) : null;
  103. }.bind(this));
  104. }
  105. },
  106. _loadStyles: function(){
  107. if (this.areaNode){
  108. if (this.json.styles) if (this.areaNode) this.areaNode.setStyles(this.json.styles);
  109. if (this.json.inputStyles) this.node.setStyles(this.json.inputStyles);
  110. }else{
  111. if (this.json.styles) this.node.setStyles(this.json.styles);
  112. }
  113. },
  114. _resetNodeEdit: function(){
  115. this.node.empty();
  116. var select = new Element("select");
  117. select.set(this.json.properties);
  118. select.inject(this.node);
  119. },
  120. _loadNodeEdit: function(){
  121. if (!this.json.preprocessing) this._resetNodeEdit();
  122. var select = this.node.getFirst();
  123. this.areaNode = this.node;
  124. this.node = select;
  125. this.node.set({
  126. "id": this.json.id,
  127. "MWFType": this.json.type,
  128. "styles": {
  129. "margin-right": "12px"
  130. }
  131. });
  132. this.setOptions();
  133. this.node.addEvent("change", function(){
  134. var v = this.getInputData("change");
  135. this._setBusinessData(v);
  136. this.validationMode();
  137. if (this.validation()) this._setBusinessData(v);
  138. }.bind(this));
  139. },
  140. /**
  141. * @summary 刷新选择项,如果选择项是脚本,重新计算。
  142. * @example
  143. * this.form.get('fieldId').resetOption();
  144. */
  145. resetOption: function(){
  146. this.node.empty();
  147. this.setOptions();
  148. this.fireEvent("resetOption")
  149. },
  150. /**
  151. * @summary 获取选择项。
  152. * @return {Array} 返回选择项数组,如果使用选择项脚本,根据脚本返回决定
  153. * @example
  154. * this.form.get('fieldId').getOptions();
  155. */
  156. getOptions: function(){
  157. if (this.json.itemType == "values"){
  158. return this.json.itemValues;
  159. }else{
  160. return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  161. }
  162. return [];
  163. },
  164. setOptions: function(){
  165. var optionItems = this.getOptions();
  166. this._setOptions(optionItems);
  167. },
  168. _setOptions: function(optionItems){
  169. var p = o2.promiseAll(optionItems).then(function(options){
  170. this.moduleSelectAG = null;
  171. if (!options) options = [];
  172. if (o2.typeOf(options)==="array"){
  173. options.each(function(item){
  174. var tmps = item.split("|");
  175. var text = tmps[0];
  176. var value = tmps[1] || text;
  177. var option = new Element("option", {
  178. "value": value,
  179. "text": text
  180. }).inject(this.node);
  181. }.bind(this));
  182. this.fireEvent("setOptions", [options])
  183. }
  184. }.bind(this), function(){
  185. this.moduleSelectAG = null;
  186. }.bind(this));
  187. this.moduleSelectAG = p;
  188. if (p) p.then(function(){
  189. this.moduleSelectAG = null;
  190. }.bind(this), function(){
  191. this.moduleSelectAG = null;
  192. }.bind(this));
  193. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(options){
  194. // this.moduleSelectAG = null;
  195. // if (!options) options = [];
  196. // if (o2.typeOf(options)==="array"){
  197. // options.each(function(item){
  198. // var tmps = item.split("|");
  199. // var text = tmps[0];
  200. // var value = tmps[1] || text;
  201. //
  202. // var option = new Element("option", {
  203. // "value": value,
  204. // "text": text
  205. // }).inject(this.node);
  206. // }.bind(this));
  207. // this.fireEvent("setOptions", [options])
  208. // }
  209. // }.bind(this));
  210. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  211. // this.moduleSelectAG = null;
  212. // }.bind(this));
  213. },
  214. // __setOptions: function(){
  215. // var optionItems = this.getOptions();
  216. // if (!optionItems) optionItems = [];
  217. // if (o2.typeOf(optionItems)==="array"){
  218. // optionItems.each(function(item){
  219. // var tmps = item.split("|");
  220. // var text = tmps[0];
  221. // var value = tmps[1] || text;
  222. //
  223. // var option = new Element("option", {
  224. // "value": value,
  225. // "text": text
  226. // }).inject(this.node);
  227. // }.bind(this));
  228. // this.fireEvent("setOptions", [optionItems])
  229. // }
  230. // },
  231. addOption: function(text, value){
  232. var option = new Element("option", {
  233. "value": value || text,
  234. "text": text
  235. }).inject(this.node);
  236. this.fireEvent("addOption", [text, value])
  237. },
  238. _setValue: function(value, m){
  239. var mothed = m || "__setValue";
  240. if (!!value){
  241. var p = o2.promiseAll(value).then(function(v){
  242. if (o2.typeOf(v)=="array") v = v[0];
  243. if (this.moduleSelectAG){
  244. this.moduleValueAG = this.moduleSelectAG;
  245. this.moduleSelectAG.then(function(){
  246. this[mothed](v);
  247. return v;
  248. }.bind(this), function(){});
  249. }else{
  250. this[mothed](v)
  251. }
  252. return v;
  253. }.bind(this), function(){});
  254. this.moduleValueAG = p;
  255. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  256. this.moduleValueAG = null;
  257. }.bind(this), function(){
  258. this.moduleValueAG = null;
  259. }.bind(this));
  260. }else{
  261. this[mothed](value);
  262. }
  263. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  264. // if (o2.typeOf(v)=="array") v = v[0];
  265. // if (this.moduleSelectAG){
  266. // this.moduleValueAG = this.moduleSelectAG;
  267. // this.moduleSelectAG.then(function(){
  268. // this.__setValue(v);
  269. // }.bind(this));
  270. // }else{
  271. // this.__setValue(v)
  272. // }
  273. // return v;
  274. // }.bind(this));
  275. // if (value && value.isAG){
  276. // this.moduleValueAG = o2.AG.all(value),then(function(v){
  277. // this._setValue(v);
  278. // }.bind(this));
  279. // // this.moduleValueAG = value;
  280. // // value.addResolve(function(v){
  281. // // this._setValue(v);
  282. // // }.bind(this));
  283. // }else{
  284. //
  285. // }
  286. },
  287. __setValue: function(value){
  288. if (!this.readonly && !this.json.isReadonly ) {
  289. this._setBusinessData(value);
  290. for (var i=0; i<this.node.options.length; i++){
  291. var option = this.node.options[i];
  292. if (option.value==value){
  293. option.selected = true;
  294. // break;
  295. }else{
  296. option.selected = false;
  297. }
  298. }
  299. }
  300. this.moduleValueAG = null;
  301. },
  302. // _setValue: function(value){
  303. // if (!this.readonly && !this.json.isReadonly ) {
  304. // this._setBusinessData(value);
  305. // for (var i=0; i<this.node.options.length; i++){
  306. // var option = this.node.options[i];
  307. // if (option.value==value){
  308. // option.selected = true;
  309. // // break;
  310. // }else{
  311. // option.selected = false;
  312. // }
  313. // }
  314. // }
  315. // //this.node.set("value", value);
  316. // },
  317. /**
  318. * @summary 获取选中项的value和text。
  319. * @return {Object} 返回选中项的value和text,如:
  320. * <pre><code class='language-js'>{"value": ["male"], "text": ["男"]}
  321. * {"value": [""], "text": [""]}
  322. * </code></pre>
  323. * @example
  324. * var data = this.form.get('fieldId').getTextData();
  325. * var text = data.text[0] //获取选中项的文本
  326. */
  327. getTextData: function(){
  328. var value = [];
  329. var text = [];
  330. if (this.readonly|| this.json.isReadonly){
  331. var ops = this.getOptionsObj();
  332. var data = this._getBusinessData();
  333. var d = typeOf(data) === "array" ? data : [data];
  334. d.each( function (v) {
  335. var idx = ops.valueList.indexOf( v );
  336. value.push( v || "" );
  337. text.push( idx > -1 ? ops.textList[idx] : (v || "") );
  338. });
  339. }else{
  340. var ops = this.node.getElements("option");
  341. ops.each(function(op){
  342. if (op.selected){
  343. var v = op.get("value");
  344. var t = op.get("text");
  345. value.push(v || "");
  346. text.push(t || v || "");
  347. }
  348. });
  349. }
  350. if (!value.length) value = [""];
  351. if (!text.length) text = [""];
  352. return {"value": value, "text": text};
  353. },
  354. getInputData: function(){
  355. if( this.readonly || this.json.isReadonly ){
  356. return this._getBusinessData();
  357. }else{
  358. var ops = this.node.getElements("option");
  359. var value = [];
  360. ops.each(function(op){
  361. if (op.selected){
  362. var v = op.get("value");
  363. if (v) value.push(v);
  364. }
  365. });
  366. if (!value.length) return null;
  367. return (value.length==1) ? value[0] : value;
  368. }
  369. },
  370. resetData: function(){
  371. this.setData(this.getValue());
  372. },
  373. /**
  374. * @summary 获取整理后的选择项。
  375. * @return {Object} 返回整理后的选择项,如:
  376. * <pre><code class='language-js'>{"value": ["","female","male"], "text": ["","女","男"]}
  377. * </code></pre>
  378. * @example
  379. * var optionData = this.form.get('fieldId').getOptionsObj();
  380. */
  381. getOptionsObj : function(){
  382. var textList = [];
  383. var valueList = [];
  384. var optionItems = this.getOptions();
  385. if (!optionItems) optionItems = [];
  386. if (o2.typeOf(optionItems)==="array"){
  387. optionItems.each(function(item){
  388. var tmps = item.split("|");
  389. textList.push( tmps[0] );
  390. valueList.push( tmps[1] || tmps[0] );
  391. }.bind(this));
  392. }
  393. return { textList : textList, valueList : valueList };
  394. },
  395. setData: function(data){
  396. return this._setValue(data, "__setData");
  397. // if (data && data.isAG){
  398. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  399. // if (o2.typeOf(v)=="array") v = v[0];
  400. // this.__setData(v);
  401. // }.bind(this));
  402. // }else{
  403. // this.__setData(data);
  404. // }
  405. // if (data && data.isAG){
  406. // this.moduleValueAG = data;
  407. // data.addResolve(function(v){
  408. // this.setData(v);
  409. // }.bind(this));
  410. // }else{
  411. // this.__setData(data);
  412. // this.moduleValueAG = null;
  413. // }
  414. },
  415. __setData: function(data){
  416. this._setBusinessData(data);
  417. if (this.readonly|| this.json.isReadonly){
  418. var d = typeOf(data) === "array" ? data : [data];
  419. var ops = this.getOptionsObj();
  420. var result = [];
  421. d.each( function (v) {
  422. var idx = ops.valueList.indexOf( v );
  423. result.push( idx > -1 ? ops.textList[idx] : v);
  424. })
  425. this.node.set("text", result.join(","));
  426. }else{
  427. var ops = this.node.getElements("option");
  428. ops.each(function(op){
  429. if (typeOf(data)=="array"){
  430. if (data.indexOf(op.get("value"))!=-1){
  431. op.set("selected", true);
  432. }else{
  433. op.set("selected", false);
  434. }
  435. }else{
  436. if (data == op.get("value")){
  437. op.set("selected", true);
  438. }else{
  439. op.set("selected", false);
  440. }
  441. }
  442. });
  443. this.validationMode();
  444. }
  445. this.fireEvent("setData", [data]);
  446. }
  447. });