Created
February 1, 2018 19:56
-
-
Save mitrallex/963374dd933c06d9fb03b8c81c568f26 to your computer and use it in GitHub Desktop.
laravel-file-hosting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| window.Vue = require('vue'); | |
| window.axios = require('axios'); | |
| let token = document.head.querySelector('meta[name="csrf-token"]'); | |
| if (token) { | |
| window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; | |
| } else { | |
| console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); | |
| } | |
| const app = new Vue({ | |
| el: '#app', | |
| directives: { | |
| 'autofocus': { | |
| inserted(el) { | |
| el.focus(); | |
| } | |
| } | |
| }, | |
| data: { | |
| files: {}, | |
| file: {}, | |
| pagination: {}, | |
| offset: 5, | |
| activeTab: 'image', | |
| isVideo: false, | |
| loading: false, | |
| formData: {}, | |
| fileName: '', | |
| attachment: '', | |
| editingFile: {}, | |
| deletingFile: {}, | |
| notification: false, | |
| showConfirm: false, | |
| modalActive: false, | |
| message: '', | |
| errors: {} | |
| }, | |
| methods: { | |
| isActive(tabItem) { | |
| return this.activeTab === tabItem; | |
| }, | |
| setActive(tabItem) { | |
| this.activeTab = tabItem; | |
| }, | |
| isCurrentPage(page) { | |
| return this.pagination.current_page === page; | |
| }, | |
| fetchFile(type, page) { | |
| this.loading = true; | |
| axios.get('files/' + type + '?page=' + page).then(result => { | |
| this.loading = false; | |
| this.files = result.data.data.data; | |
| this.pagination = result.data.pagination; | |
| }).catch(error => { | |
| console.log(error); | |
| this.loading = false; | |
| }); | |
| }, | |
| getFiles(type) { | |
| this.setActive(type); | |
| this.fetchFile(type); | |
| if (this.activeTab === 'video') { | |
| this.isVideo = true; | |
| } else { | |
| this.isVideo = false; | |
| } | |
| }, | |
| submitForm() { | |
| this.formData = new FormData(); | |
| this.formData.append('name', this.fileName); | |
| this.formData.append('file', this.attachment); | |
| axios.post('files/add', this.formData, {headers: {'Content-Type': 'multipart/form-data'}}) | |
| .then(response => { | |
| this.resetForm(); | |
| this.showNotification('File successfully upload!', true); | |
| this.fetchFile(this.activeTab); | |
| }) | |
| .catch(error => { | |
| this.errors = error.response.data.errors; | |
| this.showNotification(error.response.data.message, false); | |
| this.fetchFile(this.activeTab); | |
| }); | |
| }, | |
| addFile() { | |
| this.attachment = this.$refs.file.files[0]; | |
| }, | |
| prepareToDelete(file) { | |
| this.deletingFile = file; | |
| this.showConfirm = true; | |
| }, | |
| cancelDeleting() { | |
| this.deletingFile = {}; | |
| this.showConfirm = false; | |
| }, | |
| deleteFile() { | |
| axios.post('files/delete/' + this.deletingFile.id) | |
| .then(response => { | |
| this.showNotification('File successfully deleted!', true); | |
| this.fetchFile(this.activeTab, this.pagination.current_page); | |
| }) | |
| .catch(error => { | |
| this.errors = error.response.data.errors(); | |
| this.showNotification('Something went wrong! Please try again later.', false); | |
| this.fetchFile(this.activeTab, this.pagination.current_page); | |
| }); | |
| this.cancelDeleting(); | |
| }, | |
| editFile(file) { | |
| this.editingFile = file; | |
| }, | |
| endEditing(file) { | |
| this.editingFile = {}; | |
| if (file.name.trim() === '') { | |
| alert('Filename cannot be empty!'); | |
| this.fetchFile(this.activeTab); | |
| } else { | |
| let formData = new FormData(); | |
| formData.append('name', file.name); | |
| formData.append('type', file.type); | |
| formData.append('extension', file.extension); | |
| axios.post('files/edit/' + file.id, formData) | |
| .then(response => { | |
| if (response.data === true) { | |
| this.showNotification('Filename successfully changed!', true); | |
| var src = document.querySelector('[alt="' + file.name +'"]').getAttribute("src"); | |
| document.querySelector('[alt="' + file.name +'"]').setAttribute('src', src); | |
| } | |
| }) | |
| .catch(error => { | |
| this.errors = error.response.data.errors; | |
| this.showNotification(error.response.data.message, false); | |
| }); | |
| this.fetchFile(this.activeTab, this.pagination.current_page); | |
| } | |
| }, | |
| showNotification(text, success) { | |
| if (success === true) { | |
| this.clearErrors(); | |
| } | |
| var application = this; | |
| application.message = text; | |
| application.notification = true; | |
| setTimeout(function() { | |
| application.notification = false; | |
| }, 15000); | |
| }, | |
| showModal(file) { | |
| this.file = file; | |
| this.modalActive = true; | |
| }, | |
| closeModal() { | |
| this.modalActive = false; | |
| this.file = {}; | |
| }, | |
| changePage(page) { | |
| if (page > this.pagination.last_page) { | |
| page = this.pagination.last_page; | |
| } | |
| this.pagination.current_page = page; | |
| this.fetchFile(this.activeTab, page); | |
| }, | |
| resetForm() { | |
| this.formData = {}; | |
| this.fileName = ''; | |
| this.attachment = ''; | |
| }, | |
| anyError() { | |
| return Object.keys(this.errors).length > 0; | |
| }, | |
| clearErrors() { | |
| this.errors = {}; | |
| } | |
| }, | |
| mounted() { | |
| this.fetchFile(this.activeTab, this.pagination.current_page); | |
| }, | |
| computed: { | |
| pages() { | |
| let pages = []; | |
| let from = this.pagination.current_page - Math.floor(this.offset / 2); | |
| if (from < 1) { | |
| from = 1; | |
| } | |
| let to = from + this.offset - 1; | |
| if (to > this.pagination.last_page) { | |
| to = this.pagination.last_page; | |
| } | |
| while (from <= to) { | |
| pages.push(from); | |
| from++; | |
| } | |
| return pages; | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment