Radio.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.require("MWF.widget.UUID", null, false);
  3. MWF.xApplication.process.Xform.Radio = MWF.APPRadio = new Class({
  4. Implements: [Events],
  5. Extends: MWF.APP$Input,
  6. loadDescription: function(){},
  7. _loadNode: function(){
  8. if (this.readonly || this.json.isReadonly ){
  9. this._loadNodeRead();
  10. }else{
  11. this._loadNodeEdit();
  12. }
  13. },
  14. _loadNodeRead: function(){
  15. this.node.empty();
  16. var radioValues = this.getOptions();
  17. var value = this.getValue();
  18. if (value){
  19. var texts = "";
  20. for (var i=0; i<radioValues.length; i++){
  21. var item = radioValues[i];
  22. var tmps = item.split("|");
  23. var t = tmps[0];
  24. var v = tmps[1] || t;
  25. // if (value.indexOf(v)!=-1){
  26. // texts = t;
  27. // break;
  28. // }
  29. if (value == v){
  30. texts = t;
  31. break;
  32. }
  33. }
  34. this.node.set("text", texts);
  35. }
  36. },
  37. _resetNodeEdit: function(){
  38. var div = new Element("div");
  39. div.set(this.json.properties);
  40. div.inject(this.node, "after");
  41. this.node.destroy();
  42. this.node = div;
  43. },
  44. _loadNodeEdit: function(){
  45. //this.container = new Element("select");
  46. if (!this.json.preprocessing) this._resetNodeEdit();
  47. this.node.set({
  48. "id": this.json.id,
  49. "MWFType": this.json.type,
  50. "styles": {
  51. "display": "inline"
  52. }
  53. });
  54. this.setOptions();
  55. },
  56. _loadDomEvents: function(){
  57. },
  58. _loadEvents: function(){
  59. Object.each(this.json.events, function(e, key){
  60. if (e.code){
  61. if (this.options.moduleEvents.indexOf(key)!=-1){
  62. this.addEvent(key, function(event){
  63. return this.form.Macro.fire(e.code, this, event);
  64. }.bind(this));
  65. }else{
  66. //this.node.addEvent(key, function(event){
  67. // return this.form.Macro.fire(e.code, this, event);
  68. //}.bind(this));
  69. }
  70. }
  71. }.bind(this));
  72. },
  73. addModuleEvent: function(key, fun){
  74. if (this.options.moduleEvents.indexOf(key)!==-1){
  75. this.addEvent(key, function(event){
  76. return (fun) ? fun(this, event) : null;
  77. }.bind(this));
  78. }else{
  79. var inputs = this.node.getElements("input");
  80. inputs.each(function(input){
  81. input.addEvent(key, function(event){
  82. return (fun) ? fun(this, event) : null;
  83. }.bind(this));
  84. }.bind(this));
  85. }
  86. },
  87. resetOption: function(){
  88. this.node.empty();
  89. this.setOptions();
  90. },
  91. getOptions: function(){
  92. if (this.json.itemType == "values"){
  93. return this.json.itemValues;
  94. }else{
  95. return this.form.Macro.exec(this.json.itemScript.code, this);
  96. }
  97. return [];
  98. },
  99. setOptions: function(){
  100. var optionItems = this.getOptions();
  101. this._setOptions(optionItems);
  102. },
  103. _setOptions: function(optionItems){
  104. var p = o2.promiseAll(optionItems).then(function(radioValues){
  105. this.moduleSelectAG = null;
  106. if (!radioValues) radioValues = [];
  107. if (o2.typeOf(radioValues)==="array"){
  108. var flag = (new MWF.widget.UUID).toString();
  109. radioValues.each(function(item){
  110. var tmps = item.split("|");
  111. var text = tmps[0];
  112. var value = tmps[1] || text;
  113. var radio = new Element("input", {
  114. "type": "radio",
  115. "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  116. "value": value,
  117. "showText": text,
  118. "styles": this.json.buttonStyles
  119. }).inject(this.node);
  120. //radio.appendText(text, "after");
  121. var textNode = new Element( "span", {
  122. "text" : text,
  123. "styles" : { "cursor" : "default" }
  124. }).inject(this.node);
  125. textNode.addEvent("click", function( ev ){
  126. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  127. this.radio.checked = true;
  128. this.radio.fireEvent("change");
  129. this.radio.fireEvent("click");
  130. }.bind( {radio : radio} ) );
  131. radio.addEvent("click", function(){
  132. this.validationMode();
  133. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  134. }.bind(this));
  135. Object.each(this.json.events, function(e, key){
  136. if (e.code){
  137. if (this.options.moduleEvents.indexOf(key)!=-1){
  138. }else{
  139. radio.addEvent(key, function(event){
  140. return this.form.Macro.fire(e.code, this, event);
  141. }.bind(this));
  142. }
  143. }
  144. }.bind(this));
  145. }.bind(this));
  146. }
  147. }.bind(this));
  148. this.moduleSelectAG = p;
  149. if (p) p.then(function(){
  150. this.moduleSelectAG = null;
  151. }.bind(this));
  152. // this.moduleSelectAG = o2.AG.all(optionItems).then(function(radioValues){
  153. // this.moduleSelectAG = null;
  154. //
  155. // if (!radioValues) radioValues = [];
  156. // if (o2.typeOf(radioValues)==="array"){
  157. // var flag = (new MWF.widget.UUID).toString();
  158. // radioValues.each(function(item){
  159. // var tmps = item.split("|");
  160. // var text = tmps[0];
  161. // var value = tmps[1] || text;
  162. //
  163. // var radio = new Element("input", {
  164. // "type": "radio",
  165. // "name": (this.json.properties && this.json.properties.name) ? this.json.properties.name : flag+this.json.id,
  166. // "value": value,
  167. // "showText": text,
  168. // "styles": this.json.buttonStyles
  169. // }).inject(this.node);
  170. // //radio.appendText(text, "after");
  171. //
  172. // var textNode = new Element( "span", {
  173. // "text" : text,
  174. // "styles" : { "cursor" : "default" }
  175. // }).inject(this.node);
  176. // textNode.addEvent("click", function( ev ){
  177. // if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  178. // this.radio.checked = true;
  179. // this.radio.fireEvent("change");
  180. // this.radio.fireEvent("click");
  181. // }.bind( {radio : radio} ) );
  182. //
  183. // radio.addEvent("click", function(){
  184. // this.validationMode();
  185. // if (this.validation()) this._setBusinessData(this.getInputData("change"));
  186. // }.bind(this));
  187. //
  188. // Object.each(this.json.events, function(e, key){
  189. // if (e.code){
  190. // if (this.options.moduleEvents.indexOf(key)!=-1){
  191. // }else{
  192. // radio.addEvent(key, function(event){
  193. // return this.form.Macro.fire(e.code, this, event);
  194. // }.bind(this));
  195. // }
  196. // }
  197. // }.bind(this));
  198. // }.bind(this));
  199. // }
  200. // }.bind(this))
  201. // if (this.moduleSelectAG) this.moduleSelectAG.then(function(){
  202. // this.moduleSelectAG = null;
  203. // }.bind(this));
  204. },
  205. _setValue: function(value, m){
  206. var mothed = m || "__setValue";
  207. if (!!value){
  208. var p = o2.promiseAll(value).then(function(v){
  209. if (o2.typeOf(v)=="array") v = v[0];
  210. if (this.moduleSelectAG){
  211. this.moduleValueAG = this.moduleSelectAG;
  212. this.moduleSelectAG.then(function(){
  213. this[mothed](v);
  214. return v;
  215. }.bind(this));
  216. }else{
  217. this[mothed](v)
  218. }
  219. return v;
  220. }.bind(this));
  221. this.moduleValueAG = p;
  222. if (this.moduleValueAG) this.moduleValueAG.then(function(){
  223. this.moduleValueAG = null;
  224. }.bind(this));
  225. }else{
  226. this[mothed](value);
  227. }
  228. // this.moduleValueAG = o2.AG.all(value).then(function(v){
  229. // if (o2.typeOf(v)=="array") v = v[0];
  230. // if (this.moduleSelectAG){
  231. // this.moduleValueAG = this.moduleSelectAG;
  232. // this.moduleSelectAG.then(function(){
  233. // this.__setValue(v);
  234. // }.bind(this));
  235. // }else{
  236. // this.__setValue(v)
  237. // }
  238. // return v;
  239. // }.bind(this));
  240. //
  241. // if (this.moduleValueAG) this.moduleValueAG.then(function(){
  242. // this.moduleValueAG = null;
  243. // }.bind(this));
  244. },
  245. __setValue: function(value){
  246. this._setBusinessData(value);
  247. var radios = this.node.getElements("input");
  248. for (var i=0; i<radios.length; i++){
  249. var radio = radios[i];
  250. if (radio.value==value){
  251. radio.checked = true;
  252. break;
  253. }
  254. }
  255. },
  256. getTextData: function(){
  257. var inputs = this.node.getElements("input");
  258. var value = "";
  259. var text = "";
  260. if (inputs.length){
  261. for (var i=0; i<inputs.length; i++){
  262. var input = inputs[i];
  263. if (input.checked){
  264. value = input.get("value");
  265. text = input.get("showText");
  266. break;
  267. }
  268. }
  269. }
  270. return {"value": [value] || "", "text": [text || value || ""]};
  271. },
  272. getInputData: function(){
  273. if (this.readonly || this.json.isReadonly ){
  274. return this._getBusinessData();
  275. }else{
  276. var inputs = this.node.getElements("input");
  277. var value = "";
  278. if (inputs.length){
  279. for (var i=0; i<inputs.length; i++){
  280. var input = inputs[i];
  281. if (input.checked){
  282. value = input.get("value");
  283. break;
  284. }
  285. }
  286. }
  287. return value;
  288. }
  289. },
  290. resetData: function(){
  291. this.setData(this.getValue());
  292. },
  293. getSelectedInput: function(){
  294. var inputs = this.node.getElements("input");
  295. if (inputs.length){
  296. for (var i=0; i<inputs.length; i++){
  297. if (inputs[i].checked) return inputs[i];
  298. }
  299. }
  300. return null;
  301. },
  302. setData: function(data){
  303. return this._setValue(data, "__setData");
  304. // if (data && data.isAG){
  305. // this.moduleValueAG = o2.AG.all(data).then(function(v){
  306. // if (o2.typeOf(v)=="array") v = v[0];
  307. // this.__setData(v);
  308. // }.bind(this));
  309. // }else{
  310. // this.__setData(data);
  311. // }
  312. // if (data && data.isAG){
  313. // this.moduleValueAG = data;
  314. // data.addResolve(function(v){
  315. // this.setData(v);
  316. // }.bind(this));
  317. // }else{
  318. // this.__setData(data);
  319. // this.moduleValueAG = null;
  320. // }
  321. },
  322. __setData: function(data){
  323. this._setBusinessData(data);
  324. var inputs = this.node.getElements("input");
  325. if (inputs.length){
  326. for (var i=0; i<inputs.length; i++){
  327. if (data==inputs[i].get("value")){
  328. inputs[i].set("checked", true);
  329. }else{
  330. inputs[i].set("checked", false);
  331. }
  332. }
  333. }
  334. this.fireEvent("setData");
  335. },
  336. notValidationMode: function(text){
  337. if (!this.isNotValidationMode){
  338. this.isNotValidationMode = true;
  339. this.node.store("background", this.node.getStyles("background"));
  340. this.node.setStyle("background", "#ffdcdc");
  341. this.errNode = this.createErrorNode(text);
  342. if (this.iconNode){
  343. this.errNode.inject(this.iconNode, "after");
  344. }else{
  345. this.errNode.inject(this.node, "after");
  346. }
  347. this.showNotValidationMode(this.node);
  348. if (!this.node.isIntoView()) this.node.scrollIntoView();
  349. }
  350. },
  351. validationMode: function(routeName, opinion){
  352. if (!this.validationConfig(routeName, opinion)) return false;
  353. if (this.isNotValidationMode){
  354. this.isNotValidationMode = false;
  355. this.node.setStyles(this.node.retrieve("background"));
  356. if (this.errNode){
  357. this.errNode.destroy();
  358. this.errNode = null;
  359. }
  360. }
  361. }
  362. });