@@ -13,26 +13,25 @@ export default async function configDialog(style) {
1313 const AUTOSAVE_DELAY = 400 ;
1414 let saving = false ;
1515 let bodyStyle ;
16+ let ucd , varsHash , varNames , vars , varsInitial ;
1617
1718 if ( typeof style === 'number' )
1819 style = await API . styles . getCore ( { id : style , vars : true } ) ;
19- const ucd = style [ UCD ] ;
20- const varsHash = deepCopy ( ucd . vars ) || { } ;
21- const varNames = Object . keys ( varsHash ) ;
22- const vars = varNames . map ( name => varsHash [ name ] ) ;
23- let varsInitial = getInitialValues ( varsHash ) ;
24-
25- const elements = [ ] ;
26- const pathname = location . pathname . slice ( 1 ) ;
27- const isPopup = pathname === 'popup.html' ;
20+
21+ init ( false , false ) ;
22+ const isPopup = document . body . id === 'stylus-popup' ;
2823 const colorpicker = vars . some ( v => v . type === 'color' )
2924 ? ( await import ( '@/js/color/color-picker' ) ) . default ( )
3025 : null ;
31- const buttons = { } ;
32-
33- buildConfigForm ( ) ;
34- renderValues ( ) ;
35- vars . forEach ( renderValueState ) ;
26+ const elBody = $create ( '.config-body' ) ;
27+ const btnSave = $create ( 'button[data-cmd=save]' ,
28+ { disabled : true , onclick : save } ,
29+ t ( 'confirmSave' ) ) ;
30+ const btnDefault = $create ( 'button[data-cmd=default]' ,
31+ { disabled : true , onclick : useDefault , title : t ( 'optionsReset' ) } ,
32+ t ( 'genericResetLabel' ) ) ;
33+ const btnClose = $create ( 'button[data-cmd=close]' ,
34+ t ( 'confirmClose' ) ) ;
3635
3736 return messageBox . show ( {
3837 title : `${ style . customName || style . name } v${ ucd . version } ` ,
@@ -41,18 +40,9 @@ export default async function configDialog(style) {
4140 $create ( '.config-heading' , ucd . supportURL &&
4241 $createLink ( { className : '.external-support' , href : ucd . supportURL } ,
4342 t ( 'externalFeedback' ) ) ) ,
44- $create ( '.config-body' , elements ) ,
45- ] ,
46- buttons : [
47- $create ( 'button[data-cmd=save]' ,
48- { disabled : true , onclick : save } ,
49- t ( 'confirmSave' ) ) ,
50- $create ( 'button[data-cmd=default]' ,
51- { disabled : true , onclick : useDefault , title : t ( 'optionsReset' ) } ,
52- t ( 'genericResetLabel' ) ) ,
53- $create ( 'button[data-cmd=close]' ,
54- t ( 'confirmClose' ) ) ,
43+ elBody ,
5544 ] ,
45+ buttons : [ btnSave , btnDefault , btnClose ] ,
5646 onshow,
5747 } ) . then ( onhide ) ;
5848
@@ -74,19 +64,38 @@ export default async function configDialog(style) {
7464 t ( 'configOnChange' ) ,
7565 ] ) ) ;
7666 setupLivePrefs ( [ 'config.autosave' ] ) ;
77- box . style . setProperty ( '--num' , vars . length ) ;
78- if ( isPopup && ! MOBILE ) {
79- adjustSizeForPopup ( box ) ;
80- }
8167 box . on ( 'change' , onchange ) ;
82- buttons . save = box . $ ( '[data-cmd="save"]' ) ;
83- buttons . default = box . $ ( '[data-cmd="default"]' ) ;
84- buttons . close = box . $ ( '[data-cmd="close"]' ) ;
85- updateButtons ( ) ;
86- updateOverlayScrollbar ( $id ( 'message-box-contents' ) ) ;
68+ init ( null , box ) ;
69+ }
70+
71+ function init ( newUCD , box = messageBox . element ) {
72+ if ( newUCD || ! ucd ) {
73+ ucd = newUCD || style [ UCD ] ;
74+ Object . defineProperty ( style , UCD , {
75+ get : ( ) => ucd ,
76+ set : init ,
77+ } ) ;
78+ varsHash = deepCopy ( ucd . vars ) || { } ;
79+ varNames = Object . keys ( varsHash ) ;
80+ vars = varNames . map ( name => varsHash [ name ] ) ;
81+ varsInitial = getInitialValues ( varsHash ) ;
82+ }
83+ if ( box ) {
84+ elBody . textContent = '' ;
85+ buildConfigForm ( ) ;
86+ renderValues ( ) ;
87+ vars . forEach ( renderValueState ) ;
88+ box . style . setProperty ( '--num' , vars . length ) ;
89+ if ( isPopup && ! MOBILE ) {
90+ adjustSizeForPopup ( box ) ;
91+ }
92+ updateButtons ( ) ;
93+ updateOverlayScrollbar ( $id ( 'message-box-contents' ) ) ;
94+ }
8795 }
8896
8997 function onhide ( ) {
98+ Object . defineProperty ( style , UCD , { value : ucd } ) ;
9099 if ( bodyStyle != null ) document . body . style . cssText = bodyStyle ;
91100 colorpicker ?. hide ( ) ;
92101 }
@@ -109,9 +118,9 @@ export default async function configDialog(style) {
109118
110119 function updateButtons ( ) {
111120 const someDirty = vars . some ( va => va . dirty ) ;
112- buttons . save . disabled = ! someDirty ;
113- buttons . default . disabled = vars . every ( isDefault ) ;
114- buttons . close . textContent = t ( someDirty ? 'confirmCancel' : 'confirmClose' ) ;
121+ btnSave . disabled = ! someDirty ;
122+ btnDefault . disabled = vars . every ( isDefault ) ;
123+ btnClose . textContent = t ( someDirty ? 'confirmCancel' : 'confirmClose' ) ;
115124 }
116125
117126 function updateOverlayScrollbar ( el ) {
@@ -207,6 +216,7 @@ export default async function configDialog(style) {
207216 }
208217
209218 function buildConfigForm ( ) {
219+ const elements = [ ] ;
210220 let resetter =
211221 $create ( 'a.config-reset-icon' , { tabIndex : 0 , title : t ( 'genericResetLabel' ) } ,
212222 $create ( 'i.i-close' ) ) ;
@@ -304,6 +314,7 @@ export default async function configDialog(style) {
304314
305315 va . savedValue = va . value ;
306316 }
317+ elBody . append ( ...elements ) ;
307318 }
308319
309320 function updateVarOnBlur ( ) {
0 commit comments