1
- {% if site.use_hide_code_button %}
2
1
< script >
3
- /**
4
- Add buttons to hide code cells
5
- */
6
-
7
-
8
- var setCodeCellVisibility = function ( inputField , kind ) {
9
- // Update the image and class for hidden
10
- var id = inputField . getAttribute ( 'data-id' ) ;
11
- var codeCell = document . querySelector ( `#${ id } div.highlight` ) ;
12
-
13
- if ( kind === "visible" ) {
14
- codeCell . classList . remove ( 'hidden' ) ;
15
- inputField . checked = true ;
16
- } else {
17
- codeCell . classList . add ( 'hidden' ) ;
18
- inputField . checked = false ;
19
- }
20
- }
21
-
22
- var toggleCodeCellVisibility = function ( event ) {
23
- // The label is clicked, and now we decide what to do based on the input field's clicked status
24
- if ( event . target . tagName === "LABEL" ) {
25
- var inputField = event . target . previousElementSibling ;
26
- } else {
27
- // It is the span inside the target
28
- var inputField = event . target . parentElement . previousElementSibling ;
2
+ /**
3
+ Add buttons to hide code cells
4
+ */
5
+
6
+
7
+ var setCodeCellVisibility = function ( inputField , kind ) {
8
+ // Update the image and class for hidden
9
+ var id = inputField . getAttribute ( 'data-id' ) ;
10
+ var codeCell = document . querySelector ( `#${ id } div.highlight` ) ;
11
+
12
+ if ( kind === "visible" ) {
13
+ codeCell . classList . remove ( 'hidden' ) ;
14
+ inputField . checked = true ;
15
+ } else {
16
+ codeCell . classList . add ( 'hidden' ) ;
17
+ inputField . checked = false ;
18
+ }
29
19
}
30
20
31
- if ( inputField . checked === true ) {
32
- setCodeCellVisibility ( inputField , "visible" ) ;
33
- } else {
34
- setCodeCellVisibility ( inputField , "hidden" ) ;
21
+ var toggleCodeCellVisibility = function ( event ) {
22
+ // The label is clicked, and now we decide what to do based on the input field's clicked status
23
+ if ( event . target . tagName === "LABEL" ) {
24
+ var inputField = event . target . previousElementSibling ;
25
+ } else {
26
+ // It is the span inside the target
27
+ var inputField = event . target . parentElement . previousElementSibling ;
28
+ }
29
+
30
+ if ( inputField . checked === true ) {
31
+ setCodeCellVisibility ( inputField , "visible" ) ;
32
+ } else {
33
+ setCodeCellVisibility ( inputField , "hidden" ) ;
34
+ }
35
35
}
36
- }
37
36
38
37
39
- // Button constructor
40
- const hideCodeButton = id => `<input class="hidebtn" type="checkbox" id="hidebtn${ id } " data-id="${ id } "><label title="Toggle cell" for="hidebtn${ id } " class="plusminus"><span class="pm_h"></span><span class="pm_v"></span></label>`
38
+ // Button constructor
39
+ const hideCodeButton = id => `<input class="hidebtn" type="checkbox" id="hidebtn${ id } " data-id="${ id } "><label title="Toggle cell" for="hidebtn${ id } " class="plusminus"><span class="pm_h"></span><span class="pm_v"></span></label>`
41
40
42
- var addHideButton = function ( ) {
43
- // If a hide button is already added, don't add another
44
- if ( document . querySelector ( 'div.hidecode input' ) !== null ) {
45
- return ;
46
- }
41
+ var addHideButton = function ( ) {
42
+ // If a hide button is already added, don't add another
43
+ if ( document . querySelector ( 'div.hidecode input' ) !== null ) {
44
+ return ;
45
+ }
47
46
48
- // Find the input cells and add a hide button
49
- document . querySelectorAll ( 'div.input_area' ) . forEach ( function ( item , index ) {
50
- if ( ! item . classList . contains ( "hidecode" ) ) {
51
- // Skip the cell if it doesn't have a hidecode class
52
- return ;
53
- }
47
+ // Find the input cells and add a hide button
48
+ document . querySelectorAll ( 'div.input_area' ) . forEach ( function ( item , index ) {
49
+ if ( ! item . classList . contains ( "hidecode" ) ) {
50
+ // Skip the cell if it doesn't have a hidecode class
51
+ return ;
52
+ }
54
53
55
- const id = codeCellId ( index )
56
- item . setAttribute ( 'id' , id ) ;
57
- // Insert the button just inside the end of the next div
58
- item . querySelector ( 'div' ) . insertAdjacentHTML ( 'beforeend' , hideCodeButton ( id ) )
54
+ const id = codeCellId ( index )
55
+ item . setAttribute ( 'id' , id ) ;
56
+ // Insert the button just inside the end of the next div
57
+ item . querySelector ( 'div' ) . insertAdjacentHTML ( 'beforeend' , hideCodeButton ( id ) )
59
58
60
- // Set up the visibility toggle
61
- hideLink = document . querySelector ( `#${ id } div.highlight + input + label` ) ;
62
- hideLink . addEventListener ( 'click' , toggleCodeCellVisibility )
63
- } ) ;
64
- }
59
+ // Set up the visibility toggle
60
+ hideLink = document . querySelector ( `#${ id } div.highlight + input + label` ) ;
61
+ hideLink . addEventListener ( 'click' , toggleCodeCellVisibility )
62
+ } ) ;
63
+ }
65
64
66
65
67
- // Initialize the hide buttos
68
- var initHiddenCells = function ( ) {
69
- // Add hide buttons to the cells
70
- addHideButton ( ) ;
66
+ // Initialize the hide buttos
67
+ var initHiddenCells = function ( ) {
68
+ // Add hide buttons to the cells
69
+ addHideButton ( ) ;
71
70
72
- // Toggle the code cells that should be hidden
73
- document . querySelectorAll ( 'div.hidecode input' ) . forEach ( function ( item ) {
74
- setCodeCellVisibility ( item , 'hidden' ) ;
75
- item . checked = true ;
76
- } )
77
- }
71
+ // Toggle the code cells that should be hidden
72
+ document . querySelectorAll ( 'div.hidecode input' ) . forEach ( function ( item ) {
73
+ setCodeCellVisibility ( item , 'hidden' ) ;
74
+ item . checked = true ;
75
+ } )
76
+ }
78
77
79
- initFunction ( initHiddenCells ) ;
78
+ initFunction ( initHiddenCells ) ;
80
79
81
- </ script >
82
- {% endif %}
80
+ </ script >
0 commit comments