2
0
mirror of https://github.com/frappe/books.git synced 2025-01-11 18:38:47 +00:00
books/client/ui/modal.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-02-07 13:23:52 +00:00
const $ = require('jquery');
const bootstrap = require('bootstrap'); // eslint-disable-line
const Observable = require('frappejs/utils/observable');
2018-02-07 13:23:52 +00:00
module.exports = class Modal extends Observable {
2018-02-09 12:55:55 +00:00
constructor({ title, body, primary, secondary }) {
super();
2018-02-07 13:23:52 +00:00
Object.assign(this, arguments[0]);
this.make();
this.show();
}
make() {
2018-02-07 13:23:52 +00:00
this.$modal = $(`<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">${this.title}</h5>
2018-02-07 13:23:52 +00:00
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
${this.getBodyHTML()}
2018-02-07 13:23:52 +00:00
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>`).appendTo(document.body);
2018-02-15 16:39:50 +00:00
this.modal = this.$modal.get(0);
2018-02-07 13:23:52 +00:00
2018-02-09 12:55:55 +00:00
if (this.primary) {
this.addPrimary(this.primary.label, this.primary.action);
2018-02-07 13:23:52 +00:00
}
2018-02-09 12:55:55 +00:00
if (this.secondary) {
this.addSecondary(this.secondary.label, this.secondary.action);
2018-02-07 13:23:52 +00:00
}
this.$modal.on('hidden.bs.modal', () => this.trigger('hide'));
2018-02-15 16:39:50 +00:00
this.$modal.on('shown.bs.modal', () => {
this.trigger('show');
});
}
getBodyHTML() {
return this.body || '';
2018-02-07 13:23:52 +00:00
}
2018-02-09 12:55:55 +00:00
addPrimary(label, action) {
return $(`<button type="button" class="btn btn-primary">
2018-02-07 13:23:52 +00:00
${label}</button>`)
.appendTo(this.$modal.find('.modal-footer'))
.on('click', () => action(this));
}
2018-02-09 12:55:55 +00:00
addSecondary(label, action) {
return $(`<button type="button" class="btn btn-secondary">
2018-02-07 13:23:52 +00:00
${label}</button>`)
.appendTo(this.$modal.find('.modal-footer'))
.on('click', () => action(this));
}
setTitle(title) {
this.$modal.find('.modal-title').text(title);
}
2018-02-07 13:23:52 +00:00
show() {
this.$modal.modal('show');
}
hide() {
this.$modal.modal('hide');
}
2018-02-09 12:55:55 +00:00
getBody() {
2018-02-07 13:23:52 +00:00
return this.$modal.find('.modal-body').get(0);
}
}