Radio.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. _loadNodeEdit: function(){
  38. //this.container = new Element("select");
  39. var div = new Element("div");
  40. div.set(this.json.properties);
  41. div.inject(this.node, "after");
  42. this.node.destroy();
  43. this.node = div;
  44. this.node.set({
  45. "id": this.json.id,
  46. "MWFType": this.json.type,
  47. "styles": {
  48. "display": "inline"
  49. }
  50. });
  51. this.setOptions();
  52. },
  53. _loadDomEvents: function(){
  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. var inputs = this.node.getElements("input");
  77. inputs.each(function(input){
  78. input.addEvent(key, function(event){
  79. return (fun) ? fun(this, event) : null;
  80. }.bind(this));
  81. }.bind(this));
  82. }
  83. },
  84. resetOption: function(){
  85. this.node.empty();
  86. this.setOptions();
  87. },
  88. getOptions: function(){
  89. if (this.json.itemType == "values"){
  90. return this.json.itemValues;
  91. }else{
  92. return this.form.Macro.exec(this.json.itemScript.code, this);
  93. }
  94. return [];
  95. },
  96. setOptions: function(){
  97. var radioValues = this.getOptions();
  98. if (!radioValues) radioValues = [];
  99. if (o2.typeOf(radioValues)==="array"){
  100. var flag = (new MWF.widget.UUID).toString();
  101. radioValues.each(function(item){
  102. var tmps = item.split("|");
  103. var text = tmps[0];
  104. var value = tmps[1] || text;
  105. var radio = new Element("input", {
  106. "type": "radio",
  107. "name": this.json.properties.name || flag+this.json.id,
  108. "value": value,
  109. "showText": text,
  110. "styles": this.json.buttonStyles
  111. }).inject(this.node);
  112. //radio.appendText(text, "after");
  113. var textNode = new Element( "span", {
  114. "text" : text,
  115. "styles" : { "cursor" : "default" }
  116. }).inject(this.node);
  117. textNode.addEvent("click", function( ev ){
  118. if( this.radio.get("disabled") === true || this.radio.get("disabled") === "true" )return;
  119. this.radio.checked = true;
  120. this.radio.fireEvent("change");
  121. this.radio.fireEvent("click");
  122. }.bind( {radio : radio} ) );
  123. radio.addEvent("click", function(){
  124. this.validationMode();
  125. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  126. }.bind(this));
  127. Object.each(this.json.events, function(e, key){
  128. if (e.code){
  129. if (this.options.moduleEvents.indexOf(key)!=-1){
  130. }else{
  131. radio.addEvent(key, function(event){
  132. return this.form.Macro.fire(e.code, this, event);
  133. }.bind(this));
  134. }
  135. }
  136. }.bind(this));
  137. }.bind(this));
  138. }
  139. },
  140. _setValue: function(value){
  141. this._setBusinessData(value);
  142. var radios = this.node.getElements("input");
  143. for (var i=0; i<radios.length; i++){
  144. var radio = radios[i];
  145. if (radio.value==value){
  146. radio.checked = true;
  147. break;
  148. }
  149. }
  150. },
  151. getTextData: function(){
  152. var inputs = this.node.getElements("input");
  153. var value = "";
  154. var text = "";
  155. if (inputs.length){
  156. for (var i=0; i<inputs.length; i++){
  157. var input = inputs[i];
  158. if (input.checked){
  159. value = input.get("value");
  160. text = input.get("showText");
  161. break;
  162. }
  163. }
  164. }
  165. return {"value": [value] || "", "text": [text || value || ""]};
  166. },
  167. getInputData: function(){
  168. if (this.readonly || this.json.isReadonly ){
  169. return this._getBusinessData();
  170. }else{
  171. var inputs = this.node.getElements("input");
  172. var value = "";
  173. if (inputs.length){
  174. for (var i=0; i<inputs.length; i++){
  175. var input = inputs[i];
  176. if (input.checked){
  177. value = input.get("value");
  178. break;
  179. }
  180. }
  181. }
  182. return value;
  183. }
  184. },
  185. resetData: function(){
  186. this.setData(this.getValue());
  187. },
  188. getSelectedInput: function(){
  189. var inputs = this.node.getElements("input");
  190. if (inputs.length){
  191. for (var i=0; i<inputs.length; i++){
  192. if (inputs[i].checked) return inputs[i];
  193. }
  194. }
  195. return null;
  196. },
  197. setData: function(data){
  198. this._setBusinessData(data);
  199. var inputs = this.node.getElements("input");
  200. if (inputs.length){
  201. for (var i=0; i<inputs.length; i++){
  202. if (data==inputs[i].get("value")){
  203. inputs[i].set("checked", true);
  204. }else{
  205. inputs[i].set("checked", false);
  206. }
  207. }
  208. }
  209. this.fireEvent("setData");
  210. },
  211. notValidationMode: function(text){
  212. if (!this.isNotValidationMode){
  213. this.isNotValidationMode = true;
  214. this.node.store("background", this.node.getStyles("background"));
  215. this.node.setStyle("background", "#ffdcdc");
  216. this.errNode = this.createErrorNode(text);
  217. if (this.iconNode){
  218. this.errNode.inject(this.iconNode, "after");
  219. }else{
  220. this.errNode.inject(this.node, "after");
  221. }
  222. this.showNotValidationMode(this.node);
  223. if (!this.node.isIntoView()) this.node.scrollIntoView();
  224. }
  225. },
  226. validationMode: function(routeName, opinion){
  227. if (!this.validationConfig(routeName, opinion)) return false;
  228. if (this.isNotValidationMode){
  229. this.isNotValidationMode = false;
  230. this.node.setStyles(this.node.retrieve("background"));
  231. if (this.errNode){
  232. this.errNode.destroy();
  233. this.errNode = null;
  234. }
  235. }
  236. }
  237. });