Remove Create and Toast #4

Merged
Ray merged 1 commits from release/0.2.0.013 into master 2023-08-21 20:25:46 +00:00
3 changed files with 408 additions and 149 deletions

View File

@ -16,89 +16,254 @@
<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">
<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> <p>Launch an empty modal</p>
<div class="alert alert-secondary">BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');</div> <div class="alert alert-secondary text-sm">
<p><button id="button1" type="button" class="btn btn-primary">Launch Modal</button></p> <pre>
BSDialog5.Show({
ID: "modalL1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
});
</pre>
</div> </div>
</div> <p><button id="buttonL1" type="button" class="btn btn-primary">Launch Modal</button></p>
<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>
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
$("#button1").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', null, 'xl');
});
$("#button2").on('click', function(){ $("#buttonL1").on('click', function(){
BSDialog5.Create('abc123', 'My Modal Box 123', 'Hello momo!', 'lg'); BSDialog5.Show({
ID: "modalL1",
Title: "Modal Title",
Message: "Hello Momo!",
URL: null,
Size: "md",
Colour: "secondary"
}); });
$("#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> </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>
</body> </body>
</html> </html>

View File

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