2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/client/ui/modal.js

59 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-02-07 13:23:52 +00:00
const $ = require('jquery');
const bootstrap = require('bootstrap');
module.exports = class Modal {
2018-02-09 12:55:55 +00:00
constructor({ title, body, primary, secondary }) {
2018-02-07 13:23:52 +00:00
Object.assign(this, arguments[0]);
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">${title}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
${body}
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>`).appendTo(document.body);
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.show();
}
2018-02-09 12:55:55 +00:00
addPrimary(label, action) {
2018-02-07 13:23:52 +00:00
this.$primary = $(`<button type="button" class="btn btn-primary">
${label}</button>`)
.appendTo(this.$modal.find('.modal-footer'))
.on('click', () => action(this));
}
2018-02-09 12:55:55 +00:00
addSecondary(label, action) {
2018-02-07 13:23:52 +00:00
this.$primary = $(`<button type="button" class="btn btn-secondary">
${label}</button>`)
.appendTo(this.$modal.find('.modal-footer'))
.on('click', () => action(this));
}
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);
}
}