Radio.js 13 KB

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