| 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 |
function getClientPreferences() { |
| 26 |
return Array.from( document.documentElement.classList ).filter( |
| 27 |
( className ) => className.match( /-clientpref-/ ) |
| 28 |
).map( ( className ) => className.split( '-clientpref-' )[ 0 ] ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
function isFeatureExcluded( featureName ) { |
| 38 |
return document.documentElement.classList.contains( featureName + '-clientpref--excluded' ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
function getVisibleClientPreferences( config ) { |
| 48 |
const active = getClientPreferences(); |
| 49 |
|
| 50 |
return Object.keys( config ).filter( ( key ) => active.indexOf( key ) > -1 ); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
function toggleDocClassAndSave( featureName, value, config, userPreferences ) { |
| 60 |
const pref = config[ featureName ]; |
| 61 |
const callback = pref.callback || ( () => {} ); |
| 62 |
if ( mw.user.isNamed() ) { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
config[ featureName ].options.forEach( ( possibleValue ) => { |
| 68 |
document.documentElement.classList.remove( `${ featureName }-clientpref-${ possibleValue }` ); |
| 69 |
} ); |
| 70 |
document.documentElement.classList.add( `${ featureName }-clientpref-${ value }` ); |
| 71 |
|
| 72 |
|
| 73 |
window.dispatchEvent( new Event( 'resize' ) ); |
| 74 |
|
| 75 |
mw.util.debounce( () => { |
| 76 |
userPreferences = userPreferences || new mw.Api(); |
| 77 |
userPreferences.saveOptions( { [ pref.preferenceKey ]: value } ).then( () => { |
| 78 |
callback(); |
| 79 |
} ); |
| 80 |
}, 100 )(); |
| 81 |
|
| 82 |
} else { |
| 83 |
|
| 84 |
mw.user.clientPrefs.set( featureName, value ); |
| 85 |
callback(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
const getInputId = ( featureName, value ) => `skin-client-pref-${ featureName }-value-${ value }`; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
function makeInputElement( type, featureName, value ) { |
| 103 |
const input = document.createElement( 'input' ); |
| 104 |
const name = `skin-client-pref-${ featureName }-group`; |
| 105 |
const id = getInputId( featureName, value ); |
| 106 |
input.name = name; |
| 107 |
input.id = id; |
| 108 |
input.type = type; |
| 109 |
if ( type === 'checkbox' ) { |
| 110 |
input.checked = value === '1'; |
| 111 |
} else { |
| 112 |
input.value = value; |
| 113 |
} |
| 114 |
input.setAttribute( 'data-event-name', id ); |
| 115 |
return input; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
function makeLabelElement( featureName, value ) { |
| 124 |
const label = document.createElement( 'label' ); |
| 125 |
|
| 126 |
label.textContent = mw.msg( `${ featureName }-${ value }-label` ); |
| 127 |
label.setAttribute( 'for', getInputId( featureName, value ) ); |
| 128 |
return label; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
function makeExclusionNotice( featureName ) { |
| 140 |
const p = document.createElement( 'p' ); |
| 141 |
|
| 142 |
const noticeMessage = mw.message( `${ featureName }-exclusion-notice` ); |
| 143 |
p.classList.add( 'exclusion-notice', `${ featureName }-exclusion-notice` ); |
| 144 |
p.textContent = noticeMessage.text(); |
| 145 |
return p; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
function makeBetaInfoTag() { |
| 152 |
const infoTag = document.createElement( 'span' ); |
| 153 |
|
| 154 |
const infoTagText = document.createElement( 'span' ); |
| 155 |
infoTagText.textContent = mw.message( 'vector-night-mode-beta-tag' ).text(); |
| 156 |
infoTag.appendChild( infoTagText ); |
| 157 |
return infoTag; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
function appendRadioToggle( parent, featureName, value, currentValue, config, userPreferences ) { |
| 169 |
const input = makeInputElement( 'radio', featureName, value ); |
| 170 |
input.classList.add( 'cdx-radio__input' ); |
| 171 |
if ( currentValue === value ) { |
| 172 |
input.checked = true; |
| 173 |
} |
| 174 |
|
| 175 |
if ( isFeatureExcluded( featureName ) ) { |
| 176 |
input.disabled = true; |
| 177 |
} |
| 178 |
|
| 179 |
const icon = document.createElement( 'span' ); |
| 180 |
icon.classList.add( 'cdx-radio__icon' ); |
| 181 |
const label = makeLabelElement( featureName, value ); |
| 182 |
label.classList.add( 'cdx-radio__label' ); |
| 183 |
const container = document.createElement( 'div' ); |
| 184 |
container.classList.add( 'cdx-radio' ); |
| 185 |
container.appendChild( input ); |
| 186 |
container.appendChild( icon ); |
| 187 |
container.appendChild( label ); |
| 188 |
parent.appendChild( container ); |
| 189 |
input.addEventListener( 'change', () => { |
| 190 |
toggleDocClassAndSave( featureName, value, config, userPreferences ); |
| 191 |
} ); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
function makeFeedbackLink( betaMessageElement ) { |
| 198 |
if ( !mw.msg( 'vector-night-mode-issue-reporting-notice-url' ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
const pageWikiLink = `[https://${ window.location.hostname + mw.util.getUrl( mw.config.get( 'wgPageName' ) ) } ${ mw.config.get( 'wgTitle' ) }]`; |
| 202 |
const preloadTitle = mw.message( 'vector-night-mode-issue-reporting-preload-title', pageWikiLink ).text(); |
| 203 |
const link = mw.msg( 'vector-night-mode-issue-reporting-notice-url', window.location.host, preloadTitle ); |
| 204 |
const linkLabel = mw.message( 'vector-night-mode-issue-reporting-link-label' ).text(); |
| 205 |
const anchor = document.createElement( 'a' ); |
| 206 |
anchor.setAttribute( 'href', link ); |
| 207 |
anchor.setAttribute( 'target', '_blank' ); |
| 208 |
anchor.setAttribute( 'title', mw.msg( 'vector-night-mode-issue-reporting-notice-tooltip' ) ); |
| 209 |
anchor.textContent = linkLabel; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
const showSuccessFeedback = function ( event ) { |
| 219 |
event.stopPropagation(); |
| 220 |
const icon = document.createElement( 'span' ); |
| 221 |
icon.classList.add( 'vector-icon', 'vector-icon--heart' ); |
| 222 |
anchor.textContent = mw.msg( 'vector-night-mode-issue-reporting-link-notification' ); |
| 223 |
anchor.classList.add( 'skin-theme-beta-notice-success' ); |
| 224 |
anchor.prepend( icon ); |
| 225 |
anchor.removeEventListener( 'click', showSuccessFeedback ); |
| 226 |
}; |
| 227 |
anchor.addEventListener( 'click', ( event ) => showSuccessFeedback( event ) ); |
| 228 |
betaMessageElement.appendChild( anchor ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
function appendToggleSwitch( |
| 240 |
form, featureName, labelElement, currentValue, config, userPreferences |
| 241 |
) { |
| 242 |
const input = makeInputElement( 'checkbox', featureName, currentValue ); |
| 243 |
input.classList.add( 'cdx-toggle-switch__input' ); |
| 244 |
const switcher = document.createElement( 'span' ); |
| 245 |
switcher.classList.add( 'cdx-toggle-switch__switch' ); |
| 246 |
const grip = document.createElement( 'span' ); |
| 247 |
grip.classList.add( 'cdx-toggle-switch__switch__grip' ); |
| 248 |
switcher.appendChild( grip ); |
| 249 |
const label = labelElement || makeLabelElement( featureName, currentValue ); |
| 250 |
label.classList.add( 'cdx-toggle-switch__label' ); |
| 251 |
const toggleSwitch = document.createElement( 'span' ); |
| 252 |
toggleSwitch.classList.add( 'cdx-toggle-switch' ); |
| 253 |
toggleSwitch.appendChild( input ); |
| 254 |
toggleSwitch.appendChild( switcher ); |
| 255 |
toggleSwitch.appendChild( label ); |
| 256 |
input.addEventListener( 'change', () => { |
| 257 |
toggleDocClassAndSave( featureName, input.checked ? '1' : '0', config, userPreferences ); |
| 258 |
} ); |
| 259 |
form.appendChild( toggleSwitch ); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
function createRow( className ) { |
| 267 |
const row = document.createElement( 'div' ); |
| 268 |
row.setAttribute( 'class', className ); |
| 269 |
return row; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
const getFeatureLabelMsg = ( featureName ) => mw.message( `${ featureName }-name` ); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
function makeControl( featureName, config, userPreferences ) { |
| 290 |
const pref = config[ featureName ]; |
| 291 |
const isExcluded = isFeatureExcluded( featureName ); |
| 292 |
|
| 293 |
if ( !pref ) { |
| 294 |
return null; |
| 295 |
} |
| 296 |
const currentValue = mw.user.clientPrefs.get( featureName ); |
| 297 |
|
| 298 |
|
| 299 |
if ( typeof currentValue === 'boolean' && !isExcluded ) { |
| 300 |
return null; |
| 301 |
} |
| 302 |
const row = createRow( '' ); |
| 303 |
const form = document.createElement( 'form' ); |
| 304 |
const type = pref.type || 'radio'; |
| 305 |
switch ( type ) { |
| 306 |
case 'radio': |
| 307 |
pref.options.forEach( ( value ) => { |
| 308 |
appendRadioToggle( |
| 309 |
form, featureName, value, String( currentValue ), config, userPreferences |
| 310 |
); |
| 311 |
} ); |
| 312 |
break; |
| 313 |
case 'switch': { |
| 314 |
const labelElement = document.createElement( 'label' ); |
| 315 |
labelElement.textContent = getFeatureLabelMsg( featureName ).text(); |
| 316 |
appendToggleSwitch( |
| 317 |
form, featureName, labelElement, String( currentValue ), config, userPreferences |
| 318 |
); |
| 319 |
break; |
| 320 |
} default: |
| 321 |
throw new Error( 'Unknown client preference! Only switch or radio are supported.' ); |
| 322 |
} |
| 323 |
row.appendChild( form ); |
| 324 |
|
| 325 |
if ( isExcluded ) { |
| 326 |
const exclusionNotice = makeExclusionNotice( featureName ); |
| 327 |
row.appendChild( exclusionNotice ); |
| 328 |
} |
| 329 |
return row; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
function makeClientPreference( parent, featureName, config, userPreferences ) { |
| 339 |
const labelMsg = getFeatureLabelMsg( featureName ); |
| 340 |
|
| 341 |
|
| 342 |
if ( !labelMsg.exists() && mw.config.get( 'wgUserLanguage' ) !== 'qqx' ) { |
| 343 |
return; |
| 344 |
} else { |
| 345 |
const id = `skin-client-prefs-${ featureName }`; |
| 346 |
|
| 347 |
const portlet = mw.util.addPortlet( id, labelMsg.text() ); |
| 348 |
|
| 349 |
if ( config[ featureName ].betaMessage ) { |
| 350 |
const betaInfoTag = makeBetaInfoTag(); |
| 351 |
if ( !portlet.querySelector( '.vector-menu-heading span' ) ) { |
| 352 |
portlet.querySelector( '.vector-menu-heading' ).textContent += ' '; |
| 353 |
portlet.querySelector( '.vector-menu-heading' ).appendChild( betaInfoTag ); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
const labelElement = portlet.querySelector( 'label' ); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
const descriptionMsg = mw.message( `${ featureName }-description` ); |
| 362 |
if ( descriptionMsg.exists() ) { |
| 363 |
const desc = document.createElement( 'span' ); |
| 364 |
desc.classList.add( 'skin-client-pref-description' ); |
| 365 |
desc.textContent = descriptionMsg.text(); |
| 366 |
if ( labelElement && labelElement.parentNode ) { |
| 367 |
labelElement.appendChild( desc ); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
const exclusionNoticeMsg = mw.message( `${ featureName }-exclusion-notice` ); |
| 374 |
if ( exclusionNoticeMsg.exists() ) { |
| 375 |
const content = portlet.querySelector( '.vector-menu-content' ); |
| 376 |
const notice = document.createElement( 'span' ); |
| 377 |
notice.classList.add( 'skin-client-pref-exclusion-notice' ); |
| 378 |
notice.textContent = exclusionNoticeMsg.text(); |
| 379 |
if ( content ) { |
| 380 |
content.appendChild( notice ); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
parent.appendChild( portlet ); |
| 385 |
const row = makeControl( featureName, config, userPreferences ); |
| 386 |
if ( row ) { |
| 387 |
const tmp = mw.util.addPortletLink( id, '', '' ); |
| 388 |
|
| 389 |
if ( tmp ) { |
| 390 |
const link = tmp.querySelector( 'a' ); |
| 391 |
if ( link ) { |
| 392 |
link.replaceWith( row ); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
if ( config[ featureName ].betaMessage && !isFeatureExcluded( featureName ) ) { |
| 397 |
const betaMessageElement = document.createElement( 'span' ); |
| 398 |
betaMessageElement.id = `${ featureName }-beta-notice`; |
| 399 |
|
| 400 |
if ( !mw.user.isAnon() ) { |
| •401 |
|
| 402 |
|
| 403 |
|
| 404 |
} |
| 405 |
row.appendChild( betaMessageElement ); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
function render( selector, config, userPreferences ) { |
| 420 |
const node = document.querySelector( selector ); |
| 421 |
if ( !node ) { |
| 422 |
return Promise.reject(); |
| 423 |
} |
| 424 |
return new Promise( ( resolve ) => { |
| 425 |
getVisibleClientPreferences( config ).forEach( ( pref ) => { |
| 426 |
userPreferences = userPreferences || new mw.Api(); |
| 427 |
makeClientPreference( node, pref, config, userPreferences ); |
| 428 |
} ); |
| 429 |
mw.requestIdleCallback( () => { |
| 430 |
resolve( node ); |
| 431 |
} ); |
| 432 |
} ); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
function bind( clickSelector, renderSelector, config, userPreferences ) { |
| 442 |
let enhanced = false; |
| 443 |
const chk = ( |
| 444 |
document.querySelector( clickSelector ) |
| 445 |
); |
| 446 |
if ( !chk ) { |
| 447 |
return; |
| 448 |
} |
| 449 |
if ( !userPreferences ) { |
| 450 |
userPreferences = new mw.Api(); |
| 451 |
} |
| 452 |
if ( chk.checked ) { |
| 453 |
render( renderSelector, config, userPreferences ); |
| 454 |
enhanced = true; |
| 455 |
} else { |
| 456 |
chk.addEventListener( 'input', () => { |
| 457 |
if ( enhanced ) { |
| 458 |
return; |
| 459 |
} |
| 460 |
render( renderSelector, config, userPreferences ); |
| 461 |
enhanced = true; |
| 462 |
} ); |
| 463 |
} |
| 464 |
} |
| 465 |
module.exports = { |
| 466 |
bind, |
| 467 |
toggleDocClassAndSave, |
| 468 |
render |
| 469 |
}; |