-
Notifications
You must be signed in to change notification settings - Fork 1
/
global-custom-nav-context-items.js
305 lines (277 loc) · 12.1 KB
/
global-custom-nav-context-items.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
// @name CanvasLMS - Global Custom Navigation
// @namespace https://github.com/robert-carroll/canvaslms-global-custom-navigation
// @description items only, context capable
//
**/
(function () {
// handle css, if you aren't adding .css to themes
if (document.querySelectorAll('[data-global-custom-nav-css="set"]').length == 0) {
let styles = {
// for item icons
'i.gcn_inst_menu_icon:before': 'font-size: 1.625rem;',
'i.gcn_inst_rspv_icon': 'color: var(--ic-brand-primary);',
};
if (typeof styles !== 'undefined' && Object.keys(styles).length > 0) {
let style = document.createElement('style');
style.setAttribute('data-global-custom-nav-css', 'set');
document.head.appendChild(style);
let sheet = style.sheet;
Object.keys(styles).forEach(function (key) {
sheet.insertRule(`${key} { ${styles[key]} }`, sheet.cssRules.length);
});
}
}
})();
(function () {
'use strict';
// TODO context items globally applied to consortium instances, should handle a root url, and ensure active class
// continue if curious
const globalCustomNav = {
cfg: {
context_item: '',
glbl: {
nav_selector: '#menu',
tray_portal: '#nav-tray-portal',
tray_container: 'navigation-tray-container',
space: 'tray-with-space-for-global-nav',
menuItemClass: `ic-app-header__menu-list-item`,
trayActiveClass: `ic-app-header__menu-list-item--active`
},
rspv: {
tray_portal: `span[dir="${(document.querySelector('html').getAttribute('dir') ?? 'ltr')}"] div[role="dialog"] ul`,
tray_container: 'div[class$="-tray__content"]'
},
targets: ['_self', '_blank', '_parent', '_top']
},
load: (opts) => {
if (!document.querySelector(globalCustomNav.cfg.glbl.nav_selector) && !document.querySelector(globalCustomNav.cfg.rspv.tray_portal)) return;
if (document.querySelector(globalCustomNav.cfg.glbl.nav_selector) !== 'undefined') {
globalCustomNav.dir = document.querySelector('html').getAttribute('dir') ?? 'ltr';
globalCustomNav.nav_items = Array.isArray(opts.nav_items) ? opts.nav_items : opts;
// preserve the nav item to restore active class when a tray is closed
// handle primary routes, external tools, and custom contexts
var active_context = document.querySelector(`${globalCustomNav.cfg.glbl.nav_selector} li.${globalCustomNav.cfg.glbl.trayActiveClass} a`);
if(active_context) {
globalCustomNav.cfg.context_item = active_context.id || active_context.closest('li').id;
}
globalCustomNav.prepare_nav_items(globalCustomNav.nav_items, false);
}
globalCustomNav.watch_glbl_tray();
globalCustomNav.watch_burger_tray();
},
watch_burger_tray: (_mtx, observer) => {
const portal = document.querySelector(globalCustomNav.cfg.rspv.tray_portal);
const tray_action_complete = document.querySelector('div.rspv-global-custom-nav');
if (!portal) {
if (typeof observer === 'undefined') {
const obs = new MutationObserver(globalCustomNav.watch_burger_tray);
obs.observe(document.body, {
childList: true,
subtree: true
});
}
return;
}
if (portal != null && (document.querySelector('.mobile-header-hamburger').offsetParent != null) && !tray_action_complete) {
observer.disconnect();
globalCustomNav.prepare_nav_items(globalCustomNav.nav_items, true);
document.querySelector(globalCustomNav.cfg.rspv.tray_container).classList.add('rspv-global-custom-nav');
globalCustomNav.exit_burger_tray();
}
},
exit_burger_tray: (_mtx, observer) => {
const tray_portal_open = document.querySelector(globalCustomNav.cfg.rspv.tray_portal);
if (tray_portal_open) {
if (typeof observer === 'undefined') {
const obs = new MutationObserver(globalCustomNav.exit_burger_tray);
obs.observe(document.body, {
childList: true,
subtree: true
});
}
return;
}
if (!tray_portal_open) {
observer.disconnect();
globalCustomNav.watch_burger_tray();
}
},
watch_glbl_tray: (_mtx, observer) => {
const portal = document.querySelector(globalCustomNav.cfg.glbl.tray_portal);
if (!portal) {
if (typeof observer === 'undefined') {
const obs = new MutationObserver(globalCustomNav.watch_glbl_tray);
obs.observe(document.body, {
childList: true,
});
}
return;
}
if (typeof observer !== 'undefined') {
observer.disconnect();
}
globalCustomNav.glbl_ensure_active_class(globalCustomNav.cfg.context_item);
//
const watch = new MutationObserver(globalCustomNav.exit_glbl_tray);
watch.observe(portal, {
childList: true,
subtree: true
});
},
exit_glbl_tray: (_mtx, observer) => {
const tray_portal_open = document.querySelector(`${globalCustomNav.cfg.glbl.tray_portal} div.${globalCustomNav.cfg.glbl.tray_container}`);
if (tray_portal_open) {
let ui_tray = [...tray_portal_open.classList].filter(c => c.endsWith('-tray'))[0].replace('-tray', '');
globalCustomNav.glbl_ensure_active_class(`global_nav_${ui_tray}_link`);
if (typeof observer === 'undefined') {
const obs = new MutationObserver(globalCustomNav.exit_glbl_tray);
obs.observe(document.body, {
childList: true
});
}
return;
}
if (!tray_portal_open) {
observer.disconnect();
globalCustomNav.watch_glbl_tray();
}
},
glbl_ensure_active_class: context_item => {
// if no active context is set
if(!context_item) return;
// otherwise ensure active class is restored to appropriate icon based on context
Array.from(document.querySelectorAll(`${globalCustomNav.cfg.glbl.nav_selector} .${globalCustomNav.cfg.glbl.trayActiveClass}`)).forEach(e => {
e.classList.toggle(globalCustomNav.cfg.glbl.trayActiveClass);
});
document.getElementById(context_item).closest('li').classList.add(globalCustomNav.cfg.glbl.trayActiveClass);
},
prepare_nav_items: (items, hamb = true) => {
items.forEach(item => {
// if roles for the current item are not set, the user can see it, otherwise
const user_gets_item = (typeof item.roles === 'undefined') || item.roles();
if (user_gets_item) {
globalCustomNav.create_nav_icon(item, hamb);
globalCustomNav.append_item(item, hamb);
if (item.tray) {
globalCustomNav.tray(item, hamb);
}
}
});
},
create_nav_icon: (item, hamb = true) => {
// create a DOM safe string from the title for the id, or replace it with a random string if regex returns an empty string
item.tidle = item.title.replace(/[\W_]+/g,'') || Math.random().toString(18).slice(2);
item.slug = `global_nav_${item.tidle}_link`;
// clone and create the icon, consider c4e
const icon_to_copy = (ENV.K5_USER == true && hamb == true) ? 'Home' : 'Dashboard';
const nav_icon = hamb ? `${globalCustomNav.cfg.rspv.tray_portal} svg[name="Icon${icon_to_copy}"]` : `#global_nav_${icon_to_copy.toLowerCase()}_link`;
const nav_icon_li = document.querySelector(nav_icon).closest('li');
// replace contents
const icon = nav_icon_li.cloneNode(true);
icon.setAttribute('id', (hamb ? 'rspv-' : '') + `${item.slug}-item`);
icon.querySelector('svg').parentElement.classList.add((hamb ? 'rspv-' : '') + `svg-${item.tidle}-holder`);
const icon_id = (hamb ? 'rspv-' : '') + item.slug;
icon.querySelector('a').setAttribute('id', icon_id);
icon.querySelector('a').href = item.href;
if (typeof item.target !== 'undefined' && globalCustomNav.cfg.targets.includes(item.target)) {
icon.querySelector('a').setAttribute('target', item.target);
}
// get the text for the cloned nav item, global or hamb
var icon_text = icon.querySelector('.menu-item__text') || icon.querySelector('span[letter-spacing="normal"]');
// set the clones text to the item text
icon_text.textContent = item.title;
// prepare for svg
const svg_holder = icon.querySelector((hamb ? '.rspv-svg' : '.svg') + `-${item.tidle}-holder`);
icon.querySelector('svg').classList.remove('ic-icon-svg--dashboard', 'svg-icon-home');
let svg_class = [...icon.querySelector('svg').classList];
if (!hamb) {
icon.classList.remove(globalCustomNav.cfg.glbl.trayActiveClass);
}
// remove cloned svg
icon.querySelector('svg').remove();
// import svg
if (/^icon-[a-z]/.test(item.icon_svg) == true) {
// instructure icon
let instuicon = `<div id="${(hamb ? 'rspv-' : '') + `${item.slug}-svg`}" role="presentation">`;
instuicon += `<i class="icon-line ${item.icon_svg}${hamb ? ' gcn_inst_rspv_icon' : ''} gcn_inst_menu_icon"></i></div>`;
svg_holder.insertAdjacentHTML('afterbegin', instuicon);
} else if (/^https/.test(item.icon_svg)) {
// externally hosted svg, you must handle cors policies yourself
fetch(item.icon_svg, {
mode: 'cors',
method: 'GET',
headers: {
'Access-Control-Request-Method': 'GET',
'Accept': 'text/plain',
'Content-Type': 'text/plain',
}
})
.then(r => r.text())
.then(svg => {
svg_holder.insertAdjacentHTML('afterbegin', svg);
icon.querySelector('svg').setAttribute('id', (hamb ? 'rspv-' : '') + `${item.slug}-svg`);
icon.querySelector('svg').classList.add(...svg_class);
})
.catch(console.error.bind(console));
} else if (/^<svg/.test(item.icon_svg)) {
// inline/script svg
svg_holder.insertAdjacentHTML('afterbegin', item.icon_svg);
icon.querySelector('svg').setAttribute('id', (hamb ? 'rspv-' : '') + `${item.slug}-svg`);
icon.querySelector('svg').classList.add(...svg_class);
}
item.icon = icon;
return;
},
append_item: (item, hamb = true) => {
const target_ul = hamb ? globalCustomNav.cfg.rspv.tray_portal : globalCustomNav.cfg.glbl.nav_selector;
const target_li = document.querySelector(`${target_ul} li:last-child`);
// nav item placement
if (item.position !== 'undefined' && typeof item.position === 'number') {
// positioned
const position = (hamb == true ? globalCustomNav.cfg.rspv.tray_portal : globalCustomNav.cfg.glbl.nav_selector) + ` > li:nth-of-type(${item.position})`;
document.querySelector(position).after(item.icon);
} else if (item.position !== 'undefined' && item.position == 'after') {
target_li.after(item.icon);
} else {
target_li.before(item.icon);
}
const regex = new RegExp(`^${item.href}`);
if (!hamb && regex.test(window.location.pathname)) {
globalCustomNav.cfg.context_item = item.slug;
globalCustomNav.glbl_ensure_active_class(globalCustomNav.cfg.context_item);
}
}
};
// configure custom opts
const globalCustomNav_items = [{
title: 'Instructure Icon',
icon_svg: 'icon-pin',
href: 'https://instructure.design/#icons-font',
target: '_blank',
position: 1, // can be one of : integer (position after first), 'after' (help or last), 'before' (help or last)
},
{
title: 'Custom Context',
// custom context handles active class in global nav
icon_svg: 'icon-expand-start',
href: '/courses/101',
target: '_top',
roles: function () {
return ['user'].some(a => ENV.current_user_roles.includes(a));
}
},
{
title: 'Custom Account Context',
// custom context handles active class in global nav
icon_svg: 'icon-ruler',
href: '/accounts/self',
target: '_self',
roles: function () {
return ['admin'].some(a => ENV.current_user_roles.includes(a));
}
}
];
// load custom nav options
globalCustomNav.load(globalCustomNav_items);
})();