From 462f69f221ff361638fa33882b92a0d1ef7c7be1 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 21 Jan 2024 15:30:00 +0000 Subject: [PATCH] Added movable support --- bsdialog4.js | 127 +++++++++++++++++++++++++++++------------------ bsdialog4.min.js | 4 +- 2 files changed, 80 insertions(+), 51 deletions(-) diff --git a/bsdialog4.js b/bsdialog4.js index 146f8ed..22fab5d 100644 --- a/bsdialog4.js +++ b/bsdialog4.js @@ -1,54 +1,11 @@ /** * BSDialog4 - * @version v0.1.6.015 (2023/12/08 22:04) + * @version v0.1.7.022 (2024/01/21 03:40) */ class BSDialog4 { constructor() { } - - get Options() { - return { - ShowModal: { - ID: null, - Title: "", - Message: "", - URL: null, - Size: "md", - Colour: "secondary", - ShowFooter: true, - EasyClose: true - }, - ShowPrompt: { - Type: "button", - Title: "", - Message: "", - Size: "md", - EasyClose: true, - 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 - } - }, - UpdateModal: { - ID: null, - Title: null, - Body: null, - URL: null, - Footer: null, - Size: null - } - } - } - get #prefix() { return "bsdia4_"; } @@ -70,9 +27,19 @@ class BSDialog4 { async Show(options) { const a = this; - const _options = Object.assign(a.Options.ShowModal, options); + const _options = Object.assign({ + ID: null, + Title: "", + Message: "", + URL: null, + Size: "md", + Colour: "secondary", + ShowFooter: true, + EasyClose: true, + IsMovable: true + }, options); - a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.Colour); + a.addModal(_options.ID, _options.Title, _options.Size, _options.ShowFooter, _options.IsMovable, _options.Colour); $("#" + a.#prefix + _options.ID).modal({ backdrop: _options.EasyClose, @@ -91,7 +58,26 @@ class BSDialog4 { async Prompt(options) { const a = this; const id = Math.floor(Math.random() * 10000) + 1000; - const _options = Object.assign(a.Options.ShowPrompt, options); + const _options = Object.assign({ + Type: "button", + Title: "", + Message: "", + Size: "md", + EasyClose: true, + IsMovable: true, + 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 + } + }, options); switch (_options.Type) { case "textbox": @@ -146,7 +132,14 @@ class BSDialog4 { async Update(options) { const a = this; - let _options = Object.assign(a.Options.UpdateModal, options); + const _options = Object.assign({ + ID: null, + Title: null, + Body: null, + URL: null, + Footer: null, + Size: null + }, options); const modal = a.Find(_options.ID); if (modal === null) { @@ -210,7 +203,7 @@ class BSDialog4 { } - addModal(id, title, size, showFooter, closeColour) { + addModal(id, title, size, showFooter, isMovable, closeColour) { const a = this; // don't allow duplicates @@ -269,6 +262,42 @@ class BSDialog4 { a.#toggleSize(id); }); + if (isMovable) { + const panel = modal.Modal.querySelectorAll(".modal-dialog")[0]; + let isMovable = false; + let currentPos = { X: 0, Y: 0 }; + let movePos = { X: 0, Y: 0 }; + + modal.Title.addEventListener("mousedown", function(e) { + if ((e.buttons == 1) && !isMovable) { + movePos = { X: e.clientX, Y: e.clientY }; + currentPos = { + X: (a.#isNullOrWhitespace(panel.style.left) ? 0 : parseInt(panel.style.left.replace("px", ""))), + Y: (a.#isNullOrWhitespace(panel.style.top) ? 0 : parseInt(panel.style.top.replace("px", ""))) + }; + isMovable = true; + + modal.Title.style.cursor = "move"; + + e.preventDefault(); + } + }); + + document.addEventListener("mouseup", function(e) { + if (isMovable) { + isMovable = false; + modal.Title.style.cursor = "default"; + } + }); + + document.addEventListener("mousemove", function(e) { + if ((e.buttons == 1) && isMovable) { + panel.style.left = ((e.clientX - movePos.X) + currentPos.X) + 'px'; + panel.style.top = ((e.clientY - movePos.Y) + currentPos.Y) + 'px'; + } + }); + } + } async #showButtonPrompt(id, options) { diff --git a/bsdialog4.min.js b/bsdialog4.min.js index 1c73da9..0c67cea 100644 --- a/bsdialog4.min.js +++ b/bsdialog4.min.js @@ -1,5 +1,5 @@ /** * BSDialog4 - * @version v0.1.6.015 (2023/12/08 22:04) + * @version v0.1.7.022 (2024/01/21 03:40) */ -class BSDialog4{constructor(){}get Options(){return{ShowModal:{ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0,EasyClose:!0},ShowPrompt:{Type:"button",Title:"",Message:"",Size:"md",EasyClose:!0,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}},UpdateModal:{ID:null,Title:null,Body:null,URL:null,Footer:null,Size:null}}}get#e(){return"bsdia4_"}get#t(){return document.getElementsByTagName("body")[0]}get#l(){return["modal-sm","modal-md","modal-lg","modal-xl","modal-xxl"]}async Show(e){const t=this,l=Object.assign(t.Options.ShowModal,e);if(t.addModal(l.ID,l.Title,l.Size,l.ShowFooter,l.Colour),$("#"+t.#e+l.ID).modal({backdrop:l.EasyClose,show:!0}),t.#o(l.URL)){const e=await t.#a(l.URL);await t.Update({ID:l.ID,Body:e})}else await t.Update({ID:l.ID,Body:l.Message})}async Prompt(e){const t=this,l=Math.floor(1e4*Math.random())+1e3,o=Object.assign(t.Options.ShowPrompt,e);return"textbox"===o.Type?await t.#s(l,o):await t.#i(l,o)}async Clear(){const e=this;document.querySelectorAll(".modal").forEach((async function(t){await e.Close(t.getAttribute("id"))}))}async Close(e){const t=this;e.toString().startsWith(t.#e)&&(e=e.toString().substr(t.#e.length));const l=document.getElementById(t.#e+e);l&&(await $(l).modal("hide"),l.parentNode.removeChild(l)),document.querySelectorAll(".modal").length>0&&(t.#t.classList.contains("modal-open")||(t.#t.classList.add("modal-open"),t.#t.style.paddingRight="17px"))}async Pop(){const e=this;if(document.querySelectorAll(".modal").length>0){const t=document.querySelectorAll(".modal")[0].getAttribute("id");await e.Close(t)}}async Update(e){const t=this;let l=Object.assign(t.Options.UpdateModal,e);const o=t.Find(l.ID);if(null!==o){if(t.#n(l.Title)||(o.Title.innerHTML=l.Title),t.#n(l.Body)||t.#r(o.Body,l.Body),this.#n(l.Body)){if(this.#o(l.URL)){const e=await t.#a(l.URL);await t.Update({ID:l.ID,Body:e})}}else t.#r(o.Body,l.Body);t.#n(l.Footer)||t.#r(o.Footer,l.Footer),t.#n(l.Size)||t.#d(l.ID,"modal-"+l.Size)}}Exists(e){return null!==this.Find(e)}Find(e){const t=this.#t.querySelectorAll("#"+this.#e+e+".modal");return t?t.length<=0?null:{Title:t[0].querySelectorAll(".modal-title")[0],Header:t[0].querySelectorAll(".modal-header")[0],Body:t[0].querySelectorAll(".modal-body")[0],Footer:t[0].querySelectorAll(".modal-footer")[0],Close:t[0].querySelectorAll("[data-dismiss='modal']"),Modal:t[0]}:null}addModal(e,t,l,o,a){const s=this;let i=s.Find(e);if(null!==i)return;let n="";n+='",s.#c(s.#t,n),i=s.Find(e),null!==i&&($("#"+s.#e+e).on("hidden.bs.modal",(function(t){s.Close(e)})),i.Title.addEventListener("dblclick",(function(t){t.stopPropagation(),t.preventDefault(),s.#u(e)})))}async#i(e,t){const l=this;return await new Promise((async(o,a)=>{await l.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";t.Buttons.forEach((function(e){s+='"})),l.Update({ID:e,Footer:s});const i=l.Find(e);i.Footer.querySelectorAll("button").forEach((function(t){t.addEventListener("click",(function(a){a.stopPropagation(),a.preventDefault();const s=t.getAttribute("data-prompt-value");l.Close(e),o(s)}))})),i.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}async#s(e,t){const l=this;return await new Promise((async(o,a)=>{t.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await l.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let s="";l.#n(t.Message)||(s+="

"+t.Message+"

"),s+='
',l.#n(t.Textbox.Label)||t.Textbox.LabelSize<=0?(s+='
',s+='',s+="
"):(s+='",s+='
',s+='',s+="
"),s+="
";let i="";t.Buttons.forEach((function(e){i+='"})),l.Update({ID:e,Body:s,Footer:i});const n=l.Find(e),r=n.Footer.querySelectorAll("button");r[0].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault();const a=n.Body.querySelectorAll("input")[0].value;l.Close(e),o(a)})),r[1].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")})),n.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}#c(e,t){let l=document.createElement("template");l.innerHTML=t,l=l.content.firstChild,e.appendChild(l)}#r(e,t){jQuery?jQuery(e).html(t):e.innerHTML=t}#n(e){return void 0===e||(null==e||(0==e||e.trim().length<=0))}async#a(e){return await new Promise((async t=>{await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((e=>e.text())).then((e=>{t(e)})).catch((e=>{t("Error: "+e)}))}))}#o(e){return!this.#n(e)&&!!(e.startsWith("http://")||e.startsWith("https://")||e.startsWith("/"))}#d(e,t){const l=this,o=l.Find(e);if(null===o)return;let a=o.Modal.querySelectorAll(".modal-dialog")[0];for(let e=0;et.#l.length-1&&(a=0),o.classList.add(t.#l[a])}}var BSDialog=new BSDialog4; \ No newline at end of file +class BSDialog4{constructor(){}get#e(){return"bsdia4_"}get#t(){return document.getElementsByTagName("body")[0]}get#l(){return["modal-sm","modal-md","modal-lg","modal-xl","modal-xxl"]}async Show(e){const t=this,l=Object.assign({ID:null,Title:"",Message:"",URL:null,Size:"md",Colour:"secondary",ShowFooter:!0,EasyClose:!0,IsMovable:!0},e);if(t.addModal(l.ID,l.Title,l.Size,l.ShowFooter,l.IsMovable,l.Colour),$("#"+t.#e+l.ID).modal({backdrop:l.EasyClose,show:!0}),t.#o(l.URL)){const e=await t.#s(l.URL);await t.Update({ID:l.ID,Body:e})}else await t.Update({ID:l.ID,Body:l.Message})}async Prompt(e){const t=this,l=Math.floor(1e4*Math.random())+1e3,o=Object.assign({Type:"button",Title:"",Message:"",Size:"md",EasyClose:!0,IsMovable:!0,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}},e);return"textbox"===o.Type?await t.#a(l,o):await t.#i(l,o)}async Clear(){const e=this;document.querySelectorAll(".modal").forEach((async function(t){await e.Close(t.getAttribute("id"))}))}async Close(e){const t=this;e.toString().startsWith(t.#e)&&(e=e.toString().substr(t.#e.length));const l=document.getElementById(t.#e+e);l&&(await $(l).modal("hide"),l.parentNode.removeChild(l)),document.querySelectorAll(".modal").length>0&&(t.#t.classList.contains("modal-open")||(t.#t.classList.add("modal-open"),t.#t.style.paddingRight="17px"))}async Pop(){const e=this;if(document.querySelectorAll(".modal").length>0){const t=document.querySelectorAll(".modal")[0].getAttribute("id");await e.Close(t)}}async Update(e){const t=this,l=Object.assign({ID:null,Title:null,Body:null,URL:null,Footer:null,Size:null},e),o=t.Find(l.ID);if(null!==o){if(t.#n(l.Title)||(o.Title.innerHTML=l.Title),t.#n(l.Body)||t.#r(o.Body,l.Body),this.#n(l.Body)){if(this.#o(l.URL)){const e=await t.#s(l.URL);await t.Update({ID:l.ID,Body:e})}}else t.#r(o.Body,l.Body);t.#n(l.Footer)||t.#r(o.Footer,l.Footer),t.#n(l.Size)||t.#d(l.ID,"modal-"+l.Size)}}Exists(e){return null!==this.Find(e)}Find(e){const t=this.#t.querySelectorAll("#"+this.#e+e+".modal");return t?t.length<=0?null:{Title:t[0].querySelectorAll(".modal-title")[0],Header:t[0].querySelectorAll(".modal-header")[0],Body:t[0].querySelectorAll(".modal-body")[0],Footer:t[0].querySelectorAll(".modal-footer")[0],Close:t[0].querySelectorAll("[data-dismiss='modal']"),Modal:t[0]}:null}addModal(e,t,l,o,s,a){const i=this;let n=i.Find(e);if(null!==n)return;let r="";if(r+='",i.#c(i.#t,r),n=i.Find(e),null!==n&&($("#"+i.#e+e).on("hidden.bs.modal",(function(t){i.Close(e)})),n.Title.addEventListener("dblclick",(function(t){t.stopPropagation(),t.preventDefault(),i.#u(e)})),s)){const e=n.Modal.querySelectorAll(".modal-dialog")[0];let t=!1,l={X:0,Y:0},o={X:0,Y:0};n.Title.addEventListener("mousedown",(function(s){1!=s.buttons||t||(o={X:s.clientX,Y:s.clientY},l={X:i.#n(e.style.left)?0:parseInt(e.style.left.replace("px","")),Y:i.#n(e.style.top)?0:parseInt(e.style.top.replace("px",""))},t=!0,n.Title.style.cursor="move",s.preventDefault())})),document.addEventListener("mouseup",(function(e){t&&(t=!1,n.Title.style.cursor="default")})),document.addEventListener("mousemove",(function(s){1==s.buttons&&t&&(e.style.left=s.clientX-o.X+l.X+"px",e.style.top=s.clientY-o.Y+l.Y+"px")}))}}async#i(e,t){const l=this;return await new Promise((async(o,s)=>{await l.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let a="";t.Buttons.forEach((function(e){a+='"})),l.Update({ID:e,Footer:a});const i=l.Find(e);i.Footer.querySelectorAll("button").forEach((function(t){t.addEventListener("click",(function(s){s.stopPropagation(),s.preventDefault();const a=t.getAttribute("data-prompt-value");l.Close(e),o(a)}))})),i.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}async#a(e,t){const l=this;return await new Promise((async(o,s)=>{t.Buttons=[{Label:"OK",Value:"",Colour:"primary"},{Label:"Cancel",Value:"",Colour:"secondary"}],await l.Show({ID:e,Title:t.Title,Message:t.Message,Size:t.Size,EasyClose:t.EasyClose});let a="";l.#n(t.Message)||(a+="

"+t.Message+"

"),a+='
',l.#n(t.Textbox.Label)||t.Textbox.LabelSize<=0?(a+='
',a+='',a+="
"):(a+='",a+='
',a+='',a+="
"),a+="
";let i="";t.Buttons.forEach((function(e){i+='"})),l.Update({ID:e,Body:a,Footer:i});const n=l.Find(e),r=n.Footer.querySelectorAll("button");r[0].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault();const s=n.Body.querySelectorAll("input")[0].value;l.Close(e),o(s)})),r[1].addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")})),n.Close.forEach((function(t){t.addEventListener("click",(function(t){t.stopPropagation(),t.preventDefault(),l.Close(e),o("")}))}))}))}#c(e,t){let l=document.createElement("template");l.innerHTML=t,l=l.content.firstChild,e.appendChild(l)}#r(e,t){jQuery?jQuery(e).html(t):e.innerHTML=t}#n(e){return void 0===e||(null==e||(0==e||e.trim().length<=0))}async#s(e){return await new Promise((async t=>{await fetch(e,{cache:"no-cache",credentials:"same-origin"}).then((e=>e.text())).then((e=>{t(e)})).catch((e=>{t("Error: "+e)}))}))}#o(e){return!this.#n(e)&&!!(e.startsWith("http://")||e.startsWith("https://")||e.startsWith("/"))}#d(e,t){const l=this,o=l.Find(e);if(null===o)return;let s=o.Modal.querySelectorAll(".modal-dialog")[0];for(let e=0;et.#l.length-1&&(s=0),o.classList.add(t.#l[s])}}var BSDialog=new BSDialog4; \ No newline at end of file -- 2.45.2