| 1 |
|
| 2 |
|
| 3 |
const EXPAND_LIMIT = 250; |
| 4 |
const MAGIC_CLASSES = { |
| 5 |
"box--expand": "handleBoxExpand"}; |
| 6 |
|
| 7 |
|
| 8 |
function addExpandListeners(elt, expand_elt, reduce_elt) { |
| 9 |
let on_click = function(){ |
| 10 |
clicked_mh_fix(elt); |
| 11 |
}; |
| 12 |
expand_elt.addEventListener('click', on_click, false); |
| 13 |
reduce_elt.addEventListener('click', on_click, false); |
| 14 |
} |
| 15 |
|
| 16 |
function handleBoxExpand(box_elt) { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
let content_elt = box_elt.getElementsByClassName("box__content")[0]; |
| 23 |
|
| 24 |
if (content_elt === undefined) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
if (content_elt.offsetHeight > EXPAND_LIMIT) { |
| 29 |
|
| 30 |
let reduce_elt = document.createElement("div"); |
| 31 |
reduce_elt.className = "box__expand_zone box__expand_zone--top show_if_parent_clicked"; |
| 32 |
let p_elt = document.createElement("p"); |
| 33 |
p_elt.textContent = reduce_txt; |
| 34 |
reduce_elt.appendChild(p_elt); |
| 35 |
box_elt.insertBefore(reduce_elt, box_elt.firstChild); |
| 36 |
|
| 37 |
|
| 38 |
let expand_elt = document.createElement("div"); |
| 39 |
expand_elt.className = "box__expand_zone box__expand_zone--bottom"; |
| 40 |
let p_elt_clicked = document.createElement("p"); |
| 41 |
p_elt_clicked.textContent = expand_txt; |
| 42 |
p_elt_clicked.className = "show_if_grandparent_not_clicked"; |
| 43 |
let p_elt_not_clicked = document.createElement("p"); |
| 44 |
p_elt_not_clicked.textContent = reduce_txt; |
| 45 |
p_elt_not_clicked.className = "show_if_grandparent_clicked"; |
| 46 |
expand_elt.appendChild(p_elt_clicked); |
| 47 |
expand_elt.appendChild(p_elt_not_clicked); |
| 48 |
box_elt.appendChild(expand_elt); |
| 49 |
|
| 50 |
addExpandListeners(box_elt, expand_elt, reduce_elt); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
function handleStateInit(elt) { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
function onClick() { |
| 62 |
elt.removeEventListener('click', onClick, false); |
| 63 |
elt.classList.remove("state_init"); |
| 64 |
|
| 65 |
for (let [className, funcName] of Object.entries(MAGIC_CLASSES)){ |
| 66 |
if (elt.classList.contains(className)) { |
| 67 |
window[funcName](elt); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
elt.addEventListener('click', onClick, false); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
for (let elt of document.getElementsByClassName("state_init")) { |
| 76 |
handleStateInit(elt); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
for (let [className, funcName] of Object.entries(MAGIC_CLASSES)){ |
| 82 |
for (let box_elt of document.getElementsByClassName(className)) { |
| 83 |
if (!box_elt.classList.contains("state_init")) { |
| 84 |
window[funcName](box_elt); |
| 85 |
} |
| 86 |
} |
| 87 |
} |