-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotfm-spoiler.js
67 lines (62 loc) · 2.91 KB
/
otfm-spoiler.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
jQuery(document).ready(function($){
$('.otfm-sp__title').click(function(){
$(this).parent().toggleClass('js-otfm-sp__opened js-otfm-sp__closed');
if( $(this).parent().hasClass('js-otfm-sp__opened') ){
$(this).parent().attr('aria-expanded','true');
} else {
$(this).parent().attr('aria-expanded','false');
}
});
$('.otfm-sp__wrapper:not([role="button"])').each(function() {
$(this).attr('role','button');
});
$('.otfm-sp__wrapper:not([tabindex="0"])').each(function() {
$(this).attr('tabindex','0');
});
$('.otfm-sp__wrapper:not([aria-expanded="false"])').each(function() {
$(this).attr('aria-expanded','false');
});
var spWrapper = '<div class="otfm-sp__content" style="height:0;opacity:0;visibility:hidden;"></div>';
$('.wp-block-otfm-box-spoiler-start').each(function() {
$(this).removeClass('js-otfm-sp-box__closed').addClass('js-otfm-sp__closed');
var spContent = $(this).nextUntil('.wp-block-otfm-box-spoiler-end');
if ( spContent.next().hasClass('wp-block-otfm-box-spoiler-end') && !spContent.next().hasClass('wp-block-otfm-box-spoiler-start') ){
$(spContent).detach().appendTo(this).wrapAll(spWrapper);
} else {
$(this).addClass('js-otfm-sp__no_closed_tag').next().detach().appendTo(this).wrapAll(spWrapper);
}
});
$('#ogs_cr_st').remove();
$('.otfm-sp__wrapper').keydown(function(event) {
var keyCode = event.which; // захват клавиши
switch(keyCode){
case 38: // up arrow - если открыт, закрыть
$(this).removeClass('js-otfm-sp__opened').addClass('js-otfm-sp__closed').attr('aria-expanded','false');
event.preventDefault();
break;
case 40: // down key - если закрыт, открыть
$(this).removeClass('js-otfm-sp__closed').addClass('js-otfm-sp__opened').attr('aria-expanded','true');
event.preventDefault();
break;
case 13: case 32: // enter или space - переключить состояние
$(this).toggleClass('js-otfm-sp__opened js-otfm-sp__closed');
if( $(this).hasClass('js-otfm-sp__opened') ){
$(this).attr('aria-expanded','true');
} else {
$(this).attr('aria-expanded','false');
}
event.preventDefault();
break;
case 35: // end key - к последнему
$('.otfm-sp__wrapper').last().focus();
event.preventDefault();
break;
case 36: // home key - к первому
$('.otfm-sp__wrapper').first().focus();
event.preventDefault();
break;
default:
// ничего
}
});
});