Merge pull request 'release/0.1.3.017' (#5) from release/0.1.3.017 into master

Reviewed-on: #5
This commit is contained in:
Ray 2023-09-06 22:30:46 +00:00
commit 44ab87e867
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">
<p><b>Example. Prompt Modal</b></p>
<p>Launch a prompt modal and wait for a response</p>
<p><b>Example. Prompt Modal (Buttons)</b></p>
<p>Launch a prompt modal and wait for a button response</p>
<div class="alert alert-secondary text-sm">
<pre>
let response = await BSDialog.Prompt({
@ -166,6 +166,94 @@ BSDialog.Show({
</div>
<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">
<p><b>Example. Simple Text Modal</b> <span class="badge badge-warning">Deprecated</span></p>
<p>Launch an empty modal</p>
@ -200,11 +288,11 @@ BSDialog.UpdateFooter('abc123', '');
BSDialog.UpdateSize('abc123', 'lg');
</pre>
</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>
$(document).ready(function(){
$("#button2").on('click', function(){
$("#buttonR3").on('click', function(){
BSDialog.Create('abc123', 'My Modal Box 123', null, 'sm');
BSDialog.UpdateTitle('abc123', 'My Modal Box 567');
BSDialog.UpdateBody('abc123', 'Help, I\'m toast!');

View File

@ -1,17 +1,48 @@
/**
* BSDialog4
* @version v0.1.2.046 (2023/08/20 00:39)
* @version v0.1.3.017 (2023/08/30 00:17)
*/
var BSDialog = {
Create: async function (id, title, url, size) {
await this.Show({
ID: id,
Title: title,
Message: url,
URL: url,
Size: size,
Colour: "secondary"
});
Show: async function (options) {
const a = this;
const _options = Object.assign(a.Default().ShowOptions, options);
a.id = _options.ID;
a.pfx = "bsdia4_";
a.body = document.getElementsByTagName("body")[0];
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 () {
this.body.querySelectorAll(".modal").forEach(function(e) {
@ -37,77 +68,71 @@ var BSDialog = {
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;
}
modal.Title.forEach(function(e) {
e.innerHTML = title;
});
},
UpdateSize: function (id, size) {
let modal = this.Find(id);
if (modal === null) {
return;
if (!this.isNullOrWhitespace(_options.Title)) {
modal.Title.forEach(function(e) {
e.innerHTML = _options.Title;
});
}
modal.Modal.forEach(function(e) {
let modalDialog = e.querySelectorAll(".modal-dialog")[0];
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;
if (!this.isNullOrWhitespace(_options.Body)) {
modal.Body.forEach(function(e) {
a.html(e, _options.Body);
});
}
modal.Body.forEach(function(e) {
a.html(e, content);
});
},
UpdateBodyRemote: async function (id, url) {
var a = this;
if (!a.Exists(id)) {
return;
if (!this.isNullOrWhitespace(_options.BodyURL)) {
if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) {
// ok
} else {
_options.BodyURL = null;
}
}
await fetch(url, {
cache: 'no-cache',
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;
if (!this.isNullOrWhitespace(_options.BodyURL)) {
let response = await a.retrieveURL(_options.BodyURL);
let modal = a.Find(id);
if (modal === null) {
return;
await a.Update({ ID: _options.ID, Body: response });
}
modal.Footer.forEach(function(e) {
a.html(e, content);
});
if (!this.isNullOrWhitespace(_options.Footer)) {
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) {
return (this.Find(id) !== null);
},
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) {
return null;
}
@ -124,122 +149,50 @@ var BSDialog = {
Modal: modal
};
},
Show: async function (options) {
const a = this;
const _options = Object.assign(a.DefaultShowOptions, options);
a.id = _options.ID;
a.pfx = "bsdia4_";
a.body = document.getElementsByTagName("body")[0];
a.addBackdrop();
a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour);
if (_options.URL == null) {
await a.UpdateBody(_options.ID, _options.Message);
} else {
if (a.isNullOrWhitespace(_options.URL)) {
await a.UpdateBody(_options.ID, _options.URL);
} else {
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) {
await a.UpdateBodyRemote(_options.ID, _options.URL);
} else {
await a.UpdateBody(_options.ID, _options.URL);
Default: function() {
return {
ShowOptions: {
ID: null,
Title: "",
Message: "",
URL: null,
Size: "md",
Colour: "secondary",
ShowFooter: true
},
PromptOptions: {
Type: "button",
Title: "",
Message: "",
Size: "md",
Buttons: [
{ Label: "Yes", Value: "Yes", Colour: "primary" },
{ Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" }
],
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 () {
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) {
let node = document.createElement('template');
node.innerHTML = html;
@ -378,6 +460,18 @@ var BSDialog = {
this.body.classList.remove("modal-open");
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 () {
var a = this;
@ -388,23 +482,56 @@ var BSDialog = {
let modalDialog = modal.Modal[0].querySelectorAll(".modal-dialog")[0];
if (modalDialog.classList.contains('modal-sm')) {
modalDialog.classList.remove("modal-sm");
modalDialog.classList.add("modal-md");
a.Update({ ID: a.id, Size: "md" });
} else if (modalDialog.classList.contains('modal-md')) {
modalDialog.classList.remove("modal-md");
modalDialog.classList.add("modal-lg");
a.Update({ ID: a.id, Size: "lg" });
} else if (modalDialog.classList.contains('modal-lg')) {
modalDialog.classList.remove("modal-lg");
modalDialog.classList.add("modal-xl");
a.Update({ ID: a.id, Size: "xl" });
} else if (modalDialog.classList.contains('modal-xl')) {
modalDialog.classList.remove("modal-xl");
modalDialog.classList.add("modal-sm");
a.Update({ ID: a.id, Size: "sm" });
} else {
modalDialog.classList.remove("modal-sm");
modalDialog.classList.remove("modal-md");
modalDialog.classList.remove("modal-lg");
modalDialog.classList.remove("modal-xl");
modalDialog.classList.add("modal-md");
a.Update({ ID: a.id, Size: "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