-
Notifications
You must be signed in to change notification settings - Fork 0
/
subway.js
132 lines (107 loc) · 3.74 KB
/
subway.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @title WET-BOEW GC Subway map mutator
* @overview Plugin used to mutate DOM elements depending on viewport size, in order to follow order accessibility criteria while respecting UI
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @gormfrank
*/
( function( $, window, wb ) {
"use strict";
var $document = wb.doc,
componentName = "gc-subway-cra",
selector = ".provisional." + componentName,
initEvent = "wb-init ." + componentName,
views = {
xxs: "xxsmallview",
xs: "xsmallview",
sm: "smallview",
md: "mediumview",
lg: "largeview",
xl: "xlargeview"
},
mainClass = "gc-subway-cra-section",
toggleClass = "wb-inv",
desktopInited = false,
$html = wb.html,
$h1, $h2, $h1Copy, $menu, $main,
/**
* @method init
* @param {jQuery Event} event Event that triggered the function call
*/
init = function( event ) {
// Start initialization
// returns DOM object = proceed with init
// returns undefined = do not proceed with init (e.g., already initialized)
var elm = wb.init( event, componentName, selector ),
h1,
$elm;
if ( elm && event.currentTarget === event.target ) {
$elm = $( elm );
$h1 = $( "h1", $elm );
h1 = $h1.get( 0 );
// Add Subway H1 to skip links only once and if it is a sub-page
if ( h1 ) {
// Ensure the element have an ID
h1.id = h1.id || wb.getId();
wb.addSkipLink( wb.i18n( "skip-prefix" ) + " " + h1.textContent, { href: "#" + h1.id } );
}
// trigger resizing
onResize( $elm );
// Identify that initialization has completed
wb.ready( $elm, componentName );
}
},
/**
* Mutate DOM depending on breakpoint
* @method onResize
* @param {jQuery DOM element | jQuery Event} $elm Element targetted by this plugin, which is the nav | Resizing event
*/
onResize = function( $elm ) {
if ( !$elm.length ) {
$elm = $( selector );
}
// Ensure the page contains at least two heading level 1
if ( $( "main h1" ).length < 2 ) {
$document.off( wb.resizeEvents, onResize );
$elm.addClass( "no-blink p-0" );
return;
}
// Desktop view, setup and mutate H1s
if ( $html.hasClass( views.md ) || $html.hasClass( views.lg ) ||
$html.hasClass( views.xl ) ) {
// Initiate desktop mode only once
if ( !desktopInited ) {
initDesktop( $elm );
}
$h1.addClass( toggleClass );
$h1Copy.prependTo( $main );
$h2.prependTo( $menu );
} else if ( ( $html.hasClass( views.sm ) || $html.hasClass( views.xs ) || $html.hasClass( views.xxs ) ) && desktopInited ) {
// Mobile view, mutate back to mobile first if needed
$h1.removeClass( toggleClass );
$h1Copy.remove();
$( "h2:first-child", $menu ).remove();
}
},
/**
* Initate setup for desktop mode
* @method initDesktop
* @param {jQuery DOM element} $elm Element targetted by this plugin, which is the nav
*/
initDesktop = function( $elm ) {
$h2 = $( "<h2 class='h3 hidden-xs visible-md visible-lg mrgn-tp-0'>Sections</h2>" );
$h1Copy = $( "<div class='gc-subway-cra-h1' aria-hidden='true'>" + $h1.text() + "</div>" );
$( "ul", $elm ).first().wrap( "<div class='gc-subway-cra-menu-nav'></div>" );
$menu = $( ".gc-subway-cra-menu-nav", $elm );
$elm.nextUntil( ".pagedetails, .gc-subway-cra-support, .gc-subway-cra-section-end" ).wrapAll( "<section class='provisional " + mainClass + "'>" );
$main = $elm.next();
// Prevent on-load blinking on desktop
$elm.addClass( "no-blink" );
desktopInited = true;
};
// Listen for resizing and mutate the DOM accordingly
$document.on( wb.resizeEvents, onResize );
// Bind the init event of the plugin
$document.on( "timerpoke.wb " + initEvent, selector + ".provisional", init );
// Add the timer poke to initialize the plugin
wb.add( selector );
} )( jQuery, window, wb );