Skip to content

Commit

Permalink
added fast attachment upload to improve system ux
Browse files Browse the repository at this point in the history
update detach word mistake fixed
  • Loading branch information
A1Gard committed Dec 28, 2024
1 parent d2e5dc4 commit 33a60eb
Show file tree
Hide file tree
Showing 13 changed files with 726 additions and 368 deletions.
14 changes: 12 additions & 2 deletions app/Http/Controllers/Admin/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ public function destroy(Attachment $item)
{
return parent::delete($item);
}
public function deattach(Attachment $item)
public function detach(Attachment $item)
{
$item->attachable_id = null;
$item->attachable_type = null;
$item->save();

logAdmin(__METHOD__,__CLASS__,$item->id);
if (request()->ajax()) {
return ['OK' => true , 'message' => __('As you wished detached successfully')];
}
return redirect()->back()
->with(['message' => __('As you wished deattached successfully')]);
->with(['message' => __('As you wished detached successfully')]);
}


Expand All @@ -132,5 +135,12 @@ public function update(Request $request, Attachment $item)
return $this->bringUp($request, $item);
}

public function attaching(Request $request){
$item = new Attachment();
$item = $this->save($item, $request);
logAdmin(__METHOD__,__CLASS__,$item->id);
return ['OK' => true,'data'=> $item,'message' => __('File uploaded successfully')];
}


}
5 changes: 5 additions & 0 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import './panel/prototypes.js';
import './panel/panel-window-loader.js';
import './panel/responsive-control.js';
import './panel/fast-edit.js';
import './panel/fast-attachment.js';
// import './panel/seo-analyzer.js';

// chartjs.defaults.defaultFontFamily = "Vazir";
Expand Down Expand Up @@ -123,6 +124,10 @@ app.component('menu-item-input', MenuItemInput);
import VueTimepicker from "./components/vueTimePicker.vue";
app.component('vue-time-picker', VueTimepicker);


import fastAttaching from "./components/FastAttaching.vue";
app.component('fast-attaching', fastAttaching);

/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
Expand Down
230 changes: 230 additions & 0 deletions resources/js/components/FastAttaching.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<template>
<div id="fast-attaching">

<i class="ri-add-line" id="new-attach" data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="Add new attachment" @click="isShowNew = !isShowNew"></i>

<div class="row mt-4" v-if="isShowNew">
<div class="col-md">
<input type="text" placeholder="Title" v-model="title" class="form-control">
</div>
<div class="col-md">
<input type="file" class="form-control" id="upload-attachment-file" ref="fileInput">
</div>
<div class="col-md-2">
<button class="btn btn-primary w-100"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="Upload"
@click="upload">
<i class="ri-upload-2-line"></i>
</button>
</div>
</div>

<div id="progress" v-if="isShowProgress">
Uploading {{ title }}
<div id="percent" :style="percentStyle"></div>
</div>

<ul class="list-group my-4">
<li v-for="(attach,i) in attachments" :key="attach.id" class="list-group-item">
<div class="row">
<div class="col-md-1 pt-1">
{{ i + 1 }}
</div>

<div class="col-md pt-1">
<span class="badge bg-success">
{{ attach.ext }}
</span>
</div>
<div class="col-md-4 pt-1">
<template v-if="xlang == null">
{{ attach.title }}
</template>
<template v-else>
{{ attach['title']?.[xlang] ?? attach.title }}
</template>
</div>

<div class="col-md-4 pt-1">
{{ attach.file }}
</div>
<div class="col-md-1">
<div class="btn btn-secondary btn-sm w-100" data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
data-bs-title="Detach"
@click="detach(attach.slug,i)"
>
<i class="ri-close-line"></i>
</div>
</div>
</div>
</li>
</ul>
</div>
</template>

<script>
import axios from 'axios';
export default {
name: "fast-attaching",
data() {
return {
title: '',
isShowNew: false,
isShowProgress: false,
percent: 0,
}
},
props: {
attachments: {
type: Array,
default: () => [],
},
xlang: {
default: null
},
uploadUrl: {
type: String,
default: 'http://127.0.0.1:8000/dashboard/attachments/attaching',
},
detachUrl: {
type: String,
default: 'http://127.0.0.1:8000/dashboard/attachments/attaching',
},
model: {
type: String,
default: 'App\\Models\\Post'
},
id: {
type: Number,
default: '1',
},
},
computed: {
percentStyle() {
return 'width:' + this.percent + '%;';
}
},
methods: {
async detach(slug, index) {
if (!confirm(window.TR.deleteConfirm)) {
return false;
}
try {
const response = await axios.get(this.detachUrl + slug);
if (response.data.OK) {
$toast.success(response.data.message);
this.attachments.splice(index, 1);
if (document.querySelector('#attach-number') != null){
document.querySelector('#attach-number').innerText = this.attachments.length;
}
this.$forceUpdate();
} else {
$toast.error("Detach problem!");
}
} catch (e) {
$toast.error(e.message);
}
},
async upload() {
console.log('upload start');
if (this.title.length < 2) {
this.$toast.error('Title is incorrect');
return false;
}
const fileInput = this.$refs.fileInput;
if (fileInput.files.length === 0) {
this.$toast.error('File is incorrect');
return false;
}
this.isShowNew = false;
this.isShowProgress = true;
this.percent = 0; // Reset the percent
const formData = new FormData();
formData.append('title', this.title);
formData.append('attachable_id', this.id);
formData.append('attachable_type', this.model);
formData.append('file', fileInput.files[0]); // Assuming only one file is selected
try {
const response = await axios.post(this.uploadUrl, formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
onUploadProgress: progressEvent => {
if (progressEvent.lengthComputable) {
this.percent = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
}
});
// Handle the response
console.log(response.data);
if (response.data.OK) {
this.$toast.success(response.data.message);
this.attachments.push(response.data.data); // Adjust based on your response
if (document.querySelector('#attach-number') != null){
document.querySelector('#attach-number').innerText = this.attachments.length;
}
this.title = '';
}
// Optionally, add the new attachment to the attachments array
} catch (error) {
console.error(error);
this.$toast.error('Upload failed!');
} finally {
this.isShowProgress = false;
}
}
}
}
</script>
<style scoped>
#fast-attaching {
}
#new-attach {
position: absolute;
inset-inline-end: 80px;
top: 0;
padding: 5px;
background: rgba(8, 255, 0, 0.2);
font-size: 30px;
border-radius: 0 0 7px 7px;
cursor: pointer;
&:hover {
background: rgba(8, 255, 0, 0.4);
}
}
#progress {
margin: 2rem 0;
border: 1px solid #ffffff22;
border-radius: 7px;
overflow: hidden;
text-align: center;
position: relative;
}
#percent {
background: rgba(8, 255, 0, 0.4);
position: absolute;
inset-inline-start: 0;
top: 0;
bottom: 0;
}
</style>
11 changes: 11 additions & 0 deletions resources/js/panel/fast-attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.addEventListener('DOMContentLoaded', function () {

let attachFrom = document.querySelector('#attaching-form');
document.querySelector('#attach-down')?.addEventListener('click', function () {
attachFrom.style.bottom = (window.innerHeight * -.5+'px');
});
document.querySelector('#show-attach-form')?.addEventListener('click', function (e) {
e.preventDefault();
attachFrom.style.bottom = ('0px');
});
});
9 changes: 6 additions & 3 deletions resources/lang/fa.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"Area design": "طراحی محیط",
"Areas": "محیط‌ها ",
"As you wished created successfully": "همانطور که شما مایل بودید ایجاد شد",
"As you wished deattached successfully": "همانطور که شما مایل بودید از پیوست خارج شد",
"As you wished detached successfully": "همانطور که شما مایل بودید از پیوست خارج شد",
"As you wished removed successfully": "همانطور که شما مایل بودید حذف شد",
"As you wished restored successfully": "همانطور که شما مایل بودید بازیافت شد",
"As you wished sort saved": "همانطور که شما مایل بودید مرتب شدند",
Expand Down Expand Up @@ -137,6 +137,7 @@
"Clips list": "فهرست کلیپ‌ها",
"Close": "بستن",
"Code": "کد",
"Collapse attachment form": "بستن فرم پیوست‌ها",
"Colleague": "همکار",
"Command ignored, segment part exists!": "",
"Comment": "دیدگاه",
Expand Down Expand Up @@ -170,7 +171,6 @@
"Date": "تاریخ",
"Date of born": "تاریخ تولد",
"Datetime": "تاریخ و زمان",
"Deattach": "عدم پیوست",
"Default": "پیش‌فرض",
"Default admin email is :E1 (developer) or :E2 (admin) and default password is: :P": "در نگارش آزمایشی رایانامه توسعه دهنده :E1 است و رایانامه مدیر عادی :E2 و گذرواژه هر دو :P است",
"DefaultFooter": "فوتر پیش‌فرض",
Expand All @@ -181,7 +181,8 @@
"Description Text": "نوشته توضیحات",
"Design": "طراحی",
"Design :AREA": "طراحی :AREA",
"Design guide": "",
"Design guide": "راهنمای طراحی",
"Detach": "عدم پیوست",
"Detail": "جزئیات",
"Developer guide": "راهنمای توسعه",
"Devices": "دستگاه‌ها",
Expand Down Expand Up @@ -244,6 +245,7 @@
"File ext": "پسوند پرونده",
"File name": "نام پرونده",
"File size": "حجم پرونده",
"File uploaded successfully": "پرونده با موفقیت بارگزاری شد",
"Filter": "صافی",
"Find more": "موارد بیشتر",
"Flag": "پرچم",
Expand Down Expand Up @@ -271,6 +273,7 @@
"GuestLogs list": "فهرست لاگ میهمان ها",
"Height": "قد",
"Hello world": "سلام دنیا",
"Hide in menu": "عدم نمایش در فهرست (منو)",
"Home": "خانه",
"ID": "",
"Icon": "نماد",
Expand Down
Loading

0 comments on commit 33a60eb

Please sign in to comment.