| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
( function () { |
| 6 |
let jqXhr, $multipageimage, $spinner, |
| 7 |
cacheOrder = []; |
| 8 |
const cache = {}; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
function fetchPageData( url ) { |
| 15 |
if ( jqXhr && jqXhr.abort ) { |
| 16 |
|
| 17 |
jqXhr.abort(); |
| 18 |
} |
| 19 |
jqXhr = undefined; |
| 20 |
|
| 21 |
|
| 22 |
if ( cache[ url ] ) { |
| 23 |
|
| 24 |
cacheOrder.splice( cacheOrder.indexOf( url ), 1 ); |
| 25 |
cacheOrder.push( url ); |
| 26 |
return $.Deferred().resolve( cache[ url ] ).promise(); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
jqXhr = $.ajax( url ).then( ( data ) => $( data ).find( '.mw-filepage-multipage' ).contents() ); |
| 32 |
|
| 33 |
|
| 34 |
jqXhr.done( ( $contents ) => { |
| 35 |
jqXhr = undefined; |
| 36 |
|
| 37 |
|
| 38 |
cache[ url ] = $contents; |
| 39 |
cacheOrder.push( url ); |
| 40 |
|
| 41 |
|
| 42 |
if ( cacheOrder.length > 10 ) { |
| 43 |
delete cache[ cacheOrder[ 0 ] ]; |
| 44 |
cacheOrder = cacheOrder.slice( 1 ); |
| 45 |
} |
| 46 |
} ); |
| 47 |
|
| 48 |
return jqXhr.promise(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
function switchPage( url, hist ) { |
| 57 |
|
| 58 |
const promise = fetchPageData( url ); |
| 59 |
|
| 60 |
|
| 61 |
if ( !$spinner && promise.state() !== 'resolved' ) { |
| 62 |
$spinner = $.createSpinner( { |
| 63 |
size: 'large', |
| 64 |
type: 'block' |
| 65 |
} ) |
| 66 |
|
| 67 |
|
| 68 |
.css( { |
| 69 |
height: $multipageimage.outerHeight(), |
| 70 |
width: $multipageimage.outerWidth() |
| 71 |
} ); |
| 72 |
|
| 73 |
$multipageimage.empty().append( $spinner ); |
| 74 |
} |
| 75 |
|
| 76 |
promise.done( ( $contents ) => { |
| 77 |
$spinner = undefined; |
| 78 |
|
| 79 |
|
| 80 |
$multipageimage.empty().append( $contents.clone() ); |
| 81 |
|
| 82 |
bindPageNavigation( $multipageimage ); |
| 83 |
|
| 84 |
|
| 85 |
mw.hook( 'wikipage.content' ).fire( $multipageimage ); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if ( !hist ) { |
| 90 |
history.pushState( { tag: 'mw-pagination' }, document.title, url ); |
| 91 |
} |
| 92 |
} ); |
| 93 |
} |
| 94 |
|
| 95 |
function bindPageNavigation( $container ) { |
| 96 |
$container.find( '.mw-filepage-multipage-navigation' ).one( 'click', 'a', function ( e ) { |
| 97 |
|
| 98 |
|
| 99 |
const page = mw.util.getParamValue( 'page', this.href ); |
| 100 |
const url = mw.util.getUrl( null, page ? { page: page } : {} ); |
| 101 |
|
| 102 |
switchPage( url ); |
| 103 |
e.preventDefault(); |
| 104 |
} ); |
| 105 |
|
| 106 |
$container.find( 'form[name="pageselector"]' ).one( 'change submit', function ( e ) { |
| 107 |
switchPage( this.action + '?' + $( this ).serialize() ); |
| 108 |
e.preventDefault(); |
| 109 |
} ); |
| 110 |
} |
| 111 |
|
| 112 |
$( () => { |
| 113 |
if ( mw.config.get( 'wgCanonicalNamespace' ) !== 'File' ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
$multipageimage = $( '.mw-filepage-multipage' ); |
| 117 |
if ( !$multipageimage.length ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
bindPageNavigation( $multipageimage ); |
| 122 |
|
| 123 |
|
| 124 |
history.replaceState( { tag: 'mw-pagination' }, '' ); |
| 125 |
$( window ).on( 'popstate', ( e ) => { |
| 126 |
const state = e.originalEvent.state; |
| 127 |
if ( state && state.tag === 'mw-pagination' ) { |
| 128 |
switchPage( location.href, true ); |
| 129 |
} |
| 130 |
} ); |
| 131 |
} ); |
| 132 |
}() ); |