2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-modal
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2020-01-23 10:18:41 +01:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2020-06-08 16:08:50 +02:00
|
|
|
import { View } from '@converse/skeletor/src/view.js';
|
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
2020-01-23 10:18:41 +01:00
|
|
|
import { render } from 'lit-html';
|
2020-09-10 07:33:00 +02:00
|
|
|
import { __ } from './i18n';
|
2020-01-23 10:18:41 +01:00
|
|
|
import bootstrap from "bootstrap.native";
|
2020-04-23 13:22:31 +02:00
|
|
|
import { converse } from "@converse/headless/converse-core";
|
2020-01-23 10:18:41 +01:00
|
|
|
import log from "@converse/headless/log";
|
|
|
|
import tpl_alert_component from "templates/alert.js";
|
|
|
|
import tpl_alert_modal from "templates/alert_modal.js";
|
|
|
|
import tpl_prompt from "templates/prompt.js";
|
2018-05-09 10:09:42 +02:00
|
|
|
|
2020-01-23 10:18:41 +01:00
|
|
|
const { sizzle } = converse.env;
|
2019-04-06 22:10:00 +02:00
|
|
|
const u = converse.env.utils;
|
2018-05-09 10:09:42 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
let _converse;
|
2018-05-09 10:09:42 +02:00
|
|
|
|
2019-07-04 14:12:12 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
export const BootstrapModal = View.extend({
|
|
|
|
className: "modal",
|
|
|
|
events: {
|
|
|
|
'click .nav-item .nav-link': 'switchTab'
|
|
|
|
},
|
2019-07-04 14:12:12 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
initialize () {
|
|
|
|
this.render()
|
|
|
|
|
|
|
|
this.el.setAttribute('tabindex', '-1');
|
|
|
|
this.el.setAttribute('role', 'dialog');
|
|
|
|
this.el.setAttribute('aria-hidden', 'true');
|
|
|
|
const label_id = this.el.querySelector('.modal-title').getAttribute('id');
|
|
|
|
label_id && this.el.setAttribute('aria-labelledby', label_id);
|
|
|
|
|
|
|
|
this.insertIntoDOM();
|
|
|
|
const Modal = bootstrap.Modal;
|
|
|
|
this.modal = new Modal(this.el, {
|
2020-08-27 21:23:00 +02:00
|
|
|
backdrop: true,
|
2020-03-06 11:41:16 +01:00
|
|
|
keyboard: true
|
|
|
|
});
|
|
|
|
this.el.addEventListener('hide.bs.modal', () => u.removeClass('selected', this.trigger_el), false);
|
|
|
|
},
|
|
|
|
|
|
|
|
insertIntoDOM () {
|
|
|
|
const container_el = _converse.chatboxviews.el.querySelector("#converse-modals");
|
|
|
|
container_el.insertAdjacentElement('beforeEnd', this.el);
|
|
|
|
},
|
|
|
|
|
|
|
|
switchTab (ev) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
sizzle('.nav-link.active', this.el).forEach(el => {
|
|
|
|
u.removeClass('active', this.el.querySelector(el.getAttribute('href')));
|
|
|
|
u.removeClass('active', el);
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2020-03-06 11:41:16 +01:00
|
|
|
u.addClass('active', ev.target);
|
|
|
|
u.addClass('active', this.el.querySelector(ev.target.getAttribute('href')))
|
|
|
|
},
|
|
|
|
|
|
|
|
alert (message, type='primary') {
|
|
|
|
const body = this.el.querySelector('.modal-alert');
|
|
|
|
if (body === null) {
|
|
|
|
log.error("Could not find a .modal-alert element in the modal to show an alert message in!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME: Instead of adding the alert imperatively, we should
|
|
|
|
// find a way to let the modal rerender with an alert message
|
|
|
|
render(tpl_alert_component({'type': `alert-${type}`, 'message': message}), body);
|
|
|
|
const el = body.firstElementChild;
|
|
|
|
setTimeout(() => {
|
|
|
|
u.addClass('fade-out', el);
|
|
|
|
setTimeout(() => u.removeElement(el), 600);
|
|
|
|
}, 5000);
|
|
|
|
},
|
|
|
|
|
|
|
|
show (ev) {
|
|
|
|
if (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.trigger_el = ev.target;
|
|
|
|
this.trigger_el.classList.add('selected');
|
|
|
|
}
|
|
|
|
this.modal.show();
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-10-21 18:32:47 +02:00
|
|
|
converse.env.BootstrapModal = BootstrapModal; // expose to plugins
|
2019-09-24 15:33:41 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
export const Confirm = BootstrapModal.extend({
|
|
|
|
events: {
|
|
|
|
'submit .confirm': 'onConfimation'
|
|
|
|
},
|
2019-09-24 15:33:41 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
initialize () {
|
|
|
|
this.confirmation = u.getResolveablePromise();
|
|
|
|
BootstrapModal.prototype.initialize.apply(this, arguments);
|
|
|
|
this.listenTo(this.model, 'change', this.render)
|
|
|
|
this.el.addEventListener('closed.bs.modal', () => this.confirmation.reject(), false);
|
|
|
|
},
|
|
|
|
|
|
|
|
toHTML () {
|
|
|
|
return tpl_prompt(this.model.toJSON());
|
|
|
|
},
|
|
|
|
|
|
|
|
afterRender () {
|
|
|
|
if (!this.close_handler_registered) {
|
|
|
|
this.el.addEventListener('closed.bs.modal', () => {
|
|
|
|
if (!this.confirmation.isResolved) {
|
|
|
|
this.confirmation.reject()
|
2019-09-24 15:33:41 +02:00
|
|
|
}
|
2020-03-06 11:41:16 +01:00
|
|
|
}, false);
|
|
|
|
this.close_handler_registered = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onConfimation (ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
const form_data = new FormData(ev.target);
|
2020-04-14 17:18:58 +02:00
|
|
|
const fields = (this.model.get('fields') || [])
|
|
|
|
.map(field => {
|
|
|
|
const value = form_data.get(field.name).trim();
|
|
|
|
field.value = value;
|
|
|
|
if (field.challenge) {
|
|
|
|
field.challenge_failed = (value !== field.challenge);
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (fields.filter(c => c.challenge_failed).length) {
|
|
|
|
this.model.set('fields', fields);
|
|
|
|
// Setting an array doesn't trigger a change event
|
|
|
|
this.model.trigger('change');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.confirmation.resolve(fields);
|
2020-03-06 11:41:16 +01:00
|
|
|
this.modal.hide();
|
|
|
|
}
|
|
|
|
});
|
2019-09-24 15:33:41 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
export const Alert = BootstrapModal.extend({
|
|
|
|
initialize () {
|
|
|
|
BootstrapModal.prototype.initialize.apply(this, arguments);
|
|
|
|
this.listenTo(this.model, 'change', this.render)
|
|
|
|
},
|
2019-09-24 15:33:41 +02:00
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
toHTML () {
|
|
|
|
return tpl_alert_modal(Object.assign({__}, this.model.toJSON()));
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
|
2020-03-06 11:41:16 +01:00
|
|
|
converse.plugins.add('converse-modal', {
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
_converse = this._converse
|
2019-09-24 15:33:41 +02:00
|
|
|
|
|
|
|
/************************ BEGIN Event Listeners ************************/
|
2019-11-21 14:48:38 +01:00
|
|
|
_converse.api.listen.on('disconnect', () => {
|
|
|
|
const container = document.querySelector("#converse-modals");
|
2018-10-23 03:41:38 +02:00
|
|
|
if (container) {
|
|
|
|
container.innerHTML = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN API ************************/
|
|
|
|
// We extend the default converse.js API to add methods specific to MUC chat rooms.
|
2020-04-14 17:18:58 +02:00
|
|
|
let alert;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(_converse.api, {
|
2019-09-24 15:33:41 +02:00
|
|
|
/**
|
|
|
|
* Show a confirm modal to the user.
|
|
|
|
* @method _converse.api.confirm
|
|
|
|
* @param { String } title - The header text for the confirmation dialog
|
|
|
|
* @param { (String[]|String) } messages - The text to show to the user
|
2020-04-14 17:18:58 +02:00
|
|
|
* @param { Array<Field> } fields - An object representing a fields presented to the user.
|
|
|
|
* @property { String } Field.label - The form label for the input field.
|
|
|
|
* @property { String } Field.name - The name for the input field.
|
|
|
|
* @property { String } [Field.challenge] - A challenge value that must be provided by the user.
|
|
|
|
* @property { String } [Field.placeholder] - The placeholder for the input field.
|
|
|
|
* @property { Boolean} [Field.required] - Whether the field is required or not
|
|
|
|
* @returns { Promise<Array|false> } A promise which resolves with an array of
|
|
|
|
* filled in fields or `false` if the confirm dialog was closed or canceled.
|
2019-09-24 15:33:41 +02:00
|
|
|
*/
|
2020-04-14 17:18:58 +02:00
|
|
|
async confirm (title, messages=[], fields=[]) {
|
2020-10-01 11:13:13 +02:00
|
|
|
if (typeof messages === 'string') {
|
2019-09-24 15:33:41 +02:00
|
|
|
messages = [messages];
|
|
|
|
}
|
2020-04-14 17:18:58 +02:00
|
|
|
const model = new Model({title, messages, fields, 'type': 'confirm'})
|
|
|
|
const confirm = new Confirm({model});
|
2019-09-24 15:33:41 +02:00
|
|
|
confirm.show();
|
2020-04-14 17:18:58 +02:00
|
|
|
let result;
|
2019-09-24 15:33:41 +02:00
|
|
|
try {
|
2020-04-14 17:18:58 +02:00
|
|
|
result = await confirm.confirmation;
|
2019-09-24 15:33:41 +02:00
|
|
|
} catch (e) {
|
2020-04-14 17:18:58 +02:00
|
|
|
result = false;
|
2019-09-24 15:33:41 +02:00
|
|
|
}
|
2020-04-14 17:18:58 +02:00
|
|
|
confirm.remove();
|
|
|
|
return result;
|
2019-09-24 15:33:41 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a prompt modal to the user.
|
|
|
|
* @method _converse.api.prompt
|
|
|
|
* @param { String } title - The header text for the prompt
|
|
|
|
* @param { (String[]|String) } messages - The prompt text to show to the user
|
|
|
|
* @param { String } placeholder - The placeholder text for the prompt input
|
2020-04-14 17:18:58 +02:00
|
|
|
* @returns { Promise<String|false> } A promise which resolves with the text provided by the
|
2019-09-24 15:33:41 +02:00
|
|
|
* user or `false` if the user canceled the prompt.
|
|
|
|
*/
|
|
|
|
async prompt (title, messages=[], placeholder='') {
|
2020-10-01 11:13:13 +02:00
|
|
|
if (typeof messages === 'string') {
|
2019-09-24 15:33:41 +02:00
|
|
|
messages = [messages];
|
|
|
|
}
|
2020-04-14 17:18:58 +02:00
|
|
|
const model = new Model({
|
|
|
|
title,
|
|
|
|
messages,
|
|
|
|
'fields': [{
|
|
|
|
'name': 'reason',
|
2019-09-24 15:33:41 +02:00
|
|
|
'placeholder': placeholder,
|
2020-04-14 17:18:58 +02:00
|
|
|
}],
|
|
|
|
'type': 'prompt'
|
|
|
|
})
|
|
|
|
const prompt = new Confirm({model});
|
2019-09-24 15:33:41 +02:00
|
|
|
prompt.show();
|
2020-04-14 17:18:58 +02:00
|
|
|
let result;
|
2019-09-24 15:33:41 +02:00
|
|
|
try {
|
2020-04-14 17:18:58 +02:00
|
|
|
result = (await prompt.confirmation).pop()?.value;
|
2019-09-24 15:33:41 +02:00
|
|
|
} catch (e) {
|
2020-04-14 17:18:58 +02:00
|
|
|
result = false;
|
2019-09-24 15:33:41 +02:00
|
|
|
}
|
2020-04-14 17:18:58 +02:00
|
|
|
prompt.remove();
|
|
|
|
return result;
|
2019-09-24 15:33:41 +02:00
|
|
|
},
|
|
|
|
|
2019-10-31 14:42:28 +01:00
|
|
|
/**
|
|
|
|
* Show an alert modal to the user.
|
|
|
|
* @method _converse.api.alert
|
|
|
|
* @param { ('info'|'warn'|'error') } type - The type of alert.
|
2019-09-24 15:33:41 +02:00
|
|
|
* @param { String } title - The header text for the alert.
|
|
|
|
* @param { (String[]|String) } messages - The alert text to show to the user.
|
2019-10-31 14:42:28 +01:00
|
|
|
*/
|
|
|
|
alert (type, title, messages) {
|
2020-10-01 11:13:13 +02:00
|
|
|
if (typeof messages === 'string') {
|
2019-10-31 14:42:28 +01:00
|
|
|
messages = [messages];
|
2018-05-09 10:09:42 +02:00
|
|
|
}
|
2019-09-24 15:33:41 +02:00
|
|
|
let level;
|
2019-10-31 14:42:28 +01:00
|
|
|
if (type === 'error') {
|
2019-09-24 15:33:41 +02:00
|
|
|
level = 'alert-danger';
|
2019-10-31 14:42:28 +01:00
|
|
|
} else if (type === 'info') {
|
2019-09-24 15:33:41 +02:00
|
|
|
level = 'alert-info';
|
2019-10-31 14:42:28 +01:00
|
|
|
} else if (type === 'warn') {
|
2019-09-24 15:33:41 +02:00
|
|
|
level = 'alert-warning';
|
2019-10-31 14:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alert === undefined) {
|
2019-09-19 16:54:55 +02:00
|
|
|
const model = new Model({
|
2019-10-31 14:42:28 +01:00
|
|
|
'title': title,
|
|
|
|
'messages': messages,
|
2019-09-24 15:33:41 +02:00
|
|
|
'level': level,
|
|
|
|
'type': 'alert'
|
2019-10-31 14:42:28 +01:00
|
|
|
})
|
2020-03-06 11:41:16 +01:00
|
|
|
alert = new Alert({model});
|
2019-10-31 14:42:28 +01:00
|
|
|
} else {
|
|
|
|
alert.model.set({
|
|
|
|
'title': title,
|
|
|
|
'messages': messages,
|
2019-09-24 15:33:41 +02:00
|
|
|
'level': level
|
2019-10-31 14:42:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
alert.show();
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|