2 * Implement AJAX navigation for multi-page images so the user may browse without a full page reload.
5 /* eslint-disable no-use-before-define */
8 var jqXhr
, $multipageimage
, $spinner
,
12 /* Fetch the next page, caching up to 10 last-loaded pages.
14 * @return {jQuery.Promise}
16 function fetchPageData( url
) {
17 if ( jqXhr
&& jqXhr
.abort
) {
18 // Prevent race conditions and piling up pending requests
25 // Update access freshness
26 cacheOrder
.splice( $.inArray( url
, cacheOrder
), 1 );
27 cacheOrder
.push( url
);
28 return $.Deferred().resolve( cache
[ url
] ).promise();
31 // TODO Don't fetch the entire page. Ideally we'd only fetch the content portion or the data
32 // (thumbnail urls) and update the interface manually.
33 jqXhr
= $.ajax( url
).then( function ( data
) {
34 return $( data
).find( 'table.multipageimage' ).contents();
37 // Handle cache updates
38 jqXhr
.done( function ( $contents
) {
41 // Cache the newly loaded page
42 cache
[ url
] = $contents
;
43 cacheOrder
.push( url
);
45 // Remove the oldest entry if we're over the limit
46 if ( cacheOrder
.length
> 10 ) {
47 delete cache
[ cacheOrder
[ 0 ] ];
48 cacheOrder
= cacheOrder
.slice( 1 );
52 return jqXhr
.promise();
55 /* Fetch the next page and use jQuery to swap the table.multipageimage contents.
57 * @param {boolean} [hist=false] Whether this is a load triggered by history navigation (if
58 * true, this function won't push a new history state, for the browser did so already).
60 function switchPage( url
, hist
) {
63 // Start fetching data (might be cached)
64 promise
= fetchPageData( url
);
66 // Add a new spinner if one doesn't already exist and the data is not already ready
67 if ( !$spinner
&& promise
.state() !== 'resolved' ) {
68 $tr
= $multipageimage
.find( 'tr' );
69 $spinner
= $.createSpinner( {
73 // Copy the old content dimensions equal so that the current scroll position is not
74 // lost between emptying the table is and receiving the new contents.
76 height
: $tr
.outerHeight(),
77 width
: $tr
.outerWidth()
80 $multipageimage
.empty().append( $spinner
);
83 promise
.done( function ( $contents
) {
86 // Replace table contents
87 $multipageimage
.empty().append( $contents
.clone() );
89 bindPageNavigation( $multipageimage
);
91 // Fire hook because the page's content has changed
92 mw
.hook( 'wikipage.content' ).fire( $multipageimage
);
94 // Update browser history and address bar. But not if we came here from a history
95 // event, in which case the url is already updated by the browser.
96 if ( history
.pushState
&& !hist
) {
97 history
.pushState( { tag
: 'mw-pagination' }, document
.title
, url
);
102 function bindPageNavigation( $container
) {
103 $container
.find( '.multipageimagenavbox' ).one( 'click', 'a', function ( e
) {
106 // Generate the same URL on client side as the one generated in ImagePage::openShowImage.
107 // We avoid using the URL in the link directly since it could have been manipulated (bug 66608)
108 page
= Number( mw
.util
.getParamValue( 'page', this.href
) );
109 url
= mw
.util
.getUrl( mw
.config
.get( 'wgPageName' ), { page
: page
} );
115 $container
.find( 'form[name="pageselector"]' ).one( 'change submit', function ( e
) {
116 switchPage( this.action
+ '?' + $( this ).serialize() );
122 if ( mw
.config
.get( 'wgCanonicalNamespace' ) !== 'File' ) {
125 $multipageimage
= $( 'table.multipageimage' );
126 if ( !$multipageimage
.length
) {
130 bindPageNavigation( $multipageimage
);
132 // Update the url using the History API (if available)
133 if ( history
.pushState
&& history
.replaceState
) {
134 history
.replaceState( { tag
: 'mw-pagination' }, '' );
135 $( window
).on( 'popstate', function ( e
) {
136 var state
= e
.originalEvent
.state
;
137 if ( state
&& state
.tag
=== 'mw-pagination' ) {
138 switchPage( location
.href
, true );
143 }( mediaWiki
, jQuery
) );