Select.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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} 返回选择项数组,如果使用选择项脚本,根据脚本返回决定,如:<pre><code class='language-js'>[
  154. * "女|female",
  155. * "男|male"
  156. * ]</code></pre>
  157. * @example
  158. * this.form.get('fieldId').getOptions();
  159. */
  160. getOptions: function(){
  161. if (this.json.itemType == "values"){
  162. return this.json.itemValues;
  163. }else{
  164. return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  165. }
  166. return [];
  167. },
  168. /**
  169. * @summary 获取整理后的选择项。
  170. * @return {Object} 返回整理后的选择项,如:
  171. * <pre><code class='language-js'>{"valueList": ["","female","male"], "textList": ["","女","男"]}
  172. * </code></pre>
  173. * @example
  174. * var optionData = this.form.get('fieldId').getOptionsObj();
  175. */
  176. getOptionsObj : function(){
  177. var textList = [];
  178. var valueList = [];
  179. var optionItems = this.getOptions();
  180. if (!optionItems) optionItems = [];
  181. if (o2.typeOf(optionItems)==="array"){
  182. optionItems.each(function(item){
  183. var tmps = item.split("|");
  184. textList.push( tmps[0] );
  185. valueList.push( tmps[1] || tmps[0] );
  186. }.bind(this));
  187. }
  188. return { textList : textList, valueList : valueList };
  189. },
  190. setOptions: function(){
  191. var optionItems = this.getOptions();
  192. this._setOptions(optionItems);
  193. },
  194. _setOptions: function(optionItems){
  195. var p = o2.promiseAll(optionItems).then(function(options){
  196. this.moduleSelectAG = null;
  197. if (!options) options = [];
  198. if (o2.typeOf(options)==="array"){
  199. options.each(function(item){
  200. var tmps = item.split("|");
  201. var text = tmps[0];
  202. var value = tmps[1] || text;
  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), function(){
  211. this.moduleSelectAG = null;
  212. }.bind(this));
  213. this.moduleSelectAG = p;
  214. if (p) p.then(function(){
  215. this.moduleSelectAG = null;
  216. }.bind(this), function(){
  217. this.moduleSelectAG = null;
  218. }.bind(this));
  219. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(options){
  220. // this.moduleSelectAG = null;
  221. // if (!options) options = [];
  222. // if (o2.typeOf(options)==="array"){
  223. // options.each(function(item){
  224. // var tmps = item.split("|");
  225. // var text = tmps[0];
  226. // var value = tmps[1] || text;
  227. //
  228. // var option = new Element("option", {
  229. // "value": value,
  230. // "text": text
  231. // }).inject(this.node);
  232. // }.bind(this));
  233. // this.fireEvent("setOptions", [options])
  234. // }
  235. // }.bind(this));
  236. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  237. // this.moduleSelectAG = null;
  238. // }.bind(this));
  239. },
  240. // __setOptions: function(){
  241. // var optionItems = this.getOptions();
  242. // if (!optionItems) optionItems = [];
  243. // if (o2.typeOf(optionItems)==="array"){
  244. // optionItems.each(function(item){
  245. // var tmps = item.split("|");
  246. // var text = tmps[0];
  247. // var value = tmps[1] || text;
  248. //
  249. // var option = new Element("option", {
  250. // "value": value,
  251. // "text": text
  252. // }).inject(this.node);
  253. // }.bind(this));
  254. // this.fireEvent("setOptions", [optionItems])
  255. // }
  256. // },
  257. addOption: function(text, value){
  258. var option = new Element("option", {
  259. "value": value || text,
  260. "text": text
  261. }).inject(this.node);
  262. this.fireEvent("addOption", [text, value])
  263. },
  264. _setValue: function(value, m){
  265. var mothed = m || "__setValue";
  266. if (!!value){
  267. var p = o2.promiseAll(value).then(function(v){
  268. if (o2.typeOf(v)=="array") v = v[0];
  269. if (this.moduleSelectAG){
  270. this.moduleValueAG = this.moduleSelectAG;
  271. this.moduleSelectAG.then(function(){
  272. this[mothed](v);
  273. return v;
  274. }.bind(this), function(){});
  275. }else{
  276. this[mothed](v)
  277. }
  278. return v;
  279. }.bind(this), function(){});
  280. this.moduleValueAG = p;
  281. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  282. this.moduleValueAG = null;
  283. }.bind(this), function(){
  284. this.moduleValueAG = null;
  285. }.bind(this));
  286. }else{
  287. this[mothed](value);
  288. }
  289. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  290. // if (o2.typeOf(v)=="array") v = v[0];
  291. // if (this.moduleSelectAG){
  292. // this.moduleValueAG = this.moduleSelectAG;
  293. // this.moduleSelectAG.then(function(){
  294. // this.__setValue(v);
  295. // }.bind(this));
  296. // }else{
  297. // this.__setValue(v)
  298. // }
  299. // return v;
  300. // }.bind(this));
  301. // if (value && value.isAG){
  302. // this.moduleValueAG = o2.AG.all(value),then(function(v){
  303. // this._setValue(v);
  304. // }.bind(this));
  305. // // this.moduleValueAG = value;
  306. // // value.addResolve(function(v){
  307. // // this._setValue(v);
  308. // // }.bind(this));
  309. // }else{
  310. //
  311. // }
  312. },
  313. __setValue: function(value){
  314. if (!this.readonly && !this.json.isReadonly ) {
  315. this._setBusinessData(value);
  316. for (var i=0; i<this.node.options.length; i++){
  317. var option = this.node.options[i];
  318. if (option.value==value){
  319. option.selected = true;
  320. // break;
  321. }else{
  322. option.selected = false;
  323. }
  324. }
  325. }
  326. this.moduleValueAG = null;
  327. },
  328. // _setValue: function(value){
  329. // if (!this.readonly && !this.json.isReadonly ) {
  330. // this._setBusinessData(value);
  331. // for (var i=0; i<this.node.options.length; i++){
  332. // var option = this.node.options[i];
  333. // if (option.value==value){
  334. // option.selected = true;
  335. // // break;
  336. // }else{
  337. // option.selected = false;
  338. // }
  339. // }
  340. // }
  341. // //this.node.set("value", value);
  342. // },
  343. /**
  344. * @summary 获取选中项的value和text。
  345. * @return {Object} 返回选中项的value和text,如:
  346. * <pre><code class='language-js'>{"value": ["male"], "text": ["男"]}
  347. * {"value": [""], "text": [""]}
  348. * </code></pre>
  349. * @example
  350. * var data = this.form.get('fieldId').getTextData();
  351. * var text = data.text[0] //获取选中项的文本
  352. */
  353. getTextData: function(){
  354. var value = [];
  355. var text = [];
  356. if (this.readonly|| this.json.isReadonly){
  357. var ops = this.getOptionsObj();
  358. var data = this._getBusinessData();
  359. var d = typeOf(data) === "array" ? data : [data];
  360. d.each( function (v) {
  361. var idx = ops.valueList.indexOf( v );
  362. value.push( v || "" );
  363. text.push( idx > -1 ? ops.textList[idx] : (v || "") );
  364. });
  365. }else{
  366. var ops = this.node.getElements("option");
  367. ops.each(function(op){
  368. if (op.selected){
  369. var v = op.get("value");
  370. var t = op.get("text");
  371. value.push(v || "");
  372. text.push(t || v || "");
  373. }
  374. });
  375. }
  376. if (!value.length) value = [""];
  377. if (!text.length) text = [""];
  378. return {"value": value, "text": text};
  379. },
  380. getInputData: function(){
  381. if( this.readonly || this.json.isReadonly ){
  382. return this._getBusinessData();
  383. }else{
  384. var ops = this.node.getElements("option");
  385. var value = [];
  386. ops.each(function(op){
  387. if (op.selected){
  388. var v = op.get("value");
  389. if (v) value.push(v);
  390. }
  391. });
  392. if (!value.length) return null;
  393. return (value.length==1) ? value[0] : value;
  394. }
  395. },
  396. resetData: function(){
  397. this.setData(this.getValue());
  398. },
  399. setData: function(data){
  400. return this._setValue(data, "__setData");
  401. // if (data && data.isAG){
  402. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  403. // if (o2.typeOf(v)=="array") v = v[0];
  404. // this.__setData(v);
  405. // }.bind(this));
  406. // }else{
  407. // this.__setData(data);
  408. // }
  409. // if (data && data.isAG){
  410. // this.moduleValueAG = data;
  411. // data.addResolve(function(v){
  412. // this.setData(v);
  413. // }.bind(this));
  414. // }else{
  415. // this.__setData(data);
  416. // this.moduleValueAG = null;
  417. // }
  418. },
  419. __setData: function(data){
  420. this._setBusinessData(data);
  421. if (this.readonly|| this.json.isReadonly){
  422. var d = typeOf(data) === "array" ? data : [data];
  423. var ops = this.getOptionsObj();
  424. var result = [];
  425. d.each( function (v) {
  426. var idx = ops.valueList.indexOf( v );
  427. result.push( idx > -1 ? ops.textList[idx] : v);
  428. })
  429. this.node.set("text", result.join(","));
  430. }else{
  431. var ops = this.node.getElements("option");
  432. ops.each(function(op){
  433. if (typeOf(data)=="array"){
  434. if (data.indexOf(op.get("value"))!=-1){
  435. op.set("selected", true);
  436. }else{
  437. op.set("selected", false);
  438. }
  439. }else{
  440. if (data == op.get("value")){
  441. op.set("selected", true);
  442. }else{
  443. op.set("selected", false);
  444. }
  445. }
  446. });
  447. this.validationMode();
  448. }
  449. this.fireEvent("setData", [data]);
  450. }
  451. });