Combox.js 5.7 KB

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