Select.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. _resetNodeEdit: function(){
  86. this.node.empty();
  87. var select = new Element("select");
  88. select.set(this.json.properties);
  89. select.inject(this.node);
  90. },
  91. _loadNodeEdit: function(){
  92. if (!this.json.preprocessing) this._resetNodeEdit();
  93. var select = this.node.getFirst();
  94. this.areaNode = this.node;
  95. this.node = select;
  96. this.node.set({
  97. "id": this.json.id,
  98. "MWFType": this.json.type,
  99. "styles": {
  100. "margin-right": "12px"
  101. }
  102. });
  103. this.setOptions();
  104. this.node.addEvent("change", function(){
  105. var v = this.getInputData("change");
  106. this._setBusinessData(v);
  107. this.validationMode();
  108. if (this.validation()) this._setBusinessData(v);
  109. }.bind(this));
  110. },
  111. resetOption: function(){
  112. this.node.empty();
  113. this.setOptions();
  114. this.fireEvent("resetOption")
  115. },
  116. getOptions: function(){
  117. if (this.json.itemType == "values"){
  118. return this.json.itemValues;
  119. }else{
  120. return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  121. }
  122. return [];
  123. },
  124. setOptions: function(){
  125. var optionItems = this.getOptions();
  126. if (!optionItems) optionItems = [];
  127. if (o2.typeOf(optionItems)==="array"){
  128. optionItems.each(function(item){
  129. var tmps = item.split("|");
  130. var text = tmps[0];
  131. var value = tmps[1] || text;
  132. var option = new Element("option", {
  133. "value": value,
  134. "text": text
  135. }).inject(this.node);
  136. }.bind(this));
  137. this.fireEvent("setOptions", [optionItems])
  138. }
  139. },
  140. addOption: function(text, value){
  141. var option = new Element("option", {
  142. "value": value || text,
  143. "text": text
  144. }).inject(this.node);
  145. this.fireEvent("addOption", [text, value])
  146. },
  147. _setValue: function(value){
  148. if (!this.readonly && !this.json.isReadonly ) {
  149. this._setBusinessData(value);
  150. for (var i=0; i<this.node.options.length; i++){
  151. var option = this.node.options[i];
  152. if (option.value==value){
  153. option.selected = true;
  154. // break;
  155. }else{
  156. option.selected = false;
  157. }
  158. }
  159. }
  160. //this.node.set("value", value);
  161. },
  162. getTextData: function(){
  163. var value = [];
  164. var text = [];
  165. if (this.readonly|| this.json.isReadonly){
  166. var ops = this.getOptionsObj();
  167. var data = this._getBusinessData();
  168. var d = typeOf(data) === "array" ? data : [data];
  169. d.each( function (v) {
  170. var idx = ops.valueList.indexOf( v );
  171. value.push( v || "" );
  172. text.push( idx > -1 ? ops.textList[idx] : (v || "") );
  173. });
  174. }else{
  175. var ops = this.node.getElements("option");
  176. ops.each(function(op){
  177. if (op.selected){
  178. var v = op.get("value");
  179. var t = op.get("text");
  180. value.push(v || "");
  181. text.push(t || v || "");
  182. }
  183. });
  184. }
  185. if (!value.length) value = [""];
  186. if (!text.length) text = [""];
  187. return {"value": value, "text": text};
  188. },
  189. getInputData: function(){
  190. var ops = this.node.getElements("option");
  191. var value = [];
  192. ops.each(function(op){
  193. if (op.selected){
  194. var v = op.get("value");
  195. if (v) value.push(v);
  196. }
  197. });
  198. if (!value.length) return null;
  199. return (value.length==1) ? value[0] : value;
  200. },
  201. resetData: function(){
  202. this.setData(this.getValue());
  203. },
  204. getOptionsObj : function(){
  205. var textList = [];
  206. var valueList = [];
  207. var optionItems = this.getOptions();
  208. if (!optionItems) optionItems = [];
  209. if (o2.typeOf(optionItems)==="array"){
  210. optionItems.each(function(item){
  211. var tmps = item.split("|");
  212. textList.push( tmps[0] );
  213. valueList.push( tmps[1] || tmps[0] );
  214. }.bind(this));
  215. }
  216. return { textList : textList, valueList : valueList };
  217. },
  218. setData: function(data){
  219. this._setBusinessData(data);
  220. if (this.readonly|| this.json.isReadonly){
  221. var d = typeOf(data) === "array" ? data : [data];
  222. var ops = this.getOptionsObj();
  223. var result = [];
  224. d.each( function (v) {
  225. var idx = ops.valueList.indexOf( v );
  226. result.push( idx > -1 ? ops.textList[idx] : v);
  227. })
  228. this.node.set("text", result.join(","))
  229. }else{
  230. var ops = this.node.getElements("option");
  231. ops.each(function(op){
  232. if (typeOf(data)=="array"){
  233. if (data.indexOf(op.get("value"))!=-1){
  234. op.set("selected", true);
  235. }else{
  236. op.set("selected", false);
  237. }
  238. }else{
  239. if (data == op.get("value")){
  240. op.set("selected", true);
  241. }else{
  242. op.set("selected", false);
  243. }
  244. }
  245. });
  246. }
  247. this.fireEvent("setData", [data]);
  248. }
  249. });