563 lines
20 KiB
JavaScript
563 lines
20 KiB
JavaScript
var csrf_token = document
|
|
.querySelector("meta[name='csrf-token']")
|
|
.getAttribute("content");
|
|
$.ajaxSetup({
|
|
headers: {
|
|
// 'Content-Type': 'multipart/form-data',
|
|
// 使用 multipart/form-data 在此不需要設定 Content-Type。
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
'X-CSRF-Token': csrf_token,
|
|
}
|
|
});
|
|
function replace_explorer_section(file_manager_section, exclude_upload_section){
|
|
var org_file_manager_section = $('#file_manager_section');
|
|
if(org_file_manager_section.find(' > .file-input').length != 0 && file_manager_section.find('> .file-uploader-script').length != 0){
|
|
org_file_manager_section.find(' > *').each(function(i,v){
|
|
if(!($(v).hasClass('file-input') || $(v).hasClass('file-uploader-script'))){
|
|
$(v).replaceWith(file_manager_section.find('>*').eq(i).clone());
|
|
}
|
|
})
|
|
if(!exclude_upload_section){
|
|
$("#file-uploader").fileinput('clear');
|
|
var fileinput_data = $("#file-uploader").data('fileinput');
|
|
fileinput_data.uploadUrl = "/xhr/file_manager_upload/" + window.current_path + (window.explorer_setting_id ? ('?setting_id='+window.explorer_setting_id) : '');
|
|
}
|
|
}else{
|
|
org_file_manager_section.find('#index_table').html(file_manager_section);
|
|
}
|
|
}
|
|
function reload_explorer_section(url, exclude_upload_section, callback,...args){
|
|
if(url == undefined){
|
|
url = get_path();
|
|
}
|
|
$.get(url).done(function(data){
|
|
var $file_manager_section = $(data);
|
|
replace_explorer_section($file_manager_section, exclude_upload_section);
|
|
if(window.scroll_top){
|
|
$(window).scrollTop(window.scroll_top);
|
|
}
|
|
if(typeof(callback) == 'function'){
|
|
callback.apply( this, args );
|
|
}
|
|
}).fail(function() {
|
|
alert('No such file or directory!');
|
|
});
|
|
}
|
|
function reload_page(url, is_recycle_bin, extra_params){
|
|
var tmp_url = url, request_url;
|
|
if(url == undefined){
|
|
request_url = get_path(undefined,undefined, extra_params);
|
|
}else{
|
|
request_url = get_path(url,is_recycle_bin, extra_params);
|
|
}
|
|
$.get(request_url).done(function(data){
|
|
var $file_manager_section = $(data);
|
|
replace_explorer_section($file_manager_section);
|
|
if(!window.only_select_folder){
|
|
push_history_url(request_url);
|
|
}
|
|
if(window.scroll_top){
|
|
$(window).scrollTop(window.scroll_top);
|
|
}
|
|
}).fail(function() {
|
|
alert('No such file or directory!');
|
|
});
|
|
}
|
|
function push_history_url(new_url, state){
|
|
if(state == undefined)
|
|
state = {body: $('body').prop('outerHTML')};
|
|
if(history.state == null){
|
|
history.replaceState({need_reload: true} ,$('title').text(), window.location.href);
|
|
}
|
|
window.history.pushState(state ,$('title').text(), new_url);
|
|
}
|
|
function get_current_url(path, override_recycle_bin, extra_params){
|
|
if(override_recycle_bin == undefined){
|
|
override_recycle_bin = window.is_recycle_bin;
|
|
}
|
|
var params = {};
|
|
if(override_recycle_bin){
|
|
path = '';
|
|
params['recycle_bin'] = true
|
|
}
|
|
if(extra_params){
|
|
Object.keys(extra_params).forEach(function(k){
|
|
params[k] = extra_params[k];
|
|
})
|
|
}
|
|
var url = '';
|
|
if(window.path_mode == 'params'){
|
|
params["path"] = path;
|
|
url = window.location.pathname
|
|
}else{
|
|
url = "/xhr/file_manager/" + path;
|
|
}
|
|
if(window.only_select_folder){
|
|
params['select_mode'] = true;
|
|
}
|
|
return url + params_to_query(params);
|
|
}
|
|
function get_path(path, override_recycle_bin, extra_params){
|
|
var params = get_params();
|
|
if(path == undefined){
|
|
path = window.location.pathname;
|
|
}
|
|
if(override_recycle_bin == undefined){
|
|
override_recycle_bin = window.is_recycle_bin;
|
|
}
|
|
if(override_recycle_bin){
|
|
path = ""
|
|
params["recycle_bin"] = true;
|
|
window.is_recycle_bin = true;
|
|
}else{
|
|
delete params["recycle_bin"];
|
|
window.is_recycle_bin = false;
|
|
}
|
|
if(window.only_select_folder){
|
|
params['select_mode'] = true;
|
|
}else{
|
|
delete params["select_mode"];
|
|
}
|
|
delete params["page"];
|
|
delete params["file_id"];
|
|
if(window.explorer_setting_id){
|
|
params['setting_id'] = window.explorer_setting_id;
|
|
}
|
|
if(extra_params){
|
|
Object.keys(extra_params).forEach(function(k){
|
|
if (extra_params[k] != null)
|
|
params[k] = extra_params[k];
|
|
})
|
|
}
|
|
if(window.use_params_request){
|
|
params['path'] = path;
|
|
path = '';
|
|
}else{
|
|
delete params['path'];
|
|
}
|
|
return path + params_to_query(params);
|
|
}
|
|
function get_basename(file){
|
|
var tmp = file.split('/');
|
|
return tmp[tmp.length - 1] || '';
|
|
}
|
|
function pathJoin(parts, sep){
|
|
var separator = sep || '/';
|
|
var replace = new RegExp(separator+'{1,}', 'g');
|
|
if(parts.length > 1){
|
|
parts.forEach(function(p, i){
|
|
if(p[0] == '/'){
|
|
for(var k=0; k<i; k++){
|
|
parts[k] = "";
|
|
}
|
|
}
|
|
})
|
|
}
|
|
parts = parts.filter(function(s){ return s != "" });
|
|
var new_path = parts.join(separator).replace(replace, separator).replace(new RegExp(`[^${separator}]+${separator}\\.\\.${separator}`, 'g'),'');
|
|
return new_path.replace(/^(http|ftp):\//,'$1://').replace(/file:\//,'file:///').replace(new RegExp(`^.{1,2}${separator}`),'').replace(/^\/\.\.\//,'/').replace(/\.\//g,'');
|
|
}
|
|
function get_current_path(path, override_recycle_bin){
|
|
if(path == undefined){
|
|
path = window.current_path;
|
|
}
|
|
if(override_recycle_bin == undefined){
|
|
override_recycle_bin = window.is_recycle_bin;
|
|
}
|
|
if(override_recycle_bin && window.recycle_bin_text){
|
|
return window.recycle_bin_text;
|
|
}else{
|
|
return path;
|
|
}
|
|
}
|
|
function change_folder(id, folder, is_recycle_bin){
|
|
window.scroll_top = $('#file_manager_section thead').scrollTop();
|
|
if (!folder) {
|
|
folder = window.module_key;
|
|
}
|
|
var new_path = window.file_manager_path + '/' + folder + '/module';
|
|
new_path = get_path(new_path, (is_recycle_bin ? true : false), {"file_id": id});
|
|
console.log(new_path, id);
|
|
$.get(new_path).done(function(data){
|
|
var $file_manager_section = $(data);
|
|
replace_explorer_section($file_manager_section);
|
|
window.module_key = folder;
|
|
if(!window.only_select_folder){
|
|
push_history_url(new_path);
|
|
}
|
|
if(window.scroll_top){
|
|
$(window).scrollTop(window.scroll_top);
|
|
}
|
|
}).fail(function() {
|
|
alert('No such file or directory!');
|
|
});
|
|
}
|
|
function get_params(){
|
|
var search_split = window.location.search.split("?");
|
|
var params = {}
|
|
if( search_split.length >= 2 ){
|
|
$(search_split[1].split("&")).each(function(i, s){
|
|
var tmp = s.split("=");
|
|
var k = tmp[0], v = tmp[1];
|
|
if(k.search('[]') != -1){
|
|
k = k.split('[]')[0];
|
|
if(params.hasOwnProperty(k)){
|
|
params[k].push(v);
|
|
}else{
|
|
params[k] = [v];
|
|
}
|
|
}else{
|
|
params[k] = v;
|
|
}
|
|
})
|
|
}
|
|
return params;
|
|
}
|
|
function params_to_query(params){
|
|
var query_text = '';
|
|
var keys = Object.keys(params);
|
|
if(keys.length != 0){
|
|
keys.forEach(function(k, i){
|
|
if(i == 0){
|
|
query_text += '?';
|
|
}else{
|
|
query_text += '&';
|
|
}
|
|
var v = params[k];
|
|
if(Array.isArray(v)){
|
|
v.forEach(function(vv, j){
|
|
if(j != 0){
|
|
query_text += '&';
|
|
}
|
|
query_text += `${k}[]=${vv}`;
|
|
})
|
|
}else{
|
|
query_text += `${k}=${v}`;
|
|
}
|
|
})
|
|
}
|
|
return query_text;
|
|
}
|
|
$(document).on("click",".delete",function(){
|
|
var that = $(this);
|
|
window.scroll_top = $(window).scrollTop();
|
|
if( window.confirm(that.data("tmp-confirm")) ){
|
|
if( window.confirm(that.data("tmp-confirm")) ){
|
|
$.ajax({
|
|
url: that.data("link"),
|
|
method: 'DELETE',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
statusCode: {
|
|
204: reload_explorer_section,
|
|
404: function() {
|
|
alert( "Delete failed!\nFile not exist!" );
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
$(document).on("click",".delete_file",function(){
|
|
var that = $(this);
|
|
window.scroll_top = $(window).scrollTop();
|
|
if( window.confirm(that.data("tmp-confirm")) ){
|
|
if( window.confirm(that.data("tmp-confirm")) ){
|
|
$.ajax({
|
|
url: that.data("link"),
|
|
method: 'DELETE',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
statusCode: {
|
|
200: function(data) {
|
|
var params = get_params();
|
|
var extra_params;
|
|
if (data.id) {
|
|
extra_params = {"version": data.version, "file_id": data.id};
|
|
} else {
|
|
extra_params = {};
|
|
}
|
|
var url = get_path(undefined, undefined, extra_params);
|
|
var callback = push_history_url;
|
|
reload_explorer_section(url, false, callback, url);
|
|
},
|
|
404: function() {
|
|
alert( "Delete failed!\nFile not exist!" );
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
$(document).on("click",".edit",function(){
|
|
$(this).hide();
|
|
var pre_height = $('#file_manager_section pre').height();
|
|
$('#file_manager_section textarea.edit-area').html($('#file_manager_section pre').html());
|
|
$('#file_manager_section textarea.edit-area').removeClass('hide');
|
|
$('#file_manager_section pre').addClass('hide');
|
|
$('#file_manager_section textarea.edit-area').height(pre_height);
|
|
$(".save").removeClass('hide');
|
|
})
|
|
$(document).on("click",".save",function(){
|
|
$(this).hide();
|
|
var data = new FormData();
|
|
data.append("content", $('#file_manager_section textarea.edit-area').val());
|
|
$.ajax({
|
|
url: '/admin/file_managers/' + $(this).data('id') + '/save',
|
|
method: 'POST',
|
|
data: data,
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data) {
|
|
console.log(data);
|
|
var params = get_params(), url;
|
|
var callback = push_history_url;
|
|
if(params.version){
|
|
var new_version = Number(params.version) + 1;
|
|
url = get_path(undefined, undefined, {"version": new_version, "file_id": data.id});
|
|
params.version = new_version.toString();
|
|
} else {
|
|
url = get_path(undefined, undefined, {"file_id": data.id});
|
|
}
|
|
reload_explorer_section(url, false, callback, url);
|
|
$(".save").addClass('hide');
|
|
$(".edit").removeClass('hide');
|
|
},
|
|
400: function() {
|
|
alert( "Bad Request!\nThere are no content of this file!" );
|
|
}
|
|
}
|
|
})
|
|
})
|
|
$(document).on("click",".module-key",function(){
|
|
change_folder(null, $(this).data("key"));
|
|
})
|
|
$(document).on("click",".directory, .file",function(){
|
|
change_folder($(this).data("id"));
|
|
})
|
|
$(document).on("click",".breadcrumb-index",function(){
|
|
var path = window.current_path[0] == '/' ? window.current_path : ('/' + window.current_path);
|
|
$("#breadcrumb-relative-url").addClass('hide');
|
|
$("#breadcrumb-editable").text(path).removeClass("hide").focus();
|
|
window.tmp_current_path = path;
|
|
})
|
|
$(document).on("click",'#breadcrumb-relative-url .directory, #breadcrumb-editable',function(e){
|
|
e.stopPropagation();
|
|
})
|
|
$(document).on("blur","#breadcrumb-editable",function(){
|
|
if(window.tmp_current_path != $(this).text().trim()){
|
|
change_folder($(this).text().trim());
|
|
}
|
|
$("#breadcrumb-relative-url").removeClass('hide');
|
|
$(this).addClass('hide');
|
|
})
|
|
$(document).on("keydown","#breadcrumb-editable",function(evt){
|
|
if(evt.keyCode == 13){
|
|
evt.preventDefault();
|
|
$(this).trigger("blur");
|
|
}
|
|
})
|
|
$(document).on("click", ".copyButton", function(){
|
|
var link = $(this).data('link');
|
|
// Copy the link
|
|
navigator.clipboard.writeText(link);
|
|
if (!($(this).data('org-text')))
|
|
$(this).data('org-text', $(this).find('.tooltiptext').text());
|
|
$(this).find('.tooltiptext').text($(this).data('prompt') + ': ' + link);
|
|
})
|
|
$(document).on("mouseout", ".copyButton", function(){
|
|
var org_text = $(this).data('org-text');
|
|
if (org_text)
|
|
$(this).find('.tooltiptext').text(org_text);
|
|
})
|
|
$(document).on("click", ".redirect_recycle_bin", function(){
|
|
change_folder(null, null, true);
|
|
})
|
|
$(document).on("click", ".recycle_bin", function(){
|
|
change_folder(null, $(this).data('key'), true);
|
|
})
|
|
$(document).on("click", ".recover_all", function(){
|
|
var that = $(this);
|
|
var prompt_message = that.data("prompt");
|
|
$.ajax({
|
|
url: '/admin/file_managers/' + window.module_key + '/get_trash_count',
|
|
method: 'POST',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data) {
|
|
var count = data["count"] + that.data('unit');
|
|
prompt_message = prompt_message.replace('${num}', count);
|
|
if(window.confirm(prompt_message)){
|
|
if(window.confirm(prompt_message)){
|
|
$.ajax({
|
|
url: '/admin/file_managers/' + window.module_key + '/recover_all',
|
|
method: 'POST',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data) {
|
|
var thread_id = data["id"];
|
|
var thread_title = data["title"];
|
|
reload_explorer_section(get_path(), false, function show_modal(thread_id, thread_title){
|
|
$("#threadModal").data('id', thread_id);
|
|
$("#threadModal .modal-header h3").text(thread_title);
|
|
$("#threadModal").removeClass('hide').modal('show');
|
|
}, thread_id, thread_title);
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
$(document).on("click",".clean_up_recycle_bin", function(){
|
|
var that = $(this);
|
|
var prompt_message = that.data("prompt");
|
|
$.ajax({
|
|
url: '/admin/file_managers/' + window.module_key + '/get_trash_count',
|
|
method: 'POST',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data) {
|
|
var count = data["count"] + that.data('unit');
|
|
prompt_message = prompt_message.replace('${num}', count);
|
|
if(window.confirm(prompt_message)){
|
|
if(window.confirm(prompt_message)){
|
|
$.ajax({
|
|
url: '/admin/file_managers/' + window.module_key + '/clean_trashes',
|
|
method: 'POST',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data) {
|
|
var thread_id = data["id"];
|
|
var thread_title = data["title"];
|
|
reload_explorer_section(get_path(), false, function show_modal(thread_id, thread_title){
|
|
$("#threadModal").data('id', thread_id);
|
|
$("#threadModal .modal-header h3").text(thread_title);
|
|
$("#threadModal").removeClass('hide').modal('show');
|
|
}, thread_id, thread_title);
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
$(document).on('hidden.bs.modal', "#threadModal", function(){
|
|
window.clearTimeout(window.time_out_id);
|
|
})
|
|
$(document).on('shown.bs.modal', "#threadModal", function(){
|
|
var thread_id = $(this).data('id');
|
|
window.time_out_id = window.setTimeout(function(){
|
|
update_thread(thread_id, reload_page);
|
|
}, 1000);
|
|
})
|
|
$(document).on('click', ".change_version", function(){
|
|
var that = $(this);
|
|
var version = that.data('version');
|
|
var file_id = that.data('id');
|
|
reload_page(undefined,undefined,{"version": version, "file_id": file_id});
|
|
})
|
|
$(document).on('click', ".recover", function(){
|
|
var that = $(this);
|
|
window.scroll_top = $(window).scrollTop();
|
|
if( window.confirm(that.data("tmp-confirm")) ){
|
|
$.ajax({
|
|
url: that.data("link"),
|
|
method: 'POST',
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
statusCode: {
|
|
204: reload_explorer_section,
|
|
404: function() {
|
|
alert( "Recover failed!\nFile not exist!" );
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
function update_thread(id, finish_callback, ...args){
|
|
var data = new FormData();
|
|
data.append('id',id);
|
|
$.ajax({
|
|
url: "/admin/threads/get_status",
|
|
method: 'POST',
|
|
data: data,
|
|
contentType: false, //required
|
|
processData: false, // required
|
|
dataType: 'json',
|
|
mimeType: 'multipart/form-data',
|
|
statusCode: {
|
|
200: function(data){
|
|
var finish_percent = data["finish_percent"];
|
|
var current_count = data["current_count"];
|
|
var all_count = data["all_count"];
|
|
var is_finish = (data["status"] == "finish");
|
|
if(finish_percent){
|
|
$("#threadModal .modal-body .thread-status").text(data["status"]);
|
|
if(data["status"] != 'Processing'){
|
|
$("#threadModal .modal-body .thread-current-count").text(current_count);
|
|
$("#threadModal .modal-body .thread-all-count").text(all_count);
|
|
}
|
|
$("#threadModal .modal-body .thread-finish_percent").text(finish_percent);
|
|
}else if(is_finish){
|
|
$("#threadModal .modal-body .thread-status").text(data["status"]);
|
|
$("#threadModal .modal-body .thread-finish_percent").text(100);
|
|
}
|
|
var that = this;
|
|
if(!is_finish){
|
|
var tmp_arguments = arguments;
|
|
window.time_out_id = window.setTimeout(function(){
|
|
update_thread.apply(that, tmp_arguments);
|
|
}, 1000);
|
|
}else{
|
|
if(window.time_out_id)
|
|
window.clearTimeout(window.time_out_id);
|
|
var wait_time = 3000;
|
|
if(finish_callback)
|
|
wait_time = 0;
|
|
window.setTimeout(function(){
|
|
$("#threadModal").modal("hide");
|
|
if(finish_callback){
|
|
finish_callback.apply(that, args);
|
|
}
|
|
alert(data["status"]);
|
|
}, wait_time);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
window.onpopstate = function(event) {
|
|
var state = event.state;
|
|
if(state != null){
|
|
if(state.need_reload){
|
|
window.location.reload();
|
|
}else if(state.body){
|
|
var $body = $(state.body);
|
|
$('.breadcrumb-index').replaceWith($body.find('.breadcrumb-index'));
|
|
$('#file_manager_section').replaceWith($body.find('#file_manager_section'));
|
|
}
|
|
}
|
|
}; |