Combox.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.xApplication.process.Xform.Combox = MWF.APPCombox = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Input,
  5. iconStyle: "selectIcon",
  6. options: {
  7. "moduleEvents": ["load", "queryLoad", "postLoad", "commitInput", "change"]
  8. },
  9. initialize: function(node, json, form, options){
  10. this.node = $(node);
  11. this.node.store("module", this);
  12. this.json = json;
  13. this.form = form;
  14. this.field = true;
  15. },
  16. _loadNode: function(){
  17. if (this.readonly){
  18. this._loadNodeRead();
  19. }else{
  20. this._loadNodeEdit();
  21. }
  22. },
  23. _loadNodeRead: function(){
  24. this.node.empty();
  25. this.node.set({
  26. "nodeId": this.json.id,
  27. "MWFType": this.json.type
  28. });
  29. //new Element("select").inject(this.node);
  30. },
  31. _loadNodeEdit: function(){
  32. this.node.empty();
  33. MWF.require("MWF.widget.Combox", function(){
  34. this.combox = select = new MWF.widget.Combox({
  35. "onlySelect": this.json.onlySelect==="y",
  36. "count": this.json.count.toInt() || 0,
  37. "splitStr": this.json.splitStr || ",\\s*|;\\s*|,\\s*|;\\s*",
  38. "splitShow": this.json.splitShow || ", ",
  39. "list": this.getOptions(),
  40. "onCommitInput": function(item){
  41. this.fireEvent("commitInput");
  42. }.bind(this),
  43. "onChange": function(){
  44. this.fireEvent("change");
  45. }.bind(this),
  46. "optionsMethod": this._searchOptions()
  47. });
  48. }.bind(this), false);
  49. // var select = new Element("select");
  50. // select.set(this.json.properties);
  51. select.inject(this.node);
  52. //this.node.destroy();
  53. // this.areaNode = this.node;
  54. // this.node = select;
  55. this.node.set({
  56. "id": this.json.id,
  57. "MWFType": this.json.type
  58. });
  59. this.combox.addEvent("change", function(){
  60. this.validationMode();
  61. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  62. }.bind(this));
  63. },
  64. _searchOptions: function(){
  65. if (this.json.itemType === "dynamic"){
  66. return function(value, callback){
  67. var event = {
  68. "value": value,
  69. "callback": callback
  70. };
  71. this.form.Macro.fire(((this.json.itemDynamic) ? this.json.itemDynamic.code : ""), this, event);
  72. }.bind(this);
  73. }else{
  74. return null;
  75. }
  76. },
  77. getOptions: function(){
  78. var list = [];
  79. if (this.json.itemType === "values"){
  80. list = this.json.itemValues;
  81. }else if (this.json.itemType === "script"){
  82. list = this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  83. }
  84. if (list.length){
  85. var options = [];
  86. list.each(function(v){
  87. if (typeOf(v)==="object"){
  88. options.push(v);
  89. }else{
  90. v = v.toString();
  91. arr = v.split("|");
  92. var o = { "text": "", "keyword": "", "value": "" };
  93. switch (arr.length){
  94. case 0: break;
  95. case 1:
  96. o.text = arr[0];
  97. o.keyword = arr[0];
  98. o.value = arr[0];
  99. break;
  100. case 2:
  101. o.text = arr[0];
  102. o.keyword = arr[0];
  103. o.value = arr[1];
  104. break;
  105. case 3:
  106. o.text = arr[0];
  107. o.keyword = arr[1];
  108. o.value = arr[2];
  109. break;
  110. default:
  111. o.text = arr[0];
  112. o.keyword = arr[1];
  113. o.value = arr[2];
  114. }
  115. options.push(o);
  116. }
  117. }.bind(this));
  118. return options;
  119. }
  120. return [];
  121. },
  122. setData: function(value){
  123. this._setBusinessData(value);
  124. this._setValue(value);
  125. },
  126. _setValue: function(value){
  127. if (!value) value = [];
  128. if (value.length==1 && (!value[0])) value = [];
  129. if (typeOf(value) !=="array") value = [value];
  130. if (this.combox){
  131. this.combox.clear();
  132. comboxValues = [];
  133. value.each(function(v){
  134. if (typeOf(v)==="object"){
  135. comboxValues.push({
  136. "text": v.text || v.title || v.subject || v.name,
  137. "value": v
  138. });
  139. }else{
  140. comboxValues.push(v.toString());
  141. }
  142. }.bind(this));
  143. this.combox.addNewValues(comboxValues);
  144. }else{
  145. value.each(function(v, i){
  146. var text = "";
  147. if (typeOf(v)==="object"){
  148. text = v.text || v.title || v.subject || v.name;
  149. }else{
  150. text = v.toString();
  151. }
  152. if (i<value.length-1) text += this.json.splitShow;
  153. new Element("div", {"styles": {
  154. "float": "left",
  155. "margin-right": "5px"
  156. },"text": text}).inject(this.node.getFirst() || this.node);
  157. }.bind(this));
  158. }
  159. },
  160. resetOption: function(){
  161. if (this.combox){
  162. var list = this.getOptions();
  163. this.combox.setOptions({"list": list});
  164. }
  165. },
  166. addOption: function(text, value){
  167. if (this.combox){
  168. var list = this.getOptions();
  169. list.push({
  170. "text": text,
  171. "value": value
  172. });
  173. this.combox.setOptions({"list": list});
  174. }
  175. },
  176. isEmpty : function(){
  177. var data = this.getData();
  178. if( typeOf(data) === "array" ){
  179. return data.length === 0;
  180. }else{
  181. return !data;
  182. }
  183. },
  184. getInputData: function(){
  185. if (this.combox) return this.combox.getData();
  186. return this._getBusinessData();
  187. },
  188. getTextData: function(){
  189. var v = this.getData();
  190. return {"value": v, "text": v};
  191. //return this.node.get("text");
  192. },
  193. resetData: function(){
  194. this.setData(this.getValue());
  195. }
  196. });