FileSelect.hx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package hide.comp;
  2. class FileSelect extends Component {
  3. var extensions : Array<String>;
  4. public var path(default, set) : String;
  5. public function new(extensions,?parent,?root) {
  6. if( root == null )
  7. root = new Element("<input>");
  8. super(parent,root);
  9. root.addClass("file");
  10. this.extensions = extensions;
  11. path = null;
  12. root.mousedown(function(e) {
  13. e.preventDefault();
  14. if( e.button == 0 ) {
  15. ide.chooseFile(extensions, function(path) {
  16. this.path = path;
  17. onChange();
  18. }, false, path);
  19. }
  20. });
  21. function contextMenu(e) {
  22. e.preventDefault();
  23. var fpath = getFullPath();
  24. new ContextMenu([
  25. { label : "View", enabled : fpath != null, click : function() ide.openFile(fpath) },
  26. { label : "Clear", enabled : path != null, click : function() { path = null; onChange(); } },
  27. { label : "Copy Path", enabled : path != null, click : function() ide.setClipboard(path) },
  28. { label : "Paste Path", click : function() {
  29. path = ide.getClipboard();
  30. onChange();
  31. }},
  32. { label : "Open in explorer", enabled : fpath != null, click : function() Sys.command("explorer.exe",["/select,"+fpath.split("/").join("\\")]) },
  33. ]);
  34. return false;
  35. }
  36. root.parent().prev("dt").contextmenu(contextMenu);
  37. root.contextmenu(contextMenu);
  38. // allow drag files
  39. root.on("dragover", function(e) {
  40. root.addClass("dragover");
  41. });
  42. root.on("dragleave", function(e) {
  43. root.removeClass("dragover");
  44. });
  45. root.on("drop", function(e) {
  46. root.removeClass("dragover");
  47. });
  48. }
  49. public function onDragDrop( items : Array<String>, isDrop : Bool ) : Bool {
  50. if( items.length == 0 )
  51. return false;
  52. var newPath = ide.makeRelative(items[0]);
  53. if( pathIsValid(newPath) ) {
  54. if( isDrop ) {
  55. path = newPath;
  56. onChange();
  57. }
  58. return true;
  59. }
  60. return false;
  61. }
  62. function pathIsValid( path : String ) : Bool {
  63. return (
  64. path != null
  65. && sys.FileSystem.exists(ide.getPath(path))
  66. && extensions.indexOf(path.split(".").pop().toLowerCase()) >= 0
  67. );
  68. }
  69. public function getFullPath() {
  70. if( path == null )
  71. return null;
  72. var fpath = ide.getPath(path);
  73. if( sys.FileSystem.exists(fpath) )
  74. return fpath;
  75. return null;
  76. }
  77. function set_path(p:String) {
  78. var text = p == null ? "-- select --" : (sys.FileSystem.exists(ide.getPath(p)) ? "" : "[NOT FOUND] ") + p;
  79. element.val(text);
  80. element.attr("title", p == null ? "" : p);
  81. return this.path = p;
  82. }
  83. public dynamic function onChange() {
  84. }
  85. }