This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbinary.js
More file actions
420 lines (375 loc) · 16.1 KB
/
binary.js
File metadata and controls
420 lines (375 loc) · 16.1 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* js code for Drop-Down Menu
*/
require('select2');
export function select2Matcher(params, data) {
const query = params.term || '';
const text = data.text || '';
if (text.toUpperCase().indexOf(query.toUpperCase()) === 0) {
return data;
}
return false;
};
export function select2onFocus() {
let select2_open;
// Trigger Select2 on TAB button or focus
$(document).on('focus', '.select2-selection--single', (e) => {
select2_open = $(e.target).parent().parent().siblings('select');
select2_open.select2('open');
});
// ie11 fix
if (/rv:11.0/i.test(navigator.userAgent)) {
$(document).on('blur', '.select2-search__field', () => {
select2_open.select2('close');
});
}
};
export function hide_menu($element) {
$element.animate({'opacity': 0}, 100, () => {
$element.css('visibility', 'hidden')
.css('display', 'none');
});
}
export function show_menu($element) {
$element.css('visibility', 'visible')
.css('display', 'block')
.animate({'opacity': 1}, 100);
}
export function navMenuListener() {
$('.nav-menu').on('click', (event) => {
event.stopPropagation();
hide_menu($('.top-nav-menu li ul'));
hide_menu($('#language_select, #select_language'));
const $el = $('#all-accounts, #all-accounts-top');
if (+$el.css('opacity') === 1 ) {
hide_menu($el);
} else {
show_menu($el);
}
});
}
export function selectDropdown(selector, has_label) {
if (!selector) return;
const $selector = $(selector);
let $select_dropdown, $list, $list_items;
function init() {
const is_initialized = $selector.hasClass('select-hidden');
if (!is_initialized) {
$selector.addClass('select-hidden')
.wrap('<div class="select"></div>')
.after('<div class="select-dropdown" tabindex="0"></div>');
}
$select_dropdown = $selector.next('div.select-dropdown');
$select_dropdown.text($selector.find(':selected').text()).wrapInner('<span></span>');
if ($selector.attr('disabled') === 'disabled') {
$select_dropdown.addClass('disabled');
}
$list = $select_dropdown.parent().find('.select-options');
if ($list.length) {
// empty list to repopulate
$list.empty();
} else {
// create list
$list = $('<ul />', { class: 'select-options' }).insertAfter($select_dropdown);
}
const optgroups = $selector.children('optgroup');
if (optgroups.length) {
// break down group into labels with its list items
optgroups.each((idx, el) => {
const options = $(el).children();
const label = $(el).attr('label');
appendToList(options, label);
});
} else {
const options = $selector.children('option');
appendToList(options);
}
// Attach event listeners
$select_dropdown.off('click').on('click', (e) => {
e.stopPropagation();
if ($select_dropdown.hasClass('disabled')) return;
// expand dropdown expand/collapse
const $siblings = $('.select-dropdown').not(e.target);
if ($siblings.hasClass('show')) {
$siblings.removeClass('show');
}
$select_dropdown.toggleClass('show');
const list = $select_dropdown.parent().find('.select-options');
const forceScroll = (element) => {
element.scrollTop += 1;
element.scrollTop -= 1;
};
if (list[0].clientHeight < list[0].scrollHeight) {
const interval = setInterval(forceScroll.bind(null, list[0]), 300);
setTimeout(() => {
clearInterval(interval);
}, 1500);
}
});
$list_items = $list.children('li');
$list_items.off('click').on('click', (e) => {
e.stopPropagation();
const $target = $(e.target);
if ($target.hasClass('disabled') || $target.hasClass('label')) return;
$select_dropdown.text($target.text()).removeClass('show').wrapInner('<span></span>');
const selected_value = $selector.val();
const dropdown_value = $target.data('value');
// sync original select with selected dropdown value
if (selected_value !== dropdown_value) {
triggerEventChange(dropdown_value);
$list_items.not(e.target).each((idx, el) => {
$(el).removeClass('selected');
});
$target.addClass('selected');
removeActiveClasses();
}
});
const removeActiveClasses = () => {
const list = $select_dropdown.parent().find('.select-options');
list.find('li.select-items').removeClass('active');
};
const triggerEventChange = (value) => {
const event = new Event('change');
// dispatch event to trigger onChange value
$selector.val(value).get(0).dispatchEvent(event);
};
// attach focus event/class
$select_dropdown.on('focusin', () => $select_dropdown.addClass('focused'));
$select_dropdown.on('focusout', () => $select_dropdown.removeClass('focused'));
// attach keypress events
$select_dropdown.off('keydown').on('keydown', (e) => {
const selected_value = $selector.val();
const key = e.keyCode || e.which;
const key_string = String.fromCharCode(key);
const $target = $(e.target).next('ul.select-options');
const $active = $target.children().filter('.active');
const active_target = $target.find('li.select-items.active').first();
let $current;
const key_matching_item = $target.find('li.select-items').filter((idx, item) => {
const found_item = $(item).not('.selected').not('.active').not('.disabled').text().charAt(0).toLowerCase() === key_string.toLowerCase();
return found_item;
});
const isAlphaNumeric = /^[a-z0-9]+$/i.test(key_string);
const isActiveFocus = ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused'));
if (!key === 9 || isActiveFocus) {
e.preventDefault();
}
switch (key) {
case 9:
if ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
removeActiveClasses();
$select_dropdown.removeClass('show');
}
break;
case 13:
if (active_target && active_target.data('value') && (active_target.data('value') !== selected_value) || active_target.data('value') === '') {
$target.find('li.select-items').removeClass('selected');
triggerEventChange(active_target.data('value'));
active_target.addClass('selected');
$select_dropdown.text(active_target.text()).removeClass('show').wrapInner('<span></span>');
}
removeActiveClasses();
$select_dropdown.removeClass('show');
break;
case 32:
if ($select_dropdown.hasClass('focused') && !$select_dropdown.hasClass('show')) {
e.preventDefault();
removeActiveClasses();
$select_dropdown.addClass('show');
}
else if ($select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
if (active_target && active_target.data('value') && (active_target.data('value') !== selected_value) || active_target.data('value') === '') {
$target.find('li.select-items').removeClass('selected');
triggerEventChange(active_target.data('value'));
active_target.addClass('selected');
$select_dropdown.text(active_target.text()).removeClass('show').wrapInner('<span></span>');
}
removeActiveClasses();
$select_dropdown.removeClass('show');
}
break;
case 38:
removeActiveClasses();
$current = $active.prevAll(':not(.disabled):not(.selected)').eq(0);
if (!$active.length || $active.is(':first-child')) {
$current = $target.children().not('.disabled').not('.selected').last();
}
$current.addClass('active');
break;
case 40:
if ($select_dropdown.hasClass('focused')) {
e.preventDefault();
}
removeActiveClasses();
if (!$select_dropdown.hasClass('show')) {
$select_dropdown.addClass('show');
}
$current = $active.nextAll(':not(.disabled):not(.selected)').eq(0);
if (!$active.length || $active.is(':last-child')) {
$current = $target.children().not('.disabled').not('.selected').first();
}
$current.addClass('active');
break;
default:
if (isAlphaNumeric && key_matching_item.length && $select_dropdown.hasClass('show') && $select_dropdown.hasClass('focused')) {
removeActiveClasses();
key_matching_item.first().addClass('active');
}
}
});
};
function appendToList(options, label) {
if (has_label && label) {
$('<li />', {
text : label,
class: 'select-items label',
}).appendTo($list);
}
$.map(options, (el) => {
const $el = $(el);
const is_disabled = $el.is(':disabled');
const is_selected = $el.is(':selected');
const className = `select-items${is_selected ? ' selected': ''}${is_disabled ? ' disabled': ''}`;
$('<li />', {
'text' : $el.text(),
'data-value': $el.val(),
'class' : className,
}).appendTo($list);
});
}
init();
}
export function topNavMenuListener() {
const $menu = $('.top-nav-menu li ul');
const $menus_to_hide = $('#all-accounts, #all-accounts-top, #language_select, #select_language');
const nav_caret_class = 'nav-caret';
$('.top-nav-menu > li.nav-dropdown-toggle').on('click', function(event) {
const $target = $(event.target);
if ($target.find('span').hasClass(nav_caret_class) || $target.hasClass(nav_caret_class)) {
event.stopPropagation();
const $child_menu = $(this).find(' > ul');
if (+$child_menu.css('opacity') === 1) {
hide_menu($menu);
} else if (+$child_menu.css('opacity') === 0) {
hide_menu($menus_to_hide);
$menu.animate({'opacity': 0}, 100, () => {
$menu.css('visibility', 'hidden');
}).promise().then(() => { show_menu($child_menu); });
}
}
});
}
export function documentListener() {
const select_els = document.getElementsByClassName('select-dropdown');
$(document).on('click', () => {
hide_menu($('#all-accounts, #all-accounts-top'));
hide_menu($('.top-nav-menu li ul'));
hide_menu($('#language_select, #select_language'));
if (select_els.length && select_els.length > 0) {
// collapse dropdowns when clicking outside
Array.from(select_els).forEach(el => {
if ($(el).hasClass('show') || $(el).hasClass('focused')) {
$(el).removeClass('show focused');
}
});
}
});
}
export function langListener() {
$('.languages').on('click', (event) => {
event.stopPropagation();
hide_menu($('.top-nav-menu li ul'));
hide_menu($('#all-accounts, #all-accounts-top'));
const $el = $('#language_select, #select_language');
if (+$el.css('opacity') === 1 ) {
hide_menu($el);
} else {
show_menu($el);
}
});
}
export function initMenuContent(_menu_containers) {
const listeners_events = [];
_menu_containers.filter(':not(.follow-default)').delegate('.tm-a,.tm-a-2', 'click', (event) => {
event.preventDefault();
const target = $(event.target);
const tab_id = target.parents('li:first').attr('id');
if (tab_id) {
const tab_container = target.parents('.tm-ul');
let selected_tab =
// find previously active tab
tab_container.find('.tm-a,.tm-a-2')
// remove previously active tab
.removeClass('a-active').end()
// unwrap previously active tab
.find('.menu-wrap-a .tm-a').unwrap().unwrap()
// go back to selected target
.end().end()
// set active class to it
.addClass('a-active')
// set active class to its parent as well
.parents('.tm-li').addClass('active').removeClass('hover').find('.tm-li-2').addClass('active').end()
// wrap it
.find('.tm-a').wrap('<span class="menu-wrap-a"><span class="menu-wrap-b"></span></span>').end()
// remove previously active parent
.siblings().removeClass('active').find('.tm-li-2').removeClass('active').end()
.end().end();
// replace span to a, to make it clickable for real
const span_tm_a = tab_container.find('span.tm-a');
span_tm_a.replaceWith(`<a href="#" class="${span_tm_a.attr('class')}">${span_tm_a.html()}</a>`);
const menu_li = selected_tab.parents('li');
let sub_menu_selected = menu_li.find('.tm-ul-2 .a-active');
let selected_tab_id = menu_li.attr('id');
if (!sub_menu_selected.length) {
sub_menu_selected = menu_li.find('.tm-a-2:first').addClass('a-active');
if (sub_menu_selected.length) {
selected_tab = sub_menu_selected;
selected_tab_id = sub_menu_selected.parents('li').attr('id');
} else {
selected_tab_id = menu_li.attr('id');
}
}
const selected_content = $(`#${selected_tab_id}-content`)
// show selected tab content
.removeClass('invisible')
// and hide the rest
.siblings(':not(.sticky)').addClass('invisible').end();
push_to_listeners({
id : selected_tab_id,
target : selected_tab,
content: selected_content,
menu : menu_li.parents('ul.tm-ul'),
event,
});
}
return false;
});
function push_to_listeners(info) {
// push to listeners events
for (let i = 0; i < listeners_events.length; i++) {
listeners_events[i](info);
}
}
}
/*
* js code for tabs with subsections
*/
export function tabListener() {
$('.tm-ul > li').hover(
function () {
$(this).addClass('hover');
},
function () {
$(this).removeClass('hover');
}
);
initMenuContent($('.content-tab-container').find('.tm-ul'));
}
$(document).ready(() => {
navMenuListener();
topNavMenuListener();
documentListener();
langListener();
tabListener();
select2onFocus();
});