Select.js 13 KB

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