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'; $(document).ready(function () { /* actions.reverse().forEach(function (action) { $('#nav-buttons > ul').prepend( $('
  • ').addClass('nav-item').append( $('').addClass('nav-link d-none') .attr('data-action', action.name) .attr('data-multiple', action.multiple) .append($('').addClass('fas fa-fw fa-' + action.icon)) .append($('').text(action.label)) ) ); }); sortings.forEach(function (sort) { $('#nav-buttons .dropdown-menu').append( $('').addClass('dropdown-item').attr('data-sortby', sort.by) .append($('').addClass('fas fa-fw fa-' + sort.icon)) .append($('').text(sort.label)) .click(function() { sort_type = sort.by; loadItems(); }) ); }); */ if($(lfm_content).length) { loadItems(); performLfmRequest('errors') .done(function (response) { JSON.parse(response).forEach(function (message) { $('#alerts').append( $('
    ').addClass('alert alert-warning') .append($('').addClass('fas fa-exclamation-circle')) .append(' ' + message) ); }); }); $(lfm_content).on('dragenter', function () { $('#uploadModal').modal('show'); }); } /* if (usingWysiwygEditor()) { $('#multi_selection_toggle').hide(); }*/ }); // ====================== // == Navbar actions == // ====================== /* $('#multi_selection_toggle').click(function () { multi_selection_enabled = !multi_selection_enabled; $('#multi_selection_toggle i') .toggleClass('fa-times', multi_selection_enabled) .toggleClass('fa-check-double', !multi_selection_enabled); if (!multi_selection_enabled) { clearSelected(); } }); */ $(document).on('click', '#lfm_add-file', function () { performLfmRequest('add-file', {}) .done(function (data) { hideNavAndShowEditor(data, function () { 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(refreshFoldersAndItems); }); }); }); $(document).on('click', '#lfm_add-folder', function () { dialog(lang['message-name'], '', createFolder); }); $(document).on('click', '#lfm_upload', function () { $('#uploadModal').modal('show'); }); /* $(document).on('click', '[data-display]', function() { show_list = $(this).data('display'); loadItems(); }); $(document).on('click', '[data-action]', function() { window[$(this).data('action')]($(this).data('multiple') ? getSelectedItems() : getOneSelectedElement()); }); // ========================== // == Multiple Selection == // ========================== */ /* function clearSelected () { selected = []; multi_selection_enabled = false; updateSelectedStyle(); } function updateSelectedStyle() { items.forEach(function (item, index) { $('[data-id=' + index + ']') .find('.square') .toggleClass('selected', selected.indexOf(index) > -1); }); toggleActions(); } function getOneSelectedElement(orderOfItem) { var index = orderOfItem !== undefined ? orderOfItem : selected[0]; return items[index]; } function getSelectedItems() { return selected.reduce(function (arr_objects, id) { arr_objects.push(getOneSelectedElement(id)); return arr_objects }, []); } */ function getSelectedItems() { var items = []; $('input.lfm-control-input').each(function () { if($(this).prop('checked')){ items.push(getOneSelectedElement($(this).parents('.file-item').data('id'))); } }); return items; } function hasSelectedItems() { $('input.lfm-control-input').each(function () { //console.log($(this).prop('checked')); if($(this).prop('checked')){ return true; } }); return false; } function getOneSelectedElement(orderOfItem) { var index = orderOfItem !== undefined ? orderOfItem : lfm_selected[0]; return lfm_items[index]; } function toggleActions() { $('a[data-action=item_rename]').on('click', function () { item = getOneSelectedElement($(this).parents('.file-item').data('id')); rename(item); }); $('a[data-action=item_move]').on('click', function (e) { var items = []; items.push(getOneSelectedElement($(this).parents('.file-item').data('id'))); move(items); }); $('a[data-action=item_color]').on('click', function (e) { item = getOneSelectedElement($(this).parents('.file-item').data('id')); colors(item); }); $('a[data-action=item_crop]').on('click', function (e) { item = getOneSelectedElement($(this).parents('.file-item').data('id')); crop(item); }); $('a[data-action=item_resize]').on('click', function (e) { item = getOneSelectedElement($(this).parents('.file-item').data('id')); resize(item); }); $('a[data-action=item_trash]').on('click', function () { var items = []; items.push(getOneSelectedElement($(this).parents('.file-item').data('id'))); trash(items); }); $('a[data-action=item_download]').on('click', function () { var items = []; items.push(getOneSelectedElement($(this).parents('.file-item').data('id'))); download(items); }); $('a[data-action=selected_items_move]').on('click', function () { var items = getSelectedItems(); move(items); }); $('a[data-action=selected_items_remove]').on('click', function () { var items = getSelectedItems(); trash(items); }); $('a[data-action=selected_items_download]').on('click', function () { var items = getSelectedItems(); download(items); }); /* var one_selected = selected.length === 1; var many_selected = selected.length >= 1; var only_image = getSelectedItems() .filter(function (item) { return !item.is_image; }) .length === 0; var only_file = getSelectedItems() .filter(function (item) { return !item.is_file; }) .length === 0; $('[data-action=use]').toggleClass('d-none', !(many_selected && only_file)); $('[data-action=rename]').toggleClass('d-none', !one_selected); $('[data-action=preview]').toggleClass('d-none', !(many_selected && only_file)); $('[data-action=move]').toggleClass('d-none', !many_selected); $('[data-action=download]').toggleClass('d-none', !(many_selected && only_file)); $('[data-action=resize]').toggleClass('d-none', !(one_selected && only_image)); $('[data-action=crop]').toggleClass('d-none', !(one_selected && only_image)); $('[data-action=trash]').toggleClass('d-none', !many_selected); $('[data-action=open]').toggleClass('d-none', !one_selected || only_file); $('#multi_selection_toggle').toggleClass('d-none', usingWysiwygEditor() || !many_selected); $('#actions').toggleClass('d-none', selected.length === 0); */ } // ====================== // == Folder actions == // ====================== function goTo(new_dir) { if(!$('.dropdown-menu').hasClass('show')){ $('#working_dir').val(new_dir); loadItems(); } } function getPreviousDir() { var working_dir = $('#working_dir').val(); return working_dir.substring(0, working_dir.lastIndexOf('/')); } // ==================== // == Ajax actions == // ==================== function performLfmRequest(url, parameter, type) { var data = defaultParameters(); if (parameter != null) { $.each(parameter, function (key, value) { data[key] = value; }); } // console.log(lfm_route + '/' + url); // console.log(type); // console.log(data); return $.ajax({ type: 'GET', beforeSend: function(request) { var token = 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); displayErrorResponse(jqXHR); refreshFoldersAndItems(); }); } function displayErrorResponse(jqXHR) { notify('
    ' + jqXHR.responseText + '
    '); }; function isJSON(text) { if (typeof text!=="string"){ return false; } try{ JSON.parse(text); return true; } catch (error){ return false; } } var refreshFoldersAndItems = function (data) { loadItems(); if(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('
    ') : data; notify(data); } } }; var hideNavAndShowEditor = function (data, callback = false) { // $('#nav-buttons > ul').addClass('d-none'); //console.log(data); notify(data, callback); }; function loadItems() { loading(true); 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 toprev = $('
    '); $(lfm_content).html('').append(toprev); 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')){ preview_item(item); }else{ if($(this).hasClass('file-item')){ $(this).find('.lfm-control-input').click(); } } } else { goTo(item.url); } } if($(e.target).hasClass('lfm-control-input')){ if(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 = $('
    ').addClass('file-item-img').css('background-image', 'url("' + item.thumb_url + '?timestamp=' + item.time + '")'); var dot = $('').addClass('badge badge-dot badge-default indicator').css({"background-color":item.color}); } else { var image = $('
    ').addClass('file-item-icon fa ' + item.icon).css({"color":item.color}); var dot = $('').addClass('badge badge-dot badge-default indicator').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); template.find('.file-item-name').text(item.name); template.find('.file-item-name').append(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 = $('
  • ').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 goTo('/' + validSegments.slice(0, 1 + index).join('/')); }); } $('#lfm_breadcrumbs > ol').append(li); }); var atRootFolder = getPreviousDir() == ''; $('#to-previous').toggleClass('d-none', atRootFolder); $('#to-previous').click(function () { var previous_dir = getPreviousDir(); if (previous_dir == '') return; goTo(previous_dir); }); loading(false); toggleActions(); }); } function loading(show_loading) { $('#lfm_loading').toggleClass('d-none', !show_loading); } function createFolder(folder_name) { performLfmRequest('newfolder', {name: folder_name}) .done(refreshFoldersAndItems); } // ================================== // == File Actions == // ================================== function rename(item) { dialog(lang['message-rename'], item.name, function (new_name) { performLfmRequest('rename', { file: item.name, new_name: new_name }).done(refreshFoldersAndItems); }); } function colors(item) { performLfmRequest('colorhue', {color: item.color}) .done(function (data) { hideNavAndShowEditor(data, function () { performLfmRequest('docolorhue', { file: item.name, new_color: $('#notify').find('input').val(), }).done(refreshFoldersAndItems); }); }); } function trash(items) { notify(lang['message-delete'], function () { var d = performLfmRequest('delete', { items: items.map(function (item) { return item.name; }) }).done(refreshFoldersAndItems); // console.log(d); }); } function crop(item) { performLfmRequest('crop', {img: item.name}) .done(hideNavAndShowEditor); } function resize(item) { performLfmRequest('resize', {img: item.name}) .done(hideNavAndShowEditor); } function move(items) { performLfmRequest('move', { items: items.map(function (item) { return item.name; }) }) .done(refreshFoldersAndItems); } function download(items) { items.forEach(function (item, index) { var data = defaultParameters(); data['file'] = item.name; var token = getUrlParam('token'); if (token) { data['token'] = token; } setTimeout(function () { location.href = lfm_route + '/download?' + $.param(data); }, index * 100); }); } function open(item) { goTo(item.url); } function preview_item(item) { //var items = []; //items.push(item); preview(item); } function preview(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 = $('
    ').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); callback = function () { 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(refreshFoldersAndItems); }; /* if (item.thumb_url) { media.find('.carousel-image').css('background-image', 'url(\'' + item.url + '?timestamp=' + item.time + '\')'); } else { media.find('.carousel-image').css('width', '50vh').append($('
    ').addClass('mime-icon ico-' + item.icon)); } media.find('.carousel-label') .append($('')); */ notify(media, callback); } /*function preview(items) { var carousel = $('#carouselTemplate').clone().attr('id', 'previewCarousel').removeClass('d-none'); var imageTemplate = carousel.find('.carousel-item').clone().removeClass('active'); var indicatorTemplate = carousel.find('.carousel-indicators > li').clone().removeClass('active'); carousel.children('.carousel-inner').html(''); carousel.children('.carousel-indicators').html(''); carousel.children('.carousel-indicators,.carousel-control-prev,.carousel-control-next').toggle(items.length > 1); items.forEach(function (item, index) { var carouselItem = imageTemplate.clone() .addClass(index === 0 ? 'active' : ''); if (item.thumb_url) { carouselItem.find('.carousel-image').css('background-image', 'url(\'' + item.url + '?timestamp=' + item.time + '\')'); } else { carouselItem.find('.carousel-image').css('width', '50vh').append($('
    ').addClass('mime-icon ico-' + item.icon)); } carouselItem.find('.carousel-label').attr('target', '_blank').attr('href', item.url) .append(item.name) .append($('')); carousel.children('.carousel-inner').append(carouselItem); var carouselIndicator = indicatorTemplate.clone() .addClass(index === 0 ? 'active' : '') .attr('data-slide-to', index); carousel.children('.carousel-indicators').append(carouselIndicator); }); // carousel swipe control var touchStartX = null; carousel.on('touchstart', function (event) { var e = event.originalEvent; if (e.touches.length == 1) { var touch = e.touches[0]; touchStartX = touch.pageX; } }).on('touchmove', function (event) { var e = event.originalEvent; if (touchStartX != null) { var touchCurrentX = e.changedTouches[0].pageX; if ((touchCurrentX - touchStartX) > 60) { touchStartX = null; carousel.carousel('prev'); } else if ((touchStartX - touchCurrentX) > 60) { touchStartX = null; carousel.carousel('next'); } } }).on('touchend', function () { touchStartX = null; }); // end carousel swipe control notify(carousel); }*/ /* function use(items) { function useTinymce3(url) { if (!usingTinymce3()) { return; } var win = tinyMCEPopup.getWindowArg("window"); win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = url; if (typeof(win.ImageDialog) != "undefined") { // Update image dimensions if (win.ImageDialog.getImageData) { win.ImageDialog.getImageData(); } // Preview if necessary if (win.ImageDialog.showPreviewImage) { win.ImageDialog.showPreviewImage(url); } } tinyMCEPopup.close(); } function useTinymce4AndColorbox(url) { if (!usingTinymce4AndColorbox()) { return; } parent.document.getElementById(getUrlParam('field_name')).value = url; if(typeof parent.tinyMCE !== "undefined") { parent.tinyMCE.activeEditor.windowManager.close(); } if(typeof parent.$.fn.colorbox !== "undefined") { parent.$.fn.colorbox.close(); } } function useCkeditor3(url) { if (!usingCkeditor3()) { return; } if (window.opener) { // Popup window.opener.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url); } else { // Modal (in iframe) parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorFuncNum'), url); parent.CKEDITOR.tools.callFunction(getUrlParam('CKEditorCleanUpFuncNum')); } } function useFckeditor2(url) { if (!usingFckeditor2()) { return; } var p = url; var w = data['Properties']['Width']; var h = data['Properties']['Height']; window.opener.SetUrl(p,w,h); } var url = items[0].url; var callback = getUrlParam('callback'); var useFileSucceeded = true; if (usingWysiwygEditor()) { useTinymce3(url); useTinymce4AndColorbox(url); useCkeditor3(url); useFckeditor2(url); } else if (callback && window[callback]) { window[callback](getSelectedItems()); } else if (callback && parent[callback]) { parent[callback](getSelecteditems()); } else if (window.opener) { // standalone button or other situations window.opener.SetUrl(getSelectedItems()); } else { useFileSucceeded = false; } if (useFileSucceeded) { if (window.opener) { window.close(); } } else { console.log('window.opener not found'); // No editor found, open/download file using browser's default method window.open(url); } } //end useFile // ================================== // == WYSIWYG Editors Check == // ================================== function usingTinymce3() { return !!window.tinyMCEPopup; } function usingTinymce4AndColorbox() { return !!getUrlParam('field_name'); } function usingCkeditor3() { return !!getUrlParam('CKEditor') || !!getUrlParam('CKEditorCleanUpFuncNum'); } function usingFckeditor2() { return window.opener && typeof data != 'undefined' && data['Properties']['Width'] != ''; } function usingWysiwygEditor() { return usingTinymce3() || usingTinymce4AndColorbox() || usingCkeditor3() || usingFckeditor2(); } */ // ================================== // == Others == // ================================== function getUrlParam(paramName) { var reParam = new RegExp('(?:[\?&]|&)' + paramName + '=([^&]+)', 'i'); var match = window.location.search.match(reParam); return ( match && match.length > 1 ) ? match[1] : null; } function defaultParameters() { return { working_dir: $('#working_dir').val(), type: $('#type').val() }; } function notImp() { notify('Not yet implemented!'); } function notify(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); } function dialog(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); }