Radio.js 8.0 KB

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