Combox.js 6.1 KB

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