Wrap up remaining legacy javascript (IEFixes, wikibits)
[lhc/web/wiklou.git] / skins / common / wikibits.js
1 /**
2 * MediaWiki legacy wikibits
3 */
4 ( function ( mw, $ ) {
5 var msg,
6 win = window,
7 ua = navigator.userAgent.toLowerCase(),
8 isIE6 = ( /msie ([0-9]{1,}[\.0-9]{0,})/.exec( ua ) && parseFloat( RegExp.$1 ) <= 6.0 ),
9 isGecko = /gecko/.test( ua ) && !/khtml|spoofer|netscape\/7\.0/.test( ua ),
10 onloadFuncts = [];
11
12 if ( mw.config.get( 'wgBreakFrames' ) ) {
13 // Note: In IE < 9 strict comparison to window is non-standard (the standard didn't exist yet)
14 // it works only comparing to window.self or window.window (http://stackoverflow.com/q/4850978/319266)
15 if ( win.top !== win.self ) {
16 // Un-trap us from framesets
17 win.top.location = win.location;
18 }
19 }
20
21 win.redirectToFragment = function ( fragment ) {
22 var webKitVersion,
23 match = navigator.userAgent.match( /AppleWebKit\/(\d+)/ );
24 if ( match ) {
25 webKitVersion = parseInt( match[1], 10 );
26 if ( webKitVersion < 420 ) {
27 // Released Safari w/ WebKit 418.9.1 messes up horribly
28 // Nightlies of 420+ are ok
29 return;
30 }
31 }
32 if ( !win.location.hash ) {
33 win.location.hash = fragment;
34
35 // Mozilla needs to wait until after load, otherwise the window doesn't
36 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
37 // There's no obvious way to detect this programmatically, so we use
38 // version-testing. If Firefox fixes the bug, they'll jump twice, but
39 // better twice than not at all, so make the fix hit future versions as
40 // well.
41 if ( isGecko ) {
42 $( function () {
43 if ( win.location.hash === fragment ) {
44 win.location.hash = fragment;
45 }
46 } );
47 }
48 }
49 };
50
51 /**
52 * User-agent sniffing.
53 * To be removed in MediaWiki 1.23.
54 *
55 * @deprecated since 1.17 Use jquery.client instead
56 */
57
58 msg = 'Use feature detection or module jquery.client instead';
59
60 mw.log.deprecate( win, 'clientPC', ua, msg );
61
62 // Ignored dummy values
63 mw.log.deprecate( win, 'is_gecko', false, msg );
64 mw.log.deprecate( win, 'is_chrome_mac', false, msg );
65 mw.log.deprecate( win, 'is_chrome', false, msg );
66 mw.log.deprecate( win, 'webkit_version', false, msg );
67 mw.log.deprecate( win, 'is_safari_win', false, msg );
68 mw.log.deprecate( win, 'is_safari', false, msg );
69 mw.log.deprecate( win, 'webkit_match', false, msg );
70 mw.log.deprecate( win, 'is_ff2', false, msg );
71 mw.log.deprecate( win, 'ff2_bugs', false, msg );
72 mw.log.deprecate( win, 'is_ff2_win', false, msg );
73 mw.log.deprecate( win, 'is_ff2_x11', false, msg );
74 mw.log.deprecate( win, 'opera95_bugs', false, msg );
75 mw.log.deprecate( win, 'opera7_bugs', false, msg );
76 mw.log.deprecate( win, 'opera6_bugs', false, msg );
77 mw.log.deprecate( win, 'is_opera_95', false, msg );
78 mw.log.deprecate( win, 'is_opera_preseven', false, msg );
79 mw.log.deprecate( win, 'is_opera', false, msg );
80 mw.log.deprecate( win, 'ie6_bugs', false, msg );
81
82 /**
83 * DOM utilities for handling of events, text nodes and selecting elements
84 *
85 * To be removed in MediaWiki 1.23.
86 *
87 * @deprecated since 1.17 Use jQuery instead
88 */
89 msg = 'Use jQuery instead';
90
91 // Ignored dummy values
92 mw.log.deprecate( win, 'doneOnloadHook', undefined );
93 mw.log.deprecate( win, 'onloadFuncts', [] );
94 mw.log.deprecate( win, 'runOnloadHook', $.noop );
95 mw.log.deprecate( win, 'changeText', $.noop );
96 mw.log.deprecate( win, 'killEvt', $.noop );
97 mw.log.deprecate( win, 'addHandler', $.noop );
98 mw.log.deprecate( win, 'hookEvent', $.noop );
99 mw.log.deprecate( win, 'addClickHandler', $.noop );
100 mw.log.deprecate( win, 'removeHandler', $.noop );
101 mw.log.deprecate( win, 'getElementsByClassName', function () { return []; } );
102 mw.log.deprecate( win, 'getInnerText', function () { return ''; } );
103
104 // Run a function after the window onload event is fired
105 mw.log.deprecate( win, 'addOnloadHook', function ( hookFunct ) {
106 if ( onloadFuncts ) {
107 onloadFuncts.push(hookFunct);
108 } else {
109 // If func queue is gone the event has happened already,
110 // run immediately instead of queueing.
111 hookFunct();
112 }
113 } );
114
115 $( win ).on( 'load', function () {
116 var i, functs;
117
118 // Don't run twice
119 if ( !onloadFuncts ) {
120 return;
121 }
122
123 // Deference and clear onloadFuncts before running any
124 // hooks to make sure we don't miss any addOnloadHook
125 // calls.
126 functs = onloadFuncts.slice();
127 onloadFuncts = undefined;
128
129 // Execute the queued functions
130 for ( i = 0; i < functs.length; i++ ) {
131 functs[i]();
132 }
133 } );
134
135 /**
136 * Toggle checkboxes with shift selection
137 *
138 * To be removed in MediaWiki 1.23.
139 *
140 * @deprecated since 1.17 Use jquery.checkboxShiftClick instead
141 */
142 msg = 'Use jquery.checkboxShiftClick instead';
143 mw.log.deprecate( win, 'checkboxes', [], msg );
144 mw.log.deprecate( win, 'lastCheckbox', null, msg );
145 mw.log.deprecate( win, 'setupCheckboxShiftClick', $.noop, msg );
146 mw.log.deprecate( win, 'addCheckboxClickHandlers', $.noop, msg );
147 mw.log.deprecate( win, 'checkboxClickHandler', $.noop, msg );
148
149 /**
150 * Add a button to the default editor toolbar
151 *
152 * To be removed in MediaWiki 1.23.
153 *
154 * @deprecated since 1.17 Use mw.toolbar instead
155 */
156 mw.log.deprecate( win, 'mwEditButtons', [], 'Use mw.toolbar instead' );
157 mw.log.deprecate( win, 'mwCustomEditButtons', [], 'Use mw.toolbar instead' );
158
159 /**
160 * Spinner creation, injection and removal
161 *
162 * To be removed in MediaWiki 1.23.
163 *
164 * @deprecated since 1.18 Use jquery.spinner instead
165 */
166 mw.log.deprecate( win, 'injectSpinner', $.noop, 'Use jquery.spinner instead' );
167 mw.log.deprecate( win, 'removeSpinner', $.noop, 'Use jquery.spinner instead' );
168
169 /**
170 * Escape utilities
171 *
172 * To be removed in MediaWiki 1.23.
173 *
174 * @deprecated since 1.18 Use mw.html instead
175 */
176 mw.log.deprecate( win, 'escapeQuotes', $.noop,'Use mw.html instead' );
177 mw.log.deprecate( win, 'escapeQuotesHTML', $.noop,'Use mw.html instead' );
178
179 /**
180 * Display a message to the user
181 *
182 * To be removed in MediaWiki 1.23.
183 *
184 * @deprecated since 1.17 Use mediawiki.notify instead
185 * @param {string|HTMLElement} message To be put inside the message box
186 */
187 mw.log.deprecate( win, 'jsMsg', mw.util.jsMessage, 'Use mediawiki.notify instead' );
188
189 /**
190 * Misc. utilities
191 *
192 * To be removed in MediaWiki 1.23.
193 *
194 * @deprecated since 1.17 Use mediawiki.util instead
195 */
196 msg = 'Use mediawiki.util instead';
197 mw.log.deprecate( win, 'tooltipAccessKeyPrefix', 'alt-', msg );
198 mw.log.deprecate( win, 'tooltipAccessKeyRegexp', /\[(alt-)?(.)\]$/, msg );
199 mw.log.deprecate( win, 'updateTooltipAccessKeys', mw.util.updateTooltipAccessKeys, msg );
200 mw.log.deprecate( win, 'addPortletLink', mw.util.addPortletLink, msg );
201 mw.log.deprecate( win, 'appendCSS', mw.util.addCSS );
202
203 /**
204 * Wikipage import methods
205 */
206
207 // included-scripts tracker
208 win.loadedScripts = {};
209
210 win.importScript = function ( page ) {
211 var uri = mw.config.get( 'wgScript' ) + '?title=' +
212 mw.util.wikiUrlencode( page ) +
213 '&action=raw&ctype=text/javascript';
214 return win.importScriptURI( uri );
215 };
216
217 win.importScriptURI = function ( url ) {
218 if ( win.loadedScripts[url] ) {
219 return null;
220 }
221 win.loadedScripts[url] = true;
222 var s = document.createElement( 'script' );
223 s.setAttribute( 'src', url );
224 s.setAttribute( 'type', 'text/javascript' );
225 document.getElementsByTagName( 'head' )[0].appendChild( s );
226 return s;
227 };
228
229 win.importStylesheet = function( page ) {
230 return win.importStylesheetURI( mw.config.get( 'wgScript' ) + '?action=raw&ctype=text/css&title=' + mw.util.wikiUrlencode( page ) );
231 };
232
233 win.importStylesheetURI = function( url, media ) {
234 var l = document.createElement( 'link' );
235 l.rel = 'stylesheet';
236 l.href = url;
237 if ( media ) {
238 l.media = media;
239 }
240 document.getElementsByTagName('head')[0].appendChild( l );
241 return l;
242 };
243
244 if ( isIE6 ) {
245 win.importScriptURI( mw.config.get( 'stylepath' ) + '/common/IEFixes.js' );
246 }
247
248 }( mediaWiki, jQuery ) );