Select.js 11 KB

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