Merge "Add @since tags for initial Action.php methods"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.view.redirect.js
1 /*!
2 * JavaScript to update page URL when a redirect is viewed, ensuring that the
3 * page is scrolled to the id when it's a redirect with fragment.
4 *
5 * This is loaded in the top queue, so avoid unnecessary dependencies
6 * like mediawiki.Title or mediawiki.Uri.
7 */
8 ( function ( mw, $ ) {
9 var profile = $.client.profile(),
10 canonical = mw.config.get( 'wgInternalRedirectTargetUrl' ),
11 fragment = null,
12 shouldChangeFragment, index;
13
14 // Clear internal mw.config entries, so that no one tries to depend on them
15 mw.config.set( 'wgInternalRedirectTargetUrl', null );
16
17 // Deployment hack for compatibility with cached HTML, remove before 1.24 release
18 if ( !canonical ) {
19 canonical = mw.config.get( 'wgRedirectToFragment' );
20 }
21
22 index = canonical.indexOf( '#' );
23 if ( index !== -1 ) {
24 fragment = canonical.slice( index );
25 }
26
27 // Never override the fragment if the user intended to look at a different section
28 shouldChangeFragment = fragment && !location.hash;
29
30 // Replace the whole URL if possible, otherwise just change the fragment
31 if ( canonical && history.replaceState ) {
32 if ( !shouldChangeFragment ) {
33 // If the current page view has a fragment already, don't override it
34 canonical = canonical.replace( /#.*$/, '' );
35 canonical += location.hash;
36 }
37
38 // This will also cause the browser to scroll to given fragment
39 history.replaceState( /*data=*/ history.state, /*title=*/ document.title, /*url=*/ canonical );
40
41 // …except for IE 10 and 11. Prod it with a location.hash change.
42 if ( shouldChangeFragment && profile.name === 'msie' && profile.versionNumber >= 10 ) {
43 location.hash = fragment;
44 }
45
46 } else if ( shouldChangeFragment ) {
47 if ( profile.layout === 'webkit' && profile.layoutVersion < 420 ) {
48 // Released Safari w/ WebKit 418.9.1 messes up horribly
49 // Nightlies of 420+ are ok
50 return;
51 }
52
53 location.hash = fragment;
54 }
55
56 if ( shouldChangeFragment && profile.layout === 'gecko' ) {
57 // Mozilla needs to wait until after load, otherwise the window doesn't
58 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
59 // There's no obvious way to detect this programmatically, so we use
60 // version-testing. If Firefox fixes the bug, they'll jump twice, but
61 // better twice than not at all, so make the fix hit future versions as
62 // well.
63 $( function () {
64 if ( location.hash === fragment ) {
65 location.hash = fragment;
66 }
67 } );
68 }
69
70 }( mediaWiki, jQuery ) );