224 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-11-16 10:21:14 +03:00
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import NProgressAxios from './nprogress-axios';
export default class BulkAction {
constructor(path) {
//This path use action url
this['path'] = path;
// Count selected items
this['count'] = '';
// Select action value ex: delete, export
this['value'] = '*';
// Select action message
this['message'] = '';
// Action type
this['type'] = '';
2019-11-16 10:21:14 +03:00
// Bulk action view status
this['show'] = false;
// Bulk action modal status
this['modal'] = false;
// Bulk action modal action
this['loading'] = false;
// Selected item list
this['selected'] = [];
// Select all items
this['select_all'] = false;
}
// Change checkbox status
select() {
this.show = true;
this.select_all = false;
this.count = this.selected.length;
if (this.count == document.querySelectorAll('[data-bulk-action]').length) {
this.select_all = true;
}
2022-06-01 10:15:55 +03:00
if (! this.count) {
2019-11-16 10:21:14 +03:00
this.show = false;
2022-06-01 10:15:55 +03:00
this.hideSearchHTML();
2019-11-16 10:21:14 +03:00
}
}
// Select all items action
selectAll() {
this.show = false;
this.selected = [];
this.hideSearchHTML();
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
if (! this.select_all) {
2019-11-16 10:21:14 +03:00
this.show = true;
for (let input of document.querySelectorAll('[data-bulk-action]')) {
this.selected.push(input.getAttribute('value'));
}
}
this.count = this.selected.length;
}
2022-06-01 10:15:55 +03:00
change(type) {
2022-09-19 16:59:28 +03:00
let action = document.getElementById('index-bulk-actions-' + type);
2022-06-01 10:15:55 +03:00
this.value = type;
this.message = action.getAttribute('data-message');
2019-11-16 10:21:14 +03:00
if (typeof(this.message) == "undefined") {
this.message = '';
}
2022-06-01 10:15:55 +03:00
this.path = action.getAttribute('data-path');
this.type = '*';
2022-06-01 10:15:55 +03:00
if (action.getAttribute('data-type')) {
this.type = action.getAttribute('data-type');
}
2019-11-16 10:21:14 +03:00
return this.message;
}
// Selected item use action
action() {
2020-02-22 12:29:46 +03:00
if (this.value == '*') {
return;
}
2019-11-16 10:21:14 +03:00
this.loading = true;
// bwfore version 2.0.23
if (this.value == 'export') {
this.type = 'download';
}
switch (this.type) {
case 'download':
let download_promise = Promise.resolve(window.axios({
url: this.path,
method: 'POST',
data:{
'handle': this.value,
'selected': this.selected
},
responseType: 'blob',
}));
download_promise.then((response) => {
2021-05-23 18:45:33 +03:00
if (response.data.type != 'application/json') {
const blob = new Blob([response.data], {type: response.data.type});
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const contentDisposition = response.headers['content-disposition'];
let fileName = 'unknown';
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename=(.+)/);
if (fileNameMatch.length === 2) {
fileName = fileNameMatch[1];
}
}
2021-05-23 18:45:33 +03:00
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
this.loading = false;
this.modal = false;
this.value = '*';
this.clear();
return;
}
2021-05-23 18:45:33 +03:00
window.location.reload(false);
});
2022-06-01 10:15:55 +03:00
break;
default:
let type_promise = Promise.resolve(window.axios.post(this.path, {
2019-12-27 17:18:45 +03:00
'handle': this.value,
'selected': this.selected
}));
2019-12-27 17:18:45 +03:00
type_promise.then(response => {
2021-07-30 14:09:27 +01:00
if (response.data.redirect === true) {
window.location.reload(false);
2021-07-30 14:09:27 +01:00
} else if (typeof response.data.redirect === 'string') {
window.location.href = response.data.redirect;
2019-12-27 17:18:45 +03:00
}
})
.catch(error => {
//this.loading = false;
//this.modal = false;
//window.location.reload(false);
})
.finally(function () {
//window.location.reload(false);
});
2019-12-27 17:18:45 +03:00
}
2019-11-16 10:21:14 +03:00
}
// Selected items clear
clear() {
this.show = false;
this.select_all = false;
this.selected = [];
2022-06-01 10:15:55 +03:00
this.hideSearchHTML();
2019-11-16 10:21:14 +03:00
}
hideSearchHTML() {
setInterval(() => {
const search_box_html = document.querySelector('.js-search-box-hidden');
2022-06-01 10:15:55 +03:00
if (search_box_html) {
search_box_html.classList.add('d-none');
}
}, 5);
};
2019-11-16 10:21:14 +03:00
// Change enabled status
status(item_id, event, notify) {
2022-06-01 10:15:55 +03:00
let item = event.target;
let status = (event.target.checked) ? 'enable' : 'disable';
2019-11-16 10:21:14 +03:00
window.axios.get(this.path + '/' + item_id + '/' + status)
2019-11-16 10:21:14 +03:00
.then(response => {
2022-06-01 10:15:55 +03:00
let type = (response.data.success) ? 'success' : 'warning';
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
if (! response.data.success) {
2019-11-16 10:21:14 +03:00
if (item.checked) {
item.checked = false;
} else {
item.checked = true;
}
}
notify({
message: response.data.message,
timeout: 5000,
icon: 'fas fa-bell',
type
});
})
.catch(error => {
});
}
}