Merge "Work around T87871 to avoid double-loading OOjs UI PHP styles"
[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 /*jshint unused: false, evil: true */
7 /*globals mw, RLQ: true, NORLQ: true, $VARS, $CODE, performance */
8
9 var mediaWikiLoadStart = ( new Date() ).getTime(),
10
11 mwPerformance = ( window.performance && performance.mark ) ? performance : {
12 mark: function () {}
13 };
14
15 mwPerformance.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 function isCompatible( ua ) {
28 if ( ua === undefined ) {
29 ua = navigator.userAgent;
30 }
31
32 // Browsers with outdated or limited JavaScript engines get the no-JS experience
33 return !(
34 // Internet Explorer < 9
35 ( ua.indexOf( 'MSIE' ) !== -1 && parseFloat( ua.split( 'MSIE' )[ 1 ] ) < 9 ) ||
36 // Firefox < 3
37 ( ua.indexOf( 'Firefox/' ) !== -1 && parseFloat( ua.split( 'Firefox/' )[ 1 ] ) < 3 ) ||
38 // Opera < 12
39 ( ua.indexOf( 'Opera/' ) !== -1 && ( ua.indexOf( 'Version/' ) === -1 ?
40 // "Opera/x.y"
41 parseFloat( ua.split( 'Opera/' )[ 1 ] ) < 10 :
42 // "Opera/9.80 ... Version/x.y"
43 parseFloat( ua.split( 'Version/' )[ 1 ] ) < 12
44 ) ) ||
45 // "Mozilla/0.0 ... Opera x.y"
46 ( ua.indexOf( 'Opera ' ) !== -1 && parseFloat( ua.split( ' Opera ' )[ 1 ] ) < 10 ) ||
47 // BlackBerry < 6
48 ua.match( /BlackBerry[^\/]*\/[1-5]\./ ) ||
49 // Open WebOS < 1.5
50 ua.match( /webOS\/1\.[0-4]/ ) ||
51 // Anything PlayStation based.
52 ua.match( /PlayStation/i ) ||
53 // Any Symbian based browsers
54 ua.match( /SymbianOS|Series60/ ) ||
55 // Any NetFront based browser
56 ua.match( /NetFront/ ) ||
57 // Opera Mini, all versions
58 ua.match( /Opera Mini/ ) ||
59 // Nokia's Ovi Browser
60 ua.match( /S40OviBrowser/ ) ||
61 // MeeGo's browser
62 ua.match( /MeeGo/ ) ||
63 // Google Glass browser groks JS but UI is too limited
64 ( ua.match( /Glass/ ) && ua.match( /Android/ ) )
65 );
66 }
67
68 // Conditional script injection
69 ( function () {
70 var NORLQ, script;
71 if ( !isCompatible() ) {
72 // Undo class swapping in case of an unsupported browser.
73 // See OutputPage::getHeadScripts().
74 document.documentElement.className = document.documentElement.className
75 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
76
77 NORLQ = window.NORLQ || [];
78 while ( NORLQ.length ) {
79 NORLQ.shift()();
80 }
81 window.NORLQ = {
82 push: function ( fn ) {
83 fn();
84 }
85 };
86
87 // Clear and disable the other queue
88 window.RLQ = {
89 // No-op
90 push: function () {}
91 };
92
93 return;
94 }
95
96 /**
97 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
98 */
99 function startUp() {
100 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
101
102 $CODE.registrations();
103
104 mw.config.set( $VARS.configuration );
105
106 // Must be after mw.config.set because these callbacks may use mw.loader which
107 // needs to have values 'skin', 'debug' etc. from mw.config.
108 var RLQ = window.RLQ || [];
109 while ( RLQ.length ) {
110 RLQ.shift()();
111 }
112 window.RLQ = {
113 push: function ( fn ) {
114 fn();
115 }
116 };
117
118 // Clear and disable the other queue
119 window.NORLQ = {
120 // No-op
121 push: function () {}
122 };
123 }
124
125 script = document.createElement( 'script' );
126 script.src = $VARS.baseModulesUri;
127 script.onload = script.onreadystatechange = function () {
128 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
129 // Clean up
130 script.onload = script.onreadystatechange = null;
131 script = null;
132 // Callback
133 startUp();
134 }
135 };
136 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
137 }() );