-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspDialog.min.js
33 lines (33 loc) · 9.59 KB
/
spDialog.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class spDialog
{static __instances=0;static __version='00.00.08 ALPHA';constructor(params){this.textTitle=params.textTitle||'';this.textBody=params.textBody||(params.showAnswerFalse?'Do you want to continue?':'');this.customBody=params.customBody||null;this.textAnswerTrue=params.textAnswerTrue||(params.showAnswerFalse?'Yes':'OK');this.textAnswerFalse=params.textAnswerFalse||'No';this.textAnswerCancel=params.textAnswerCancel||'Cancel';this.dialogWidth=params.dialogWidth||'375px';this.dialogHeight=params.dialogHeight||'250px';this.dialogMinWidth=params.dialogMinWidth||'150px';this.dialogMinHeight=params.dialogMinHeight||'100px';this.dialogFont=params.dialogFont||'Verdana, Geneva, Tahoma, sans-serif';this.dialogFontSize=params.dialogFontSize||'1rem';this.dialogNoAnimate=params.dialogNoAnimate||!1;this.dialogAnimateTime=params.dialogAnimateTime||.25;this.dialogSquareCorners=params.dialogSquareCorners||!1;this.weightAnswer=params.weightAnswer||'bold';this.zIndex=params.zIndex||7000000;this.allowBodyScroll=params.allowBodyScroll||!1;this.showAnswerFalse=params.showAnswerFalse||!1;this.showAnswerCancel=params.showAnswerCancel||!1;this.colorAnswerTrue=params.colorAnswerTrue||'#dbdbdb';this.colorAnswerFalse=params.colorAnswerFalse||'#dbdbdb';this.colorAnswerCancel=params.colorAnswerCancel||'#dbdbdb';this.colorAnswerHoverTrue=params.colorAnswerHoverTrue||'#d0d0d0';this.colorAnswerHoverFalse=params.colorAnswerHoverFalse||'#d0d0d0';this.colorAnswerHoverCancel=params.colorAnswerHoverCancel||'#d0d0d0';if(!this.showAnswerFalse){this.showAnswerCancel=!1}
this.onClose=params.onClose||undefined;this.onOpen=params.onOpen||undefined;this.dialogBuilt=!1;this.modalCover=null;this.modalDialog=null;this.dialogTitle=null;this.dialogBody=null;this.dialogAnswers=null;this.answerWrapper=null;this.answerTrue=null;this.answerFalse=null;this.answerCancel=null;this.userResponse=undefined;this._instances=++spDialog.__instances;this.__firstRun=!0;this.__previousScrollType=null;this.__result;this.Init()}
GetVersion(){return spDialog.__version}
ShowDialog(){let self=this;if(self.__firstRun){setTimeout(()=>{self.__firstRun=!1;self.__ShowDialogReady()},5)}else{self.__ShowDialogReady()}
return new Promise((resolve,reject)=>{self.__result=resolve})}
__ShowDialogReady(){try
{if(!this.dialogBuilt){throw new Error(`The dialog has not yet been built.`)}
this.userResponse=undefined;if(this.modalCover!=null){this.modalCover.style.opacity=1;this.modalCover.style.pointerEvents='all';this.__previousScrollType=document.body.style.overflow;if(!this.allowBodyScroll){document.body.style.overflow='hidden'}
if(this.onOpen!=undefined){this.onOpen()}
return!0}else{throw new Error(`This instance of spDialog has not been initialized. Call Init() to reinitialize the dialog`)}}catch(e){console.error(e.message,e.name);return!1}}
CloseDialog(response){try
{this.modalCover.style.opacity=0;this.modalCover.style.pointerEvents='none';this.userResponse=response;if(!this.allowBodyScroll){document.body.style.overflow=this.__previousScrollType}
if(this.onClose!=undefined){this.onClose(response)}
this.__result(response);return!0}catch(e){console.error(e.message,e.name);this.__result(response);return!1}}
Init(){this.Destroy();try
{let suffix=this._instances;let n=document.createElement('div');n.id=`spDialogCover_${suffix}`;n.style.backgroundColor='rgba(0,0,0,.5)';n.style.opacity=0;if(!this.dialogNoAnimate){n.style.transition=`${this.dialogAnimateTime}s ease-in-out`}
n.style.position='fixed';n.style.top=0;n.style.left=0;n.style.bottom=0;n.style.right=0;n.style.zIndex=this.zIndex;n.style.boxSizing='content-box';this.modalCover=document.body.appendChild(n);this.modalCover.style.fontFamily=this.dialogFont;this.modalCover.style.fontSize=this.dialogFontSize;let nd=document.createElement('div');nd.id=`spDialog_Box_${suffix}`;nd.style.backgroundColor='#ffffff';nd.style.border='2px solid #000000';nd.style.borderRadius=(!this.dialogSquareCorners?'10px':'0px');nd.style.padding='20px';nd.style.width=this.dialogWidth;nd.style.height=this.dialogHeight;nd.style.minHeight=this.dialogMinHeight;nd.style.minWidth=this.dialogMinWidth;nd.style.maxWidth='95%';nd.style.maxHeight='95%';nd.style.position='relative';nd.style.top='50%';nd.style.left='50%';nd.style.transform='translate(-50%, -50%)';nd.style.display='block';nd.style.boxSizing='inherit';this.modalDialog=this.modalCover.appendChild(nd);this.modalDialog.addEventListener('click',function(event){event.stopPropagation();return!1});let ndt=document.createElement('div');ndt.id=`spDialog_Box_Title_${suffix}`;ndt.style.textAlign='center';ndt.style.fontWeight='bold';ndt.style.fontSize='1em';ndt.innerHTML=this.textTitle;ndt.style.height='20%';ndt.style.overflow='hidden';ndt.style.boxSizing='inherit';this.dialogTitle=ndt;document.getElementById(`spDialog_Box_${suffix}`).appendChild(ndt);let ndb=document.createElement('div');ndb.id=`spDialog_Box_Body_${suffix}`;ndb.style.display='block';ndb.style.textAlign='left';ndb.style.fontWeight='normal';ndb.style.fontSize='1em';ndb.style.paddingBottom='10px';ndb.style.height='60%';ndb.style.overflowY='auto';ndb.style.boxSizing='inherit';if(this.customBody==null){ndb.innerHTML=this.textBody}else{ndb.appendChild(this.customBody);if(this.customBody.style.display=='none'){this.customBody.style.display='block'}}
this.dialogBody=ndb;document.getElementById(`spDialog_Box_${suffix}`).appendChild(ndb);let ndacnt=document.createElement('div');ndacnt.id=`spDialog_Box_Answers_Container_${suffix}`;ndacnt.style.height='20%';ndacnt.style.display='flex';ndacnt.style.flexDirection='column';ndacnt.style.width='100%';document.getElementById(`spDialog_Box_${suffix}`).appendChild(ndacnt);let nda=document.createElement('div');nda.id=`spDialog_Box_Answers_${suffix}`;nda.style.textAlign='center';nda.style.fontWeight='normal';nda.style.position='relative';nda.style.display='table';nda.style.borderCollapse='separate';nda.style.borderSpacing='5px';nda.style.marginTop='auto';nda.style.marginLeft='auto';nda.style.marginRight='auto';nda.style.alignSelf='flex-start';nda.style.boxSizing='inherit';let answers=`
<div id='spDialog_Box_Answers_Wrapper_${suffix}'>
<div id='spDialog_Box_Answer_True_${suffix}'>${this.textAnswerTrue}</div>
<div id='spDialog_Box_Answer_False_${suffix}' ${ (!this.showAnswerFalse ? "style='display: none'" : '') }>${this.textAnswerFalse}</div>
<div id='spDialog_Box_Answer_Cancel_${suffix}' ${ (!this.showAnswerCancel ? "style='display: none'" : '') }>${this.textAnswerCancel}</div>
</div>`;nda.innerHTML=answers;ndacnt.appendChild(nda);let self=this;let ndat=document.getElementById(`spDialog_Box_Answer_True_${suffix}`);ndat.addEventListener('click',function(event){self.CloseDialog(1)});ndat.addEventListener('mouseenter',function(event){this.style.backgroundColor=self.colorAnswerHoverTrue});ndat.addEventListener('mouseleave',function(event){this.style.backgroundColor=self.colorAnswerTrue});ndat.style.display='table-cell';ndat.style.cursor='pointer';ndat.style.border='1px solid #000000';ndat.style.borderRadius='5px';ndat.style.padding='5px';ndat.style.fontSize='.75em';ndat.style.backgroundColor=this.colorAnswerTrue;ndat.style.width='33%';ndat.style.textAlign='center';ndat.style.boxSizing='inherit';this.answerTrue=ndat;if(this.showAnswerFalse){let ndaf=document.getElementById(`spDialog_Box_Answer_False_${suffix}`);ndaf.addEventListener('click',function(event){self.CloseDialog(0)});ndaf.addEventListener('mouseenter',function(event){this.style.backgroundColor=self.colorAnswerHoverFalse});ndaf.addEventListener('mouseleave',function(event){this.style.backgroundColor=self.colorAnswerFalse});ndaf.style.display='table-cell';ndaf.style.cursor='pointer';ndaf.style.border='1px solid #000000';ndaf.style.borderRadius='5px';ndaf.style.padding='5px';ndaf.style.fontSize='.75em';ndaf.style.backgroundColor=this.colorAnswerFalse;ndaf.style.width='33%';ndaf.style.textAlign='center';ndaf.style.boxSizing='inherit';this.answerFalse=ndaf}
if(this.showAnswerCancel){let ndac=document.getElementById(`spDialog_Box_Answer_Cancel_${suffix}`);ndac.addEventListener('click',function(event){self.CloseDialog(-1)});ndac.addEventListener('mouseenter',function(event){this.style.backgroundColor=self.colorAnswerHoverCancel});ndac.addEventListener('mouseleave',function(event){this.style.backgroundColor=self.colorAnswerCancel});ndac.style.display='table-cell';ndac.style.cursor='pointer';ndac.style.border='1px solid #000000';ndac.style.borderRadius='5px';ndac.style.padding='5px';ndac.style.fontSize='.75em';ndac.style.backgroundColor=this.colorAnswerCancel;ndac.style.width='33%';ndac.style.textAlign='center';ndac.style.boxSizing='inherit';this.answerCancel=ndac}
let ndaw=document.getElementById(`spDialog_Box_Answers_Wrapper_${suffix}`);ndaw.style.position='relative';ndaw.style.fontWeight=this.weightAnswer;ndaw.style.display='table-row';ndaw.style.boxSizing='inherit';this.answerWrapper=ndaw;this.dialogAnswers=ndacnt;this.modalCover.style.pointerEvents='none';if(!this.showAnswerFalse&&!this.showAnswerCancel){this.modalCover.addEventListener('click',function(event){self.CloseDialog(1)})}
this.dialogBuilt=!0;return!0}catch(e){console.error(e.message,e.name);return!1}}
Destroy(){try
{if(this.modalCover!=null){let tmp=document.getElementById(this.modalCover.id);if(tmp!=null){while(tmp.firstChild){tmp.firstChild.remove()}
tmp.parentNode.removeChild(tmp)}
this.modalCover=null;this.modalDialog=null;this.dialogTitle=null;this.dialogBody=null;this.answerWrapper=null;this.answerTrue=null;this.answerFalse=null;this.answerCancel=null;this.dialogBuilt=!1}
return!0}catch(e){console.error(e.message,e.name);return!1}}
__DelayDialog(ms){return Promise.resolve(setTimeout(()=>{return!0},ms))}}