Compare commits

..

No commits in common. "94bbd618f5b15db453bcb475b7f11098e008380b" and "9cb2a09b3afab22d94a5595c7e5e82e371d4ec9f" have entirely different histories.

3 changed files with 153 additions and 412 deletions

View File

@ -16,254 +16,89 @@
<script src="bsdialog5.js"></script> <script src="bsdialog5.js"></script>
<title></title> <title></title>
<style>
.text-sm {
font-size: 0.8em;
}
</style>
</head> </head>
<body class="py-5"> <body class="py-5">
<div class="container"> <div class="container">
<div class="row mb-3 border-bottom">
<div class="col-12">
<div class="row mb-3"> <p><b>Example One</b></p>
<div class="col-6 border-right"> <p>Launch an empty modal</p>
<div class="alert alert-secondary">BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');</div>
<div class="pb-3 mb-3 border-bottom"> <p><button id="button1" type="button" class="btn btn-primary">Launch Modal</button></p>
<p><b>Example. Simple Text Modal</b></p>
<p>Launch an empty modal</p>
<div class="alert alert-secondary text-sm">
<pre>
BSDialog5.Show({
ID: "modalL1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
</pre>
</div>
<p><button id="buttonL1" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL1").on('click', function(){
BSDialog5.Show({
ID: "modalL1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Prompt Modal</b></p>
<p>Launch a prompt modal and wait for a response</p>
<div class="alert alert-secondary text-sm">
<pre>
let response = await BSDialog5.Prompt({
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" }
]
});
alert(response);
</pre>
</div>
<p><button id="buttonL2" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL2").on('click', async function(){
let response = await BSDialog5.Prompt({
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" }
]
});
alert(response);
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Multiple Modals</b></p>
<p>Launch multiple modals</p>
<div class="alert alert-secondary text-sm">
<pre>
BSDialog5.Show({
ID: "modalL3a",
Title: "Modal A Title",
Message: "First!",
URL: null,
Size: "md",
Colour: "secondary"
});
BSDialog5.Show({
ID: "modalL3b",
Title: "Modal B Title",
Message: "Second!",
URL: null,
Size: "md",
Colour: "secondary"
});
</pre>
</div>
<p><button id="buttonL3" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL3").on('click', function(){
BSDialog5.Show({
ID: "modalL3a",
Title: "Modal A Title",
Message: "First!",
URL: null,
Size: "md",
Colour: "secondary"
});
BSDialog5.Show({
ID: "modalL3b",
Title: "Modal B Title",
Message: "Second!",
URL: null,
Size: "md",
Colour: "secondary"
});
});
});
</script>
</div>
</div>
<div class="col-6">
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Text Modal with Updates</b></p>
<p>Launch a basic modal, make updates to the title, body, size and footer.</p>
<div class="alert alert-secondary text-sm">
<pre>
BSDialog5.Show({
ID: "modalR1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
BSDialog5.Update({
ID: "modalR1",
Title: "Modal Changed Title",
Body: "Hello momo again!",
BodyURL: null,
Size: "lg",
Footer: null
});
</pre>
</div>
<p><button id="buttonR1" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonR1").on('click', function(){
BSDialog5.Show({
ID: "modalR1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
BSDialog5.Update({
ID: "modalR1",
Title: "Modal Changed Title",
Body: "Hello momo again!",
BodyURL: null,
Size: "lg",
Footer: null
});
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Close Modal</b></p>
<p>Close a modal using its identifier</p>
<div class="alert alert-secondary text-sm">
<pre>
BSDialog5.Close("modalR1");
</pre>
</div>
<p><button id="buttonR2" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonR2").on('click', function(){
BSDialog5.Close("modalR1");
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Clear Modal</b></p>
<p>Close all modals</p>
<div class="alert alert-secondary text-sm">
<pre>
BSDialog5.Clear();
</pre>
</div>
<p><button id="buttonR3" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonR3").on('click', function(){
BSDialog5.Clear();
});
});
</script>
</div>
</div> </div>
</div> </div>
<div class="row mb-3 border-bottom">
<div class="col-12">
<p><b>Example Two</b></p>
<p>Launch a modal with text</p>
<div class="alert alert-secondary">BSDialog5.Create('abc123', 'My Modal Box 123', 'Hello momo!', 'lg');</div>
<p><button id="button2" type="button" class="btn btn-primary">Launch Modal</button></p>
</div>
</div>
<div class="row mb-3 border-bottom">
<div class="col-12">
<p><b>Example Three</b></p>
<p>Launch an empty modal then update its title, body, footer and size.</p>
<div class="alert alert-secondary">
BSDialog5.ShowToast('abc123', 'My Modal Box 123', null, 'sm');<br />
BSDialog5.UpdateTitle('abc123', 'My Modal Box 567');<br />
BSDialog5.UpdateBody('abc123', 'Help, I\'m toast!');<br />
BSDialog5.UpdateFooter('abc123', '');<br />
BSDialog5.UpdateSize('abc123', 'lg');<br />
</div>
<p><button id="button3" type="button" class="btn btn-primary">Launch Modal</button></p>
</div>
</div>
<div class="row mb-3 border-bottom">
<div class="col-12">
<p><b>Example Four</b></p>
<p>Launch a multiple modals</p>
<div class="alert alert-secondary">
BSDialog5.ShowToast('abc123', 'My Modal Box 123', 'Help! I\'m toast.', 'sm');<br />
BSDialog5.ShowToast('abc123456', 'My Modal Box 123.567', 'Help! I\'m a second toast.', 'md');<br />
</div>
<p><button id="button4" type="button" class="btn btn-primary">Launch Modal</button></p>
</div>
</div>
</div> </div>
<script>
$(document).ready(function(){
$("#button1").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');
});
$("#button2").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', 'Hello momo!', 'lg');
});
$("#button3").on('click', function(){
BSDialog5.ShowToast('abc123', 'My Modal Box 123', null, 'sm');
BSDialog5.UpdateTitle('abc123', 'My Modal Box 567');
BSDialog5.UpdateBody('abc123', 'Help, I\'m toast!');
BSDialog5.UpdateFooter('abc123', '');
BSDialog5.UpdateSize('abc123', 'lg');
});
$("#button4").on('click', function(){
BSDialog5.ShowToast('abc123', 'My Modal Box 123', 'Help! I\'m toast.', 'sm');
BSDialog5.ShowToast('abc123456', 'My Modal Box 123.567', 'Help! I\'m a second toast.', 'md');
});
});
</script>
</body> </body>
</html> </html>

View File

@ -1,115 +1,28 @@
/** /**
* BSDialog5 * BSDialog5
* @version v0.2.0.013 (2023/08/21 1945) * @version v0.1.2.014 (2023/08/19 1351)
*/ */
var BSDialog5 = { var BSDialog5 = {
Default: function() { Create: async function (id, title, url, size) {
return { var a = this;
ShowOptions: {
ID: null,
Title: "",
Message: "",
URL: null,
Size: "md",
Colour: "secondary",
ShowFooter: true
},
PromptOptions: {
Title: "",
Message: "",
Size: "md",
Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" }
]
},
UpdateOptions: {
ID: null,
Title: null,
Body: null,
BodyURL: null,
Footer: null,
Size: null
}
};
},
Show: async function (options) {
const a = this;
const _options = Object.assign(a.Default().ShowOptions, options);
a.id = _options.ID; a.id = id;
a.pfx = "bsdia5_"; a.pfx = "bsdia5_";
a.body = document.getElementsByTagName("body")[0];
a.addBackdrop(); a.addBackdrop();
a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour); a.addModal(id, title, size);
if (_options.URL == null) { if (a.isNullOrWhitespace(url)) {
await a.Update({ ID: _options.ID, Body: _options.Message }); a.UpdateBody(id, "");
} else if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("/")) {
a.UpdateBodyRemote(id, url);
} else { } else {
if (a.isNullOrWhitespace(_options.URL)) { await a.UpdateBody(id, url);
await a.Update({ ID: _options.ID, Body: URL });
} else {
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) {
await a.UpdateBodyRemote(_options.ID, _options.URL);
} else {
await a.Update({ ID: _options.ID, Body: URL });
}
}
} }
}, },
Prompt: async function (options) {
const a = this;
const id = "prompt" + Math.floor(Math.random() * 10000) + 1000;
const _options = Object.assign(a.Default().PromptOptions, options);
return await new Promise(async (resolve) => {
await a.Show({
ID: id,
Title: _options.Title,
Message: _options.Message,
Size: _options.Size
});
let html = '';
_options.Buttons.forEach(function(e) {
html += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
});
a.Update({ ID: id, Footer: html });
const modal = a.Find(id);
modal.Footer[0].querySelectorAll("button").forEach(function(button) {
button.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
const value = button.getAttribute("data-prompt-value");
a.Close(id);
resolve(value);
});
});
modal.Close.forEach(function(sender) {
sender.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
});
});
},
Clear: function () { Clear: function () {
this.GetBody().querySelectorAll(".modal").forEach(function(e) { this.body.querySelectorAll(".modal").forEach(function(e) {
e.parentNode.removeChild(e); e.parentNode.removeChild(e);
}); });
@ -132,58 +45,44 @@ var BSDialog5 = {
this.removeBackdrop(); this.removeBackdrop();
}, },
Update: async function (options) { UpdateTitle: function (id, title) {
const a = this; let modal = this.Find(id);
let _options = Object.assign(a.Default().UpdateOptions, options);
const modal = a.Find(_options.ID);
if (modal === null) { if (modal === null) {
return; return;
} }
if (!this.isNullOrWhitespace(_options.Title)) { modal.Title.forEach(function(e) {
modal.Title.forEach(function(e) { e.innerHTML = title;
e.innerHTML = _options.Title; });
}); },
UpdateSize: function (id, size) {
let modal = this.Find(id);
if (modal === null) {
return;
} }
if (!this.isNullOrWhitespace(_options.Body)) { modal.Modal.forEach(function(e) {
modal.Body.forEach(function(e) { e.classList.remove("modal-sm");
a.html(e, _options.Body); e.classList.remove("modal-md");
}); e.classList.remove("modal-lg");
} e.classList.remove("modal-xl");
e.classList.add("modal-" + size);
if (!this.isNullOrWhitespace(_options.BodyURL)) { });
if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) { },
// ok UpdateBody: function (id, content) {
} else { var a = this;
_options.BodyURL = null;
} let modal = a.Find(id);
} if (modal === null) {
return;
if (!this.isNullOrWhitespace(_options.BodyURL)) {
await a.UpdateBodyRemote(_options.ID, _options.BodyURL);
}
if (!this.isNullOrWhitespace(_options.Footer)) {
modal.Footer.forEach(function(e) {
a.html(e, _options.Footer);
});
}
if (!this.isNullOrWhitespace(_options.Size)) {
modal.Modal.forEach(function(e) {
e.classList.remove("modal-sm");
e.classList.remove("modal-md");
e.classList.remove("modal-lg");
e.classList.remove("modal-xl");
e.classList.add("modal-" + _options.Size);
});
} }
modal.Body.forEach(function(e) {
a.html(e, content);
});
}, },
UpdateBodyRemote: async function (id, url) { UpdateBodyRemote: async function (id, url) {
const a = this; var a = this;
if (!a.Exists(id)) { if (!a.Exists(id)) {
return; return;
@ -193,16 +92,28 @@ var BSDialog5 = {
cache: 'no-cache', cache: 'no-cache',
credentials: 'same-origin' credentials: 'same-origin'
}).then(data => data.text()).then(data => { }).then(data => data.text()).then(data => {
a.Update({ ID: id, Body: data }); a.UpdateBody(id, data);
}).catch((error) => { }).catch((error) => {
a.Update({ ID: id, Body: "Error: " + error }); a.UpdateBody(id, "Error: " + error);
});
},
UpdateFooter: function (id, content) {
var a = this;
let modal = a.Find(id);
if (modal === null) {
return;
}
modal.Footer.forEach(function(e) {
a.html(e, content);
}); });
}, },
Exists: function (id) { Exists: function (id) {
return (this.Find(id) !== null); return (this.Find(id) !== null);
}, },
Find: function (id) { Find: function (id) {
const modal = this.GetBody().querySelectorAll("#" + this.pfx + id + ".modal"); let modal = this.body.querySelectorAll("#" + this.pfx + id + ".modal");
if (!modal) { if (!modal) {
return null; return null;
} }
@ -219,25 +130,26 @@ var BSDialog5 = {
Modal: modal Modal: modal
}; };
}, },
GetBody: function() { ShowToast: function (id, title, message, size) {
return document.getElementsByTagName("body")[0]; this.Create(id, title, null, size);
this.UpdateBody(id, message);
}, },
addBackdrop: function () { addBackdrop: function () {
const a = this; let a = this;
// don't allow duplicates // don't allow duplicates
if (a.GetBody().querySelectorAll(".modal-backdrop").length > 0) { if (a.body.querySelectorAll(".modal-backdrop").length > 0) {
return; return;
} }
a.appendHtml(a.GetBody(), '<div class="modal-backdrop fade show"></div>'); a.appendHtml(a.body, '<div class="modal-backdrop fade show"></div>');
// lock background // lock background
a.GetBody().classList.add("modal-open"); a.body.classList.add("modal-open");
a.GetBody().style.overflow = "hidden"; a.body.style.overflow = "hidden";
// close // close
let backdrop = a.GetBody().querySelectorAll(".modal-backdrop")[0]; let backdrop = a.body.querySelectorAll(".modal-backdrop")[0];
backdrop.addEventListener("click", function(e){ backdrop.addEventListener("click", function(e){
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
@ -245,8 +157,8 @@ var BSDialog5 = {
a.Clear(); a.Clear();
}); });
}, },
addModal: function (id, title, size, showFooter, closeColour) { addModal: function (id, title, size) {
const a = this; var a = this;
// don't allow duplicates // don't allow duplicates
let modal = a.Find(id); let modal = a.Find(id);
@ -273,18 +185,14 @@ var BSDialog5 = {
html += '</div>'; html += '</div>';
html += ' </div>'; html += ' </div>';
html += ' <div class="modal-footer">';
if (showFooter === true) { html += ' <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>';
html += ' <div class="modal-footer">'; html += ' </div>';
html += ' <button type="button" class="btn btn-' + closeColour + '" data-bs-dismiss="modal">Close</button>';
html += ' </div>';
}
html += ' </div>'; html += ' </div>';
html += ' </div>'; html += ' </div>';
html += '</div>'; html += '</div>';
a.appendHtml(a.GetBody(), html); a.appendHtml(a.body, html);
modal = a.Find(id); modal = a.Find(id);
if (modal === null) { if (modal === null) {
@ -349,25 +257,23 @@ var BSDialog5 = {
return (e.trim().length <= 0); return (e.trim().length <= 0);
}, },
removeBackdrop: function () { removeBackdrop: function () {
const a = this; if (this.body.querySelectorAll(".modal-backdrop").length <= 0) {
if (a.GetBody().querySelectorAll(".modal-backdrop").length <= 0) {
return; return;
} }
if (a.GetBody().querySelectorAll(".modal").length > 0) { if (this.body.querySelectorAll(".modal").length > 0) {
return; return;
} }
let backdrop = a.GetBody().querySelectorAll(".modal-backdrop")[0]; let backdrop = this.body.querySelectorAll(".modal-backdrop")[0];
backdrop.parentNode.removeChild(backdrop); backdrop.parentNode.removeChild(backdrop);
// unlock background // unlock background
a.GetBody().classList.remove("modal-open"); this.body.classList.remove("modal-open");
a.GetBody().style.overflow = null; this.body.style.overflow = null;
}, },
toggleSize: function () { toggleSize: function () {
const a = this; var a = this;
let modal = a.Find(a.id); let modal = a.Find(a.id);
if (modal === null) { if (modal === null) {

4
bsdialog5.min.js vendored

File diff suppressed because one or more lines are too long