forked from viur-framework/viur-vi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpane.py
More file actions
337 lines (262 loc) · 8.89 KB
/
Copy pathpane.py
File metadata and controls
337 lines (262 loc) · 8.89 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# -*- coding: utf-8 -*-
from vi import html5
from .embedsvg import embedsvg
from .framework.components.icon import Icon
from .config import conf
from .priorityqueue import HandlerClassSelector
from .network import DeferredCall
from .framework.event import EventDispatcher
class Pane(html5.Div):
"""
Base class for Panes.
A pane represents a entry in the module list as well
as a list of widgets associated with this pane.
It is possible to stack panes on-top of each other.
If a pane is active, _all_ its child widgets are visible
(through they might overlap).
"""
def __init__(self, descr=None, iconURL=None, iconClasses=None,
closeable=False, collapseable=True, focusable=True,
path=None):
super(Pane, self).__init__()
self.addClass("vi-pane", "is-active")
self.parentPane = None
self.sinkEvent("onClick")
self.groupPrefix = None
self.item = html5.Div()
self.item.addClass("item has-hover")
self.appendChild(self.item)
self.descr = descr
self.iconURL = iconURL
self.iconClasses = iconClasses
self.collapseable = collapseable
self.focusable = focusable
self.path = path
self.childPanes = []
self.widgetsDomElm = html5.Div()
self.widgetsDomElm.addClass("vi-viewer-pane", "has-no-child")
self.childDomElem = None
self.img = None
self.label = html5.Div()
self.label.addClass("item-link")
self.item.appendChild(self.label)
self.defaultIconURL = self.iconURL
self.itemImage = None
self.setText(descr, iconURL)
self.closeBtn = html5.ext.Button(u"×", self.onBtnCloseReleased)
self.closeBtn["class"] = "item-action"
self.item.appendChild(self.closeBtn)
if not closeable:
self.closeBtn.hide()
self.closeable = closeable
self.isExpanded = False
DeferredCall(self.setText, _delay=250)
def __setattr__(self, key, value):
super(Pane, self).__setattr__(key, value)
if key == "closeable":
if value:
self.closeBtn.show()
else:
self.closeBtn.hide()
def setImage( self,loading=False ):
try:
self.label.removeChild(self.itemImage)
except:
pass
if loading:
self.itemImage = Icon(self.descr, "./public/images/is-loading32.gif")
else:
self.itemImage = Icon(self.descr, self.defaultIconURL)
self.label.appendChild(self.itemImage)
def lock(self):
self.disable()
self.setText(loading = True)
def unlock(self):
self.setText(loading = False)
self.enable()
def setText(self, descr = None, iconURL = None,loading=False):
try:
# It might already be gone...
self.label.removeAllChildren()
except:
return
if descr is None:
descr = self.descr
else:
self.descr = descr
self.setImage(loading=loading)
if self.iconClasses is not None:
for cls in self.iconClasses:
self.label.addClass(cls)
self.itemContent = html5.Div()
self.itemContent.addClass("item-content")
self.label.appendChild(self.itemContent)
if descr is not None:
h = html5.H3()
h.appendChild(descr)
h.addClass("item-headline")
self.itemContent.appendChild(h)
def onBtnCloseReleased(self, *args, **kwargs):
conf["mainWindow"].removePane(self)
def addChildPane(self, pane):
"""
Stack a pane under this one.
It gets displayed as a subpane.
:param pane: Another pane
:type pane: pane
"""
assert pane != self, "A pane cannot be a child of itself"
self.childPanes.append(pane)
pane.parentPane = self
if not self.childDomElem:
self.childDomElem = html5.Div()
self.childDomElem.addClass("list list--sub")
if self.collapseable and not pane.closeable:
self.childDomElem.removeClass("is-active")
else:
self.childDomElem.addClass("is-active")
self.appendChild(self.childDomElem)
if self.closeable:
self.closeBtn.hide()
if (pane.closeable and "is-active" not in self.childDomElem["class"]):
self.childDomElem.addClass("is-active")
self.childDomElem.appendChild(pane)
def removeChildPane(self, pane):
"""
Removes a subpane.
:param pane: The pane to remove. Must be a direct child of this pane
:type pane: Pane
"""
assert pane in self.childPanes, "Cannot remove unknown child-pane %s from %s" % (str(pane), str(self))
self.childPanes.remove(pane)
self.childDomElem.removeChild(pane)
pane.parentPane = None
# DOM.removeChild( self.childDomElem, pane.getElement() )
if len(self.childPanes) == 0: # No more children, remove the UL element
self.removeChild(self.childDomElem)
# DOM.removeChild( self.getElement(), self.childDomElem )
self.childDomElem = None
if self.closeable:
self.closeBtn.show()
def onDetach(self):
# assert len(self.childPanes)==0, "Attempt to detach a pane which still has subpanes!"
# Kill all remaining children
for widget in self.widgetsDomElm.children():
self.widgetsDomElm.removeChild(widget)
self.closeBtn = None
self.label = None
super(Pane, self).onDetach()
def addWidget(self, widget,disableOtherWidgets=True):
"""
Adds a widget to this pane.
Note: all widgets of a pane are visible at the same time!
:param widget: The widget to add
:type widget: Widget
"""
if disableOtherWidgets:
for w in self.widgetsDomElm.children():
w.disable()
widget["class"].append("vi-operator")
self.widgetsDomElm.appendChild(widget)
self.rebuildChildrenClassInfo()
def rebuildChildrenClassInfo(self):
if "has-no-child" in self.widgetsDomElm["class"]:
self.widgetsDomElm.removeClass("has-no-child")
if "has-single-child" in self.widgetsDomElm["class"]:
self.widgetsDomElm.removeClass("has-single-child")
if "has-multiple-children" in self.widgetsDomElm["class"]:
self.widgetsDomElm.removeClass("has-multiple-children")
if len(self.widgetsDomElm._children) == 0:
self.widgetsDomElm.addClass("has-no-child")
elif len(self.widgetsDomElm._children) == 1:
self.widgetsDomElm.addClass("has-single-child")
else:
self.widgetsDomElm.addClass("has-multiple-children")
def removeWidget(self, widget):
"""
Removes a widget.
:param widget: The widget to remove. Must be a direct child of this pane.
:type widget: Widget
"""
if widget in self.widgetsDomElm.children():
self.widgetsDomElm.removeChild(widget)
if self.closeable and len(self.widgetsDomElm._children) == 0:
conf["mainWindow"].removePane(self)
for w in self.widgetsDomElm._children[:]:
w["disabled"] = False
self.rebuildChildrenClassInfo()
return
raise ValueError("Cannot remove unknown widget %s" % str(widget))
def containsWidget(self, widget):
"""
Tests wherever widget is a direct child of this pane.
:returns: bool
"""
return widget in self.widgetsDomElm.children()
def onClick(self, event=None, *args, **kwargs):
self.focus()
if event:
event.stopPropagation()
def expand(self):
if self.childDomElem and self.collapseable and not self.isExpanded:
self.item.addClass("is-active")
self.childDomElem.show()
self.isExpanded = True
def collapse(self):
if self.childDomElem and self.collapseable and self.isExpanded:
self.item.removeClass("is-active")
self.childDomElem.hide()
self.isExpanded = False
def focus(self):
conf["mainWindow"].focusPane(self)
class GroupPane(Pane):
"""
This pane groups subpanes; it cannot have direct childrens
"""
def __init__(self, *args, **kwargs):
super(GroupPane, self).__init__(*args, **kwargs)
self.addClass("vi-pane-group")
self.childDomElem = html5.Ul()
self.childDomElem.addClass("list", "list--sub")
self.childDomElem.hide()
self.appendChild(self.childDomElem)
def loadChildren(self):
if self.groupPrefix in conf["vi.groupedModules"]:
childs = conf["vi.groupedModules"][self.groupPrefix]
childs.sort(key=lambda entry: "%d-%010d-%s" % (1 if entry[1].get("sortIndex") is None else 0, entry[1].get("sortIndex", 0), entry[1].get("name")))
for module, info in childs:
conf["modules"][module]["visibleName"] = conf["modules"][module]["name"].replace(self.groupPrefix, "")
handlerCls = HandlerClassSelector.select(module, info)
assert handlerCls is not None, "No handler available for module '%s'" % module
handler = handlerCls(module, info)
conf["mainWindow"].addPane(handler, self)
self.unlock()
def DeferredLoadChildren( self,delay=1000 ):
if not self.childPanes:
self.lock()
if not delay:
self.loadChildren()
else:
DeferredCall(self.loadChildren, _delay=delay)
def onClick(self, event = None, *args, **kwargs):
if not self.childDomElem:
self.childDomElem = html5.Ul()
self.childDomElem["style"]["display"] = "none"
self.appendChild(self.childDomElem)
self.DeferredLoadChildren()
if self.isExpanded:
self.collapse()
else:
self.expand()
event.stopPropagation()
def expand(self):
if self.childDomElem and self.collapseable and not self.isExpanded:
self.childDomElem.show()
self.isExpanded = True
def collapse(self):
if self.childDomElem and self.collapseable and self.isExpanded:
self.childDomElem.hide()
self.isExpanded = False
def onFocus(self, event):
if len(self.childPanes) > 0:
conf["mainWindow"].focusPane(self.childPanes[0])