Select.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. this.node.set({
  23. "nodeId": this.json.id,
  24. "MWFType": this.json.type
  25. });
  26. var optionItems = this.getOptions();
  27. var value = this.getValue();
  28. if (value){
  29. if (typeOf(value)!=="array") value = [value];
  30. var texts = [];
  31. optionItems.each(function(item){
  32. var tmps = item.split("|");
  33. var t = tmps[0];
  34. var v = tmps[1] || t;
  35. if (v){
  36. if (value.indexOf(v)!=-1){
  37. texts.push(t);
  38. }
  39. }
  40. });
  41. this.node.set("text", texts.join(", "));
  42. }
  43. },
  44. _loadDomEvents: function(){
  45. Object.each(this.json.events, function(e, key){
  46. if (e.code){
  47. if (this.options.moduleEvents.indexOf(key)===-1){
  48. this.node.addEvent(key, function(event){
  49. return this.form.Macro.fire(e.code, this, event);
  50. }.bind(this));
  51. }
  52. }
  53. }.bind(this));
  54. },
  55. _loadEvents: function(){
  56. Object.each(this.json.events, function(e, key){
  57. if (e.code){
  58. if (this.options.moduleEvents.indexOf(key)!=-1){
  59. this.addEvent(key, function(event){
  60. return this.form.Macro.fire(e.code, this, event);
  61. }.bind(this));
  62. }else{
  63. this.node.addEvent(key, function(event){
  64. return this.form.Macro.fire(e.code, this, event);
  65. }.bind(this));
  66. }
  67. }
  68. }.bind(this));
  69. },
  70. addModuleEvent: function(key, fun){
  71. if (this.options.moduleEvents.indexOf(key)!==-1){
  72. this.addEvent(key, function(event){
  73. return (fun) ? fun(this, event) : null;
  74. }.bind(this));
  75. }else{
  76. this.node.addEvent(key, function(event){
  77. return (fun) ? fun(this, event) : null;
  78. }.bind(this));
  79. }
  80. },
  81. _loadStyles: function(){
  82. if (this.areaNode){
  83. if (this.json.styles) if (this.areaNode) this.areaNode.setStyles(this.json.styles);
  84. if (this.json.inputStyles) this.node.setStyles(this.json.inputStyles);
  85. }else{
  86. if (this.json.styles) this.node.setStyles(this.json.styles);
  87. }
  88. },
  89. _resetNodeEdit: function(){
  90. this.node.empty();
  91. var select = new Element("select");
  92. select.set(this.json.properties);
  93. select.inject(this.node);
  94. },
  95. _loadNodeEdit: function(){
  96. if (!this.json.preprocessing) this._resetNodeEdit();
  97. var select = this.node.getFirst();
  98. this.areaNode = this.node;
  99. this.node = select;
  100. this.node.set({
  101. "id": this.json.id,
  102. "MWFType": this.json.type,
  103. "styles": {
  104. "margin-right": "12px"
  105. }
  106. });
  107. this.setOptions();
  108. this.node.addEvent("change", function(){
  109. var v = this.getInputData("change");
  110. this._setBusinessData(v);
  111. this.validationMode();
  112. if (this.validation()) this._setBusinessData(v);
  113. }.bind(this));
  114. },
  115. resetOption: function(){
  116. this.node.empty();
  117. this.setOptions();
  118. this.fireEvent("resetOption")
  119. },
  120. getOptions: function(){
  121. if (this.json.itemType == "values"){
  122. return this.json.itemValues;
  123. }else{
  124. return this.form.Macro.exec(((this.json.itemScript) ? this.json.itemScript.code : ""), this);
  125. }
  126. return [];
  127. },
  128. setOptions: function(){
  129. var optionItems = this.getOptions();
  130. this._setOptions(optionItems);
  131. },
  132. _setOptions: function(optionItems){
  133. var p = o2.promiseAll(optionItems).then(function(options){
  134. this.moduleSelectAG = null;
  135. if (!options) options = [];
  136. if (o2.typeOf(options)==="array"){
  137. options.each(function(item){
  138. var tmps = item.split("|");
  139. var text = tmps[0];
  140. var value = tmps[1] || text;
  141. var option = new Element("option", {
  142. "value": value,
  143. "text": text
  144. }).inject(this.node);
  145. }.bind(this));
  146. this.fireEvent("setOptions", [options])
  147. }
  148. }.bind(this), function(){
  149. this.moduleSelectAG = null;
  150. }.bind(this));
  151. this.moduleSelectAG = p;
  152. if (p) p.then(function(){
  153. this.moduleSelectAG = null;
  154. }.bind(this), function(){
  155. this.moduleSelectAG = null;
  156. }.bind(this));
  157. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(options){
  158. // this.moduleSelectAG = null;
  159. // if (!options) options = [];
  160. // if (o2.typeOf(options)==="array"){
  161. // options.each(function(item){
  162. // var tmps = item.split("|");
  163. // var text = tmps[0];
  164. // var value = tmps[1] || text;
  165. //
  166. // var option = new Element("option", {
  167. // "value": value,
  168. // "text": text
  169. // }).inject(this.node);
  170. // }.bind(this));
  171. // this.fireEvent("setOptions", [options])
  172. // }
  173. // }.bind(this));
  174. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  175. // this.moduleSelectAG = null;
  176. // }.bind(this));
  177. },
  178. // __setOptions: function(){
  179. // var optionItems = this.getOptions();
  180. // if (!optionItems) optionItems = [];
  181. // if (o2.typeOf(optionItems)==="array"){
  182. // optionItems.each(function(item){
  183. // var tmps = item.split("|");
  184. // var text = tmps[0];
  185. // var value = tmps[1] || text;
  186. //
  187. // var option = new Element("option", {
  188. // "value": value,
  189. // "text": text
  190. // }).inject(this.node);
  191. // }.bind(this));
  192. // this.fireEvent("setOptions", [optionItems])
  193. // }
  194. // },
  195. addOption: function(text, value){
  196. var option = new Element("option", {
  197. "value": value || text,
  198. "text": text
  199. }).inject(this.node);
  200. this.fireEvent("addOption", [text, value])
  201. },
  202. _setValue: function(value, m){
  203. var mothed = m || "__setValue";
  204. if (!!value){
  205. var p = o2.promiseAll(value).then(function(v){
  206. if (o2.typeOf(v)=="array") v = v[0];
  207. if (this.moduleSelectAG){
  208. this.moduleValueAG = this.moduleSelectAG;
  209. this.moduleSelectAG.then(function(){
  210. this[mothed](v);
  211. return v;
  212. }.bind(this), function(){});
  213. }else{
  214. this[mothed](v)
  215. }
  216. return v;
  217. }.bind(this), function(){});
  218. this.moduleValueAG = p;
  219. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  220. this.moduleValueAG = null;
  221. }.bind(this), function(){
  222. this.moduleValueAG = null;
  223. }.bind(this));
  224. }else{
  225. this[mothed](value);
  226. }
  227. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  228. // if (o2.typeOf(v)=="array") v = v[0];
  229. // if (this.moduleSelectAG){
  230. // this.moduleValueAG = this.moduleSelectAG;
  231. // this.moduleSelectAG.then(function(){
  232. // this.__setValue(v);
  233. // }.bind(this));
  234. // }else{
  235. // this.__setValue(v)
  236. // }
  237. // return v;
  238. // }.bind(this));
  239. // if (value && value.isAG){
  240. // this.moduleValueAG = o2.AG.all(value),then(function(v){
  241. // this._setValue(v);
  242. // }.bind(this));
  243. // // this.moduleValueAG = value;
  244. // // value.addResolve(function(v){
  245. // // this._setValue(v);
  246. // // }.bind(this));
  247. // }else{
  248. //
  249. // }
  250. },
  251. __setValue: function(value){
  252. if (!this.readonly && !this.json.isReadonly ) {
  253. this._setBusinessData(value);
  254. for (var i=0; i<this.node.options.length; i++){
  255. var option = this.node.options[i];
  256. if (option.value==value){
  257. option.selected = true;
  258. // break;
  259. }else{
  260. option.selected = false;
  261. }
  262. }
  263. }
  264. this.moduleValueAG = null;
  265. },
  266. // _setValue: function(value){
  267. // if (!this.readonly && !this.json.isReadonly ) {
  268. // this._setBusinessData(value);
  269. // for (var i=0; i<this.node.options.length; i++){
  270. // var option = this.node.options[i];
  271. // if (option.value==value){
  272. // option.selected = true;
  273. // // break;
  274. // }else{
  275. // option.selected = false;
  276. // }
  277. // }
  278. // }
  279. // //this.node.set("value", value);
  280. // },
  281. getTextData: function(){
  282. var value = [];
  283. var text = [];
  284. if (this.readonly|| this.json.isReadonly){
  285. var ops = this.getOptionsObj();
  286. var data = this._getBusinessData();
  287. var d = typeOf(data) === "array" ? data : [data];
  288. d.each( function (v) {
  289. var idx = ops.valueList.indexOf( v );
  290. value.push( v || "" );
  291. text.push( idx > -1 ? ops.textList[idx] : (v || "") );
  292. });
  293. }else{
  294. var ops = this.node.getElements("option");
  295. ops.each(function(op){
  296. if (op.selected){
  297. var v = op.get("value");
  298. var t = op.get("text");
  299. value.push(v || "");
  300. text.push(t || v || "");
  301. }
  302. });
  303. }
  304. if (!value.length) value = [""];
  305. if (!text.length) text = [""];
  306. return {"value": value, "text": text};
  307. },
  308. getInputData: function(){
  309. if( this.readonly || this.json.isReadonly ){
  310. return this._getBusinessData();
  311. }else{
  312. var ops = this.node.getElements("option");
  313. var value = [];
  314. ops.each(function(op){
  315. if (op.selected){
  316. var v = op.get("value");
  317. if (v) value.push(v);
  318. }
  319. });
  320. if (!value.length) return null;
  321. return (value.length==1) ? value[0] : value;
  322. }
  323. },
  324. resetData: function(){
  325. this.setData(this.getValue());
  326. },
  327. getOptionsObj : function(){
  328. var textList = [];
  329. var valueList = [];
  330. var optionItems = this.getOptions();
  331. if (!optionItems) optionItems = [];
  332. if (o2.typeOf(optionItems)==="array"){
  333. optionItems.each(function(item){
  334. var tmps = item.split("|");
  335. textList.push( tmps[0] );
  336. valueList.push( tmps[1] || tmps[0] );
  337. }.bind(this));
  338. }
  339. return { textList : textList, valueList : valueList };
  340. },
  341. setData: function(data){
  342. return this._setValue(data, "__setData");
  343. // if (data && data.isAG){
  344. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  345. // if (o2.typeOf(v)=="array") v = v[0];
  346. // this.__setData(v);
  347. // }.bind(this));
  348. // }else{
  349. // this.__setData(data);
  350. // }
  351. // if (data && data.isAG){
  352. // this.moduleValueAG = data;
  353. // data.addResolve(function(v){
  354. // this.setData(v);
  355. // }.bind(this));
  356. // }else{
  357. // this.__setData(data);
  358. // this.moduleValueAG = null;
  359. // }
  360. },
  361. __setData: function(data){
  362. this._setBusinessData(data);
  363. if (this.readonly|| this.json.isReadonly){
  364. var d = typeOf(data) === "array" ? data : [data];
  365. var ops = this.getOptionsObj();
  366. var result = [];
  367. d.each( function (v) {
  368. var idx = ops.valueList.indexOf( v );
  369. result.push( idx > -1 ? ops.textList[idx] : v);
  370. })
  371. this.node.set("text", result.join(","));
  372. }else{
  373. var ops = this.node.getElements("option");
  374. ops.each(function(op){
  375. if (typeOf(data)=="array"){
  376. if (data.indexOf(op.get("value"))!=-1){
  377. op.set("selected", true);
  378. }else{
  379. op.set("selected", false);
  380. }
  381. }else{
  382. if (data == op.get("value")){
  383. op.set("selected", true);
  384. }else{
  385. op.set("selected", false);
  386. }
  387. }
  388. });
  389. this.validationMode();
  390. }
  391. this.fireEvent("setData", [data]);
  392. }
  393. });