Merge pull request 'Remove Create and Toast' (#4) from release/0.2.0.013 into master

Reviewed-on: #4
This commit is contained in:
Ray 2023-08-21 20:25:46 +00:00
commit 94bbd618f5
3 changed files with 408 additions and 149 deletions

View File

@ -16,89 +16,254 @@
<script src="bsdialog5.js"></script>
<title></title>
<style>
.text-sm {
font-size: 0.8em;
}
</style>
</head>
<body class="py-5">
<div class="container">
<div class="row mb-3 border-bottom">
<div class="col-12">
<p><b>Example One</b></p>
<div class="row mb-3">
<div class="col-6 border-right">
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Simple Text Modal</b></p>
<p>Launch an empty modal</p>
<div class="alert alert-secondary">BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');</div>
<p><button id="button1" type="button" class="btn btn-primary">Launch Modal</button></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>
</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>
<p><button id="buttonL1" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$(document).ready(function(){
$("#button1").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');
$("#buttonL1").on('click', function(){
BSDialog5.Show({
ID: "modalL1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
});
$("#button2").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', 'Hello momo!', 'lg');
});
</script>
</div>
$("#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');
});
<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>
</body>
</html>

View File

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

4
bsdialog5.min.js vendored

File diff suppressed because one or more lines are too long