| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
function WatchlistExpiryWidget( action, pageTitle, updateWatchLink, config ) { |
| 14 |
const dataExpiryOptions = require( './data.json' ).options, |
| 15 |
expiryOptions = []; |
| 16 |
let expiryDropdown; |
| 17 |
|
| 18 |
config = config || {}; |
| 19 |
const $link = config.$link; |
| 20 |
|
| 21 |
WatchlistExpiryWidget.super.call( this, config ); |
| 22 |
|
| 23 |
const messageLabel = new OO.ui.LabelWidget( { |
| 24 |
label: config.message |
| 25 |
} ); |
| 26 |
|
| 27 |
this.$element |
| 28 |
.addClass( 'mw-watchstar-WatchlistExpiryWidget' ) |
| 29 |
.append( messageLabel.$element ); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
function addTabKeyListener() { |
| 37 |
$( window ).one( 'keydown.watchlistExpiry', ( e ) => { |
| 38 |
if ( ( e.keyCode || e.which ) !== OO.ui.Keys.TAB ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
if ( document.activeElement.accessKey === mw.msg( 'accesskey-ca-watch' ) ) { |
| 47 |
e.preventDefault(); |
| 48 |
expiryDropdown.focus(); |
| 49 |
|
| 50 |
|
| 51 |
addTabKeyListener(); |
| 52 |
} else if ( $( e.target ).parents( '.mw-watchexpiry' ).length ) { |
| 53 |
|
| 54 |
e.preventDefault(); |
| 55 |
$( '#ca-unwatch a' ).trigger( 'focus' ); |
| 56 |
} |
| 57 |
} ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( action === 'watch' ) { |
| 61 |
addTabKeyListener(); |
| 62 |
|
| 63 |
Object.keys( dataExpiryOptions ).forEach( ( key ) => { |
| 64 |
expiryOptions.push( { data: dataExpiryOptions[ key ], label: key } ); |
| 65 |
} ); |
| 66 |
|
| 67 |
const dropdownLabel = new OO.ui.LabelWidget( { |
| 68 |
label: mw.message( 'addedwatchexpiry-options-label' ).parseDom(), |
| 69 |
classes: [ 'mw-WatchlistExpiryWidgetwatchlist-dropdown-label' ] |
| 70 |
} ); |
| 71 |
expiryDropdown = new OO.ui.DropdownInputWidget( { |
| 72 |
options: expiryOptions, |
| 73 |
classes: [ 'mw-watchexpiry' ] |
| 74 |
} ); |
| 75 |
const onDropdownChange = function ( value ) { |
| 76 |
const notif = mw.notification, |
| 77 |
optionSelectedLabel = expiryDropdown.dropdownWidget.label; |
| 78 |
|
| 79 |
if ( typeof $link !== 'undefined' ) { |
| 80 |
updateWatchLink( $link, 'watch', 'loading' ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
notif.pause(); |
| 85 |
const api = new mw.Api(); |
| 86 |
api.watch( pageTitle, value ) |
| 87 |
.done( ( watchResponse ) => { |
| 88 |
let message; |
| 89 |
const mwTitle = mw.Title.newFromText( pageTitle ), |
| 90 |
isInfinity = mw.util.isInfinity( value ); |
| 91 |
if ( mwTitle.isTalkPage() ) { |
| 92 |
message = isInfinity ? 'addedwatchindefinitelytext-talk' : 'addedwatchexpirytext-talk'; |
| 93 |
} else { |
| 94 |
message = isInfinity ? 'addedwatchindefinitelytext' : 'addedwatchexpirytext'; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
messageLabel.setLabel( |
| 103 |
mw.message( message, mwTitle.getPrefixedText(), optionSelectedLabel ).parseDom() |
| 104 |
); |
| 105 |
|
| 106 |
notif.resume(); |
| 107 |
|
| 108 |
updateWatchLink( mwTitle, 'unwatch', 'idle', watchResponse.expiry, value ); |
| 109 |
} ) |
| 110 |
.fail( ( code, data ) => { |
| 111 |
|
| 112 |
const $msg = api.getErrorMessage( data ); |
| 113 |
|
| 114 |
|
| 115 |
mw.notify( $msg, { |
| 116 |
tag: 'watch-self', |
| 117 |
type: 'error' |
| 118 |
} ); |
| 119 |
|
| 120 |
notif.resume(); |
| 121 |
} ); |
| 122 |
}; |
| 123 |
|
| 124 |
expiryDropdown.on( 'change', onDropdownChange ); |
| 125 |
this.$element.append( dropdownLabel.$element, expiryDropdown.$element ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
OO.inheritClass( WatchlistExpiryWidget, OO.ui.Widget ); |
| 130 |
|
| 131 |
module.exports = WatchlistExpiryWidget; |