-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipe_menu.js
101 lines (101 loc) · 3.6 KB
/
swipe_menu.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
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
function swipeMenu(menuArea, side='left'){
let back_button = menuArea.querySelector(".back_button")
menuArea.querySelectorAll(".submenu").forEach(el=>{
let back = document.createElement("li")
back.classList.add("back_button")
back.onclick = _=>{
if (side=="right"){el.style.right = "-100%"}
else {el.style.left = "-100%"}
el.close();
}
back.innerHTML = back_button.innerHTML;
if (el.querySelector(".menu_title")){
el.querySelector(".menu_title").after(back);
}
else{
el.prepend(back)
}
})
menuArea.querySelectorAll(".menu, .submenu").forEach(menu=>{
menu.querySelectorAll("li[submenu]").forEach(sub=>{
let target = menuArea.querySelector(`.submenu[name=${sub.getAttribute("submenu")}]`)
if (side=="right"){
target.style.right = "-100%"
}
else {
target.style.left = "-100%"
}
sub.onclick = _=>{
menu.style.filter = "brightness(0.25)"
if (side=="right"){target.style.right = 0}
else {target.style.left = 0}
menu.style.minHeight = target.scrollHeight + "px"
menuArea.style.minHeight = target.scrollHeight + "px"
}
target.close = _=> {
menu.style.filter = ""
if (side=="right"){target.style.right = "-100%"}
else {target.style.left = "-100%"}
menu.style.minHeight = ""
menuArea.style.minHeight = ""
}
target.addEventListener('close', _=> {target.close()});
})
menu.querySelectorAll("li[href]").forEach(li=>{
if (li.getAttribute("target")){
li.onclick = _=> window.open(li.getAttribute("href"), li.getAttribute("target"))
} else{
li.onclick = _=> window.open(li.getAttribute("href"),"_self")
}
li.addEventListener("mousedown", e=>{
if (e.which == 2) {
window.open(li.getAttribute("href"), "_blank")
}
})
})
})
menuArea.exitAllSubMenus = _=>{
menuArea.querySelectorAll(".menu, .submenu").forEach(menu=>{
menu.dispatchEvent(new Event('close'));
})
}
menuArea.init = (name, value, change_handler = _=>{}) => {
let main_el = menuArea.querySelector(`.menu li[submenu='${name}']`) || menuArea.querySelector(`.submenu li[submenu='${name}']`)
if (!main_el){
main_el = menuArea.querySelector(`.menu li[name='${name}']`)
main_el.innerHTML += `: <span class='helper'>${value}</span>`
return
}
let submenu = menuArea.querySelector(`.submenu[name='${name}']`)
let selected = submenu.querySelector(`li[value="${value}"]`)
selected.classList.add("selected")
main_el.innerHTML += `: <span class='helper'>${selected.textContent}</span>`
submenu.addEventListener("click", e=>{
if (e.target.tagName.toLowerCase() == "li" &&
!e.target.classList.contains('selected') &&
!e.target.classList.contains('back_button')
){
let old_selected = submenu.querySelector("li.selected")
old_selected.classList.remove("selected")
e.target.classList.add("selected")
main_el.querySelector(".helper").innerHTML = e.target.textContent
change_handler(e.target, old_selected)
}
})
}
menuArea.update = (name, value) => {
let main_el = menuArea.querySelector(`.menu li[submenu='${name}']`)
if (!main_el){
main_el = menuArea.querySelector(`.menu li[name='${name}']`)
main_el.querySelector(".helper").innerHTML = value
return
}
let submenu = menuArea.querySelector(`.submenu[name='${name}']`)
let selected_old = submenu.querySelector('li.selected')
selected_old.classList.remove('selected')
let selected = submenu.querySelector(`li[value="${value}"]`)
main_el.querySelector(".helper").innerHTML = selected.textContent
selected.classList.add("selected")
}
return menuArea
}