1+ // sp-dialog handling
2+ const spDialogs = [
3+ [ "showInfoSpDialog" , "infoSpDialog" ] ,
4+ [ "showErrorSpDialog" , "errorSpDialog" ] ,
5+ [ "showDestructiveSpDialog" , "destructiveSpDialog" ] ,
6+ [ "showConfirmSpDialog" , "confirmSpDialog" ] ,
7+ [ "showCustomSpDialog" , "customSpDialog" ] ,
8+ ] ;
9+ spDialogs . forEach ( ( [ btn , dlg ] ) => {
10+ const btnEl = document . querySelector ( `#${ btn } ` ) ;
11+ const dlgEl = document . querySelector ( `#${ dlg } ` ) ;
12+ if ( btnEl && dlgEl ) {
13+ btnEl . onclick = ( ) => dlgEl . setAttribute ( "open" ) ;
14+ }
15+ if ( dlgEl ) {
16+ const allTheButtons = document . querySelectorAll ( `#${ dlg } sp-button` ) ;
17+ allTheButtons . forEach ( el => {
18+ el . onclick = ( ) => dlgEl . removeAttribute ( "open" ) ;
19+ } ) ;
20+ }
21+ } ) ;
22+
23+
24+ // dialog handling
25+ document . querySelector ( "#btnOpenDialog" ) . onclick = ( ) => {
26+ document . querySelector ( "#dlgExample" ) . uxpShowModal ( {
27+ title : "Dialog Example" ,
28+ resize : "none" , // "both", "horizontal", "vertical",
29+ size : {
30+ width : 480 ,
31+ height : 240
32+ }
33+ } ) ;
34+ } ;
35+
36+ // enable dialog buttons to close
37+ Array . from ( document . querySelectorAll ( "#dlgExample sp-button" ) ) . forEach ( button => {
38+ button . onclick = ( ) => document . querySelector ( "#dlgExample" ) . close ( ) ;
39+ } ) ;
40+
41+
42+ // programmatic dialog
43+ const openProgrammaticDialog = async ( ) => {
44+ const theDialog = document . createElement ( "dialog" ) ;
45+ const theForm = document . createElement ( "form" ) ;
46+ const theHeading = document . createElement ( "sp-heading" ) ;
47+ const theDivider = document . createElement ( "sp-divider" ) ;
48+ const theBody = document . createElement ( "sp-body" ) ;
49+ const theFooter = document . createElement ( "footer" ) ;
50+ const theActionButton = document . createElement ( "sp-button" ) ;
51+ const theCancelButton = document . createElement ( "sp-button" ) ;
52+
53+ theHeading . textContent = "Vectorize Images?" ;
54+ theDivider . setAttribute ( "size" , "large" ) ;
55+ theBody . textContent = "Are you sure you want to vectorize the images? This might take some time." ;
56+ theActionButton . textContent = "Vectorize" ;
57+ theActionButton . setAttribute ( "variant" , "cta" ) ;
58+ theCancelButton . textContent = "Don't Vectorize" ;
59+ theCancelButton . setAttribute ( "quiet" , "true" ) ;
60+ theCancelButton . setAttribute ( "variant" , "secondary" ) ;
61+
62+ theActionButton . onclick = ( ) => {
63+ theDialog . close ( "ok" ) ;
64+ }
65+ theCancelButton . onclick = ( ) => {
66+ theDialog . close ( "reasonCanceled" ) ;
67+ }
68+
69+ theFooter . appendChild ( theCancelButton ) ;
70+ theFooter . appendChild ( theActionButton ) ;
71+
72+ theForm . appendChild ( theHeading ) ;
73+ theForm . appendChild ( theDivider ) ;
74+ theForm . appendChild ( theBody ) ;
75+ theForm . appendChild ( theFooter ) ;
76+ theDialog . appendChild ( theForm ) ;
77+ document . body . appendChild ( theDialog ) ;
78+
79+ const r = await theDialog . uxpShowModal ( {
80+ title : "Programmatic Dialog" ,
81+ resize : "none" , // "both", "horizontal", "vertical",
82+ size : {
83+ width : 480 ,
84+ height : 240
85+ }
86+ } ) ;
87+ console . log ( r ) ;
88+ theDialog . remove ( ) ;
89+ }
90+ document . querySelector ( "#btnOpenPDialog" ) . onclick = openProgrammaticDialog ;
91+
92+ module . exports = {
93+ openProgrammaticDialog
94+ } ;
0 commit comments