2 * Implement AJAX navigation for multi-page images so the user may browse without a full page reload.
5 var jqXhr
, $multipageimage
, $spinner
,
9 /* Fetch the next page, caching up to 10 last-loaded pages.
11 * @return {jQuery.Promise}
13 function fetchPageData( url
) {
14 if ( jqXhr
&& jqXhr
.abort
) {
15 // Prevent race conditions and piling up pending requests
22 // Update access freshness
23 cacheOrder
.splice( $.inArray( url
, cacheOrder
), 1 );
24 cacheOrder
.push( url
);
25 return $.Deferred().resolve( cache
[url
] ).promise();
28 // @todo Don't fetch the entire page. Ideally we'd only fetch the content portion or the data
29 // (thumbnail urls) and update the interface manually.
30 jqXhr
= $.ajax( url
).then( function ( data
) {
31 return $( data
).find( 'table.multipageimage' ).contents();
34 // Handle cache updates
35 jqXhr
.done( function ( $contents
) {
38 // Cache the newly loaded page
39 cache
[url
] = $contents
;
40 cacheOrder
.push( url
);
42 // Remove the oldest entry if we're over the limit
43 if ( cacheOrder
.length
> 10 ) {
44 delete cache
[ cacheOrder
[0] ];
45 cacheOrder
= cacheOrder
.slice( 1 );
49 return jqXhr
.promise();
52 /* Fetch the next page and use jQuery to swap the table.multipageimage contents.
54 * @param {boolean} [hist=false] Whether this is a load triggered by history navigation (if
55 * true, this function won't push a new history state, for the browser did so already).
57 function switchPage( url
, hist
) {
60 // Start fetching data (might be cached)
61 promise
= fetchPageData( url
);
63 // Add a new spinner if one doesn't already exist and the data is not already ready
64 if ( !$spinner
&& promise
.state() !== 'resolved' ) {
65 $tr
= $multipageimage
.find( 'tr' );
66 $spinner
= $.createSpinner( {
70 // Copy the old content dimensions equal so that the current scroll position is not
71 // lost between emptying the table is and receiving the new contents.
73 height
: $tr
.outerHeight(),
74 width
: $tr
.outerWidth()
77 $multipageimage
.empty().append( $spinner
);
80 promise
.done( function ( $contents
) {
83 // Replace table contents
84 $multipageimage
.empty().append( $contents
.clone() );
86 bindPageNavigation( $multipageimage
);
88 // Fire hook because the page's content has changed
89 mw
.hook( 'wikipage.content' ).fire( $multipageimage
);
91 // Update browser history and address bar. But not if we came here from a history
92 // event, in which case the url is already updated by the browser.
93 if ( history
.pushState
&& !hist
) {
94 history
.pushState( { tag
: 'mw-pagination' }, document
.title
, url
);
99 function bindPageNavigation( $container
) {
100 $container
.find( '.multipageimagenavbox' ).one( 'click', 'a', function ( e
) {
103 // Generate the same URL on client side as the one generated in ImagePage::openShowImage.
104 // We avoid using the URL in the link directly since it could have been manipulated (bug 66608)
105 page
= Number( mw
.util
.getParamValue( 'page', this.href
) );
106 uri
= new mw
.Uri( mw
.util
.wikiScript() )
107 .extend( { title
: mw
.config
.get( 'wgPageName' ), page
: page
} )
114 $container
.find( 'form[name="pageselector"]' ).one( 'change submit', function ( e
) {
115 switchPage( this.action
+ '?' + $( this ).serialize() );
121 if ( mw
.config
.get( 'wgNamespaceNumber' ) !== 6 ) {
124 $multipageimage
= $( 'table.multipageimage' );
125 if ( !$multipageimage
.length
) {
129 bindPageNavigation( $multipageimage
);
131 // Update the url using the History API (if available)
132 if ( history
.pushState
&& history
.replaceState
) {
133 history
.replaceState( { tag
: 'mw-pagination' }, '' );
134 $( window
).on( 'popstate', function ( e
) {
135 var state
= e
.originalEvent
.state
;
136 if ( state
&& state
.tag
=== 'mw-pagination' ) {
137 switchPage( location
.href
, true );
142 }( mediaWiki
, jQuery
) );