Compare commits

..

No commits in common. "7d4057fdbcbf11273d401ee24dfe2b8e2d319a21" and "ff679d739b1eed35ae01d160bfc45774d76d1e93" have entirely different histories.

2 changed files with 133 additions and 169 deletions

View File

@ -1,15 +1,152 @@
/**
* BSDialog4
* @version v0.1.5.017 (2023/11/16 17:47)
* @version v0.1.5.006 (2023/11/14 17:40)
*/
var BSDialog = {
Show: async function (options) {
const a = this;
const _options = Object.assign(a.Options.ShowModal, options);
class BSDialog4 {
constructor() {
a.id = _options.ID;
a.pfx = "bsdia4_";
a.body = document.getElementsByTagName("body")[0];
a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.Colour);
$("#" + a.pfx + _options.ID).modal({
backdrop: _options.EasyClose,
show: true
});
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.Options.ShowPrompt, 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) {
e.parentNode.removeChild(e);
});
this.removeBackdrop();
},
Close: function (id) {
let modal = this.Find(id);
if (modal === null) {
return;
}
modal.Modal.parentNode.removeChild(modal.Modal);
modal = this.Find(id);
if (modal !== null) {
return;
}
this.removeBackdrop();
},
Update: async function (options) {
const a = this;
let _options = Object.assign(a.Options.UpdateModal, options);
const modal = a.Find(_options.ID);
if (modal === null) {
return;
}
if (!this.isNullOrWhitespace(_options.Title)) {
modal.Title.innerHTML = _options.Title;
}
if (!this.isNullOrWhitespace(_options.Body)) {
a.html(modal.Body, _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)) {
let response = await a.retrieveURL(_options.BodyURL);
await a.Update({ ID: _options.ID, Body: response });
}
if (!this.isNullOrWhitespace(_options.Footer)) {
a.html(modal.Footer, _options.Footer);
}
if (!this.isNullOrWhitespace(_options.Size)) {
modal.Modal.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) {
const a = this;
const modal = a.body.querySelectorAll("#" + a.pfx + id + ".modal");
if (!modal) {
return null;
}
if (modal.length <= 0) {
return null;
}
get Options() {
return {
Title: modal[0].querySelectorAll(".modal-title")[0],
Header: modal[0].querySelectorAll(".modal-header")[0],
Body: modal[0].querySelectorAll(".modal-body")[0],
Footer: modal[0].querySelectorAll(".modal-footer")[0],
Close: modal[0].querySelectorAll("[data-dismiss='modal']"),
Modal: modal[0]
};
},
Options: {
ShowModal: {
ID: null,
Title: "",
@ -47,167 +184,8 @@ class BSDialog4 {
Footer: null,
Size: null
}
}
}
get #prefix() {
return "bsdia4_";
}
get #body() {
return document.getElementsByTagName("body")[0];
}
async Show(options) {
const a = this;
const _options = Object.assign(a.Options.ShowModal, options);
a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.Colour);
$("#" + a.#prefix + _options.ID).modal({
backdrop: _options.EasyClose,
show: true
});
if (!a.#isNullOrWhitespace(_options.URL)) {
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) {
const response = await a.#retrieveURL(_options.URL);
await a.Update({ ID: _options.ID, Body: response });
} else {
await a.Update({ ID: _options.ID, Body: _options.URL });
}
} else {
await a.Update({ ID: _options.ID, Body: _options.Message });
}
}
async Prompt(options) {
const a = this;
const id = Math.floor(Math.random() * 10000) + 1000;
const _options = Object.assign(a.Options.ShowPrompt, options);
switch (_options.Type) {
case "textbox":
return await a.#showTextboxPrompt(id, _options);
case "button":
default:
return await a.#showButtonPrompt(id, _options);
}
}
Clear() {
const a = this;
a.#body.querySelectorAll(".modal").forEach(function(e) {
e.parentNode.removeChild(e);
});
a.#removeBackdrop();
}
Close(id) {
const a = this;
let modal = a.Find(id);
if (modal === null) {
return;
}
modal.Modal.parentNode.removeChild(modal.Modal);
modal = a.Find(id);
if (modal !== null) {
return;
}
a.#removeBackdrop();
}
async Update(options) {
const a = this;
let _options = Object.assign(a.Options.UpdateModal, options);
const modal = a.Find(_options.ID);
if (modal === null) {
return;
}
if (!a.#isNullOrWhitespace(_options.Title)) {
modal.Title.innerHTML = _options.Title;
}
if (!a.#isNullOrWhitespace(_options.Body)) {
a.#html(modal.Body, _options.Body);
}
if (!a.#isNullOrWhitespace(_options.BodyURL)) {
if (_options.BodyURL.startsWith("http://") || _options.BodyURL.startsWith("https://") || _options.BodyURL.startsWith("/")) {
// ok
} else {
_options.BodyURL = null;
}
}
if (!a.#isNullOrWhitespace(_options.BodyURL)) {
const response = await a.#retrieveURL(_options.BodyURL);
await a.Update({ ID: _options.ID, Body: response });
}
if (!a.#isNullOrWhitespace(_options.Footer)) {
a.#html(modal.Footer, _options.Footer);
}
if (!a.#isNullOrWhitespace(_options.Size)) {
modal.Modal.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(id) {
const a = this;
return (a.Find(id) !== null);
}
Find(id) {
const a = this;
const modal = a.#body.querySelectorAll("#" + a.#prefix + id + ".modal");
if (!modal) {
return null;
}
if (modal.length <= 0) {
return null;
}
return {
Title: modal[0].querySelectorAll(".modal-title")[0],
Header: modal[0].querySelectorAll(".modal-header")[0],
Body: modal[0].querySelectorAll(".modal-body")[0],
Footer: modal[0].querySelectorAll(".modal-footer")[0],
Close: modal[0].querySelectorAll("[data-dismiss='modal']"),
Modal: modal[0]
};
}
addModal(id, title, size, showFooter, closeColour) {
},
addModal: function (id, title, size, showFooter, closeColour) {
const a = this;
// don't allow duplicates
@ -217,7 +195,7 @@ class BSDialog4 {
}
let html = "";
html += '<div class="modal fade" style="display: none;" id="' + a.#prefix + id + '" tabindex="-1" aria-labelledby="exampleModalSmLabel" aria-hidden="true">';
html += '<div class="modal fade" style="display: none;" id="' + a.pfx + id + '" tabindex="-1" aria-labelledby="exampleModalSmLabel" aria-hidden="true">';
html += ' <div class="modal-dialog modal-' + size + '">';
html += ' <div class="modal-content">';
html += ' <div class="modal-header">';
@ -248,14 +226,14 @@ class BSDialog4 {
html += ' </div>';
html += '</div>';
a.#appendHtml(a.#body, html);
a.appendHtml(a.body, html);
modal = a.Find(id);
if (modal === null) {
return;
}
$("#" + a.#prefix + id).on('hidden.bs.modal', function (event) {
$("#" + a.pfx + id).on('hidden.bs.modal', function (event) {
a.Close(id);
})
@ -263,12 +241,11 @@ class BSDialog4 {
e.stopPropagation();
e.preventDefault();
a.#toggleSize(id);
a.toggleSize();
});
}
async #showButtonPrompt(id, options) {
},
showButtonPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
@ -313,9 +290,8 @@ class BSDialog4 {
});
});
}
async #showTextboxPrompt(id, options) {
},
showTextboxPrompt: async function (id, options) {
const a = this;
return await new Promise(async (resolve, reject) => {
@ -334,13 +310,13 @@ class BSDialog4 {
let body = '';
if (!a.#isNullOrWhitespace(options.Message)) {
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)) {
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>';
@ -399,26 +375,24 @@ class BSDialog4 {
});
});
}
#appendHtml(el, html) {
},
appendHtml: function (el, html) {
let node = document.createElement('template');
node.innerHTML = html;
node = node.content.firstChild;
el.appendChild(node);
}
#html(el, newHtml) {
},
html: function (el, newHtml) {
/// todo: replace with pure JS
if (jQuery) {
jQuery(el).html(newHtml);
} else {
el.innerHTML = newHtml;
}
}
#isNullOrWhitespace(e) {
},
isNullOrWhitespace: function(e) {
if (typeof (e) == "undefined") {
return true;
}
@ -432,29 +406,24 @@ class BSDialog4 {
}
return (e.trim().length <= 0);
}
#removeBackdrop() {
const a = this;
if (a.#body.querySelectorAll(".modal-backdrop").length <= 0) {
},
removeBackdrop: function () {
if (this.body.querySelectorAll(".modal-backdrop").length <= 0) {
return;
}
if (a.#body.querySelectorAll(".modal").length > 0) {
if (this.body.querySelectorAll(".modal").length > 0) {
return;
}
a.#body.querySelectorAll(".modal-backdrop").forEach(function(e){
e.parentNode.removeChild(e);
});
let backdrop = this.body.querySelectorAll(".modal-backdrop")[0];
backdrop.parentNode.removeChild(backdrop);
// unlock background
a.#body.classList.remove("modal-open");
a.#body.style.overflow = null;
}
async #retrieveURL(url) {
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',
@ -465,33 +434,28 @@ class BSDialog4 {
resolve("Error: " + error);
});
});
}
#toggleSize(id) {
},
toggleSize: function () {
const a = this;
let modal = a.Find(id);
let modal = a.Find(a.id);
if (modal === null) {
return;
}
let modalDialog = modal.Modal.querySelectorAll(".modal-dialog")[0];
if (modalDialog.classList.contains('modal-sm')) {
a.Update({ ID: id, Size: "md" });
a.Update({ ID: a.id, Size: "md" });
} else if (modalDialog.classList.contains('modal-md')) {
a.Update({ ID: id, Size: "lg" });
a.Update({ ID: a.id, Size: "lg" });
} else if (modalDialog.classList.contains('modal-lg')) {
a.Update({ ID: id, Size: "xl" });
a.Update({ ID: a.id, Size: "xl" });
} else if (modalDialog.classList.contains('modal-xl')) {
a.Update({ ID: id, Size: "xxl" });
a.Update({ ID: a.id, Size: "xxl" });
} else if (modalDialog.classList.contains('modal-xxl')) {
a.Update({ ID: id, Size: "sm" });
a.Update({ ID: a.id, Size: "sm" });
} else {
a.Update({ ID: id, Size: "md" });
a.Update({ ID: a.id, Size: "md" });
}
}
}
var BSDialog = new BSDialog4();
};

4
bsdialog4.min.js vendored

File diff suppressed because one or more lines are too long