Select.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. this._setOptions(optionItems);
  127. },
  128. _setOptions: function(optionItems){
  129. var p = o2.promiseAll(optionItems).then(function(options){
  130. this.moduleSelectAG = null;
  131. if (!options) options = [];
  132. if (o2.typeOf(options)==="array"){
  133. options.each(function(item){
  134. var tmps = item.split("|");
  135. var text = tmps[0];
  136. var value = tmps[1] || text;
  137. var option = new Element("option", {
  138. "value": value,
  139. "text": text
  140. }).inject(this.node);
  141. }.bind(this));
  142. this.fireEvent("setOptions", [options])
  143. }
  144. }.bind(this));
  145. this.moduleSelectAG = p;
  146. if (p) p.then(function(){
  147. this.moduleSelectAG = null;
  148. }.bind(this));
  149. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(options){
  150. // this.moduleSelectAG = null;
  151. // if (!options) options = [];
  152. // if (o2.typeOf(options)==="array"){
  153. // options.each(function(item){
  154. // var tmps = item.split("|");
  155. // var text = tmps[0];
  156. // var value = tmps[1] || text;
  157. //
  158. // var option = new Element("option", {
  159. // "value": value,
  160. // "text": text
  161. // }).inject(this.node);
  162. // }.bind(this));
  163. // this.fireEvent("setOptions", [options])
  164. // }
  165. // }.bind(this));
  166. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  167. // this.moduleSelectAG = null;
  168. // }.bind(this));
  169. },
  170. // __setOptions: function(){
  171. // var optionItems = this.getOptions();
  172. // if (!optionItems) optionItems = [];
  173. // if (o2.typeOf(optionItems)==="array"){
  174. // optionItems.each(function(item){
  175. // var tmps = item.split("|");
  176. // var text = tmps[0];
  177. // var value = tmps[1] || text;
  178. //
  179. // var option = new Element("option", {
  180. // "value": value,
  181. // "text": text
  182. // }).inject(this.node);
  183. // }.bind(this));
  184. // this.fireEvent("setOptions", [optionItems])
  185. // }
  186. // },
  187. addOption: function(text, value){
  188. var option = new Element("option", {
  189. "value": value || text,
  190. "text": text
  191. }).inject(this.node);
  192. this.fireEvent("addOption", [text, value])
  193. },
  194. _setValue: function(value){
  195. if (!!value){
  196. var p = o2.promiseAll(value).then(function(v){
  197. if (o2.typeOf(v)=="array") v = v[0];
  198. if (this.moduleSelectAG){
  199. this.moduleValueAG = this.moduleSelectAG;
  200. this.moduleSelectAG.then(function(){
  201. this.__setValue(v);
  202. return v;
  203. }.bind(this));
  204. }else{
  205. this.__setValue(v)
  206. }
  207. return v;
  208. }.bind(this));
  209. this.moduleValueAG = p;
  210. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  211. this.moduleValueAG = null;
  212. }.bind(this));
  213. }else{
  214. this.__setValue(value);
  215. }
  216. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  217. // if (o2.typeOf(v)=="array") v = v[0];
  218. // if (this.moduleSelectAG){
  219. // this.moduleValueAG = this.moduleSelectAG;
  220. // this.moduleSelectAG.then(function(){
  221. // this.__setValue(v);
  222. // }.bind(this));
  223. // }else{
  224. // this.__setValue(v)
  225. // }
  226. // return v;
  227. // }.bind(this));
  228. // if (value && value.isAG){
  229. // this.moduleValueAG = o2.AG.all(value),then(function(v){
  230. // this._setValue(v);
  231. // }.bind(this));
  232. // // this.moduleValueAG = value;
  233. // // value.addResolve(function(v){
  234. // // this._setValue(v);
  235. // // }.bind(this));
  236. // }else{
  237. //
  238. // }
  239. },
  240. __setValue: function(value){
  241. if (!this.readonly && !this.json.isReadonly ) {
  242. this._setBusinessData(value);
  243. for (var i=0; i<this.node.options.length; i++){
  244. var option = this.node.options[i];
  245. if (option.value==value){
  246. option.selected = true;
  247. // break;
  248. }else{
  249. option.selected = false;
  250. }
  251. }
  252. }
  253. this.moduleValueAG = null;
  254. },
  255. // _setValue: function(value){
  256. // if (!this.readonly && !this.json.isReadonly ) {
  257. // this._setBusinessData(value);
  258. // for (var i=0; i<this.node.options.length; i++){
  259. // var option = this.node.options[i];
  260. // if (option.value==value){
  261. // option.selected = true;
  262. // // break;
  263. // }else{
  264. // option.selected = false;
  265. // }
  266. // }
  267. // }
  268. // //this.node.set("value", value);
  269. // },
  270. getTextData: function(){
  271. var value = [];
  272. var text = [];
  273. if (this.readonly|| this.json.isReadonly){
  274. var ops = this.getOptionsObj();
  275. var data = this._getBusinessData();
  276. var d = typeOf(data) === "array" ? data : [data];
  277. d.each( function (v) {
  278. var idx = ops.valueList.indexOf( v );
  279. value.push( v || "" );
  280. text.push( idx > -1 ? ops.textList[idx] : (v || "") );
  281. });
  282. }else{
  283. var ops = this.node.getElements("option");
  284. ops.each(function(op){
  285. if (op.selected){
  286. var v = op.get("value");
  287. var t = op.get("text");
  288. value.push(v || "");
  289. text.push(t || v || "");
  290. }
  291. });
  292. }
  293. if (!value.length) value = [""];
  294. if (!text.length) text = [""];
  295. return {"value": value, "text": text};
  296. },
  297. getInputData: function(){
  298. var ops = this.node.getElements("option");
  299. var value = [];
  300. ops.each(function(op){
  301. if (op.selected){
  302. var v = op.get("value");
  303. if (v) value.push(v);
  304. }
  305. });
  306. if (!value.length) return null;
  307. return (value.length==1) ? value[0] : value;
  308. },
  309. resetData: function(){
  310. this.setData(this.getValue());
  311. },
  312. getOptionsObj : function(){
  313. var textList = [];
  314. var valueList = [];
  315. var optionItems = this.getOptions();
  316. if (!optionItems) optionItems = [];
  317. if (o2.typeOf(optionItems)==="array"){
  318. optionItems.each(function(item){
  319. var tmps = item.split("|");
  320. textList.push( tmps[0] );
  321. valueList.push( tmps[1] || tmps[0] );
  322. }.bind(this));
  323. }
  324. return { textList : textList, valueList : valueList };
  325. },
  326. setData: function(data){
  327. return this._setValue(data);
  328. // if (data && data.isAG){
  329. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  330. // if (o2.typeOf(v)=="array") v = v[0];
  331. // this.__setData(v);
  332. // }.bind(this));
  333. // }else{
  334. // this.__setData(data);
  335. // }
  336. // if (data && data.isAG){
  337. // this.moduleValueAG = data;
  338. // data.addResolve(function(v){
  339. // this.setData(v);
  340. // }.bind(this));
  341. // }else{
  342. // this.__setData(data);
  343. // this.moduleValueAG = null;
  344. // }
  345. },
  346. __setData: function(data){
  347. this._setBusinessData(data);
  348. if (this.readonly|| this.json.isReadonly){
  349. var d = typeOf(data) === "array" ? data : [data];
  350. var ops = this.getOptionsObj();
  351. var result = [];
  352. d.each( function (v) {
  353. var idx = ops.valueList.indexOf( v );
  354. result.push( idx > -1 ? ops.textList[idx] : v);
  355. })
  356. this.node.set("text", result.join(","));
  357. }else{
  358. var ops = this.node.getElements("option");
  359. ops.each(function(op){
  360. if (typeOf(data)=="array"){
  361. if (data.indexOf(op.get("value"))!=-1){
  362. op.set("selected", true);
  363. }else{
  364. op.set("selected", false);
  365. }
  366. }else{
  367. if (data == op.get("value")){
  368. op.set("selected", true);
  369. }else{
  370. op.set("selected", false);
  371. }
  372. }
  373. });
  374. }
  375. this.fireEvent("setData", [data]);
  376. }
  377. });