-
Notifications
You must be signed in to change notification settings - Fork 1
/
accessibility-switch.js
116 lines (102 loc) · 2.86 KB
/
accessibility-switch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* global OO */
( function( $, mw ) {
function selectSimulation( type ) {
$.each(
['protanopia', 'deuteranopia', 'tritanopia', 'monochromacy'],
function( i, type ) {
$( '.container' ).removeClass( 'as-' + type );
}
);
if ( type ) {
$( '.container' ).addClass( 'as-' + type );
}
}
/* Lifted from http://stackoverflow.com/a/2880929 */
function decodeQueryString( query ) {
var match,
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace('+', " ")); },
urlParams;
query = query.substring(1);
urlParams = {};
while ( ( match = search.exec( query ) ) !== null )
urlParams[decode(match[1])] = decode(match[2]);
return urlParams;
}
window.onpopstate = function( event ) {
if ( event && event.state && event.state.simulation ) {
selectSimulation( event.state.simulation );
}
};
$( function() {
var $extraSpace = $( '#extra-space' ),
$dropdown = $( '<ul>' )
.attr( {
'role': 'menu',
'aria-labelledby': 'Accessibility Simulation',
'class': 'dropdown-menu accessibility-simulation-dropdown'
} )
.append(
$( '<li>' ).append(
$( '<a>' )
.text( mw.msg( 'accessibility-simulation-none' ) )
.data( 'name', '' )
),
$( '<li>' ).append(
$( '<a>' )
.text( mw.msg( 'accessibility-simulation-protanopia' ) )
.data( 'name', 'protanopia' )
),
$( '<li>' ).append(
$( '<a>' )
.text( mw.msg( 'accessibility-simulation-deuteranopia' ) )
.data( 'name', 'deuteranopia' )
),
$( '<li>' ).append(
$( '<a>' )
.text( mw.msg( 'accessibility-simulation-tritanopia' ) )
.data( 'name', 'tritanopia' )
),
$( '<li>' ).append(
$( '<a>' )
.text( mw.msg( 'accessibility-simulation-monochromacy' ) )
.data( 'name', 'monochromacy' )
)
),
$dropdownButton = $( '<a>' )
.attr( {
'class': 'btn btn-default dropdown-toggle accessibility-simulation-button',
'role': 'button',
'data-toggle': 'dropdown',
'aria-expanded': 'true'
} ),
queryParameters = decodeQueryString( window.location.search ),
selectedSimulation = '';
if ( 'simulation' in queryParameters )
selectedSimulation = queryParameters.simulation;
$extraSpace.before( $( '<li>' )
.addClass( 'dropdown' )
.append( $dropdownButton, $dropdown )
);
$('.accessibility-simulation-dropdown a').on( 'click', function() {
var type = $( this ).data( 'name' ),
newQueryParameters = decodeQueryString( window.location.search ),
newUrl;
selectSimulation( type );
if ( type ) {
newQueryParameters.simulation = type;
} else {
delete newQueryParameters.simulation;
}
newUrl = '?' + $.param( newQueryParameters );
if ( window.location.hash )
newUrl += window.location.hash;
window.history.pushState( {
'simulation' : type
},
'',
newUrl
);
} );
} );
} )( jQuery, mediaWiki );