function ucFirst(str) { if (!str) return str; return str[0].toUpperCase() + str.slice(1); } let img_page = 1; let mediaScanUrl = '/attachment/index?limit=20&page='+img_page+'&type=2'; // let mediaScanUrl = 'scan.php'; class MediaModal { constructor (modal = true) { this.isInit = false; this.isModal = modal; this.modalHtml = ` `; this.response = [], this.currentPath = ''; this.breadcrumbsUrls = []; this.filemanager = null; this.breadcrumbs = null; this.fileList = null; this.mediaPath = "/public/media/"; this.type = "single"; } addModalHtml() { if (this.isModal) $("body").append(this.modalHtml); $("#MediaModal .save-btn").on("click", () => this.save()); } showUploadLoading() { $("#MediaModal .upload .status").html(`
Loading...
`); } hideUploadLoading() { $("#MediaModal .upload .status").html(''); $("#MediaModal .upload .file").prop('outerHTML', $("#MediaModal .upload .file").prop('outerHTML')); $(".filemanager input[type=file]").on("change", this.onUpload); } save() { let file = $("#MediaModal .files input:checked").eq(0).val(); if (this.targetInput) { $(this.targetInput).val(file).trigger("change"); } if (file.indexOf("//") == -1) { file = this.mediaPath + file; } if (this.targetThumb) { $(this.targetThumb).attr("src", file); } if (this.callback) { this.callback(file); } if (this.isModal) $("#MediaModal").modal('hide'); } init() { if (!this.isInit) { if (this.isModal) this.addModalHtml(); let self = this; // this.initGallery(); this.isInit = true; $(".filemanager input[type=file]").on("change", this.onUpload); $(window).trigger( "mediaModal:init", { type:this.type, targetInput:this.targetInput, targetThumb:this.targetThumb, callback:this.callback} ); } } open(element, callback) { if (element instanceof Element) { this.targetInput = element.dataset.targetInput; this.targetThumb = element.dataset.targetThumb; if (element.dataset.type) { this.type = element.dataset.type; } } else if (element) { this.targetInput = element.targetInput; this.targetThumb = element.targetThumb; if (element.type) { this.type = element.type; } } this.callback = callback; this.init(); if (this.isModal) $('#MediaModal').modal('show'); } initGallery() { this.filemanager = $('.filemanager'), this.breadcrumbs = $('.breadcrumbs'), this.fileList = this.filemanager.find('.data'); let _this = this; // Start by fetching the file data from scan.php with an AJAX request $.ajax({ type: 'get', url: mediaScanUrl, headers: createHeader(), data: {}, dataType: "json", success: function(res){ // console.log(res, '接口返回'); if (res.code && res.code != 0) { alert(res.msg) return ; // window.location.href = '/admin.php' } let list = res.data; let tempData = []; list.forEach(function (item, index) { tempData[index] = { "name": item.name, "path": item.url, "type": "file", "size": item.size } }) _this.response = [{ items:tempData, name:'', path:'', type:"folder" }], _this.currentPath = '', _this.breadcrumbsUrls = []; var folders = [], files = []; $(window).trigger('hashchange'); } }); // $.get('scan.php', function(data) { // //$.get('/this.filemanager/scan.php', function(data) { // // _this.response = [data], // _this.currentPath = '', // _this.breadcrumbsUrls = []; // // var folders = [], // files = []; // // $(window).trigger('hashchange'); // }); // This event listener monitors changes on the URL. We use it to // capture back/forward navigation in the browser. $(window).on('hashchange', function(){ _this.goto(window.location.hash); // We are triggering the event. This will execute // this function on page load, so that we show the correct folder: }); // Hiding and showing the search box this.filemanager.find('.search').click(function(){ var search = $(this); search.find('span').hide(); search.find('input[type=search]').show().focus(); }); // Listening for keyboard input on the search field. // We are using the "input" event which detects cut and paste // in addition to keyboard input. this.filemanager.find('input[type=search]').on('input', function(e){ var folders = []; var files = []; var value = this.value.trim(); if(value.length) { _this.filemanager.addClass('searching'); // Update the hash on every key stroke window.location.hash = 'search=' + value.trim(); } else { _this.filemanager.removeClass('searching'); window.location.hash = encodeURIComponent(_this.currentPath); } }).on('keyup', function(e){ // Clicking 'ESC' button triggers focusout and cancels the search var search = $(this); if(e.keyCode == 27) { search.trigger('focusout'); } }).focusout(function(e){ // Cancel the search var search = $(this); if(!search.val().trim().length) { window.location.hash = encodeURIComponent(_this.currentPath); search.hide(); search.parent().find('span').show(); } }); // Clicking on folders this.fileList.on('click', 'li.folders', function(e){ e.preventDefault(); var nextDir = $(this).find('a.folders').attr('href'); if(_this.filemanager.hasClass('searching')) { // Building the this.breadcrumbs _this.breadcrumbsUrls = _this.generateBreadcrumbs(nextDir); _this.filemanager.removeClass('searching'); _this.filemanager.find('input[type=search]').val('').hide(); _this.filemanager.find('span').show(); } else { _this.breadcrumbsUrls.push(nextDir); } window.location.hash = encodeURIComponent(nextDir); _this.currentPath = nextDir; }); // Clicking on this.breadcrumbs this.breadcrumbs.on('click', 'a', function(e){ e.preventDefault(); var index = _this.breadcrumbs.find('a').index($(this)), nextDir = _this.breadcrumbsUrls[index]; _this.breadcrumbsUrls.length = Number(index); window.location.hash = encodeURIComponent(nextDir); }); } // Navigates to the given hash (path) goto(hash) { hash = decodeURIComponent(hash).slice(1).split('='); let _this = this; if (hash.length) { var rendered = ''; // if hash has search in it if (hash[0] === 'search') { this.filemanager.addClass('searching'); rendered = _this.searchData(_this.response, hash[1].toLowerCase()); if (rendered.length) { this.currentPath = hash[0]; this.render(rendered); } else { this.render(rendered); } } // if hash is some path else if (hash[0].trim().length) { rendered = this.searchByPath(hash[0]); if (rendered.length) { this.currentPath = hash[0]; this.breadcrumbsUrls = this.generateBreadcrumbs(hash[0]); this.render(rendered); } else { this.currentPath = hash[0]; this.breadcrumbsUrls = this.generateBreadcrumbs(hash[0]); this.render(rendered); } } // if there is no hash else { this.currentPath = this.response[0].path; this.breadcrumbsUrls.push(this.response[0].path); this.render(this.searchByPath(this.response[0].path)); } } } // Splits a file path and turns it into clickable breadcrumbs _ generateBreadcrumbs(nextDir){ var path = nextDir.split('/').slice(0); for(var i=1;i= 0) { folders.push(d); } } else if(d.type === 'file') { if(d.name.toLowerCase().indexOf(searchTerms) >= 0) { files.push(d); } } }); }; _searchData(data, searchTerms); return {folders: folders, files: files}; } onUpload(event) { // console.log('上传按钮修改!', this.files); let file; if (this.files && this.files[0]) { Vvveb.MediaModal.showUploadLoading(); var reader = new FileReader(); reader.onload = imageIsLoaded; reader.readAsDataURL(this.files[0]); //reader.readAsBinaryString(this.files[0]); file = this.files[0]; } function imageIsLoaded(e) { let image = e.target.result; var formData = new FormData(); formData.append("file", file); formData.append("mediaPath", Vvveb.MediaModal.mediaPath + Vvveb.MediaModal.currentPath); formData.append("onlyFilename", true); formData.append("attachment_cate_id", 0); formData.append("type", 2); $.ajax({ type: "POST", url: '/attachment/uploadAndSave',//set your server side upload script url data: formData, headers: createHeader(), processData: false, contentType: false, success: function (res) { let data = res.data; Vvveb.MediaModal.addFile({ name:data.name, type:"file", path: data.url, size:data.size },true); Vvveb.MediaModal.hideUploadLoading(); var messageModal = new bootstrap.Modal(document.getElementById('message-modal'), { keyboard: false }); $('#message-modal').css('z-index', '1065'); $("#message-modal .modal-body").html('上传成功!'); messageModal.show() $(window).trigger( "mediaModal:upload", { type:this.type, targetInput:this.targetInput, targetThumb:this.targetThumb, callback:this.callback} ); }, error: function (data) { alert(data.msg); Vvveb.MediaModal.hideUploadLoading(); } }); } } addFile(f, selected) { let _this= this; // _this.bytesToSize(f.size) var fileSize = f.size, name = _this.escapeHTML(f.name), fileType = name.split('.'), icon = ''; fileType = fileType[fileType.length-1]; if (fileType == "jpg" || fileType == "jpeg" || fileType == "png" || fileType == "gif" || fileType == "svg" || fileType == "webp") { //icon = '
'; icon = ''; } else { icon = '.'+fileType+''; } //icon = '.'+fileType+''; var file = $('
  • \
  • '); let fileelement = file.appendTo(_this.fileList); if (selected) { $("input[type='radio'], input[type='checkbox']", fileelement).prop("checked", true); } } render(data) { var scannedFolders = [], scannedFiles = []; if(Array.isArray(data)) { data.forEach(function (d) { if (d.type === 'folder') { scannedFolders.push(d); } else if (d.type === 'file') { scannedFiles.push(d); } }); } else if(typeof data === 'object') { scannedFolders = data.folders; scannedFiles = data.files; } // Empty the old result and make the new one this.fileList.empty();//.hide(); if(!scannedFolders.length && !scannedFiles.length) { this.filemanager.find('.nothingfound').show(); } else { this.filemanager.find('.nothingfound').hide(); } let _this = this; if(scannedFolders.length) { scannedFolders.forEach(function(f) { var itemsLength = f.items.length, name = _this.escapeHTML(f.name), icon = ''; if(itemsLength) { icon = ''; } if(itemsLength == 1) { itemsLength += ' item'; } else if(itemsLength > 1) { itemsLength += ' items'; } else { itemsLength = 'Empty'; } var folder = $('
  • '+icon+'
    ' + name + ' ' + itemsLength + '
  • '); folder.appendTo(_this.fileList); }); } if(scannedFiles.length) { scannedFiles.forEach(function(f) { _this.addFile(f); }); } // Generate the breadcrumbs var url = ''; if(this.filemanager.hasClass('searching')){ url = 'Search results: '; this.fileList.removeClass('animated'); } else { this.fileList.addClass('animated'); this.breadcrumbsUrls.forEach(function (u, i) { var name = u.split('/'); if (i !== _this.breadcrumbsUrls.length - 1) { url += '' + name[name.length-1] + ' '; } else { url += '' + name[name.length-1] + ''; } }); } this.breadcrumbs.html(' home').append(url); // Show the generated elements this.fileList.animate({'display':'inline-block'}); } // This function escapes special html characters in names escapeHTML(text) { return text.replace(/\&/g,'&').replace(/\/g,'>'); } // Convert file sizes from bytes to human readable units bytesToSize(bytes) { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0 Bytes'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; } } /* export { MediaModal } */