Select.js 5.4 KB

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