build: Replace jscs+jshint with eslint
[lhc/web/wiklou.git] / resources / src / startup.js
1 /**
2 * Code in this file MUST work on even the most ancient of browsers!
3 *
4 * This file is where we decide whether to initialise the modern run-time.
5 */
6
7 /* global mw, $VARS, $CODE */
8
9 // eslint-disable-next-line no-unused-vars
10 var mediaWikiLoadStart = ( new Date() ).getTime(),
11 mwPerformance = ( window.performance && performance.mark ) ? performance : {
12 mark: function () {}
13 };
14
15 mwPerformance.mark( 'mwLoadStart' );
16
17 /**
18 * See <https://www.mediawiki.org/wiki/Compatibility#Browsers>
19 *
20 * Capabilities required for modern run-time:
21 * - DOM Level 4 & Selectors API Level 1
22 * - HTML5 & Web Storage
23 * - DOM Level 2 Events
24 *
25 * Browsers we support in our modern run-time (Grade A):
26 * - Chrome
27 * - IE 9+
28 * - Firefox 3.5+
29 * - Safari 4+
30 * - Opera 10.5+
31 * - Mobile Safari (iOS 1+)
32 * - Android 2.0+
33 *
34 * Browsers we support in our no-javascript run-time (Grade C):
35 * - IE 6+
36 * - Firefox 3+
37 * - Safari 3+
38 * - Opera 10+
39 * - WebOS < 1.5
40 * - PlayStation
41 * - Symbian-based browsers
42 * - NetFront-based browser
43 * - Opera Mini
44 * - Nokia's Ovi Browser
45 * - MeeGo's browser
46 * - Google Glass
47 *
48 * Other browsers that pass the check are considered Grade X.
49 */
50 function isCompatible( str ) {
51 var ua = str || navigator.userAgent;
52 return !!(
53 // http://caniuse.com/#feat=queryselector
54 'querySelector' in document &&
55
56 // http://caniuse.com/#feat=namevalue-storage
57 // https://developer.blackberry.com/html5/apis/v1_0/localstorage.html
58 // https://blog.whatwg.org/this-week-in-html-5-episode-30
59 'localStorage' in window &&
60
61 // http://caniuse.com/#feat=addeventlistener
62 'addEventListener' in window &&
63
64 // Hardcoded exceptions for browsers that pass the requirement but we don't want to
65 // support in the modern run-time.
66 !(
67 ua.match( /webOS\/1\.[0-4]|SymbianOS|Series60|NetFront|Opera Mini|S40OviBrowser|MeeGo|Android.+Glass/ ) ||
68 ua.match( /PlayStation/i ) ||
69 // UC Mini (speed mode on)
70 ua.match( /^Mozilla\/5\.0 .+ Gecko\/$/ )
71 )
72 );
73 }
74
75 // Conditional script injection
76 ( function () {
77 var NORLQ, script;
78 if ( !isCompatible() ) {
79 // Undo class swapping in case of an unsupported browser.
80 // See ResourceLoaderClientHtml::getDocumentAttributes().
81 document.documentElement.className = document.documentElement.className
82 .replace( /(^|\s)client-js(\s|$)/, '$1client-nojs$2' );
83
84 NORLQ = window.NORLQ || [];
85 while ( NORLQ.length ) {
86 NORLQ.shift()();
87 }
88 window.NORLQ = {
89 push: function ( fn ) {
90 fn();
91 }
92 };
93
94 // Clear and disable the other queue
95 window.RLQ = {
96 // No-op
97 push: function () {}
98 };
99
100 return;
101 }
102
103 /**
104 * The $CODE and $VARS placeholders are substituted in ResourceLoaderStartUpModule.php.
105 */
106 function startUp() {
107 mw.config = new mw.Map( $VARS.wgLegacyJavaScriptGlobals );
108
109 $CODE.registrations();
110
111 mw.config.set( $VARS.configuration );
112
113 // Must be after mw.config.set because these callbacks may use mw.loader which
114 // needs to have values 'skin', 'debug' etc. from mw.config.
115 // eslint-disable-next-line vars-on-top
116 var RLQ = window.RLQ || [];
117 while ( RLQ.length ) {
118 RLQ.shift()();
119 }
120 window.RLQ = {
121 push: function ( fn ) {
122 fn();
123 }
124 };
125
126 // Clear and disable the other queue
127 window.NORLQ = {
128 // No-op
129 push: function () {}
130 };
131 }
132
133 script = document.createElement( 'script' );
134 script.src = $VARS.baseModulesUri;
135 script.onload = script.onreadystatechange = function () {
136 if ( !script.readyState || /loaded|complete/.test( script.readyState ) ) {
137 // Clean up
138 script.onload = script.onreadystatechange = null;
139 script = null;
140 // Callback
141 startUp();
142 }
143 };
144 document.getElementsByTagName( 'head' )[ 0 ].appendChild( script );
145 }() );