Jump to content

User:MarkTraceur/editPageUploadTool.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
( function ( mw ) {
	if ( mw.config.get( 'wgAction' ) !== 'edit' ) {
		return; // This is an edit page only gadget.
	}

	mw.loader.using( [ 'ext.wikiEditor' ], function () {
		window.setTimeout( function () {
		var dialogReady = $.Deferred(),
			ready = $.when( dialogReady, mw.loader.using( [ 'oojs', 'oojs-ui' ] ) ),
			$link = $( 'a[rel=file]' )
				.clone()
				.detach()
				.attr( {
					title: 'Upload file',
					href: '#'
				} )
				.appendTo( '.group-insert' ),
			working = false,
			script = importScript( 'User:MarkTraceur/commonsUploadForEditDialog.js' );
			
		script.addEventListener( 'load', function () {
			dialogReady.resolve();
		} );
	
		$link.click( function () {
			if ( working ) {
				return false;
			}
	
			ready.done( function () {
				var dialog = new mw.CommonsUploadForEditDialog( {} ),
					windowManager = new OO.ui.WindowManager();
					
				$( 'body' ).append( windowManager.$element );
	
				windowManager.addWindows( [ dialog ] );
				windowManager.openWindow( dialog );
				
				dialog.on( 'upload-complete', function ( upload ) {
					var selection = $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', { replace: true, pre: '[[File:' + upload.getFilename() + '|thumb|' + upload.getCaption() + ']]' } );
				} );
			} );
	
			return false;
		} );
		}, 1000 );
	} );
}( mediaWiki ) );