MediaWiki:Gadget-spriteEditLoader.js: Różnice pomiędzy wersjami

Z Minecraft Wiki Polska
Przejdź do nawigacji Przejdź do wyszukiwania
Majr (dyskusja | edycje)
Make sure this doesn't get loaded too soon.
Majr (dyskusja | edycje)
https://github.com/Majr-/SpriteEdit/compare/111b3b2...3e70656
Linia 1: Linia 1:
$( function() {
( function() {
'use strict';
'use strict';
/**
/**
  * Add an edit button which loads the sprite editor
  * Add an edit button which loads the sprite editor
Linia 6: Linia 7:
  * If spriteaction=edit is in the URL, the editor will be loaded
  * If spriteaction=edit is in the URL, the editor will be loaded
  * immediately, otherwise it will wait for the button to be clicked.
  * immediately, otherwise it will wait for the button to be clicked.
* Uses the History API where supported to update the URL, otherwise
* the URL isn't updated.
  */
  */
if ( !$( '#spritedoc' ).length ) {
var editPage = $( '#sprite-editor-message' ).data( 'page' ) || null;
if ( !$( '#spritedoc' ).length && !editPage ) {
return;
return;
}
}
Linia 18: Linia 18:
}
}
var $spriteEditLink = $( '<a>' ).text( 'Edit sprite' ).attr( 'href',
var $spriteEditLink = $( '<a>' ).text( 'Edit sprite' ).attr( 'href',
mw.util.getUrl( null, { spriteaction: 'edit' } )
mw.util.getUrl( editPage, { spriteaction: 'edit' } )
);
);
var $spriteEditTab = $( '<li>' ).attr( 'id', 'ca-spriteedit' ).append(
var $spriteEditTab = $( '<li>' ).attr( 'id', 'ca-spriteedit' ).append(
Linia 24: Linia 24:
);
);
$spriteEditTab.insertAfter( $editTab );
$spriteEditTab.insertAfter( $editTab );
// Page to sprite edit is not here, so no need to bind events
if ( editPage ) {
return;
}


var loadSpriteEditor = function() {
var loadSpriteEditor = function() {
$spriteEditTab.add( '#ca-view' ).toggleClass( 'selected' );
$spriteEditTab.add( '#ca-view' ).toggleClass( 'selected' );
mw.loader.load( 'ext.gadget.spriteEdit' );
return mw.loader.using( 'ext.gadget.spriteEdit' );
};
};
if ( location.search.match( 'spriteaction=edit' ) ) {
if ( location.search.match( '[?&]spriteaction=edit' ) ) {
loadSpriteEditor();
loadSpriteEditor();
} else {
return;
var $win = $( window );
}
$spriteEditLink.one( 'click', function( e ) {
 
if ( window.history && history.pushState ) {
var $win = $( window );
// Initially add the history so it is not delayed waiting
$spriteEditLink.one( 'click.spriteEditLoader', function( e ) {
// for the editor to load. The editor will handle it from now.
// Initially add the history so it is not delayed waiting
history.pushState( {}, '', this.href );
// for the editor to load. The editor will handle it from now.
}
history.pushState( {}, '', this.href );
loadSpriteEditor();
loadSpriteEditor().then( function() {
$win.off( '.spriteEditLoader' );
$win.off( '.spriteEditLoader' );
e.preventDefault();
} );
} );
if ( window.history && history.pushState ) {
e.preventDefault();
// If the page is reloaded while the editor isn't loaded, navigating
} );
// back to the editor won't work, so an initial navigation check is
 
// necessary to load the editor, where it will then monitor navigation
// If the page is reloaded while the editor isn't loaded, navigating
$win.on( 'popstate.spriteEditLoader', function() {
// back to the editor won't work, so an initial navigation check is
if (
// necessary to load the editor, where it will then monitor navigation
location.search.match( 'spriteaction=edit' ) &&
$win.on( 'popstate.spriteEditLoader', function() {
!$( 'html' ).hasClass( 'spriteedit-loaded' )
if (
) {
location.search.match( '[?&]spriteaction=edit' ) &&
loadSpriteEditor();
!$( 'html' ).hasClass( 'spriteedit-loaded' )
$win.off( '.spriteEditLoader' );
) {
}
loadSpriteEditor().then( function() {
$win.off( '.spriteEditLoader' );
} );
} );
}
}
}
} );


} );
}() );

Wersja z 16:55, 8 kwi 2018

( function() {
'use strict';

/**
 * Add an edit button which loads the sprite editor
 *
 * If spriteaction=edit is in the URL, the editor will be loaded
 * immediately, otherwise it will wait for the button to be clicked.
 */
var editPage = $( '#sprite-editor-message' ).data( 'page' ) || null;
if ( !$( '#spritedoc' ).length && !editPage ) {
	return;
}

var $editTab = $( '#ca-edit' );
if ( !$editTab.length ) {
	$editTab = $( '#ca-viewsource' );
}
var $spriteEditLink = $( '<a>' ).text( 'Edit sprite' ).attr( 'href',
	mw.util.getUrl( editPage, { spriteaction: 'edit' } )
);
var $spriteEditTab = $( '<li>' ).attr( 'id', 'ca-spriteedit' ).append(
	$( '<span>' ).append( $spriteEditLink )
);
$spriteEditTab.insertAfter( $editTab );

// Page to sprite edit is not here, so no need to bind events
if ( editPage ) {
	return;
}

var loadSpriteEditor = function() {
	$spriteEditTab.add( '#ca-view' ).toggleClass( 'selected' );
	
	return mw.loader.using( 'ext.gadget.spriteEdit' );
};
if ( location.search.match( '[?&]spriteaction=edit' ) ) {
	loadSpriteEditor();
	return;
}

var $win = $( window );
$spriteEditLink.one( 'click.spriteEditLoader', function( e ) {
	// Initially add the history so it is not delayed waiting
	// for the editor to load. The editor will handle it from now.
	history.pushState( {}, '', this.href );
	
	loadSpriteEditor().then( function() {
		$win.off( '.spriteEditLoader' );
	} );
	
	e.preventDefault();
} );

// If the page is reloaded while the editor isn't loaded, navigating
// back to the editor won't work, so an initial navigation check is
// necessary to load the editor, where it will then monitor navigation
$win.on( 'popstate.spriteEditLoader', function() {
	if (
		location.search.match( '[?&]spriteaction=edit' ) &&
		!$( 'html' ).hasClass( 'spriteedit-loaded' )
	) {
		loadSpriteEditor().then( function() {
			$win.off( '.spriteEditLoader' );
		} );
	}
} );

}() );