|
| 1 | +var app = function(){ |
| 2 | + var base_dir = (location.pathname.replace('/index.html', '/') + |
| 3 | + "/files/").replace(/\/\//g, '/'); |
| 4 | + var current_dir = (base_dir + location.hash.substring(1) + |
| 5 | + '/').replace(/\/\//g, '/'); |
| 6 | + var IMG_EXTENSIONS = ['bmp', 'gif', 'jpg', 'jpeg', 'jpe', 'png']; |
| 7 | + var IGNORED_ELEMENTS = ['../', 'Name', 'Last modified', 'Size', 'Description', |
| 8 | + 'Parent Directory']; |
| 9 | + var imgCache = []; |
| 10 | + var prev_img = ""; |
| 11 | + var next_img = ""; |
| 12 | + |
| 13 | + |
| 14 | + // create a tile |
| 15 | + function createTile(href, name) { |
| 16 | + return '<a href="'+href+name+'"><span class="glyphicon glyphicon-file" aria-hidden="true"></span>'+name+'</a>'; |
| 17 | + } |
| 18 | + |
| 19 | + // cache an image for future usage |
| 20 | + function cacheImage(file) { |
| 21 | + for (var i=0; i<imgCache.length; i++) { |
| 22 | + if (imgCache[i].src == file) return; |
| 23 | + } |
| 24 | + imgCache.push(file); |
| 25 | + } |
| 26 | + |
| 27 | + // check if the given path points to an image |
| 28 | + function isImage(path) { |
| 29 | + return $.inArray(path.split('.').pop().toLowerCase(), IMG_EXTENSIONS) != -1; |
| 30 | + } |
| 31 | + |
| 32 | + function isValidTile(name) { |
| 33 | + return $.inArray(name, IGNORED_ELEMENTS) == -1; |
| 34 | + } |
| 35 | + |
| 36 | + // load the contents of the given directory |
| 37 | + function cd(dir) { |
| 38 | + current_dir = dir; |
| 39 | + |
| 40 | + console.log(current_dir, base_dir); |
| 41 | + location.hash = current_dir.replace(base_dir, ''); |
| 42 | + |
| 43 | + // show the location bar |
| 44 | + $(".current-dir").text(''); |
| 45 | + var path = current_dir.replace(base_dir, '/').split('/'); |
| 46 | + |
| 47 | + var temp_path = ""; |
| 48 | + for (var i=0; i<path.length-1; i++) { |
| 49 | + var a = document.createElement('a'); |
| 50 | + temp_path += path[i] + '/'; |
| 51 | + $(a).text(path[i] + '/'); |
| 52 | + a.title = base_dir + temp_path.substring(1); |
| 53 | + $(a).click(function(){ |
| 54 | + cd(this.title); |
| 55 | + }); |
| 56 | + $(".current-dir").append(a); |
| 57 | + } |
| 58 | + |
| 59 | + // retrieve the contents of the directory |
| 60 | + $.get(current_dir, function(data) { |
| 61 | + html = $.parseHTML(data); |
| 62 | + $(".browser-view").html(""); |
| 63 | + |
| 64 | + // create tiles |
| 65 | + $(html).find("a").each(function(i, element){ |
| 66 | + if (isValidTile(element.text)) { |
| 67 | + $(".browser-view").append( |
| 68 | + createTile(current_dir, element.text)); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + // add events to tiles |
| 73 | + $(".browser-view a").each(function(i, element){ |
| 74 | + if (element.pathname.slice(-1) == "/" ) { |
| 75 | + // open directories |
| 76 | + $(element).click(function(e) { |
| 77 | + e.preventDefault(); |
| 78 | + cd(element.pathname); |
| 79 | + }); |
| 80 | + } else if (isImage(element.pathname)) { |
| 81 | + // show image previews |
| 82 | + $(element).click(function(e) { |
| 83 | + e.preventDefault(); |
| 84 | + showPreview(element.pathname); |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // show an image preview of the given file |
| 92 | + function showPreview(filepath){ |
| 93 | + $(".bg-translucent").css('display', 'block'); |
| 94 | + $(".file-view-img").attr('src', 'loader.gif'); |
| 95 | + $(".file-view-wrapper").css('display', 'block'); |
| 96 | + var img = new Image(); |
| 97 | + img.src = filepath; |
| 98 | + img.onload = function() { |
| 99 | + $(".file-view-img").attr('src', filepath); |
| 100 | + var imgWidth = $('.file-view-img').width(); |
| 101 | + $(".file-view-wrapper").css('left', ($(document).width()-imgWidth)/2); |
| 102 | + $(".file-view-wrapper").css('width', imgWidth); |
| 103 | + $(".file-view-prev").css('display', 'block'); |
| 104 | + $(".file-view-next").css('display', 'block'); |
| 105 | + }; |
| 106 | + cacheImage(filepath); |
| 107 | + |
| 108 | + // search for the previous and next image to be displayed |
| 109 | + var first_img = ""; |
| 110 | + var last_img = ""; |
| 111 | + prev_img = ""; |
| 112 | + next_img = ""; |
| 113 | + var img_found = false; |
| 114 | + $(".browser-view a").each(function(i, element){ |
| 115 | + if (isImage(element.pathname)) { |
| 116 | + if (first_img === "") first_img = element.pathname; |
| 117 | + if (img_found && next_img === "") { next_img = element.pathname; } |
| 118 | + if (element.pathname == filepath) img_found = true; |
| 119 | + if (!img_found) prev_img = element.pathname; |
| 120 | + last_img = element.pathname; |
| 121 | + } |
| 122 | + }); |
| 123 | + if (next_img === "") next_img = first_img; |
| 124 | + if (prev_img === "") prev_img = last_img; |
| 125 | + } |
| 126 | + |
| 127 | + // close the image preview |
| 128 | + function closePreview() { |
| 129 | + $(".bg-translucent").css('display', 'none'); |
| 130 | + $(".file-view-wrapper").css('display', 'none'); |
| 131 | + } |
| 132 | + |
| 133 | + // add various event handlers |
| 134 | + $('.file-view-prev').click(function(){ |
| 135 | + showPreview(prev_img); |
| 136 | + }); |
| 137 | + $('.file-view-next').click(function(){ |
| 138 | + showPreview(next_img); |
| 139 | + }); |
| 140 | + $("body").keydown(function(event) { |
| 141 | + switch (event.which) { |
| 142 | + case 27: // ESC |
| 143 | + closePreview(); |
| 144 | + break; |
| 145 | + case 37: // left arrow key |
| 146 | + showPreview(prev_img); |
| 147 | + break; |
| 148 | + case 39: // right arrow key |
| 149 | + showPreview(next_img); |
| 150 | + break; |
| 151 | + } |
| 152 | + }); |
| 153 | + $(".bg-translucent").click(closePreview); |
| 154 | + $('.base-dir-icon').click(function(){ |
| 155 | + cd(base_dir); |
| 156 | + }); |
| 157 | + |
| 158 | + cd(current_dir); |
| 159 | +}(); |
0 commit comments