Skip to content

Instantly share code, notes, and snippets.

@dagpacket
Last active September 14, 2021 20:16
Show Gist options
  • Save dagpacket/ba836e95f40e5e25cfd68acdbf9c2b1e to your computer and use it in GitHub Desktop.
Save dagpacket/ba836e95f40e5e25cfd68acdbf9c2b1e to your computer and use it in GitHub Desktop.
bootstrap-show-modal.js
/**
* Author and copyright: Stefan Haack (https://shaack.com)
* Repository: https://github.com/shaack/bootstrap-show-modal
* License: MIT, see file 'LICENSE'
*/
(function ($) {
"use strict"
var i = 0
function Modal(props) {
this.props = {
title: "", // the dialog title html
body: "", // the dialog body html
footer: "", // the dialog footer html (mainly used for buttons)
modalClass: "fade", // Additional css for ".modal", "fade" for fade effect
modalDialogClass: "", // Additional css for ".modal-dialog", like "modal-lg" or "modal-sm" for sizing
options: { // The Bootstrap modal options as described here: https://getbootstrap.com/docs/4.0/components/modal/#options
backdrop: 'static' // disallow closing on click in the background
},
// Events:
onCreate: null, // Callback, called after the modal was created
onShown: null, // Callback, called after the modal was shown and completely faded in
onDispose: null, // Callback, called after the modal was disposed
onSubmit: null // Callback of $.showConfirm(), called after yes or no was pressed
}
for (var prop in props) {
// noinspection JSUnfilteredForInLoop
this.props[prop] = props[prop]
}
this.id = "bootstrap-show-modal-" + i
i++
this.show()
if (this.props.onCreate) {
this.props.onCreate(this)
}
}
Modal.prototype.createContainerElement = function () {
var self = this
this.element = document.createElement("div")
this.element.id = this.id
this.element.setAttribute("class", "modal " + this.props.modalClass)
this.element.setAttribute("tabindex", "-1")
this.element.setAttribute("role", "dialog")
this.element.setAttribute("aria-labelledby", this.id)
this.element.innerHTML = '<div class="modal-dialog ' + this.props.modalDialogClass + '" role="document">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<h5 class="modal-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"></div>' +
'<div class="modal-footer"></div>' +
'</div>' +
'</div>'
document.body.appendChild(this.element)
this.titleElement = this.element.querySelector(".modal-title")
this.bodyElement = this.element.querySelector(".modal-body")
this.footerElement = this.element.querySelector(".modal-footer")
$(this.element).on('hidden.bs.modal', function () {
self.dispose()
})
$(this.element).on('shown.bs.modal', function () {
if(self.props.onShown) {
self.props.onShown(self)
}
})
}
Modal.prototype.show = function () {
if (!this.element) {
this.createContainerElement()
if (this.props.options) {
$(this.element).modal(this.props.options)
} else {
$(this.element).modal()
}
} else {
$(this.element).modal('show')
}
if (this.props.title) {
$(this.titleElement).show()
this.titleElement.innerHTML = this.props.title
} else {
$(this.titleElement).hide()
}
if (this.props.body) {
$(this.bodyElement).show()
this.bodyElement.innerHTML = this.props.body
} else {
$(this.bodyElement).hide()
}
if (this.props.footer) {
$(this.footerElement).show()
this.footerElement.innerHTML = this.props.footer
} else {
$(this.footerElement).hide()
}
}
Modal.prototype.hide = function () {
$(this.element).modal('hide')
}
Modal.prototype.dispose = function () {
$(this.element).modal('dispose')
document.body.removeChild(this.element)
if (this.props.onDispose) {
this.props.onDispose(this)
}
}
$.extend({
showModal: function (props) {
if (props.buttons) {
var footer = ""
for (var key in props.buttons) {
// noinspection JSUnfilteredForInLoop
var buttonText = props.buttons[key]
footer += '<button type="button" class="btn btn-primary" data-value="' + key + '" data-dismiss="modal">' + buttonText + '</button>'
}
props.footer = footer
}
return new Modal(props)
},
showAlert: function (props) {
props.buttons = {OK: 'OK'}
return this.showModal(props)
},
showConfirm: function (props) {
props.footer = '<button class="btn btn-secondary btn-false btn-cancel">' + props.textFalse + '</button><button class="btn btn-primary btn-true">' + props.textTrue + '</button>'
props.onCreate = function (modal) {
$(modal.element).on("click", ".btn", function (event) {
event.preventDefault()
modal.hide()
modal.props.onSubmit(event.target.getAttribute("class").indexOf("btn-true") !== -1, modal)
})
}
return this.showModal(props)
}
})
}(jQuery))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment