diff --git a/2019/08/bsdialog-v0.1.0.026.js b/2019/08/bsdialog-v0.1.0.026.js new file mode 100644 index 0000000..3acdc4b --- /dev/null +++ b/2019/08/bsdialog-v0.1.0.026.js @@ -0,0 +1,152 @@ +/** + * BSDialog + * @version v0.1.0.026 (2019/08/22 0052) + */ +var BSDialog = { + Create: function (id, title, url, is_big, update_body) { + var a = this; + + a.id = id; + a.title = title; + a.url = url; + a.isBig = ((typeof (is_big) == "undefined") ? false : (is_big == true) ? true : false); + + var updateBody = ((typeof (update_body) == "undefined") ? true : (update_body == true) ? true : false); + + if (!a.Exists(id)) { + a.renderContent(null); + } + + if (url != null) { + $.ajax({ + url: url, + cache: false, + timeout: 60000, + success: function (result, status, xhr) { + if ((xhr.status == 200) || (xhr.status == 302) || (xhr.status == 301)) { + if (updateBody) { + a.updateContentBody(id, result); + } else { + $("#dlg" + id).find(".modal-content").html(result); + } + } else { + a.updateContentBody(id, xhr.statusText + " (" + xhr.status + ")"); + } + }, + error: function (xhr) { + a.updateContentBody(id, xhr.statusText + " (" + xhr.status + ")"); + }, + complete: function (xhr, status) { + // do nothing yet + } + }); + } + + a.initialiseComponents(); + }, + Close: function (id) { + $("#dlg" + id).modal('hide'); + }, + Clear: function () { + $("body > div[class~='modal'][role='dialog']").remove(); + $("body > div[class~='modal-backdrop']").remove(); + $("body").removeClass("modal-open"); + }, + ShowToast: function (id, title, message, is_big) { + var a = this; + + if (a.Exists(id)) { + return; + } + + a.id = id; + a.title = title; + a.url = null; + a.isBig = ((typeof (is_big) == "undefined") ? false : (is_big == true) ? true : false); + + a.renderContent(message); + a.initialiseComponents(); + }, + Exists: function (id) { + return ($("body > div[id='dlg" + id + "']").length > 0); + }, + generateModalHtml: function (message) { + var a = this; + var size = ((typeof (a.isBig) == "undefined") ? "md" : (a.isBig == true ? "lg" : "md")); + + var html = ""; + html += "
"; + html += "
"; + html += "
"; + html += "
"; + html += " "; + html += "  "; + html += " "; + html += " " + a.title + ""; + html += "
"; + + if ($.trim(message).length <= 0) { + html += "
"; + } else { + html += "
" + message + "
"; + } + + html += "
"; + html += " "; + html += "
"; + html += "
"; + html += "
"; + html += "
"; + + return html; + }, + renderContent: function (content) { + $("body").append(this.generateModalHtml(content)); + }, + initialiseComponents: function () { + var a = this; + var dialog = a.getElement(); + + var btnToggleSize = $(dialog).find("button[data-modal-action='restore']"); + if ($(btnToggleSize).length > 0) { + $(btnToggleSize).off('click'); + $(btnToggleSize).on('click', function () { + a.toggleSize(); + }); + } + + $(dialog).modal('show'); + $(dialog).off('hide.bs.modal'); + $(dialog).on('hide.bs.modal', function () { + if ($(dialog).next().is("div[class~='modal-backdrop']")) { + $(dialog).next().remove(); + } + + $(dialog).remove(); + }); + }, + updateContentBody: function (id, text) { + var body = $("#dlg" + id).find(".modal-body"); + + if ($(body).hasClass("custom-loading")) $(body).removeClass("custom-loading"); + + $(body).html(text); + }, + getElement: function () { + return $("#dlg" + this.id); + }, + toggleSize: function () { + var div = $(this.getElement()).find("div[class^='modal-dialog']"); + if ($(div).length <= 0) { + return; + } + + if ($(div).hasClass("modal-md")) { + $(div).removeClass("modal-md"); + $(div).addClass("modal-lg"); + } else if ($(div).hasClass("modal-lg")) { + $(div).removeClass("modal-lg"); + $(div).addClass("modal-md"); + } + } +}; \ No newline at end of file