Select.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var texts = [];
  26. optionItems.each(function(item){
  27. var tmps = item.split("|");
  28. var t = tmps[0];
  29. var v = tmps[1] || t;
  30. if (v){
  31. if (value.indexOf(v)!=-1){
  32. texts.push(t);
  33. }
  34. }
  35. });
  36. this.node.set("text", texts.join(", "));
  37. }
  38. },
  39. _loadEvents: function(){
  40. Object.each(this.json.events, function(e, key){
  41. if (e.code){
  42. if (this.options.moduleEvents.indexOf(key)!=-1){
  43. this.addEvent(key, function(event){
  44. return this.form.Macro.fire(e.code, this, event);
  45. }.bind(this));
  46. }else{
  47. this.node.addEvent(key, function(event){
  48. return this.form.Macro.fire(e.code, this, event);
  49. }.bind(this));
  50. }
  51. }
  52. }.bind(this));
  53. },
  54. _loadNodeEdit: function(){
  55. var select = new Element("select");
  56. select.set(this.json.properties);
  57. select.inject(this.node, "after");
  58. this.node.destroy();
  59. this.node = select;
  60. this.node.set({
  61. "id": this.json.id,
  62. "MWFType": this.json.type,
  63. "styles": {
  64. "margin-right": "12px"
  65. }
  66. });
  67. this.setOptions();
  68. this.node.addEvent("change", function(){
  69. this.validationMode();
  70. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  71. }.bind(this));
  72. },
  73. resetOption: function(){
  74. this.node.empty();
  75. this.setOptions();
  76. },
  77. getOptions: function(){
  78. if (this.json.itemType == "values"){
  79. return this.json.itemValues;
  80. }else{
  81. return this.form.Macro.exec(this.json.itemScript.code, this);
  82. }
  83. return [];
  84. },
  85. setOptions: function(){
  86. var optionItems = this.getOptions();
  87. if (!optionItems) optionItems = [];
  88. optionItems.each(function(item){
  89. var tmps = item.split("|");
  90. var text = tmps[0];
  91. var value = tmps[1] || text;
  92. var option = new Element("option", {
  93. "value": value,
  94. "text": text
  95. }).inject(this.node);
  96. }.bind(this));
  97. },
  98. _setValue: function(value){
  99. if (!this.readonly) {
  100. this._setBusinessData(value);
  101. for (var i=0; i<this.node.options.length; i++){
  102. var option = this.node.options[i];
  103. if (option.value==value){
  104. option.selected = true;
  105. // break;
  106. }else{
  107. option.selected = false;
  108. }
  109. }
  110. }
  111. //this.node.set("value", value);
  112. },
  113. getTextData: function(){
  114. var ops = this.node.getElements("option");
  115. var value = [];
  116. var text = [];
  117. ops.each(function(op){
  118. if (op.selected){
  119. var v = op.get("value");
  120. var t = op.get("text");
  121. value.push(v || "");
  122. text.push(t || v || "");
  123. }
  124. });
  125. if (!value.length) value = [""];
  126. if (!text.length) text = [""];
  127. return {"value": value, "text": text};
  128. },
  129. getInputData: function(){
  130. var ops = this.node.getElements("option");
  131. var value = [];
  132. ops.each(function(op){
  133. if (op.selected){
  134. var v = op.get("value");
  135. if (v) value.push(v);
  136. }
  137. });
  138. if (!value.length) return null;
  139. return (value.length==1) ? value[0] : value;
  140. },
  141. resetData: function(){
  142. this.setData(this.getValue());
  143. },
  144. setData: function(data){
  145. this._setBusinessData(data);
  146. var ops = this.node.getElements("option");
  147. ops.each(function(op){
  148. if (typeOf(data)=="array"){
  149. if (data.indexOf(op.get("value"))!=-1){
  150. op.set("selected", true);
  151. }else{
  152. op.set("selected", false);
  153. }
  154. }else{
  155. if (data == op.get("value")){
  156. op.set("selected", true);
  157. }else{
  158. op.set("selected", false);
  159. }
  160. }
  161. });
  162. }
  163. });