Checkbox.js 16 KB

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