mein-sterntours/public/js/_bak_filemanager.js
2020-08-07 16:00:55 +02:00

720 lines
No EOL
28 KiB
JavaScript
Executable file

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jquery')) :
typeof define === 'function' && define.amd ? define(['exports', 'jquery'], factory) :
(global = global || self, factory(global.fileManager = {}, global.jQuery));
}(this, function (exports, $) { 'use strict';
$ = $ && $.hasOwnProperty('default') ? $['default'] : $;
function _extends() {
_extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
var lfm_route = location.origin + '/laravel-filemanager';
var lfm_show_list;
var lfm_sort_type = 'alphabetic';
var lfm_selected = [];
var lfm_items = [];
var LFM_CONTENT = '#file-manager-content';
var LFM_CONTAINER = '.file-manager-container';
var NAME = 'filemanager';
var VERSION = '1.0.0';
var DATA_KEY = 'fileManager';
var Default = {
toggle: true,
preventDefault: true,
triggerElement: 'a',
parentTrigger: 'li',
subMenu: 'ul'
};
var FileManager =
/*#__PURE__*/
function () {
function FileManager(element, config) {
this._element = element;
this.config = _extends({}, Default, config);
}// Getters
var _proto = FileManager.prototype;
_proto.test = function test(){
console.log("test");
};
_proto.init = function init() {
console.log("int");
if (!this._editor) {
var _self = this;
$(document).on('click', '#lfm_add-file', function () {
_self._performLfmRequest('add-file', {})
.done(function (data) {
_self._hideNavAndShowEditor(data, function () {
_self._performLfmRequest('add-dofile', {
working_dir: $("#working_dir").val(),
file_type: $("#from_file_type").val(),
// file_name: $("#form_file_name").val(),
file_content: $("#form_file_content").val(),
}).done(_self._refreshFoldersAndItems(_self));
});
});
});
$(document).on('click', '#lfm_add-folder', function () {
_self._dialog(lang['message-name'], '', _self._createFolder());
});
$(document).on('click', '#lfm_upload', function () {
$('#uploadModal').modal('show');
});
$('#uploadModal').on('hidden.bs.modal', function (e) {
$('#uploadForm')[0].dropzone.removeAllFiles();
});
$('#uploadClearButton').on('click', function () {
$('#uploadForm')[0].dropzone.removeAllFiles();
});
}
if ($(LFM_CONTENT).length) {
this._loadItems();
this._performLfmRequest('errors')
.done(function (response) {
JSON.parse(response).forEach(function (message) {
$('#alerts').append(
$('<div>').addClass('alert alert-warning')
.append($('<i>').addClass('fas fa-exclamation-circle'))
.append(' ' + message)
);
});
});
$(LFM_CONTENT).on('dragenter', function () {
$('#uploadModal').modal('show');
});
}
if ($(LFM_CONTAINER).length) {
// Checkboxes
$(LFM_CONTAINER).on('change', '.file-item-checkbox input', function () {
$(this).parents('.file-item')[this.checked ? 'addClass' : 'removeClass']('selected border-primary');
});
// Focus
$(LFM_CONTAINER).on('focusin', '.file-item', function () {
$(this).addClass('focused');
});
$(LFM_CONTAINER).on('focusout', '.file-item', function () {
if ($('.file-item-actions.show').length) return;
$(this).removeClass('focused');
});
$(LFM_CONTAINER).on('hide.bs.dropdown', '.file-item-actions', function () {
if ($(this).parents('.file-item').find(':focus').length) return;
$(this).parents('.file-item').removeClass('focused');
});
}
// Change view
$('[name="file-manager-view"]').on('change', function () {
$('.file-manager-container')
.removeClass('file-manager-col-view file-manager-row-view')
.addClass(this.value);
});
return this;
};
// ======================
// == Folder actions ==
// ======================
_proto._getSelectedItems = function() {
var items = [];
$('input.lfm-control-input').each(function () {
if ($(this).prop('checked')) {
items.push(this._getOneSelectedElement($(this).parents('.file-item').data('id')));
}
});
return items;
};
_proto._hasSelectedItems = function() {
$('input.lfm-control-input').each(function () {
//console.log($(this).prop('checked'));
if ($(this).prop('checked')) {
return true;
}
});
return false;
};
_proto._getOneSelectedElement = function(orderOfItem) {
var index = orderOfItem !== undefined ? orderOfItem : lfm_selected[0];
return lfm_items[index];
};
_proto._toggleActions = function() {
var _self = this;
$('a[data-action=item_rename]').on('click', function () {
item = this._getOneSelectedElement($(this).parents('.file-item').data('id'));
_self._rename(item);
});
$('a[data-action=item_move]').on('click', function (e) {
var items = [];
items.push(_self._getOneSelectedElement($(this).parents('.file-item').data('id')));
_self._move(items);
});
$('a[data-action=item_color]').on('click', function (e) {
item = _self._getOneSelectedElement($(this).parents('.file-item').data('id'));
_self._colors(item);
});
$('a[data-action=item_crop]').on('click', function (e) {
item = _self._getOneSelectedElement($(this).parents('.file-item').data('id'));
_self._crop(item);
});
$('a[data-action=item_resize]').on('click', function (e) {
item = _self._getOneSelectedElement($(this).parents('.file-item').data('id'));
_self._resize(item);
});
$('a[data-action=item_trash]').on('click', function () {
var items = [];
items.push(_self._getOneSelectedElement($(this).parents('.file-item').data('id')));
_self._trash(items);
});
$('a[data-action=item_download]').on('click', function () {
var items = [];
items.push(_self._getOneSelectedElement($(this).parents('.file-item').data('id')));
_self._download(items);
});
$('a[data-action=selected_items_move]').on('click', function () {
var items = _self._getSelectedItems();
_self._move(items);
});
$('a[data-action=selected_items_remove]').on('click', function () {
var items = _self._getSelectedItems();
_self._trash(items);
});
$('a[data-action=selected_items_download]').on('click', function () {
var items = _self._getSelectedItems();
_self._download(items);
});
};
_proto._goTo = function(new_dir) {
if (!$('.dropdown-menu').hasClass('show')) {
$('#working_dir').val(new_dir);
this._loadItems();
}
};
_proto._getPreviousDir = function() {
var working_dir = $('#working_dir').val();
if (!working_dir) {
working_dir = "/shares";
}
return working_dir.substring(0, working_dir.lastIndexOf('/'));
};
// ====================
// == Ajax actions ==
// ====================
_proto._performLfmRequest = function(url, parameter, type) {
var data = this._defaultParameters();
if (parameter != null) {
$.each(parameter, function (key, value) {
data[key] = value;
});
}
// console.log(lfm_route + '/' + url);
// console.log(type);
// console.log(data);
var _self = this;
return $.ajax({
type: 'GET',
beforeSend: function (request) {
var token = _self.getUrlParam('token');
if (token !== null) {
request.setRequestHeader("Authorization", 'Bearer ' + token);
}
},
dataType: type || 'text',
url: lfm_route + '/' + url,
data: data,
cache: false
}).done(function (data) {
// console.log(data);
/* if(data !== "OK"){
var response = JSON.parse(data);
console.log("done");
console.log(response);
if(response.type === "error"){
$.growl({
title: "Error",
message: response.data,
location: 'tr'
});
}
}
*/
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
_self._displayErrorResponse(jqXHR);
_self._refreshFoldersAndItems(_self);
});
};
_proto._displayErrorResponse = function(jqXHR) {
this._notify('<div style="max-height:50vh;overflow: scroll;">' + jqXHR.responseText + '</div>');
};
_proto._isJSON = function(text) {
if (typeof text !== "string") {
return false;
}
try {
JSON.parse(text);
return true;
} catch (error) {
return false;
}
};
_proto._refreshFoldersAndItems = function(_self, data) {
_self._loadItems();
if (_self._isJSON(data)) {
var response = JSON.parse(data);
//console.log("done");
//console.log(response);
if (response.type === "error") {
$.growl({
title: "Error",
message: response.data,
location: 'tr'
});
}
} else {
if (data !== 'OK') {
data = Array.isArray(data) ? data.join('<br/>') : data;
this._notify(data);
}
}
};
_proto._hideNavAndShowEditor = function(data, callback = false) {
// $('#nav-buttons > ul').addClass('d-none');
//console.log(data);
this._notify(data, callback);
};
_proto._loadItems = function() {
this._loading(true);
var _self = this;
this._performLfmRequest('jsonitems', {show_list: lfm_show_list, sort_type: lfm_sort_type},
'html')
.done(function (data) {
//console.log(data);
lfm_selected = [];
var response = JSON.parse(data);
var working_dir = response.working_dir;
lfm_items = response.items;
var hasItems = lfm_items.length !== 0;
$('#lfm_empty').toggleClass('d-none', hasItems);
//$(LFM_CONTENT).html(''); //.removeAttr('class');
var top_rev = $('<div class="file-item" id="to-previous"><div class="file-item-icon file-item-level-up fas fa-level-up-alt text-secondary"></div><a href="javascript:void(0)" class="file-item-name">zurück</a></div>');
$(LFM_CONTENT).html('').append(top_rev);
if (hasItems) {
$(LFM_CONTENT).addClass(response.display).addClass('preserve_actions_space');
lfm_items.forEach(function (item, index) {
var template = $('#lfm_item-template').clone()
.removeAttr('id').removeClass('d-none')
.attr('data-id', index)
//.click(toggleSelected)
.click(function (e) {
//console.log($(e.target));
if (!$(e.target).hasClass('lfm-click-disable') && !$(e.target).hasClass('dropdown-item')) {
if (item.is_file) {
if ($(e.target).hasClass('file-item-img') || $(e.target).hasClass('file-item-icon')) {
_self._preview_item(item);
} else {
if ($(this).hasClass('file-item')) {
$(this).find('.lfm-control-input').click();
}
}
} else {
_self._goTo(item.url);
}
}
if ($(e.target).hasClass('lfm-control-input')) {
if (this._getSelectedItems().length > 0) {
if ($('.media-multi-settings').hasClass('d-none')) {
$('.media-multi-settings').removeClass('d-none');
}
} else {
if (!$('.media-multi-settings').hasClass('d-none')) {
$('.media-multi-settings').addClass('d-none');
}
}
}
});
if (item.thumb_url) {
var image = $('<div>').addClass('file-item-img').css('background-image', 'url("' + item.thumb_url + '?timestamp=' + item.time + '")');
var dot = $('<span>').addClass('badge badge-dot badge-default indicator_before').css({"background-color": item.color});
} else {
var image = $('<div>').addClass('file-item-icon fa ' + item.icon).css({"color": item.color});
var dot = $('<span>').addClass('badge badge-dot badge-default indicator_before').css({"background-color": item.color});
}
if (!item.is_image) { //no image
template.find('.link-item-crop').remove();
template.find('.link-item-resize').remove();
} else {
// template.find('.link-item-color').remove();
}
if (!item.is_file) { //dirs
template.find('.link-item-download').remove();
}
template.find('.file-item-name').before(image);
var item_name = $('<span>').addClass('file-item-name-insert').html(item.name);
template.find('.file-item-name').html(item_name);
template.find('.file-item-name-insert').before(dot);
var str = "";
if (item.dimension) {
str += item.dimension + " | "
}
if (item.size) {
str += item.size + " | "
}
str += (new Date(item.time * 1000)).toLocaleString();
template.find('.file-item-changed').text(str);
$(LFM_CONTENT).append(template);
});
}
//$('#nav-buttons > ul').removeClass('d-none');
$('#working_dir').val(working_dir);
// console.log('Current working_dir : ' + working_dir);
var breadcrumbs = [];
var validSegments = working_dir.split('/').filter(function (e) {
return e;
});
validSegments.forEach(function (segment, index) {
if (index === 0) {
// set root folder name as the first breadcrumb
breadcrumbs.push("Medien");
//breadcrumbs.push($("[data-path='/" + segment + "']").text());
} else {
breadcrumbs.push(segment);
}
});
$('#current_folder').text(breadcrumbs[breadcrumbs.length - 1]);
$('#lfm_breadcrumbs > ol').html('');
breadcrumbs.forEach(function (breadcrumb, index) {
var li = $('<li>').addClass('breadcrumb-item').text(breadcrumb);
if (index === breadcrumbs.length - 1) {
li.addClass('active').attr('aria-current', 'page');
} else {
li.click(function () {
// go to corresponding path
this._goTo('/' + validSegments.slice(0, 1 + index).join('/'));
});
}
$('#lfm_breadcrumbs > ol').append(li);
});
var atRootFolder = _self._getPreviousDir() == '';
$('#to-previous').toggleClass('d-none', atRootFolder);
$('#to-previous').click(function () {
var previous_dir = _self._getPreviousDir();
if (previous_dir === '') return;
this._goTo(previous_dir);
});
_self._loading(false);
_self._toggleActions();
});
};
_proto._loading = function(show_loading) {
$('#lfm_loading').toggleClass('d-none', !show_loading);
};
_proto._createFolder = function(folder_name) {
var _self = this;
this._performLfmRequest('newfolder', {name: folder_name})
.done(_self._refreshFoldersAndItems(_self));
};
// ==================================
// == File Actions ==
// ==================================
_proto._rename = function(item) {
var _self = this;
this._dialog(lang['message-rename'], item.name, function (new_name) {
_self._performLfmRequest('rename', {
file: item.name,
new_name: new_name
}).done(_self._refreshFoldersAndItems(_self));
});
};
_proto._colors = function(item) {
var _self = this;
this._performLfmRequest('colorhue', {color: item.color})
.done(function (data) {
_self._hideNavAndShowEditor(data, function () {
_self._performLfmRequest('docolorhue', {
file: item.name,
new_color: $('#notify').find('input').val(),
}).done(_self._refreshFoldersAndItems(_self));
});
});
};
_proto._trash = function(items) {
var _self = this;
this._notify(lang['message-delete'], function () {
var d = _self._performLfmRequest('delete', {
items: items.map(function (item) {
return item.name;
})
}).done(r_self._efreshFoldersAndItems);
// console.log(d);
});
};
_proto._crop = function(item) {
var _self = this;
this._performLfmRequest('crop', {img: item.name})
.done(_self._hideNavAndShowEditor());
};
_proto._resize = function(item) {
var _self = this;
this._performLfmRequest('resize', {img: item.name})
.done(_self._hideNavAndShowEditor());
};
_proto._move = function(items) {
var _self = this;
this._performLfmRequest('move', {
items: items.map(function (item) {
return item.name;
})
})
.done(_self._refreshFoldersAndItems(_self));
};
_proto._download = function(items) {
items.forEach(function (item, index) {
var data = this._defaultParameters();
data['file'] = item.name;
var token = this.getUrlParam('token');
if (token) {
data['token'] = token;
}
setTimeout(function () {
location.href = lfm_route + '/download?' + $.param(data);
},
index * 100);
});
};
_proto._open = function(item) {
this._goTo(item.url);
};
_proto._preview_item = function(item) {
//var items = [];
//items.push(item);
this._preview(item);
};
_proto._preview = function(item) {
var media = $('#previewTemplate').clone().attr('id', 'previewMedia').removeClass('d-none');
var item_content_id = "";
if (item.icon === "fa-image" || item.icon === "fa-file-pdf") {
media.find('.media-preview').attr('src', item.url + '?timestamp=' + item.time).removeClass('d-none');
} else if (item.icon === "fab fa-youtube-square") {
media.find('.embed-responsive-item').attr('src', 'https://www.youtube.com/embed/' + item.content.id + '?rel=0');
media.find('.embed-responsive').removeClass('d-none');
item_content_id = item.content.id;
} else {
var icon = $('<div>').addClass('file-item-icon fa ' + item.icon).css('color', item.color);
media.find('.file-manager-col-view').html(icon).removeClass('d-none');
}
if (item.content) {
media.find('.media-content-title').val(item.content.title);
media.find('.media-content-description').val(item.content.description);
}
media.find('.media-content-title').parent().removeClass('d-none');
media.find('.media-content-description').parent().removeClass('d-none');
media.find('.media-name').html(item.name);
media.find('.media-url').val(item.url);
media.find('.media-download').attr('target', '_blank').attr('href', item.url);
var _self = this;
var callback = function () {
_self._performLfmRequest('file-content', {
file: item.name,
content_id: item_content_id,
content_title: media.find('.media-content-title').val(),
content_description: media.find('.media-content-description').val(),
}).done(_self._refreshFoldersAndItems(_self));
};
this._notify(media, callback);
};
// ==================================
// == Others ==
// ==================================
_proto.getUrlParam = function(paramName) {
var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i');
var match = window.location.search.match(reParam);
return (match && match.length > 1) ? match[1] : null;
};
_proto._defaultParameters = function() {
return {
working_dir: $('#working_dir').val(),
type: $('#type').val()
};
};
_proto._notImp = function() {
this._notify('Not yet implemented!');
};
_proto._notify = function(body, callback) {
$('#notify').find('.btn-primary').toggle(callback !== undefined);
if(callback !== 'success'){
$('#notify').find('.btn-primary').unbind().click(callback);
}else{
$('#notify').find('.btn-primary').unbind();
}
$('#notify').modal('show').find('.modal-body').html(body);
};
_proto._dialog = function(title, value, callback) {
$('#dialog').find('input').val(value);
$('#dialog').on('shown.bs.modal', function () {
$('#dialog').find('input').focus();
});
$('#dialog').find('.btn-primary').unbind('click').click(function (e) {
callback($('#dialog').find('input').val());
});
$('#dialog').find('input').unbind('keypress.key13').bind('keypress.key13', function (e) {
if (e.which === 13) {
$('#dialog').find('.btn-primary').click();
}
});
$('#dialog').modal('show').find('.modal-title').text(title);
};
FileManager.jQueryInterface = function jQueryInterface(config) {
// eslint-disable-next-line func-names
return this.each(function () {
var $this = $(this);
var data = $this.data(DATA_KEY);
var conf = _extends({}, Default, $this.data(), typeof config === 'object' && config ? config : {});
if (!data) {
data = new FileManager(this, conf);
$this.data(DATA_KEY, data);
}
if (typeof config === 'string') {
if (data[config] === undefined) {
throw new Error("No method named \"" + config + "\"");
}
data[config]();
}
});
};
return FileManager;
}();
$.fn[NAME] = FileManager.jQueryInterface; // eslint-disable-line no-param-reassign
$.fn[NAME].Constructor = FileManager; // eslint-disable-line no-param-reassign
$.fn[NAME].noConflict = function () {
// eslint-disable-line no-param-reassign
$.fn[NAME] = JQUERY_NO_CONFLICT; // eslint-disable-line no-param-reassign
return FileManager.jQueryInterface;
};
exports.FileManager = FileManager;
Object.defineProperty(exports, '__esModule', { value: true });
//return FileManager;
}));