Fossil SCM
Restructure accordion.js to not pollute the global NS and not duplicate the onclick handler for each element.
Commit
3c95062e7489bead752947cdd7c68c21a80baa2da8831f1d70d394ff3e8f3d44
Parent
20c836f94450694…
1 file changed
+33
-23
+33
-23
| --- src/accordion.js | ||
| +++ src/accordion.js | ||
| @@ -40,35 +40,27 @@ | ||
| 40 | 40 | ** .accordion_panel { |
| 41 | 41 | ** overflow-y: clip; |
| 42 | 42 | ** } |
| 43 | 43 | ** } |
| 44 | 44 | */ |
| 45 | -var acc_svgdata = ["data:image/svg+xml,"+ | |
| 46 | - "%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E"+ | |
| 47 | - "%3Cpath style='fill:black;opacity:0' d='M16,16H0V0h16v16z'/%3E"+ | |
| 48 | - "%3Cpath style='fill:rgb(240,240,240)' d='M14,14H2V2h12v12z'/%3E"+ | |
| 49 | - "%3Cpath style='fill:rgb(64,64,64)' d='M13,13H3V3h10v10z'/%3E"+ | |
| 50 | - "%3Cpath style='fill:rgb(248,248,248)' d='M12,12H4V4h8v8z'/%3E"+ | |
| 51 | - "%3Cpath style='fill:rgb(80,128,208)' d='", "'/%3E%3C/svg%3E", | |
| 52 | - "M5,7h2v-2h2v2h2v2h-2v2h-2v-2h-2z", "M11,9H5V7h6v6z"]; | |
| 53 | -var a = document.getElementsByClassName("accordion"); | |
| 54 | -for(var i=0; i<a.length; i++){ | |
| 55 | - var img = document.createElement("img"); | |
| 56 | - img.src = acc_svgdata[0]+acc_svgdata[2]+acc_svgdata[1]; | |
| 57 | - img.className = "accordion_btn accordion_btn_plus"; | |
| 58 | - a[i].insertBefore(img,a[i].firstChild); | |
| 59 | - img = document.createElement("img"); | |
| 60 | - img.src = acc_svgdata[0]+acc_svgdata[3]+acc_svgdata[1]; | |
| 61 | - img.className = "accordion_btn accordion_btn_minus"; | |
| 62 | - a[i].insertBefore(img,a[i].firstChild); | |
| 63 | - a[i].addEventListener("click",function(evt){ | |
| 45 | +(function(){ | |
| 46 | + const acc_svgdata = ["data:image/svg+xml,"+ | |
| 47 | + "%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E"+ | |
| 48 | + "%3Cpath style='fill:black;opacity:0' d='M16,16H0V0h16v16z'/%3E"+ | |
| 49 | + "%3Cpath style='fill:rgb(240,240,240)' d='M14,14H2V2h12v12z'/%3E"+ | |
| 50 | + "%3Cpath style='fill:rgb(64,64,64)' d='M13,13H3V3h10v10z'/%3E"+ | |
| 51 | + "%3Cpath style='fill:rgb(248,248,248)' d='M12,12H4V4h8v8z'/%3E"+ | |
| 52 | + "%3Cpath style='fill:rgb(80,128,208)' d='", "'/%3E%3C/svg%3E", | |
| 53 | + "M5,7h2v-2h2v2h2v2h-2v2h-2v-2h-2z", "M11,9H5V7h6v6z"]; | |
| 54 | + const a = document.getElementsByClassName("accordion"); | |
| 55 | + const xClickyHTML = /^(?:A|AREA|BUTTON|INPUT|LABEL|SELECT|TEXTAREA|DETAILS)$/; | |
| 56 | + const onclick = function(evt){ | |
| 64 | 57 | /* Ignore clicks to hyperlinks and other "click-responsive" HTML elements in |
| 65 | 58 | ** ".accordion" headers (for which Fossil uses <DIV> elements that represent |
| 66 | 59 | ** section headers). */ |
| 67 | - var xClickyHTML = /^(?:A|AREA|BUTTON|INPUT|LABEL|SELECT|TEXTAREA|DETAILS)$/; | |
| 68 | 60 | if( xClickyHTML.test(evt.target.tagName) ) return; |
| 69 | - var x = this.nextElementSibling; | |
| 61 | + const x = this.nextElementSibling; | |
| 70 | 62 | if( this.classList.contains("accordion_closed") ){ |
| 71 | 63 | x.style.maxHeight = x.scrollHeight + "px"; |
| 72 | 64 | setTimeout(function(){ |
| 73 | 65 | x.style.maxHeight = ""; |
| 74 | 66 | if( !window.CSS || !window.CSS.supports("overflow: clip") ){ |
| @@ -83,7 +75,25 @@ | ||
| 83 | 75 | setTimeout(function(){ |
| 84 | 76 | x.style.maxHeight = "0"; |
| 85 | 77 | },1); |
| 86 | 78 | } |
| 87 | 79 | this.classList.toggle("accordion_closed"); |
| 88 | - }); | |
| 89 | -} | |
| 80 | + }; | |
| 81 | + for(const e of a){ | |
| 82 | + let img = document.createElement("img"); | |
| 83 | + img.src = acc_svgdata[0]+acc_svgdata[2]+acc_svgdata[1]; | |
| 84 | + img.className = "accordion_btn accordion_btn_plus"; | |
| 85 | + e.insertBefore(img,e.firstChild); | |
| 86 | + img = document.createElement("img"); | |
| 87 | + img.src = acc_svgdata[0]+acc_svgdata[3]+acc_svgdata[1]; | |
| 88 | + img.className = "accordion_btn accordion_btn_minus"; | |
| 89 | + e.insertBefore(img,e.firstChild); | |
| 90 | + e.addEventListener("click", onclick); | |
| 91 | + if( e.classList.contains('collapsed') ){ | |
| 92 | + /* C code sets the 'collapsed' class for accordions which should | |
| 93 | + start that way. */ | |
| 94 | + e.classList.remove('collapsed'); | |
| 95 | + console.warn("WTH?", e); | |
| 96 | + e.click(); | |
| 97 | + } | |
| 98 | + } | |
| 99 | +})(); | |
| 90 | 100 |
| --- src/accordion.js | |
| +++ src/accordion.js | |
| @@ -40,35 +40,27 @@ | |
| 40 | ** .accordion_panel { |
| 41 | ** overflow-y: clip; |
| 42 | ** } |
| 43 | ** } |
| 44 | */ |
| 45 | var acc_svgdata = ["data:image/svg+xml,"+ |
| 46 | "%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E"+ |
| 47 | "%3Cpath style='fill:black;opacity:0' d='M16,16H0V0h16v16z'/%3E"+ |
| 48 | "%3Cpath style='fill:rgb(240,240,240)' d='M14,14H2V2h12v12z'/%3E"+ |
| 49 | "%3Cpath style='fill:rgb(64,64,64)' d='M13,13H3V3h10v10z'/%3E"+ |
| 50 | "%3Cpath style='fill:rgb(248,248,248)' d='M12,12H4V4h8v8z'/%3E"+ |
| 51 | "%3Cpath style='fill:rgb(80,128,208)' d='", "'/%3E%3C/svg%3E", |
| 52 | "M5,7h2v-2h2v2h2v2h-2v2h-2v-2h-2z", "M11,9H5V7h6v6z"]; |
| 53 | var a = document.getElementsByClassName("accordion"); |
| 54 | for(var i=0; i<a.length; i++){ |
| 55 | var img = document.createElement("img"); |
| 56 | img.src = acc_svgdata[0]+acc_svgdata[2]+acc_svgdata[1]; |
| 57 | img.className = "accordion_btn accordion_btn_plus"; |
| 58 | a[i].insertBefore(img,a[i].firstChild); |
| 59 | img = document.createElement("img"); |
| 60 | img.src = acc_svgdata[0]+acc_svgdata[3]+acc_svgdata[1]; |
| 61 | img.className = "accordion_btn accordion_btn_minus"; |
| 62 | a[i].insertBefore(img,a[i].firstChild); |
| 63 | a[i].addEventListener("click",function(evt){ |
| 64 | /* Ignore clicks to hyperlinks and other "click-responsive" HTML elements in |
| 65 | ** ".accordion" headers (for which Fossil uses <DIV> elements that represent |
| 66 | ** section headers). */ |
| 67 | var xClickyHTML = /^(?:A|AREA|BUTTON|INPUT|LABEL|SELECT|TEXTAREA|DETAILS)$/; |
| 68 | if( xClickyHTML.test(evt.target.tagName) ) return; |
| 69 | var x = this.nextElementSibling; |
| 70 | if( this.classList.contains("accordion_closed") ){ |
| 71 | x.style.maxHeight = x.scrollHeight + "px"; |
| 72 | setTimeout(function(){ |
| 73 | x.style.maxHeight = ""; |
| 74 | if( !window.CSS || !window.CSS.supports("overflow: clip") ){ |
| @@ -83,7 +75,25 @@ | |
| 83 | setTimeout(function(){ |
| 84 | x.style.maxHeight = "0"; |
| 85 | },1); |
| 86 | } |
| 87 | this.classList.toggle("accordion_closed"); |
| 88 | }); |
| 89 | } |
| 90 |
| --- src/accordion.js | |
| +++ src/accordion.js | |
| @@ -40,35 +40,27 @@ | |
| 40 | ** .accordion_panel { |
| 41 | ** overflow-y: clip; |
| 42 | ** } |
| 43 | ** } |
| 44 | */ |
| 45 | (function(){ |
| 46 | const acc_svgdata = ["data:image/svg+xml,"+ |
| 47 | "%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E"+ |
| 48 | "%3Cpath style='fill:black;opacity:0' d='M16,16H0V0h16v16z'/%3E"+ |
| 49 | "%3Cpath style='fill:rgb(240,240,240)' d='M14,14H2V2h12v12z'/%3E"+ |
| 50 | "%3Cpath style='fill:rgb(64,64,64)' d='M13,13H3V3h10v10z'/%3E"+ |
| 51 | "%3Cpath style='fill:rgb(248,248,248)' d='M12,12H4V4h8v8z'/%3E"+ |
| 52 | "%3Cpath style='fill:rgb(80,128,208)' d='", "'/%3E%3C/svg%3E", |
| 53 | "M5,7h2v-2h2v2h2v2h-2v2h-2v-2h-2z", "M11,9H5V7h6v6z"]; |
| 54 | const a = document.getElementsByClassName("accordion"); |
| 55 | const xClickyHTML = /^(?:A|AREA|BUTTON|INPUT|LABEL|SELECT|TEXTAREA|DETAILS)$/; |
| 56 | const onclick = function(evt){ |
| 57 | /* Ignore clicks to hyperlinks and other "click-responsive" HTML elements in |
| 58 | ** ".accordion" headers (for which Fossil uses <DIV> elements that represent |
| 59 | ** section headers). */ |
| 60 | if( xClickyHTML.test(evt.target.tagName) ) return; |
| 61 | const x = this.nextElementSibling; |
| 62 | if( this.classList.contains("accordion_closed") ){ |
| 63 | x.style.maxHeight = x.scrollHeight + "px"; |
| 64 | setTimeout(function(){ |
| 65 | x.style.maxHeight = ""; |
| 66 | if( !window.CSS || !window.CSS.supports("overflow: clip") ){ |
| @@ -83,7 +75,25 @@ | |
| 75 | setTimeout(function(){ |
| 76 | x.style.maxHeight = "0"; |
| 77 | },1); |
| 78 | } |
| 79 | this.classList.toggle("accordion_closed"); |
| 80 | }; |
| 81 | for(const e of a){ |
| 82 | let img = document.createElement("img"); |
| 83 | img.src = acc_svgdata[0]+acc_svgdata[2]+acc_svgdata[1]; |
| 84 | img.className = "accordion_btn accordion_btn_plus"; |
| 85 | e.insertBefore(img,e.firstChild); |
| 86 | img = document.createElement("img"); |
| 87 | img.src = acc_svgdata[0]+acc_svgdata[3]+acc_svgdata[1]; |
| 88 | img.className = "accordion_btn accordion_btn_minus"; |
| 89 | e.insertBefore(img,e.firstChild); |
| 90 | e.addEventListener("click", onclick); |
| 91 | if( e.classList.contains('collapsed') ){ |
| 92 | /* C code sets the 'collapsed' class for accordions which should |
| 93 | start that way. */ |
| 94 | e.classList.remove('collapsed'); |
| 95 | console.warn("WTH?", e); |
| 96 | e.click(); |
| 97 | } |
| 98 | } |
| 99 | })(); |
| 100 |