Jump to content

User:MarkTraceur/editPageUploadTool.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MarkTraceur (talk | contribs) at 14:55, 3 August 2015 (Created page with '( function ( mw ) { if ( document.location.search.indexOf( 'action=edit' ) < 0 ) { return; // This is an edit page only gadget. } mw.loader.using( [ 'ext.w...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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 ( document.location.search.indexOf( 'action=edit' ) < 0 ) {
		return; // This is an edit page only gadget.
	}

	mw.loader.using( [ 'ext.wikiEditor.toolbar' ], 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 ) );