Compare commits

...

5 Commits

Author SHA1 Message Date
Ray
44ab87e867 Merge pull request 'release/0.1.3.017' (#5) from release/0.1.3.017 into master
Reviewed-on: #5
2023-09-06 22:30:46 +00:00
Ray
d7f4f60e4d Refactored to stop using deprecated code 2023-08-30 14:26:40 +01:00
Ray
e9198dea75 Added support for textbox prompts 2023-08-30 01:55:27 +01:00
Ray
0d3df02449 Refactor resizing 2023-08-30 01:02:45 +01:00
Ray
46cda4fbf1 Fixed re-sizing fault 2023-08-30 00:51:47 +01:00
3 changed files with 411 additions and 196 deletions

View File

@ -68,8 +68,8 @@ BSDialog.Show({
<div class="pb-3 mb-3 border-bottom"> <div class="pb-3 mb-3 border-bottom">
<p><b>Example. Prompt Modal</b></p> <p><b>Example. Prompt Modal (Buttons)</b></p>
<p>Launch a prompt modal and wait for a response</p> <p>Launch a prompt modal and wait for a button response</p>
<div class="alert alert-secondary text-sm"> <div class="alert alert-secondary text-sm">
<pre> <pre>
let response = await BSDialog.Prompt({ let response = await BSDialog.Prompt({
@ -166,6 +166,94 @@ BSDialog.Show({
</div> </div>
<div class="col-6"> <div class="col-6">
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Prompt Modal (Textbox)</b></p>
<p>Launch a prompt modal and wait for a textbox response</p>
<div class="alert alert-secondary text-sm">
<pre>
let response = await BSDialog.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Label: "",
Placeholder: ""
});
alert(response);
</pre>
</div>
<p><button id="buttonL2a" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL2a").on('click', async function(){
let response = await BSDialog.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Label: "",
Placeholder: ""
});
alert(response);
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom">
<p><b>Example. Prompt Modal (Textbox)</b></p>
<p>Launch a prompt modal and wait for a textbox response</p>
<div class="alert alert-secondary text-sm">
<pre>
let response = await BSDialog.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Textbox: {
Label: "Textbox Label",
LabelSize: 4,
Placeholder: "Placeholder",
Value: "Default Value",
BoxSize: 8
}
});
alert(response);
</pre>
</div>
<p><button id="buttonL2b" type="button" class="btn btn-primary">Launch Modal</button></p>
<script>
$(document).ready(function(){
$("#buttonL2b").on('click', async function(){
let response = await BSDialog.Prompt({
Type: "textbox",
Title: "Modal Title",
Message: "Are you sure want to wait for a response?",
Size: "md",
Textbox: {
Label: "Textbox Label",
LabelSize: 4,
Placeholder: "Placeholder",
Value: "Default Value",
BoxSize: 8
}
});
alert(response);
});
});
</script>
</div>
<div class="pb-3 mb-3 border-bottom"> <div class="pb-3 mb-3 border-bottom">
<p><b>Example. Simple Text Modal</b> <span class="badge badge-warning">Deprecated</span></p> <p><b>Example. Simple Text Modal</b> <span class="badge badge-warning">Deprecated</span></p>
<p>Launch an empty modal</p> <p>Launch an empty modal</p>
@ -200,11 +288,11 @@ BSDialog.UpdateFooter('abc123', '');
BSDialog.UpdateSize('abc123', 'lg'); BSDialog.UpdateSize('abc123', 'lg');
</pre> </pre>
</div> </div>
<p><button id="button3" type="button" class="btn btn-primary">Launch Modal</button></p> <p><button id="buttonR3" type="button" class="btn btn-primary">Launch Modal</button></p>
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
$("#button2").on('click', function(){ $("#buttonR3").on('click', function(){
BSDialog.Create('abc123', 'My Modal Box 123', null, 'sm'); BSDialog.Create('abc123', 'My Modal Box 123', null, 'sm');
BSDialog.UpdateTitle('abc123', 'My Modal Box 567'); BSDialog.UpdateTitle('abc123', 'My Modal Box 567');
BSDialog.UpdateBody('abc123', 'Help, I\'m toast!'); BSDialog.UpdateBody('abc123', 'Help, I\'m toast!');

View File

@ -1,17 +1,48 @@
/** /**
* BSDialog4 * BSDialog4
* @version v0.1.2.046 (2023/08/20 00:39) * @version v0.1.3.017 (2023/08/30 00:17)
*/ */
var BSDialog = { var BSDialog = {
Create: async function (id, title, url, size) { Show: async function (options) {
await this.Show({ const a = this;
ID: id, const _options = Object.assign(a.Default().ShowOptions, options);
Title: title,
Message: url, a.id = _options.ID;
URL: url, a.pfx = "bsdia4_";
Size: size, a.body = document.getElementsByTagName("body")[0];
Colour: "secondary"
}); a.addBackdrop();
a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour);
if (_options.URL == null) {
await a.Update({ ID: _options.ID, Body: _options.Message });
} else {
if (a.isNullOrWhitespace(_options.URL)) {
await a.Update({ ID: _options.ID, Body: _options.URL });
} else {
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) {
let response = await a.retrieveURL(_options.URL);
await a.Update({ ID: _options.ID, Body: response });
} else {
await a.Update({ ID: _options.ID, Body: _options.URL });
}
}
}
},
Prompt: async function (options) {
const a = this;
let id = Math.floor(Math.random() * 10000) + 1000;
const _options = Object.assign(a.Default().PromptOptions, options);
switch (_options.Type) {
case "textbox":
return await a.showTextboxPrompt(id, _options);
case "button":
default:
return await a.showButtonPrompt(id, _options);
}
}, },
Clear: function () { Clear: function () {
this.body.querySelectorAll(".modal").forEach(function(e) { this.body.querySelectorAll(".modal").forEach(function(e) {
@ -37,77 +68,71 @@ var BSDialog = {
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;
} }
modal.Title.forEach(function(e) { if (!this.isNullOrWhitespace(_options.Title)) {
e.innerHTML = title; modal.Title.forEach(function(e) {
}); e.innerHTML = _options.Title;
}, });
UpdateSize: function (id, size) {
let modal = this.Find(id);
if (modal === null) {
return;
} }
modal.Modal.forEach(function(e) { if (!this.isNullOrWhitespace(_options.Body)) {
let modalDialog = e.querySelectorAll(".modal-dialog")[0]; modal.Body.forEach(function(e) {
a.html(e, _options.Body);
modalDialog.classList.remove("modal-sm"); });
modalDialog.classList.remove("modal-md");
modalDialog.classList.remove("modal-lg");
modalDialog.classList.remove("modal-xl");
modalDialog.classList.add("modal-" + size);
});
},
UpdateBody: function (id, content) {
var a = this;
let modal = a.Find(id);
if (modal === null) {
return;
} }
modal.Body.forEach(function(e) { if (!this.isNullOrWhitespace(_options.BodyURL)) {
a.html(e, content); if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) {
}); // ok
}, } else {
UpdateBodyRemote: async function (id, url) { _options.BodyURL = null;
var a = this; }
if (!a.Exists(id)) {
return;
} }
await fetch(url, { if (!this.isNullOrWhitespace(_options.BodyURL)) {
cache: 'no-cache', let response = await a.retrieveURL(_options.BodyURL);
credentials: 'same-origin'
}).then(data => data.text()).then(data => {
a.UpdateBody(id, data);
}).catch((error) => {
a.UpdateBody(id, "Error: " + error);
});
},
UpdateFooter: function (id, content) {
var a = this;
let modal = a.Find(id); await a.Update({ ID: _options.ID, Body: response });
if (modal === null) {
return;
} }
modal.Footer.forEach(function(e) { if (!this.isNullOrWhitespace(_options.Footer)) {
a.html(e, content); modal.Footer.forEach(function(e) {
}); a.html(e, _options.Footer);
});
}
if (!this.isNullOrWhitespace(_options.Size)) {
modal.Modal[0].querySelectorAll(".modal-dialog").forEach(function(e) {
e.classList.forEach(function(e2) {
if (e2 == "modal-dialog") {
return;
}
if (e2.startsWith("modal-")) {
e.classList.remove(e2);
}
});
e.classList.add("modal-" + _options.Size);
});
}
}, },
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 a = this;
const modal = a.body.querySelectorAll("#" + a.pfx + id + ".modal");
if (!modal) { if (!modal) {
return null; return null;
} }
@ -124,122 +149,50 @@ var BSDialog = {
Modal: modal Modal: modal
}; };
}, },
Show: async function (options) { Default: function() {
const a = this; return {
const _options = Object.assign(a.DefaultShowOptions, options); ShowOptions: {
ID: null,
a.id = _options.ID; Title: "",
a.pfx = "bsdia4_"; Message: "",
a.body = document.getElementsByTagName("body")[0]; URL: null,
Size: "md",
a.addBackdrop(); Colour: "secondary",
a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour); ShowFooter: true
},
if (_options.URL == null) { PromptOptions: {
await a.UpdateBody(_options.ID, _options.Message); Type: "button",
} else { Title: "",
if (a.isNullOrWhitespace(_options.URL)) { Message: "",
await a.UpdateBody(_options.ID, _options.URL); Size: "md",
} else { Buttons: [
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) { { Label: "Yes", Value: "Yes", Colour: "primary" },
await a.UpdateBodyRemote(_options.ID, _options.URL); { Label: "No", Value: "No", Colour: "secondary" },
} else { { Label: "Cancel", Value: "Cancel", Colour: "secondary" }
await a.UpdateBody(_options.ID, _options.URL); ],
Textbox: {
Label: "",
LabelSize: 4,
Placeholder: "",
Value: "",
BoxSize: 8
} }
},
ToastOptions: {
ID: null,
Title: "",
Message: "",
Size: "md"
},
UpdateOptions: {
ID: null,
Title: null,
Body: null,
BodyURL: null,
Footer: null,
Size: null
} }
} };
},
Toast: async function (options) {
const a = this;
const _options = Object.assign(a.DefaultToastOptions, options);
await this.Show({
ID: _options.ID,
Title: _options.Title,
Message: _options.Message,
URL: null,
Size: _options.Size,
Colour: "secondary",
ShowFooter: false
});
},
Prompt: async function (options) {
const a = this;
let id = "prompt" + Math.floor(Math.random() * 10000) + 1000;
const _options = Object.assign(a.DefaultPromptOptions, options);
return await new Promise((resolve, reject) => {
a.Create(id, _options.Title, _options.Message, _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.UpdateFooter(id, 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("");
});
});
});
},
DefaultShowOptions: {
ID: null,
Title: "",
Message: "",
URL: null,
Size: "md",
Colour: "secondary",
ShowFooter: true
},
DefaultToastOptions: {
ID: null,
Title: "",
Message: "",
Size: "md"
},
DefaultPromptOptions: {
Title: "",
Message: "",
Size: "md",
Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" }
]
},
ShowToast: function (id, title, message, size) {
this.Toast({
ID: id,
Title: title,
Message: message,
Size: size
});
}, },
addBackdrop: function () { addBackdrop: function () {
let a = this; let a = this;
@ -331,6 +284,135 @@ var BSDialog = {
}); });
}, },
showButtonPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
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("");
});
});
});
},
showTextboxPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
options.Buttons = [
{ Label: "OK", Value: "", Colour: "primary" },
{ Label: "Cancel", Value: "", Colour: "secondary" }
];
await a.Show({
ID: id,
Title: options.Title,
Message: options.Message,
Size: options.Size
});
let body = '';
if (!a.isNullOrWhitespace(options.Message)) {
body += '<p>' + options.Message + '</p>';
}
body += '<div class="form-group row">';
if (a.isNullOrWhitespace(options.Textbox.Label) || (options.Textbox.LabelSize <= 0)) {
body += '<div class="col-sm-12">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>';
} else {
body += '<label for="textbox' + id + '" class="col-sm-' + options.Textbox.LabelSize + ' col-form-label">' + options.Textbox.Label + '</label>';
body += '<div class="col-sm-' + options.Textbox.BoxSize + '">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>';
}
body += '</div>';
let footer = '';
options.Buttons.forEach(function(e) {
footer += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
});
a.Update({
ID: id,
Body: body,
Footer: footer
});
const modal = a.Find(id);
const buttons = modal.Footer[0].querySelectorAll("button");
buttons[0].addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
const value = modal.Body[0].querySelectorAll("input")[0].value;
a.Close(id);
resolve(value);
});
buttons[1].addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
modal.Close.forEach(function(sender) {
sender.addEventListener("click", function(e){
e.stopPropagation();
e.preventDefault();
a.Close(id);
resolve("");
});
});
});
},
appendHtml: function (el, html) { appendHtml: function (el, html) {
let node = document.createElement('template'); let node = document.createElement('template');
node.innerHTML = html; node.innerHTML = html;
@ -378,6 +460,18 @@ var BSDialog = {
this.body.classList.remove("modal-open"); this.body.classList.remove("modal-open");
this.body.style.overflow = null; this.body.style.overflow = null;
}, },
retrieveURL: async function (url) {
return await new Promise(async (resolve) => {
await fetch(url, {
cache: 'no-cache',
credentials: 'same-origin'
}).then(data => data.text()).then(data => {
resolve(data);
}).catch((error) => {
resolve("Error: " + error);
});
});
},
toggleSize: function () { toggleSize: function () {
var a = this; var a = this;
@ -388,23 +482,56 @@ var BSDialog = {
let modalDialog = modal.Modal[0].querySelectorAll(".modal-dialog")[0]; let modalDialog = modal.Modal[0].querySelectorAll(".modal-dialog")[0];
if (modalDialog.classList.contains('modal-sm')) { if (modalDialog.classList.contains('modal-sm')) {
modalDialog.classList.remove("modal-sm"); a.Update({ ID: a.id, Size: "md" });
modalDialog.classList.add("modal-md");
} else if (modalDialog.classList.contains('modal-md')) { } else if (modalDialog.classList.contains('modal-md')) {
modalDialog.classList.remove("modal-md"); a.Update({ ID: a.id, Size: "lg" });
modalDialog.classList.add("modal-lg");
} else if (modalDialog.classList.contains('modal-lg')) { } else if (modalDialog.classList.contains('modal-lg')) {
modalDialog.classList.remove("modal-lg"); a.Update({ ID: a.id, Size: "xl" });
modalDialog.classList.add("modal-xl");
} else if (modalDialog.classList.contains('modal-xl')) { } else if (modalDialog.classList.contains('modal-xl')) {
modalDialog.classList.remove("modal-xl"); a.Update({ ID: a.id, Size: "sm" });
modalDialog.classList.add("modal-sm");
} else { } else {
modalDialog.classList.remove("modal-sm"); a.Update({ ID: a.id, Size: "md" });
modalDialog.classList.remove("modal-md");
modalDialog.classList.remove("modal-lg");
modalDialog.classList.remove("modal-xl");
modalDialog.classList.add("modal-md");
} }
},
//
// deprecated/legacy-support
Create: async function (id, title, url, size) {
await this.Show({
ID: id,
Title: title,
Message: url,
URL: url,
Size: size,
Colour: "secondary"
});
},
UpdateTitle: function (id, title) {
this.Update({ ID: id, Title: title });
},
UpdateSize: function (id, size) {
this.Update({ ID: id, Size: size });
},
UpdateBody: function (id, content) {
this.Update({ ID: id, Body: content });
},
UpdateFooter: function (id, content) {
this.Update({ ID: id, Footer: content });
},
Toast: async function (options) {
const a = this;
const _options = Object.assign(a.Default().ToastOptions, options);
await this.Show({
ID: _options.ID,
Title: _options.Title,
Message: _options.Message,
URL: null,
Size: _options.Size,
Colour: "secondary",
ShowFooter: false
});
},
ShowToast: function (id, title, message, size) {
this.Toast({ ID: id, Title: title, Message: message, Size: size });
} }
}; };

4
bsdialog4.min.js vendored

File diff suppressed because one or more lines are too long