Select.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.xApplication.process.Xform.Select = MWF.APPSelect = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Input,
  5. iconStyle: "selectIcon",
  6. initialize: function(node, json, form, options){
  7. this.node = $(node);
  8. this.node.store("module", this);
  9. this.json = json;
  10. this.form = form;
  11. this.field = true;
  12. },
  13. _loadNode: function(){
  14. if (this.readonly){
  15. this._loadNodeRead();
  16. }else{
  17. this._loadNodeEdit();
  18. }
  19. },
  20. _loadNodeRead: function(){
  21. this.node.empty();
  22. var optionItems = this.getOptions();
  23. var value = this.getValue();
  24. if (value){
  25. if (typeOf(value)!=="array") value = [value];
  26. var texts = [];
  27. optionItems.each(function(item){
  28. var tmps = item.split("|");
  29. var t = tmps[0];
  30. var v = tmps[1] || t;
  31. if (v){
  32. if (value.indexOf(v)!=-1){
  33. texts.push(t);
  34. }
  35. }
  36. });
  37. this.node.set("text", texts.join(", "));
  38. }
  39. },
  40. _loadEvents: function(){
  41. Object.each(this.json.events, function(e, key){
  42. if (e.code){
  43. if (this.options.moduleEvents.indexOf(key)!=-1){
  44. this.addEvent(key, function(event){
  45. return this.form.Macro.fire(e.code, this, event);
  46. }.bind(this));
  47. }else{
  48. this.node.addEvent(key, function(event){
  49. return this.form.Macro.fire(e.code, this, event);
  50. }.bind(this));
  51. }
  52. }
  53. }.bind(this));
  54. },
  55. _loadStyles: function(){
  56. if (this.areaNode){
  57. if (this.json.styles) if (this.areaNode) this.areaNode.setStyles(this.json.styles);
  58. if (this.json.inputStyles) this.node.setStyles(this.json.inputStyles);
  59. }else{
  60. if (this.json.styles) this.node.setStyles(this.json.styles);
  61. }
  62. },
  63. _loadNodeEdit: function(){
  64. this.node.empty();
  65. var select = new Element("select");
  66. select.set(this.json.properties);
  67. select.inject(this.node);
  68. //this.node.destroy();
  69. this.areaNode = this.node;
  70. this.node = select;
  71. this.node.set({
  72. "id": this.json.id,
  73. "MWFType": this.json.type,
  74. "styles": {
  75. "margin-right": "12px"
  76. }
  77. });
  78. this.setOptions();
  79. this.node.addEvent("change", function(){
  80. this.validationMode();
  81. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  82. }.bind(this));
  83. },
  84. resetOption: function(){
  85. this.node.empty();
  86. this.setOptions();
  87. },
  88. getOptions: function(){
  89. if (this.json.itemType == "values"){
  90. return this.json.itemValues;
  91. }else{
  92. return this.form.Macro.exec(this.json.itemScript.code, this);
  93. }
  94. return [];
  95. },
  96. setOptions: function(){
  97. var optionItems = this.getOptions();
  98. if (!optionItems) optionItems = [];
  99. if (o2.typeOf(optionItems)==="array"){
  100. optionItems.each(function(item){
  101. var tmps = item.split("|");
  102. var text = tmps[0];
  103. var value = tmps[1] || text;
  104. var option = new Element("option", {
  105. "value": value,
  106. "text": text
  107. }).inject(this.node);
  108. }.bind(this));
  109. }
  110. },
  111. addOption: function(text, value){
  112. var option = new Element("option", {
  113. "value": value || text,
  114. "text": text
  115. }).inject(this.node);
  116. },
  117. _setValue: function(value){
  118. if (!this.readonly) {
  119. this._setBusinessData(value);
  120. for (var i=0; i<this.node.options.length; i++){
  121. var option = this.node.options[i];
  122. if (option.value==value){
  123. option.selected = true;
  124. // break;
  125. }else{
  126. option.selected = false;
  127. }
  128. }
  129. }
  130. //this.node.set("value", value);
  131. },
  132. getTextData: function(){
  133. var ops = this.node.getElements("option");
  134. var value = [];
  135. var text = [];
  136. ops.each(function(op){
  137. if (op.selected){
  138. var v = op.get("value");
  139. var t = op.get("text");
  140. value.push(v || "");
  141. text.push(t || v || "");
  142. }
  143. });
  144. if (!value.length) value = [""];
  145. if (!text.length) text = [""];
  146. return {"value": value, "text": text};
  147. },
  148. getInputData: function(){
  149. var ops = this.node.getElements("option");
  150. var value = [];
  151. ops.each(function(op){
  152. if (op.selected){
  153. var v = op.get("value");
  154. if (v) value.push(v);
  155. }
  156. });
  157. if (!value.length) return null;
  158. return (value.length==1) ? value[0] : value;
  159. },
  160. resetData: function(){
  161. this.setData(this.getValue());
  162. },
  163. setData: function(data){
  164. this._setBusinessData(data);
  165. var ops = this.node.getElements("option");
  166. ops.each(function(op){
  167. if (typeOf(data)=="array"){
  168. if (data.indexOf(op.get("value"))!=-1){
  169. op.set("selected", true);
  170. }else{
  171. op.set("selected", false);
  172. }
  173. }else{
  174. if (data == op.get("value")){
  175. op.set("selected", true);
  176. }else{
  177. op.set("selected", false);
  178. }
  179. }
  180. });
  181. }
  182. });