@@ -84,7 +84,7 @@ document.addEventListener('DOMContentLoaded', () => {
84
84
*
85
85
* @param e
86
86
*/
87
- toggleOption : function ( e ) {
87
+ toggleOption : async function ( e ) {
88
88
/**
89
89
* Make sure event target is a toggle.
90
90
*/
@@ -109,11 +109,13 @@ document.addEventListener('DOMContentLoaded', () => {
109
109
// Toggle: off
110
110
button . classList . replace ( 'bg-indigo-600' , 'bg-gray-200' ) ;
111
111
toggle . classList . replace ( 'translate-x-5' , 'translate-x-0' ) ;
112
+ button . dataset . status = 'off' ;
112
113
toggleStatus = '' ;
113
114
} else {
114
115
// Toggle: on
115
116
button . classList . replace ( 'bg-gray-200' , 'bg-indigo-600' ) ;
116
117
toggle . classList . replace ( 'translate-x-0' , 'translate-x-5' ) ;
118
+ button . dataset . status = 'on' ;
117
119
toggleStatus = 'on' ;
118
120
}
119
121
@@ -126,7 +128,13 @@ document.addEventListener('DOMContentLoaded', () => {
126
128
form . append ( 'is_list' , button . dataset . list ) ;
127
129
form . append ( '_nonce' , plausible . nonce ) ;
128
130
129
- plausible . ajax ( form ) ;
131
+ let data = await plausible . ajax ( form ) ;
132
+
133
+ if ( data . capabilities === undefined ) {
134
+ return ;
135
+ }
136
+
137
+ plausible . maybeDisableOptions ( data . capabilities ) ;
130
138
} ,
131
139
132
140
/**
@@ -160,6 +168,33 @@ document.addEventListener('DOMContentLoaded', () => {
160
168
plausible . ajax ( form , button ) ;
161
169
} ,
162
170
171
+ /**
172
+ * Disable options based on the capabilities retrieved from the API.
173
+ *
174
+ * @param capabilities
175
+ */
176
+ maybeDisableOptions : function ( capabilities ) {
177
+ let options = document . querySelectorAll ( 'button[data-caps]' ) ;
178
+
179
+ options . forEach ( function ( option ) {
180
+ let caps = option . dataset . caps . split ( ',' ) ;
181
+ let disable = false ;
182
+
183
+ caps . forEach ( function ( cap ) {
184
+ if ( capabilities [ cap ] === false ) {
185
+ disable = true ;
186
+ }
187
+ } ) ;
188
+
189
+ if ( disable === true ) {
190
+ // Trigger a click to make sure the option is disabled.
191
+ if ( option . dataset . status === 'on' ) {
192
+ option . dispatchEvent ( new Event ( 'click' , { bubbles : true } ) ) ;
193
+ }
194
+ }
195
+ } ) ;
196
+ } ,
197
+
163
198
/**
164
199
* Currently only validates the domain_name input, but can be used in the future for other custom input validations.
165
200
*
@@ -375,7 +410,8 @@ document.addEventListener('DOMContentLoaded', () => {
375
410
}
376
411
}
377
412
378
- if ( response . status === 200 ) {
413
+ // We still want the data, if it's a Payment Required error.
414
+ if ( response . status === 200 || response . status === 402 ) {
379
415
return response . json ( ) ;
380
416
}
381
417
@@ -478,6 +514,7 @@ document.addEventListener('DOMContentLoaded', () => {
478
514
} , 2000 ) ;
479
515
}
480
516
} ,
517
+
481
518
/**
482
519
* Renders a HTML box containing additional information about the enabled option.
483
520
*
0 commit comments