-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmany.py
125 lines (94 loc) · 3.31 KB
/
many.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class MySimplest( HTMLElement ): #EXAMPLE: minimal one (the className + template)
"""
<style>
div {background:yellow}
</style>
<div>Hello World</div>
"""
class MyTest( HTMLElement ): #EXAMPLE: re-render on attribte change
"""
<style>
* {background:red}
</style>
<b>Kaputt</b>
"""
def init(self):
self.root = self.shadowRoot.querySelector("b")
@react("nb")
def render(self):
self.root.innerHTML = int(self.attributes["nb"].value) * "⭐";
class MyTest2( HTMLElement ): #EXAMPLE: reuse <my-test> ^^
"""
<style>
div {background:green}
</style>
<div>
<button id="sub">-</button>
<button id="add">+</button>
<my-test id="mt"></my-test>
</div>
"""
def init(self):
self.mt = self.shadowRoot.querySelector("#mt")
self.shadowRoot.querySelector("#add").addEventListener('click', lambda e: self.setAttribute("value",self.getValue()+1) )
self.shadowRoot.querySelector("#sub").addEventListener('click', lambda e: self.setAttribute("value",self.getValue()-1) )
def getValue(self):
return int(self.attributes["value"].value)
@react("value")
def render(self):
self.mt.setAttribute( "nb", self.getValue() )
class MyBack( HTMLElement ): #EXAMPLE: draw outside with an exposed method
"""
<style>
div {background:black;padding:4px;color:red;border:4px solid black}
div > span {display: none}
div.on {border:4px solid red}
div.on > span {display: block;position:absolute;top:0px;right:0px;background:#888}
</style>
<div>
CLICK
<span>XXX</span>
</div>
"""
def init(self):
self.root = self.shadowRoot.querySelector("div")
def connectedCallback(self): #needed to force the style !
self.setAttribute("style","cursor:pointer;")
def toggle(self):
self.root.classList.toggle("on")
class MyFold(HTMLElement): #EXAMPLE: use a slot !
"""
<style>
div#line {cursor:pointer}
div#line::before {content: "▶ " }
div#detail {display:none;margin-left:8px}
div.open > div#line::before {content: "▼ "}
div.open > div#detail {display:inherit}
</style>
<div>
<div id="line"></div>
<div id="detail"><slot/></div>
<div>
"""
def init(self):
self.root = self.shadowRoot.querySelector("div")
self.shadowRoot.querySelector("#line").addEventListener('click', lambda e: self.switch() )
def connectedCallback(self):
self.setTitle( self.getAttribute("title") or "" )
def setTitle(self,title):
self.shadowRoot.querySelector("#line").innerHTML = title
def setDetail(self,detail):
self.shadowRoot.querySelector("#detail").innerHTML = detail
def getDetail(self,detail):
return self.shadowRoot.querySelector("#detail").innerHTML
def getOpen(self):
return self.getAttribute("open") not in [None, 0, False, "0","false","no","null"]
def switch(self):
self.setAttribute("open",not self.getOpen())
@react("open")
def openclose(self, *a):
if self.getOpen():
self.root.classList.add("open")
else:
self.root.classList.remove("open")
self.dispatchEvent( window.Event('change') )