|
| 1 | +import MwApi from '../../../../MwApi'; |
| 2 | +import getApiErrorText from '../../../../wiki/util/getApiErrorText'; |
| 3 | + |
| 4 | +export interface PageLatestRevisionGetButtonData extends OO.ui.ButtonWidget.ConfigOptions { |
| 5 | + titleInputWidget: OO.ui.TextInputWidget; |
| 6 | + revisionInputWidget: OO.ui.InputWidget; |
| 7 | +} |
| 8 | + |
| 9 | +let InternalPageLatestRevisionGetButton: any; |
| 10 | + |
| 11 | +/** |
| 12 | + * Initializes the process element. |
| 13 | + */ |
| 14 | +function initPageLatestRevisionGetButton() { |
| 15 | + InternalPageLatestRevisionGetButton = class PageLatestRevisionGetButton |
| 16 | + extends OO.ui.ButtonWidget { |
| 17 | + |
| 18 | + titleInputWidget: OO.ui.TextInputWidget; |
| 19 | + revisionInputWidget: OO.ui.InputWidget; |
| 20 | + |
| 21 | + /** |
| 22 | + * @param config Configuration to be passed to the element. |
| 23 | + */ |
| 24 | + constructor( config: PageLatestRevisionGetButtonData ) { |
| 25 | + super( Object.assign( { |
| 26 | + icon: 'download', |
| 27 | + invisibleLabel: true, |
| 28 | + disabled: true |
| 29 | + }, config ) ); |
| 30 | + |
| 31 | + this.titleInputWidget = config.titleInputWidget; |
| 32 | + this.revisionInputWidget = config.revisionInputWidget; |
| 33 | + |
| 34 | + this.titleInputWidget.on( 'change', this.updateButton.bind( this ) ); |
| 35 | + this.revisionInputWidget.on( 'change', this.updateButton.bind( this ) ); |
| 36 | + this.on( 'click', this.setRevisionFromPageLatestRevision.bind( this ) ); |
| 37 | + this.updateButton(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Update the disabled state of the button. |
| 42 | + */ |
| 43 | + updateButton(): void { |
| 44 | + this.setDisabled( |
| 45 | + this.titleInputWidget.getValue().trim().length === 0 || |
| 46 | + this.revisionInputWidget.getValue().trim().length !== 0 || |
| 47 | + !( this.titleInputWidget as any ).isQueryValid() |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Set the revision ID from the page provided in the value of |
| 53 | + * `this.titleInputWidget`. |
| 54 | + */ |
| 55 | + async setRevisionFromPageLatestRevision(): Promise<void> { |
| 56 | + this |
| 57 | + .setIcon( 'ellipsis' ) |
| 58 | + .setDisabled( true ); |
| 59 | + this.revisionInputWidget.setDisabled( true ); |
| 60 | + const title = this.titleInputWidget.getValue(); |
| 61 | + await MwApi.action.get( { |
| 62 | + action: 'query', |
| 63 | + prop: 'revisions', |
| 64 | + titles: title, |
| 65 | + rvprop: 'ids' |
| 66 | + } ).then( ( data ) => { |
| 67 | + if ( ( data.query as any ).pages[ 0 ].missing ) { |
| 68 | + mw.notify( |
| 69 | + mw.msg( 'deputy.ante.revisionAuto.missing', title ), |
| 70 | + { type: 'error' } |
| 71 | + ); |
| 72 | + this.updateButton(); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + this.revisionInputWidget.setValue( data.query.pages[ 0 ].revisions[ 0 ].revid ); |
| 77 | + this.revisionInputWidget.setDisabled( false ); |
| 78 | + this.setIcon( 'download' ); |
| 79 | + this.updateButton(); |
| 80 | + }, ( _error, errorData ) => { |
| 81 | + mw.notify( |
| 82 | + |
| 83 | + mw.msg( 'deputy.ante.revisionAuto.failed', getApiErrorText( errorData ) ), |
| 84 | + { type: 'error' } |
| 85 | + ); |
| 86 | + this.revisionInputWidget.setDisabled( false ); |
| 87 | + this.setIcon( 'download' ); |
| 88 | + this.updateButton(); |
| 89 | + } ); |
| 90 | + } |
| 91 | + |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Creates a new PageLatestRevisionGetButton. |
| 97 | + * |
| 98 | + * @param config Configuration to be passed to the element. |
| 99 | + * @return A PageLatestRevisionGetButton object |
| 100 | + */ |
| 101 | +export default function ( config: PageLatestRevisionGetButtonData ) { |
| 102 | + if ( !InternalPageLatestRevisionGetButton ) { |
| 103 | + initPageLatestRevisionGetButton(); |
| 104 | + } |
| 105 | + return new InternalPageLatestRevisionGetButton( config ); |
| 106 | +} |
0 commit comments