Select.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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){
  15. this._loadNodeRead();
  16. }else{
  17. this._loadNodeEdit();
  18. }
  19. },
  20. _loadNodeRead: function(){
  21. this.node.empty();
  22. var optionItems = this.getOptions();
  23. var value = this.getValue();
  24. if (value){
  25. var texts = [];
  26. optionItems.each(function(item){
  27. var tmps = item.split("|");
  28. var t = tmps[0];
  29. var v = tmps[1] || t;
  30. if (v){
  31. if (value.indexOf(v)!=-1){
  32. texts.push(t);
  33. }
  34. }
  35. });
  36. this.node.set("text", texts.join(", "));
  37. }
  38. },
  39. _loadNodeEdit: function(){
  40. var select = new Element("select");
  41. select.set(this.json.properties);
  42. select.inject(this.node, "after");
  43. this.node.destroy();
  44. this.node = select;
  45. this.node.set({
  46. "id": this.json.id,
  47. "MWFType": this.json.type,
  48. "styles": {
  49. "margin-right": "12px"
  50. }
  51. });
  52. this.setOptions();
  53. this.node.addEvent("change", function(){
  54. this.validationMode();
  55. if (this.validation()) this._setBusinessData(this.getInputData("change"));
  56. }.bind(this));
  57. },
  58. getOptions: function(){
  59. if (this.json.itemType == "values"){
  60. return this.json.itemValues;
  61. }else{
  62. return this.form.Macro.exec(this.json.itemScript.code, this);
  63. }
  64. return [];
  65. },
  66. setOptions: function(){
  67. var optionItems = this.getOptions();
  68. if (!optionItems) optionItems = [];
  69. optionItems.each(function(item){
  70. var tmps = item.split("|");
  71. var text = tmps[0];
  72. var value = tmps[1] || text;
  73. var option = new Element("option", {
  74. "value": value,
  75. "text": text
  76. }).inject(this.node);
  77. }.bind(this));
  78. },
  79. _setValue: function(value){
  80. if (!this.readonly) {
  81. this._setBusinessData(value);
  82. for (var i=0; i<this.node.options.length; i++){
  83. var option = this.node.options[i];
  84. if (option.value==value){
  85. option.selected = true;
  86. // break;
  87. }else{
  88. option.selected = false;
  89. }
  90. }
  91. }
  92. //this.node.set("value", value);
  93. },
  94. getTextData: function(){
  95. var ops = this.node.getElements("option");
  96. var value = [];
  97. var text = [];
  98. ops.each(function(op){
  99. if (op.selected){
  100. var v = op.get("value");
  101. var t = op.get("text");
  102. value.push(v || "");
  103. text.push(t || v || "");
  104. }
  105. });
  106. if (!value.length) value = [""];
  107. if (!text.length) text = [""];
  108. return {"value": value, "text": text};
  109. },
  110. getInputData: function(){
  111. var ops = this.node.getElements("option");
  112. var value = [];
  113. ops.each(function(op){
  114. if (op.selected){
  115. var v = op.get("value");
  116. if (v) value.push(v);
  117. }
  118. });
  119. if (!value.length) return null;
  120. return (value.length==1) ? value[0] : value;
  121. },
  122. resetData: function(){
  123. this.setData(this.getValue());
  124. },
  125. setData: function(data){
  126. this._setBusinessData(data);
  127. var ops = this.node.getElements("option");
  128. ops.each(function(op){
  129. if (typeOf(data)=="array"){
  130. if (data.indexOf(op.get("value"))!=-1){
  131. op.set("selected", true);
  132. }else{
  133. op.set("selected", false);
  134. }
  135. }else{
  136. if (data == op.get("value")){
  137. op.set("selected", true);
  138. }else{
  139. op.set("selected", false);
  140. }
  141. }
  142. });
  143. }
  144. });