Select.js 6.0 KB

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