User:Unicodesnowman/T64266Prototype.js

// MultimediaViewer T64266 Prototype D // This script is licensed under GPL v2 or any later version. Some parts of this // script is from the MultimediaViewer project, see https://www.mediawiki.org/wiki/Extension:MultimediaViewer mw.loader.using( ['mmv.bootstrap.autostart'], function {

console.log('MultimediaViewer prototype loaded.');

var MMVB = mw.mmv.bootstrap;

// replace MMVB.internalHashChange MMVB.internalHashChange = function ( e ) { var hash = e.hash, title = e.title; // The advantage of using pushState when it's available is that it has to ability to truly // clear the hash, not leaving "#" in the history // An entry with "#" in the history has the side-effect of resetting the scroll position when navigating the history if ( this.browserHistory && this.browserHistory.pushState ) { // In order to truly clear the hash, we need to reconstruct the hash-free URL if ( hash === '#' ) { hash = window.location.href.replace( /#.*$/, '' ); }		// Only add history entry when entering the lightbox (option D, T64266) if ( this.replaceHistoryEntry ) { this.browserHistory.replaceState( null, title, hash ); } else { this.browserHistory.pushState( null, title, hash ); }	} else { // Since we voluntarily changed the hash, we don't want MMVB.hash (which will trigger on hashchange event) to treat it		this.skipNextHashHandling = true; window.location.hash = hash; }	document.title = title; };

// this is supposed to be in MMVB.setupEventHandlers, but impractical to monkey-patch // that due to load order $( document ).on( 'mmv-open', function ( e ) {	MMVB.replaceHistoryEntry = true; });

$( document ).on( 'mmv-close', function ( e ) {	MMVB.replaceHistoryEntry = false; });

// watch for when MMVB.viewer is available, so we can monkey-patch it var patchedUiOpen = false; if ( Object.observe ) { // object.observe - chrome only Object.observe(MMVB, function {		if ( MMVB.viewer ) {			!patchedUiOpen && patchUiOpen;		}	} ); } else if( Object.prototype.watch ) { // object.prototype.watch - firefox only MMVB.watch( 'viewer', function { 		!patchedUiOpen && patchUiOpen;		}); }

function patchUiOpen { var oldOpen = MMVB.viewer.ui.open; MMVB.viewer.ui.open = function { oldOpen.apply(MMVB.viewer.ui, []); $( document ).trigger( $.Event( 'mmv-open' ) ); };	// By the time this is patched, .open has already been called // so trigger the first event manually $( document ).trigger( $.Event( 'mmv-open' ) ); patchedUiOpen = true; }

} );