Select.js 11 KB

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