debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/confirm_modal.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
2.6 KiB
Vue
Raw Normal View History

2020-04-08 14:13:33 +05:30
<script>
2023-03-04 22:38:38 +05:30
import { GlModal } from '@gitlab/ui';
2020-04-08 14:13:33 +05:30
import { uniqueId } from 'lodash';
2023-03-04 22:38:38 +05:30
import SafeHtml from '~/vue_shared/directives/safe_html';
2020-10-24 23:57:45 +05:30
import csrf from '~/lib/utils/csrf';
2022-01-26 12:08:38 +05:30
import eventHub, { EVENT_OPEN_CONFIRM_MODAL } from './confirm_modal_eventhub';
import DomElementListener from './dom_element_listener.vue';
2020-04-08 14:13:33 +05:30
export default {
components: {
GlModal,
2022-01-26 12:08:38 +05:30
DomElementListener,
2020-04-08 14:13:33 +05:30
},
2021-01-29 00:20:46 +05:30
directives: {
SafeHtml,
},
2020-04-08 14:13:33 +05:30
props: {
selector: {
type: String,
required: true,
},
2021-01-03 14:25:43 +05:30
handleSubmit: {
type: Function,
required: false,
default: null,
},
2020-04-08 14:13:33 +05:30
},
data() {
return {
modalId: uniqueId('confirm-modal-'),
path: '',
method: '',
modalAttributes: {},
};
},
mounted() {
2022-01-26 12:08:38 +05:30
eventHub.$on(EVENT_OPEN_CONFIRM_MODAL, this.onOpenEvent);
},
destroyed() {
eventHub.$off(EVENT_OPEN_CONFIRM_MODAL, this.onOpenEvent);
2020-04-08 14:13:33 +05:30
},
methods: {
2022-01-26 12:08:38 +05:30
onButtonPress(e) {
const element = e.currentTarget;
if (!element.dataset.path) {
return;
}
const modalAttributes = element.dataset.modalAttributes
? JSON.parse(element.dataset.modalAttributes)
: {};
this.onOpenEvent({
path: element.dataset.path,
method: element.dataset.method,
modalAttributes,
});
},
onOpenEvent({ path, method, modalAttributes }) {
this.path = path;
this.method = method;
this.modalAttributes = modalAttributes;
this.openModal();
},
2020-04-08 14:13:33 +05:30
openModal() {
this.$refs.modal.show();
},
closeModal() {
this.$refs.modal.hide();
},
submitModal() {
2021-01-03 14:25:43 +05:30
if (this.handleSubmit) {
this.handleSubmit(this.path);
} else {
this.$refs.form.submit();
}
2020-04-08 14:13:33 +05:30
},
},
csrf,
};
</script>
<template>
2022-01-26 12:08:38 +05:30
<dom-element-listener :selector="selector" @click.prevent="onButtonPress">
<gl-modal
ref="modal"
:modal-id="modalId"
v-bind="modalAttributes"
@primary="submitModal"
@cancel="closeModal"
>
<form ref="form" :action="path" method="post">
<!-- Rails workaround for <form method="delete" />
2020-04-08 14:13:33 +05:30
https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/features/method.coffee
-->
2022-01-26 12:08:38 +05:30
<input type="hidden" name="_method" :value="method" />
<input type="hidden" name="authenticity_token" :value="$options.csrf.token" />
<div v-if="modalAttributes.messageHtml" v-safe-html="modalAttributes.messageHtml"></div>
<div v-else>{{ modalAttributes.message }}</div>
</form>
</gl-modal>
</dom-element-listener>
2020-04-08 14:13:33 +05:30
</template>