Refactored to stop using deprecated code

This commit is contained in:
Ray 2023-08-30 14:26:40 +01:00
parent e9198dea75
commit d7f4f60e4d
3 changed files with 58 additions and 37 deletions

View File

@ -215,8 +215,13 @@ let response = await BSDialog.Prompt({
Title: "Modal Title", Title: "Modal Title",
Message: "Are you sure want to wait for a response?", Message: "Are you sure want to wait for a response?",
Size: "md", Size: "md",
Label: "Response Text", Textbox: {
Placeholder: "Response Text" Label: "Textbox Label",
LabelSize: 4,
Placeholder: "Placeholder",
Value: "Default Value",
BoxSize: 8
}
}); });
alert(response); alert(response);
@ -232,8 +237,13 @@ alert(response);
Title: "Modal Title", Title: "Modal Title",
Message: "Are you sure want to wait for a response?", Message: "Are you sure want to wait for a response?",
Size: "md", Size: "md",
Label: "Response Text", Textbox: {
Placeholder: "Response Text" Label: "Textbox Label",
LabelSize: 4,
Placeholder: "Placeholder",
Value: "Default Value",
BoxSize: 8
}
}); });
alert(response); alert(response);

View File

@ -15,15 +15,17 @@ var BSDialog = {
a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour); a.addModal(_options.ID, _options.Title, _options.Size, true, _options.Colour);
if (_options.URL == null) { if (_options.URL == null) {
await a.UpdateBody(_options.ID, _options.Message); await a.Update({ ID: _options.ID, Body: _options.Message });
} else { } else {
if (a.isNullOrWhitespace(_options.URL)) { if (a.isNullOrWhitespace(_options.URL)) {
await a.UpdateBody(_options.ID, _options.URL); await a.Update({ ID: _options.ID, Body: _options.URL });
} else { } else {
if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) { if (_options.URL.startsWith("http://") || _options.URL.startsWith("https://") || _options.URL.startsWith("/")) {
await a.UpdateBodyRemote(_options.ID, _options.URL); let response = await a.retrieveURL(_options.URL);
await a.Update({ ID: _options.ID, Body: response });
} else { } else {
await a.UpdateBody(_options.ID, _options.URL); await a.Update({ ID: _options.ID, Body: _options.URL });
} }
} }
} }
@ -96,7 +98,9 @@ var BSDialog = {
} }
if (!this.isNullOrWhitespace(_options.BodyURL)) { if (!this.isNullOrWhitespace(_options.BodyURL)) {
await a.UpdateBodyRemote(_options.ID, _options.BodyURL); let response = await a.retrieveURL(_options.BodyURL);
await a.Update({ ID: _options.ID, Body: response });
} }
if (!this.isNullOrWhitespace(_options.Footer)) { if (!this.isNullOrWhitespace(_options.Footer)) {
@ -122,27 +126,13 @@ var BSDialog = {
}); });
} }
}, },
UpdateBodyRemote: async function (id, url) {
var a = this;
if (!a.Exists(id)) {
return;
}
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);
});
},
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;
} }
@ -180,8 +170,13 @@ var BSDialog = {
{ Label: "No", Value: "No", Colour: "secondary" }, { Label: "No", Value: "No", Colour: "secondary" },
{ Label: "Cancel", Value: "Cancel", Colour: "secondary" } { Label: "Cancel", Value: "Cancel", Colour: "secondary" }
], ],
Textbox: {
Label: "", Label: "",
Placeholder: "" LabelSize: 4,
Placeholder: "",
Value: "",
BoxSize: 8
}
}, },
ToastOptions: { ToastOptions: {
ID: null, ID: null,
@ -293,17 +288,21 @@ var BSDialog = {
const a = this; const a = this;
return await new Promise(async (resolve, reject) => { return await new Promise(async (resolve, reject) => {
await a.Show(id, options.Title, options.Message, options.Size); await a.Show({
ID: id,
Title: options.Title,
Message: options.Message,
Size: options.Size
});
let html = ''; let html = '';
options.Buttons.forEach(function(e) { options.Buttons.forEach(function(e) {
html += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>'; html += '<button type="button" class="btn btn-' + e.Colour + '" data-prompt-value="' + e.Value + '">' + e.Label + '</button>';
}); });
a.UpdateFooter(id, html); a.Update({ ID: id, Footer: html });
const modal = a.Find(id); const modal = a.Find(id);
modal.Footer[0].querySelectorAll("button").forEach(function(button) { modal.Footer[0].querySelectorAll("button").forEach(function(button) {
button.addEventListener("click", function(e){ button.addEventListener("click", function(e){
e.stopPropagation(); e.stopPropagation();
@ -354,14 +353,14 @@ var BSDialog = {
body += '<div class="form-group row">'; body += '<div class="form-group row">';
if (a.isNullOrWhitespace(options.Label)) { if (a.isNullOrWhitespace(options.Textbox.Label) || (options.Textbox.LabelSize <= 0)) {
body += '<div class="col-sm-12">'; body += '<div class="col-sm-12">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Placeholder + '">'; body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>'; body += '</div>';
} else { } else {
body += '<label for="textbox' + id + '" class="col-sm-4 col-form-label">' + options.Label + '</label>'; body += '<label for="textbox' + id + '" class="col-sm-' + options.Textbox.LabelSize + ' col-form-label">' + options.Textbox.Label + '</label>';
body += '<div class="col-sm-8">'; body += '<div class="col-sm-' + options.Textbox.BoxSize + '">';
body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Placeholder + '">'; body += '<input type="text" class="form-control" id="textbox' + id + '" placeholder="' + options.Textbox.Placeholder + '" value="' + options.Textbox.Value + '">';
body += '</div>'; body += '</div>';
} }
@ -461,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;

2
bsdialog4.min.js vendored

File diff suppressed because one or more lines are too long