Select.js 14 KB

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