ui_selector.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. function onUiSelectorChange()
  2. {
  3. }
  4. class UISelector extends UIContainer {
  5. init()
  6. {
  7. this.root.empty();
  8. this.root.addClass("noMarginBottom")
  9. this.init_select();
  10. this.update();
  11. this.compteur=0;
  12. }
  13. init_select()
  14. {
  15. var t = this;
  16. var size=(this.size)?this.size:12;
  17. var r = HTMLBuilder.rootElemDiv(size);
  18. var nsize=12;
  19. if(this.facultative) nsize-=4;
  20. var data = {
  21. type: this.type,
  22. id: this.rootId+'_cb',
  23. size: nsize,
  24. label: this.placeholder
  25. };
  26. var prop ={ multiple: this.multiple?"":undefined };
  27. var options = [];
  28. for(var i=0; i<this.choices.length; i++)
  29. {
  30. var opt= { text:this.choices[i].title, prop: {}};
  31. if(i==0) opt.prop.selected="";
  32. opt.prop.value=this.choices[i].name;
  33. options.push(opt);
  34. }
  35. var root = HTMLBuilder.div();
  36. var r = HTMLBuilder.rootElemDiv(size);
  37. var sel = HTMLBuilder.select(data, prop, options);
  38. sel.on('change', function(){t.update();} )
  39. r.append(sel);
  40. root.append(r);
  41. this.root.append(root);
  42. for(var i=0; i<this.choices.length; i++)
  43. {
  44. this.create_ui(root, this.choices[i]);
  45. }
  46. doFac(root,this);
  47. return root;
  48. }
  49. create_ui(root, f)
  50. {
  51. var id = this.rootId+"_"+f.name;
  52. var r = HTMLBuilder.subUiDiv({class: _row_class+_col_class(12), id:id});
  53. root.append(r)
  54. var ui = new UIContainer(this.rootData, f, id, this);
  55. this.index[f.name]=ui;
  56. return ui;
  57. }
  58. getJson()
  59. {
  60. var res=super.getJson();
  61. if(this.multiple) return res;
  62. var out={};
  63. var sel = this.getSelection();
  64. for (var key in res) {
  65. if (res.hasOwnProperty(key) ) {
  66. if( (Array.isArray(sel) && key in sel) || key==sel)
  67. Object.assign(out, res[key])
  68. }
  69. }
  70. out[this.selectionAs]=sel;
  71. return out;
  72. }
  73. setJson(e)
  74. {
  75. if(this.selectionAs) //selection simple
  76. {
  77. var type = e[this.selectionAs];
  78. $("#"+this.rootId+"_cb").val(type).formSelect();
  79. $("#"+this.rootId+"_cb").change();
  80. this.index[type].val(e);
  81. this.update();
  82. }
  83. }
  84. getSelection()
  85. {
  86. var v = $("#"+this.rootId+"_cb").val().split(",");
  87. if(v.length==1) return v[0];
  88. return v;
  89. }
  90. update()
  91. {
  92. var sel = this.getSelection();
  93. for (var key in this.index) {
  94. if (this.index.hasOwnProperty(key)) {
  95. var id = this.rootId+"_"+key;
  96. console.log("ID="+id)
  97. if( (Array.isArray(sel) && key in sel) || key==sel)
  98. {
  99. $('#'+id).show();
  100. }else{
  101. $('#'+id).hide();
  102. }
  103. }
  104. }
  105. }
  106. }
  107. registerUiClass("uiselector", UISelector);