-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.filebrowser-0.8.0.min.js
10 lines (10 loc) · 13.7 KB
/
jquery.filebrowser-0.8.0.min.js
1
2
3
4
5
6
7
8
9
10
/**@license
*
* jQuery File Browser - directory browser jQuery plugin version 0.8.0
*
* Copyright (c) 2016-2017 Jakub Jankiewicz <http://jcubic.pl/me>
* Released under the MIT license
*
* Date: Sun, 10 Sep 2017 08:12:03 +0000
*/
(function(e,t){"use strict";function n(e,t,n){this.browser=e;this.upload=t;this.error=n}n.prototype.process=function t(n,r){var a=e.Deferred();var i=this;if(n.originalEvent){n=n.originalEvent}var s;if(n.dataTransfer.items){s=[].slice.call(n.dataTransfer.items)}var o=n.dataTransfer.files||n.target.files;if(o){o=[].slice.call(o)}if(s&&s.length){if(s[0].webkitGetAsEntry){var l=[];s.forEach(function(e){var t=e.webkitGetAsEntry();if(t){l.push(t)}});(function e(){var t=l.shift();if(t){i.upload_tree(t,r).then(e)}else{a.resolve()}})()}}else if(o&&o.length){(function e(){var t=o.shift();if(t){i.upload(t,r).then(e)}else{a.resolve()}})()}else if(n.dataTransfer.getFilesAndDirectories){n.dataTransfer.getFilesAndDirectories().then(function(e){(function t(){var n=e.shift();if(n){i.upload_tree(n,r).then(t)}else{a.resolve()}})()})}return a.promise()};n.prototype.upload_tree=function t(n,r){var a=e.Deferred();var i=this;function s(e,t){e=e.slice();(function n(){var r=e.shift();if(r){t(r).then(n).fail(function(){a.reject()})}else{a.resolve()}})()}function o(e){s(e,function(e){return i.upload_tree(e,i.browser.join(r,n.name))})}function l(e){i.upload(e,r).then(function(){a.resolve()}).fail(function(){a.reject()})}if(typeof Directory!="undefined"&&n instanceof Directory){n.getFilesAndDirectories().then(function(e){o(e)})}else if(typeof File!="undefined"&&n instanceof File){l(n)}else if(n.isFile){n.file(l)}else if(n.isDirectory){var f=n.createReader();f.readEntries(function(e){o(e)})}return a.promise()};e.browse={defaults:{dir:function(){return e.when({files:[],dirs:[]})},root:"/",separator:"/",labels:true,change:e.noop,init:e.noop,item_class:e.noop,rename_delay:300,dbclick_delay:2e3,open:e.noop,rename:e.noop,create:e.noop,remove:e.noop,copy:e.noop,exists:e.noop,upload:e.noop,name:"default",error:e.noop,menu:function(e){return{}},refresh_timer:100},strings:{toolbar:{back:"back",up:"up",refresh:"refresh"}},escape_regex:function(e){if(typeof e=="string"){var t=/([-\\\^$\[\]()+{}?*.|])/g;return e.replace(t,"\\$1")}}};var r;var a;var i={};var s;function o(e){return function(t){return e==t}}function l(t,n){var r=e(n);return r.parents().add("html,body").map(function(){return e(this)[t]()}).get().reduce(function(e,t){return e+t})}function f(t,n){return t===n||n.match(new RegExp("^"+e.browse.escape_regex(t)))}e.fn.browse=function(c){var u=e.extend({},e.browse.defaults,c);function d(t){if(L){var n=H.offset();G=t.clientX-n.left;Q=t.clientY-n.top;I.show();v();N=true;var r=q.find("li");if(!t.ctrlKey){r.removeClass("selected");i[u.name]=[]}var a=I[0].getBoundingClientRect();var s=r.filter(function(){var e=this.getBoundingClientRect();return e.top+e.height>a.top&&e.left+e.width>a.left&&e.bottom-e.height<a.bottom&&e.right-e.width<a.right});s.addClass("selected").each(function(){i[u.name].push(k.join(E,e(this).text()))})}}function h(t){if(!e(t.target).closest(".browser-menu").length){y()}}function p(e){L=false;I.hide();k.removeClass("no-select")}function v(e){var t=l("scrollTop",q);var n=Math.max(Math.min(W,G),0);var r=Math.max(Math.min(X,Q),-t);var a=Math.max(W,G);var i=Math.max(X,Q);var s=q.prop("clientWidth");var o=q.height()+q.scrollTop()-2;if(a>s){a=s}if(i>o){i=o}I.css({left:n,top:r+t,width:a-n,height:i-r})}function m(t){if(k.hasClass("selected")&&!e(t.target).is("textarea")){var n;var r=q.find(".active");if(t.ctrlKey){if(t.which==67){k.copy()}else if(t.which==88){k.cut()}else if(t.which==86){k.paste(a)}}if(t.which==32){var t=jQuery.Event("click");t.ctrlKey=true;t.target=r[0];r.trigger(t);return false}else if(t.which==8){k.back()}else{if(t.which==13&&r.length){R=(new Date).getTime();r.dblclick()}else{if(t.which>=37&&t.which<=40){if(!t.ctrlKey){q.find("li").removeClass("selected")}if(!r.length){r=q.find("li:eq(0)").addClass("active")}else{var i=q.find("li");var s=q.prop("clientWidth");var o=i.length;var l=q.find("li:eq(0)").outerWidth(true);var f=Math.floor(s/l);n=r.index();if(t.which==37){n--}else if(t.which==38){n=n-f}else if(t.which==39){n++}else if(t.which==40){n=n+f}if(n<0){n=0}else if(n>o-1){n=o-1}i.eq(n).addClass("active").siblings().removeClass("active")}}}}}}function g(t){if(!e(t.target).closest("."+_).length){e(".browser-widget").removeClass("selected")}var n=e(t.target);var r=n.closest(".browser-menu li");if(r.length){if(P){var a=P.target.closest("ul:not(.menu) li");var i=P.menu;r.parents(".ui-menu-item").addBack().each(function(){i=i[e(this).find("> div").text()]});if(!r.find("> ul").length){y()}if(e.isFunction(i)){setTimeout(function(){i.call(k,a)},0)}return false}}else if(!n.closest(".browser-menu").length||n.closest(".browser-menu li").length){y()}}function w(){e("."+_).each(function(){var t=e(this).browse();if(t.path()==E&&t.name()==u.name){t.refresh()}})}function b(t){var n=e(this);var r=n.parent();var a=n.val();if(r.hasClass("rename")){n.remove();r.removeClass("rename");var i=r.find("span").text();if(a!=i){k._rename(k.join(E,i),k.join(E,a)).then(w)}}else if(r.hasClass("new")){if(t){r.remove()}else{var s;if(r.hasClass("directory")){s="directory"}else{s="file"}k.create(s,k.join(E,a))}}}function y(){e("body > .browser-menu").menu("destroy").remove();P=null}function x(){var e;if(q.prop){e=q.prop("scrollHeight")}else{e=q.attr("scrollHeight")}q.scrollTop(e)}function C(t,n){var r=e("<ul/>");if(!n){r.addClass("browser-menu")}Object.keys(t).forEach(function(n){var a=e('<li class="'+T(n)+'"><div>'+n+"</div></li>").appendTo(r);if(e.isPlainObject(t[n])){a.append(C(t[n],true))}});return r}function T(e){return e.toLowerCase().replace(/\s+([^\s])/g,function(e,t){return"-"+t})}if(this.data("browse")){return this.data("browse")}else if(this.length>1){return this.each(function(){e.fn.browse.call(e(this),u)})}else{var _="browser-widget";i[u.name]=i[u.name]||[];var k=this;k.addClass(_+" hidden");var j=new n(k,u.upload,u.error);var E;var D=[];var K;var R;var F=false;var M=0;var A=e('<div class="toolbar"/>').appendTo(k);var B=e('<div class="adress-bar"></div>').appendTo(A);e("<button>Home</button>").addClass("home").appendTo(B);var Y=e("<ul></ul>").appendTo(A);if(u.labels){Y.addClass("labels")}var $=e("<input />").appendTo(B);var O=e.browse.strings.toolbar;Object.keys(O).forEach(function(t){e("<li/>").text(O[t]).addClass(t).appendTo(Y)});var q=e("<ul/>").wrap("<div/>").parent().addClass("content").appendTo(k);var H=q.find("ul");var W=0,X=0,G=0,Q=0;var I=e("<div/>").addClass("selection").hide().appendTo(q);var L=false;var N=false;var P;var z={li:{rename:J,delete:function(t){e.when.apply(e,q.find("li.selected").map(function(){var t=e(this).find("span").text();return u.remove(k.join(E,t))})).then(w)}},content:{new:{directory:function(e){k.create("Directory")},file:function(e){k.create("File")}}}};A.on("click.browse","li",function(){var t=e(this);if(!t.hasClass("disabled")){var n=t.text();k[n]()}}).on("click",".home",function(){if(E!=u.root){k.show(u.root)}}).on("keydown.browse","input",function(t){if(t.which==13){var n=e(this);var r=n.val();k.show(r);return false}});function J(t){if(!t.is(".new, .rename")){var n=t.find("span").text();e("<textarea>"+n+"</textarea>").appendTo(t).focus().select();t.addClass("rename")}else{t.find("textarea").focus().select()}}q.on("dblclick.browse","li",function(t){var n=e(this);var r=(new Date).getTime()-R;if(r<u.rename_delay&&r<u.dbclick_delay){var a=n.find("span").text();var i=k.join(E,a);if(n.hasClass("directory")){n.removeClass("selected");k.show(i)}else if(n.hasClass("file")){u.open(i)}}}).on("click.browse","ul:not(.menu) > li",function(t){if(!L){var n=e(t.target);var r=e(this);var a=r.find("span").text();var s=k.join(E,a);if(n.is("span")){if(M++%2===0){R=(new Date).getTime()}else{var l=(new Date).getTime()-R;if(l>u.rename_delay&&l<u.dbclick_delay){J(r);return false}}}else{R=(new Date).getTime()}if(!t.ctrlKey){r.siblings().removeClass("selected")}if(!n.is("textarea")){q.find(".active").removeClass("active");r.toggleClass("selected").addClass("active");if(r.hasClass("selected")){if(!t.ctrlKey){i[u.name]=[]}i[u.name].push(s)}else if(t.ctrlKey){i[u.name]=i[u.name].filter(o(s))}else{i[u.name]=[]}}}}).on("keydown","textarea",function(e){if(e.which==13||e.which==27){b.call(this,e.which==27)}if([13,27].indexOf(e.which)!=-1){return false}}).on("contextmenu",function(t){if(u.contextmenu&&!t.ctrlKey){y();P={target:e(t.target)};if(!P.target.is("textarea")){var n=P.target.closest("li");P.type=n.length?"li":"content";P.menu=e.extend(z[P.type],u.menu(P.type)||{});var r=C(P.menu).appendTo("body");r.menu();var a=q.offset();r.css({left:t.pageX,top:t.pageY});return false}}});k.on("click.browse",function(t){e("."+_).removeClass("selected");k.addClass("selected");var n=e(t.target);if(!N){if(!t.ctrlKey&&!n.is(".content li")&&!n.closest(".toolbar").length){q.find("li").removeClass("selected");i[u.name]=[]}}if(!n.is("textarea")){q.find("li.rename,li.new").find("textarea").each(b)}});k.on("dragover.browse",".content",function(e){if(e.originalEvent){e=e.originalEvent}e.dataTransfer.dropEffect="move";return false}).on("dragstart",".content li",function(t){t.originalEvent.dataTransfer.setData("text","anything");var n=e(this);var r=n.text();s={name:r,node:n,path:E,context:k};s.selection=n.hasClass("selected")});function S(e){if(e.originalEvent){e=e.originalEvent}if(e.dataTransfer.items&&e.dataTransfer.items.length){return!![].filter.call(e.dataTransfer.items,function(e){return e.kind=="file"}).length}else{return e.dataTransfer.files&&e.dataTransfer.files.length}}q.on("drop.browse",function(t){var n=e(t.target);var r;if(n.is(".directory")){r=k.join(E,n.text())}else{r=E}if(S(t)){j.process(t,r).then(function(){if(!n.is(".directory")){w()}})}else{if(k.name()!==s.context.name()){var a="You can't drag across different filesystems";u.error(a)}var o;if(s.selection){o=e.when.appy(e,i[u.name].map(function(e){var t=k.join(r,k.split(e).pop());if(!f(e,t)){return k._rename(e,t)}}))}else{var l=k.join(s.path,s.name);var c=k.join(r,s.name);o=k._rename(l,c)}o.then(function(){s.context.refresh();w()})}return false}).on("mousedown.browse",function(t){var n=e(t.target);if(!n.is("li")&&!n.is("span")&&!n.is("textarea")){L=true;N=false;k.addClass("no-select");var r=H.offset();W=t.clientX-r.left;X=t.clientY-r.top}});e(document).on("click",g).on("keydown",m).on("mousedown",h).on("mousemove",d).on("mouseup",p);e.extend(k,{path:function(){return E},name:function(){return u.name},current:function(){return current},back:function(){if(D.length>1){D.pop();k.show(D[D.length-1],{push:false})}return k},destroy:function(){k.off(".browse");e(document).off("click",g).off("keydown",m).off("mousedown",h).off("mousemove",d).off("mouseup",p);B.remove();q.remove()},_rename:function(t,n){if(!f(t,n)){return e.when(u.rename(t,n))}else{return e.when()}},_create:function(t,n){return e.when(u.create(t,n))},_exists:function(t){return e.when(u.exists(t))},create:function(n,r){var a=T(n);if(r==t){var i=e(['<li class="new '+a+'" draggable="true">'," <span></span>"," <textarea/>","</li>"].join("")).appendTo(H);x();i.find("textarea").val("New "+n).focus().select();return e.when()}return k._exists(r).then(function(e){if(e==true){q.find("li.new").remove();setTimeout(function(){u.error(n+" already exists")},10)}else{return k._create(n,r).then(w)}})},_copy:function(t,n){if(!f(t,n)){return u.copy(t,n)}else{return e.when()}},copy:function(){r={path:E,contents:i[u.name],source:k};a=false},cut:function(){k.copy();a=true},paste:function(t){function n(t,n){return e.when.apply(e,r.contents.map(function(r){var a=t.split(r).pop();var i=t.join(E,a);if(!f(r,i)){return t[n](r,i)}else{return e.when()}}))}if(r&&r.contents&&r.contents.length){if(k.name()!==r.source.name()){u.error("You can't paste across different filesystems")}else{var a;if(t){a=n(k,"_rename")}else{a=n(k,"_copy")}a.then(function(){r.source.refresh();w()})}}},up:function(){var e=k.split(E);e.pop();k.show(k.join.apply(k,e));return k},refresh:function(){q.addClass("hidden");var t=e.Deferred();var n=e.Deferred();if(u.refresh_timer){setTimeout(t.resolve.bind(t),u.refresh_timer)}else{t.resolve()}k.show(E,{force:true,push:false,callback:function(){n.resolve()}});e.when(t,n).then(function(){q.removeClass("hidden")})},show:function(t,n){var r={callback:e.noop,push:true,force:false};n=e.extend({},r,n);if(E!=t||n.force){k.addClass("hidden");if(n.push){D.push(t)}A.find(".up").toggleClass("disabled",t==u.root);A.find(".back").toggleClass("disabled",D.length==1);E=t;var a=u.dir(E,s);if(a&&a.then){a.then(s).catch(function(){s()})}var i=false;function s(r){if(i){return}i=true;if(!r){u.error("Invalid directory");k.removeClass("hidden")}else{K=r;H.empty();K.dirs.forEach(function(n){var r=u.item_class(t,n);var a=e('<li class="directory"><span>'+n+"</span></li>").appendTo(H).attr("draggable",true);if(r){a.addClass(r)}});K.files.forEach(function(n){var r=e('<li class="file"><span>'+n+"</span></li>").appendTo(H).attr("draggable",true);if(n.match(".")){r.addClass(n.split(".").pop())}var a=u.item_class(t,n);if(a){r.addClass(a)}});k.removeClass("hidden");var a=new RegExp(e.browse.escape_regex(u.separator)+"$");if(!t.match(a)&&t!=u.root){t+=u.separator}$.val(t);u.change.call(k);n.callback()}}}return k},join:function(){var t=[].slice.call(arguments);var n=t.map(function(t){var n=new RegExp(e.browse.escape_regex(u.separator)+"$","");return t.replace(n,"")}).filter(Boolean).join(u.separator);var r=new RegExp("^"+e.browse.escape_regex(u.root));return r.test(n)?n:u.root+n},split:function(t){var n=new RegExp("^"+e.browse.escape_regex(u.root));var r=new RegExp(e.browse.escape_regex(u.separator)+"$");t=t.replace(n,"").replace(r,"");if(t){return t.split(u.separator).filter(Boolean)}else{return[]}},walk:function(e,t){var n=this.split(e);var r;while(n.length){r=t(n.shift(),!n.length,r)}return r}});setTimeout(function(){var e=u.start_directory||u.root;k.show(e,{callback:u.init.bind(k)})},0);k.data("browse",k);return k}}})(jQuery);