Select.hx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package hide.comp;
  2. using Lambda;
  3. class Select extends Component {
  4. // The id in the choices list that correspond to the current value
  5. public var value(default, set) : String;
  6. var choices : Array<hide.comp.Dropdown.Choice> = null;
  7. var isClearable : Bool = false;
  8. function set_value(v:String) {
  9. value = v;
  10. if (value == null || value == "") {
  11. element.val("--- Choose ---");
  12. } else {
  13. element.val(choices.find((e) -> e.id == value).text);
  14. }
  15. return value;
  16. }
  17. public function new(?parent,?root, choices: Array<hide.comp.Dropdown.Choice>, isClearable : Bool = true) {
  18. if (root == null)
  19. root = new Element('<input value="--- Choose ---">');
  20. super(parent, root);
  21. this.choices = choices;
  22. value = null;
  23. this.isClearable = isClearable;
  24. if (isClearable) {
  25. this.choices.unshift({id : "", text : "--- Clear ---", classes : ["compact"]});
  26. }
  27. element.toggleClass("file", true);
  28. element.contextmenu(function(e) {
  29. e.preventDefault();
  30. onContextmenu();
  31. return false;
  32. });
  33. element.mousedown(function(e) {
  34. e.preventDefault();
  35. if (e.button == 0) {
  36. var d = new hide.comp.Dropdown(new Element(element), choices, value, (_) -> null, true);
  37. d.ignoreIdInSearch = true;
  38. d.onSelect = function(v) {
  39. change(v);
  40. }
  41. }
  42. });
  43. }
  44. public dynamic function onContextmenu() {
  45. var options = [
  46. { label : "Copy", click : () -> ide.setClipboard(value)},
  47. { label : "Paste", click : () -> change(ide.getClipboard())},
  48. ];
  49. if (isClearable) {
  50. options.unshift({ label : "Clear", click : () -> change("")});
  51. }
  52. new hide.comp.ContextMenu(options);
  53. }
  54. function change(newId : String) {
  55. value = newId;
  56. onChange(newId);
  57. }
  58. public dynamic function onChange(newId : String) {
  59. }
  60. }