Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / resources / src / startup.js
1 /**
2 * This script provides a function which is run to evaluate whether or not to
3 * continue loading jQuery and the MediaWiki modules. This code should work on
4 * even the most ancient of browsers, so be very careful when editing.
5 */
6
7 var mediaWikiLoadStart = ( new Date() ).getTime();
8
9 if ( !window.performance ) {
10 window.performance = {};
11 }
12 if ( !performance.mark ) {
13 performance.mark = function () {};
14 }
15 performance.mark( 'mwLoadStart' );
16
17 /**
18 * Returns false for Grade C supported browsers.
19 *
20 * This function should only be used by the Startup module, do not expand it to
21 * be generally useful beyond startup.
22 *
23 * See also:
24 * - https://www.mediawiki.org/wiki/Compatibility#Browsers
25 * - https://jquery.com/browser-support/
26 */
27
28 /*jshint unused: false, evil: true */
29 /*globals mw, RLQ: true, $VARS, $CODE */
30 function isCompatible( ua ) {
31 if ( ua === undefined ) {
32 ua = navigator.userAgent;
33 }
34
35 // Browsers with outdated or limited JavaScript engines get the no-JS experience
36 return !(
37 // Internet Explorer < 8
38 ( ua.indexOf( 'MSIE' ) !== -1 && parseFloat( ua.split( 'MSIE' )[ 1 ] ) < 8 ) ||
39 // Firefox < 3
40 ( ua.indexOf( 'Firefox/' ) !== -1 && parseFloat( ua.split( 'Firefox/' )[ 1 ] ) < 3 ) ||
41 // Opera < 12
42 ( ua.indexOf( 'Opera/' ) !== -1 && ( ua.indexOf( 'Version/' ) === -1 ?
43 // "Opera/x.y"
44 parseFloat( ua.split( 'Opera/' )[ 1 ] ) < 10 :
45 // "Opera/9.80 ... Version/x.y"
46 parseFloat( ua.split( 'Version/' )[ 1 ] ) < 12
47 ) ) ||
48 // "Mozilla/0.0 ... Opera x.y"
49 ( ua.indexOf( 'Opera ' ) !== -1 && parseFloat( ua.split( ' Opera ' )[ 1 ] ) < 10 ) ||
50 // BlackBerry < 6
51 ua.match( /BlackBerry[^\/]*\/[1-5]\./ ) ||
52 // Open WebOS < 1.5
53 ua.match( /webOS\/1\.[0-4]/ ) ||
54 // Anything PlayStation based.
55 ua.match( /PlayStation/i ) ||
56 // Any Symbian based browsers
57 ua.match( /SymbianOS|Series60/ ) ||
58 // Any NetFront based browser
59 ua.match( /NetFront/ ) ||
60 // Opera Mini, all versions
61 ua.match( /Opera Mini/ ) ||
62 // Nokia's Ovi Browser
63 ua.match( /S40OviBrowser/ ) ||
64 // MeeGo's browser
65 ua.match( /MeeGo/ ) ||
66 // Google Glass browser groks JS but UI is too limited
67 ( ua.match( /Glass/ ) && ua.match( /Android/ ) )
68 );
69 }
70
71 // Conditional script injection
72 ( function () {
73 if ( !isCompatible() ) {
74 // Undo class swapping in case of an unsupported browser.
75 // See OutputPage::getHeadScripts().
76 document.documentElement.className = document.documentElement.className
77 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
78 return;
79 }
80
81 /**
82 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
83 */
84 function startUp() {
85 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
86
87 $CODE.registrations();
88
89 mw.config.set( $VARS.configuration );
90
91 // Must be after mw.config.set because these callbacks may use mw.loader which
92 // needs to have values 'skin', 'debug' etc. from mw.config.
93 window.RLQ = window.RLQ || [];
94 while ( RLQ.length ) {
95 RLQ.shift()();
96 }
97 window.RLQ = {
98 push: function ( fn ) {
99 fn();
100 }
101 };
102 }
103
104 var script = document.createElement( 'script' );
105 script.src = $VARS.baseModulesUri;
106 script.onload = script.onreadystatechange = function () {
107 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
108 // Clean up
109 script.onload = script.onreadystatechange = null;
110 script = null;
111 // Callback
112 startUp();
113 }
114 };
115 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
116 }() );