Combox.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class Combox 组合框组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var field = this.form.get("fieldId"); //获取组件对象
  7. * //方法2
  8. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  9. * @extends MWF.xApplication.process.Xform.$Input
  10. * @o2category FormComponents
  11. * @o2range {Process|CMS}
  12. * @hideconstructor
  13. */
  14. MWF.xApplication.process.Xform.Combox = MWF.APPCombox = new Class(
  15. /** @lends MWF.xApplication.process.Xform.Combox# */
  16. {
  17. Implements: [Events],
  18. Extends: MWF.APP$Input,
  19. iconStyle: "selectIcon",
  20. options: {
  21. /**
  22. * 手工输入完成后触发。
  23. * @event MWF.xApplication.process.Xform.Combox#commitInput
  24. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  25. */
  26. /**
  27. * 值改变时触发。
  28. * @event MWF.xApplication.process.Xform.Combox#change
  29. * @see {@link https://www.yuque.com/o2oa/ixsnyt/hm5uft#i0zTS|组件事件说明}
  30. */
  31. "moduleEvents": ["load", "queryLoad", "postLoad", "commitInput", "change"]
  32. },
  33. initialize: function(node, json, form, options){
  34. this.node = $(node);
  35. this.node.store("module", this);
  36. this.json = json;
  37. this.form = form;
  38. this.field = true;
  39. },
  40. _loadNode: function(){
  41. if (this.readonly){
  42. this._loadNodeRead();
  43. }else{
  44. this._loadNodeEdit();
  45. }
  46. },
  47. _loadNodeRead: function(){
  48. this.node.empty();
  49. this.node.set({
  50. "nodeId": this.json.id,
  51. "MWFType": this.json.type
  52. });
  53. //new Element("select").inject(this.node);
  54. },
  55. _loadNodeEdit: function(){
  56. this.node.empty();
  57. MWF.require("MWF.widget.Combox", function(){
  58. this.combox = select = new MWF.widget.Combox({
  59. "onlySelect": this.json.onlySelect==="y",
  60. "count": this.json.count.toInt() || 0,
  61. "splitStr": this.json.splitStr || ",\\s*|;\\s*|,\\s*|;\\s*",
  62. "splitShow": this.json.splitShow || ", ",
  63. "list": this.getOptions(),
  64. "onCommitInput": function(item){
  65. this.fireEvent("commitInput");
  66. }.bind(this),
  67. "onChange": function(){
  68. this.fireEvent("change");
  69. }.bind(this),
  70. "optionsMethod": this._searchOptions()
  71. });
  72. }.bind(this), false);
  73. // var select = new Element("select");
  74. // select.set(this.json.properties);
  75. select.inject(this.node);
  76. //this.node.destroy();
  77. // this.areaNode = this.node;
  78. // this.node = select;
  79. this.node.set({
  80. "id": this.json.id,
  81. "MWFType": this.json.type
  82. });
  83. this.combox.addEvent("change", function(){
  84. this.validationMode();
  85. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  86. }.bind(this));
  87. },
  88. _searchOptions: function(){
  89. if (this.json.itemType === "dynamic"){
  90. return function(value, callback){
  91. var event = {
  92. "value": value,
  93. "callback": callback
  94. };
  95. this.form.Macro.fire(((this.json.itemDynamic) ? this.json.itemDynamic.code : ""), this, event);
  96. }.bind(this);
  97. }else{
  98. return null;
  99. }
  100. },
  101. /**
  102. * @summary 获取选择项数组.
  103. * @example
  104. * var array = this.form.get('fieldId').getOptions();
  105. * @return {Array} 选择项数组,如果配置为脚本返回计算结果.
  106. */
  107. getOptions: function(){
  108. var list = [];
  109. if (this.json.itemType === "values"){
  110. list = this.json.itemValues;
  111. }else if (this.json.itemType === "script"){
  112. list = this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  113. }
  114. if (list.length){
  115. var options = [];
  116. list.each(function(v){
  117. if (typeOf(v)==="object"){
  118. options.push(v);
  119. }else{
  120. v = v.toString();
  121. arr = v.split("|");
  122. var o = { "text": "", "keyword": "", "value": "" };
  123. switch (arr.length){
  124. case 0: break;
  125. case 1:
  126. o.text = arr[0];
  127. o.keyword = arr[0];
  128. o.value = arr[0];
  129. break;
  130. case 2:
  131. o.text = arr[0];
  132. o.keyword = arr[0];
  133. o.value = arr[1];
  134. break;
  135. case 3:
  136. o.text = arr[0];
  137. o.keyword = arr[1];
  138. o.value = arr[2];
  139. break;
  140. default:
  141. o.text = arr[0];
  142. o.keyword = arr[1];
  143. o.value = arr[2];
  144. }
  145. options.push(o);
  146. }
  147. }.bind(this));
  148. return options;
  149. }
  150. return [];
  151. },
  152. /**
  153. * 当表单上没有对应组件的时候,可以使用this.data[fieldId] = data赋值。
  154. * @summary 为组件赋值。
  155. * @param value{String} .
  156. * @example
  157. * this.form.get("fieldId").setData("test"); //赋文本值
  158. * @example
  159. * //如果无法确定表单上是否有组件,需要判断
  160. * if( this.form.get('fieldId') ){ //判断表单是否有无对应组件
  161. * this.form.get('fieldId').setData( data );
  162. * }else{
  163. * this.data['fieldId'] = data;
  164. * }
  165. */
  166. setData: function(value){
  167. this._setBusinessData(value);
  168. this._setValue(value);
  169. },
  170. _setValue: function(value){
  171. if (!value) value = [];
  172. if (value.length==1 && (!value[0])) value = [];
  173. if (typeOf(value) !=="array") value = [value];
  174. if (this.combox){
  175. this.combox.clear();
  176. comboxValues = [];
  177. value.each(function(v){
  178. if (typeOf(v)==="object"){
  179. comboxValues.push({
  180. "text": v.text || v.title || v.subject || v.name,
  181. "value": v
  182. });
  183. }else{
  184. comboxValues.push(v.toString());
  185. }
  186. }.bind(this));
  187. this.combox.addNewValues(comboxValues);
  188. }else{
  189. value.each(function(v, i){
  190. var text = "";
  191. if (typeOf(v)==="object"){
  192. text = v.text || v.title || v.subject || v.name;
  193. }else{
  194. text = v.toString();
  195. }
  196. if (i<value.length-1) text += this.json.splitShow;
  197. new Element("div", {"styles": {
  198. "float": "left",
  199. "margin-right": "5px"
  200. },"text": text}).inject(this.node.getFirst() || this.node);
  201. }.bind(this));
  202. }
  203. },
  204. /**
  205. * @summary 重新计算下拉选项,该功能通常用在下拉选项为动态计算的情况.
  206. * @example
  207. * this.form.get('fieldId').resetOption();
  208. */
  209. resetOption: function(){
  210. if (this.combox){
  211. var list = this.getOptions();
  212. this.combox.setOptions({"list": list});
  213. }
  214. },
  215. /**
  216. * @summary 添加下拉选项.
  217. * @param text {String} 下拉选项文本
  218. * @param value {String} 下拉选项值
  219. * @example
  220. * this.form.get('fieldId').addOption("秘密","level1");
  221. */
  222. addOption: function(text, value){
  223. if (this.combox){
  224. var list = this.getOptions();
  225. list.push({
  226. "text": text,
  227. "value": value
  228. });
  229. this.combox.setOptions({"list": list});
  230. }
  231. },
  232. isEmpty : function(){
  233. var data = this.getData();
  234. if( typeOf(data) === "array" ){
  235. return data.length === 0;
  236. }else{
  237. return !data;
  238. }
  239. },
  240. getInputData: function(){
  241. if (this.combox) return this.combox.getData();
  242. return this._getBusinessData();
  243. },
  244. /**
  245. * @summary 获取选中的值和文本.
  246. * @example
  247. * var array = this.form.get('fieldId').getTextData();
  248. * @return {Object} 返回选中项值和文本,格式为 { 'value' : value, 'text' : text }.
  249. */
  250. getTextData: function(){
  251. var v = this.getData();
  252. return {"value": v, "text": v};
  253. //return this.node.get("text");
  254. },
  255. resetData: function(){
  256. this.setData(this.getValue());
  257. }
  258. });