FileSelect.hx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. root.contextmenu(function(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. // allow drag files
  37. root.on("dragover", function(e) {
  38. root.addClass("dragover");
  39. });
  40. root.on("dragleave", function(e) {
  41. root.removeClass("dragover");
  42. });
  43. root.on("drop", function(e) {
  44. root.removeClass("dragover");
  45. });
  46. }
  47. public function onDragDrop( items : Array<String>, isDrop : Bool ) : Bool {
  48. if( items.length == 0 )
  49. return false;
  50. var newPath = ide.makeRelative(items[0]);
  51. if( pathIsValid(newPath) ) {
  52. if( isDrop ) {
  53. path = newPath;
  54. onChange();
  55. }
  56. return true;
  57. }
  58. return false;
  59. }
  60. function pathIsValid( path : String ) : Bool {
  61. return (
  62. path != null
  63. && sys.FileSystem.exists(ide.getPath(path))
  64. && extensions.indexOf(path.split(".").pop().toLowerCase()) >= 0
  65. );
  66. }
  67. public function getFullPath() {
  68. if( path == null )
  69. return null;
  70. var fpath = ide.getPath(path);
  71. if( sys.FileSystem.exists(fpath) )
  72. return fpath;
  73. return null;
  74. }
  75. function set_path(p:String) {
  76. var text = p == null ? "-- select --" : (sys.FileSystem.exists(ide.getPath(p)) ? "" : "[NOT FOUND] ") + p;
  77. element.val(text);
  78. element.attr("title", p == null ? "" : p);
  79. return this.path = p;
  80. }
  81. public dynamic function onChange() {
  82. }
  83. }