2023-04-26 17:20:10 +08:00
|
|
|
<template>
|
2023-05-29 22:34:07 +08:00
|
|
|
<div v-if="!paramsAreBlank"
|
2023-06-14 21:10:17 +08:00
|
|
|
class="sn-action-toolbar p-4 w-full fixed bottom-0 rounded-t-md"
|
2023-05-29 22:34:07 +08:00
|
|
|
:class="{ 'sn-action-toolbar--button-overflow': buttonOverflow }"
|
|
|
|
:style="`width: ${width}px; bottom: ${bottomOffset}px; transform: translateX(${leftOffset}px)`">
|
2023-06-14 21:10:17 +08:00
|
|
|
<div class="sn-action-toolbar__actions flex gap-4">
|
|
|
|
<div v-if="loading && !actions.length" class="sn-action-toolbar__action">
|
|
|
|
<a class="rounded flex items-center py-1.5 px-2.5 bg-sn-white color-sn-blue no-underline"></a>
|
2023-05-17 17:27:55 +08:00
|
|
|
</div>
|
2023-05-29 15:33:18 +08:00
|
|
|
<div v-if="!loading && actions.length === 0" class="sn-action-toolbar__message">
|
|
|
|
{{ i18n.t('action_toolbar.no_actions') }}
|
|
|
|
</div>
|
2023-06-16 20:17:46 +08:00
|
|
|
<div v-for="action in actions" :key="action.name" class="sn-action-toolbar__action shrink-0">
|
2023-06-14 21:10:17 +08:00
|
|
|
<a :class="`rounded flex gap-2 items-center py-1.5 px-2.5 bg-sn-white color-sn-blue no-underline ${action.button_class}`"
|
2023-04-26 21:51:22 +08:00
|
|
|
:href="(['link', 'remote-modal']).includes(action.type) ? action.path : '#'"
|
2023-04-26 17:20:10 +08:00
|
|
|
:id="action.button_id"
|
2023-05-28 07:52:24 +08:00
|
|
|
:title="action.label"
|
2023-04-26 17:20:10 +08:00
|
|
|
:data-url="action.path"
|
2023-06-16 21:14:58 +08:00
|
|
|
:data-target="action.target"
|
|
|
|
:data-toggle="action.type === 'modal' && 'modal'"
|
2023-04-26 17:20:10 +08:00
|
|
|
:data-object-type="action.item_type"
|
|
|
|
:data-object-id="action.item_id"
|
2023-04-26 21:51:22 +08:00
|
|
|
:data-action="action.type"
|
2023-05-17 20:12:40 +08:00
|
|
|
@click="doAction(action, $event)">
|
2023-06-14 21:10:17 +08:00
|
|
|
<i :class="action.icon"></i>
|
2023-05-17 20:12:40 +08:00
|
|
|
<span class="sn-action-toolbar__button-text">{{ action.label }}</span>
|
2023-04-26 17:20:10 +08:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-05-17 17:27:55 +08:00
|
|
|
import {debounce} from '../shared/debounce.js';
|
|
|
|
|
2023-04-26 17:20:10 +08:00
|
|
|
export default {
|
|
|
|
name: 'ActionToolbar',
|
|
|
|
props: {
|
|
|
|
actionsUrl: { type: String, required: true }
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
actions: [],
|
|
|
|
shown: false,
|
|
|
|
multiple: false,
|
|
|
|
params: {},
|
|
|
|
reloadCallback: null,
|
2023-05-29 15:33:18 +08:00
|
|
|
loaded: false,
|
2023-05-17 17:27:55 +08:00
|
|
|
loading: false,
|
2023-05-17 20:12:40 +08:00
|
|
|
width: 0,
|
2023-05-29 22:34:07 +08:00
|
|
|
bottomOffset: 0,
|
|
|
|
leftOffset: 0,
|
|
|
|
buttonOverflow: false
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
window.actionToolbarComponent = this;
|
|
|
|
window.onresize = this.setWidth;
|
2023-05-17 17:27:55 +08:00
|
|
|
|
|
|
|
this.debouncedFetchActions = debounce((params) => {
|
|
|
|
this.params = params;
|
|
|
|
|
|
|
|
$.get(`${this.actionsUrl}?${new URLSearchParams(this.params).toString()}`, (data) => {
|
|
|
|
this.actions = data.actions;
|
|
|
|
this.loading = false;
|
2023-05-29 22:34:07 +08:00
|
|
|
this.setButtonOverflow();
|
2023-05-17 17:27:55 +08:00
|
|
|
});
|
2023-05-17 20:12:40 +08:00
|
|
|
}, 10);
|
2023-04-26 17:20:10 +08:00
|
|
|
},
|
|
|
|
mounted() {
|
2023-05-29 22:34:07 +08:00
|
|
|
this.$nextTick(this.setWidth);
|
2023-06-19 20:10:55 +08:00
|
|
|
window.addEventListener('scroll', this.setLeftOffset);
|
2023-04-26 17:20:10 +08:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
delete window.actionToolbarComponent;
|
2023-06-19 20:10:55 +08:00
|
|
|
window.removeEventListener('scroll', this.setLeftOffset);
|
2023-04-26 17:20:10 +08:00
|
|
|
},
|
2023-05-29 15:33:18 +08:00
|
|
|
computed: {
|
|
|
|
paramsAreBlank() {
|
|
|
|
let values = Object.values(this.params);
|
|
|
|
|
|
|
|
if (values.length === 0) return true;
|
|
|
|
|
2023-05-30 18:50:33 +08:00
|
|
|
return !values.some((v) => v.length);
|
2023-05-29 15:33:18 +08:00
|
|
|
}
|
|
|
|
},
|
2023-04-26 17:20:10 +08:00
|
|
|
methods: {
|
|
|
|
setWidth() {
|
|
|
|
this.width = $(this.$el).parent().width();
|
2023-05-29 22:34:07 +08:00
|
|
|
this.setButtonOverflow();
|
|
|
|
},
|
|
|
|
setButtonOverflow() {
|
|
|
|
// detects if the last action button is outside the toolbar container
|
|
|
|
this.buttonOverflow = false;
|
|
|
|
|
|
|
|
this.$nextTick(() => {
|
2023-06-09 16:11:31 +08:00
|
|
|
if (
|
|
|
|
!(this.$el.getBoundingClientRect &&
|
|
|
|
document.querySelector('.sn-action-toolbar__action:last-child'))
|
|
|
|
) return;
|
2023-05-29 22:34:07 +08:00
|
|
|
|
|
|
|
let containerRect = this.$el.getBoundingClientRect();
|
|
|
|
let lastActionRect = document.querySelector('.sn-action-toolbar__action:last-child').getBoundingClientRect();
|
|
|
|
|
|
|
|
this.buttonOverflow = containerRect.left + containerRect.width < lastActionRect.left + lastActionRect.width;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setLeftOffset() {
|
|
|
|
this.leftOffset = -(window.pageXOffset || document.documentElement.scrollLeft);
|
2023-04-26 17:20:10 +08:00
|
|
|
},
|
2023-05-17 20:12:40 +08:00
|
|
|
setBottomOffset(pixels) {
|
2023-05-29 22:34:07 +08:00
|
|
|
this.bottomOffset = pixels;
|
2023-05-17 20:12:40 +08:00
|
|
|
},
|
2023-04-26 17:20:10 +08:00
|
|
|
fetchActions(params) {
|
2023-05-17 17:27:55 +08:00
|
|
|
this.loading = true;
|
|
|
|
this.debouncedFetchActions(params);
|
2023-04-26 17:20:10 +08:00
|
|
|
},
|
|
|
|
setReloadCallback(func) {
|
|
|
|
this.reloadCallback = func;
|
|
|
|
},
|
2023-05-17 20:12:40 +08:00
|
|
|
doAction(action, event) {
|
2023-04-26 17:20:10 +08:00
|
|
|
switch(action.type) {
|
|
|
|
case 'legacy':
|
|
|
|
// do nothing, this is handled by legacy code based on the button class
|
|
|
|
break;
|
|
|
|
case 'link':
|
2023-04-26 21:51:22 +08:00
|
|
|
// do nothing, already handled by href
|
|
|
|
break;
|
2023-06-16 21:14:58 +08:00
|
|
|
case 'modal':
|
|
|
|
// do nothihg, boostrap modal handled by data-toggle="modal" and data-target
|
2023-04-26 21:51:22 +08:00
|
|
|
case 'remote-modal':
|
|
|
|
// do nothing, handled by the data-action="remote-modal" binding
|
2023-04-26 17:20:10 +08:00
|
|
|
break;
|
2023-05-17 20:12:40 +08:00
|
|
|
case 'download':
|
|
|
|
event.stopPropagation();
|
|
|
|
window.location.href = action.path;
|
|
|
|
break;
|
2023-04-26 17:20:10 +08:00
|
|
|
case 'request':
|
2023-05-17 20:12:40 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
|
2023-04-26 17:20:10 +08:00
|
|
|
$.ajax({
|
|
|
|
type: action.request_method,
|
|
|
|
url: action.path,
|
|
|
|
data: this.params
|
|
|
|
}).done((data) => {
|
2023-04-26 21:51:22 +08:00
|
|
|
HelperModule.flashAlertMsg(data.responseJSON && data.responseJSON.message || data.message, 'success');
|
2023-04-26 17:20:10 +08:00
|
|
|
}).fail((data) => {
|
2023-04-26 21:51:22 +08:00
|
|
|
HelperModule.flashAlertMsg(data.responseJSON && data.responseJSON.message || data.message, 'danger');
|
2023-04-26 17:20:10 +08:00
|
|
|
}).complete(() => {
|
|
|
|
if (this.reloadCallback) this.reloadCallback();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|