File: ext.py

package info (click to toggle)
python-lark 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,788 kB
  • sloc: python: 13,305; javascript: 88; makefile: 34; sh: 8
file content (475 lines) | stat: -rw-r--r-- 12,170 bytes parent folder | download | duplicates (3)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# -*- coding: utf-8 -*-
from . import core as html5
from . import utils

class Button(html5.Button):

	def __init__(self, txt=None, callback=None, className=None, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self["class"] = "btn"

		if className:
			self.addClass(className)

		self["type"] = "button"

		if txt is not None:
			self.setText(txt)

		self.callback = callback
		self.sinkEvent("onClick")

	def setText(self, txt):
		if txt is not None:
			self.element.innerHTML = txt
			self["title"] = txt
		else:
			self.element.innerHTML = ""
			self["title"] = ""

	def onClick(self, event):
		event.stopPropagation()
		event.preventDefault()
		if self.callback is not None:
			self.callback(self)


class Input(html5.Input):
	def __init__(self, type="text", placeholder=None, callback=None, id=None, focusCallback=None, *args, **kwargs):
		"""

		:param type: Input type. Default: "text
		:param placeholder: Placeholder text. Default: None
		:param callback: Function to be called onChanged: callback(id, value)
		:param id: Optional id of the input element. Will be passed to callback
		:return:
		"""
		super().__init__(*args, **kwargs)
		self["class"] = "input"
		self["type"] = type
		if placeholder is not None:
			self["placeholder"] = placeholder

		self.callback = callback
		if id is not None:
			self["id"] = id
		self.sinkEvent("onChange")

		self.focusCallback = focusCallback
		if focusCallback:
			self.sinkEvent("onFocus")

	def onChange(self, event):
		event.stopPropagation()
		event.preventDefault()
		if self.callback is not None:
			self.callback(self, self["id"], self["value"])

	def onFocus(self, event):
		event.stopPropagation()
		event.preventDefault()
		if self.focusCallback is not None:
			self.focusCallback(self, self["id"], self["value"])

	def onDetach(self):
		super().onDetach()
		self.callback = None


class Popup(html5.Div):
	def __init__(self, title=None, id=None, className=None, icon=None, enableShortcuts=True, closeable=True, *args, **kwargs):
		super().__init__("""
			<div class="box" [name]="popupBox">
				<div class="box-head" [name]="popupHead">
					<div class="item" [name]="popupHeadItem">
						<div class="item-image">
							<i class="i i--small" [name]="popupIcon"></i>
						</div>
						<div class="item-content">
							<div class="item-headline" [name]="popupHeadline"></div>
						</div>
					</div>
				</div>
				<div class="box-body box--content" [name]="popupBody"></div>
				<div class="box-foot box--content bar" [name]="popupFoot"></div>
			</div>
		""")

		self.appendChild = self.popupBody.appendChild
		self.fromHTML = lambda *args, **kwargs: self.popupBody.fromHTML(*args, **kwargs) if kwargs.get("bindTo") else self.popupBody.fromHTML(bindTo=self, *args, **kwargs)

		self["class"] = "popup popup--center is-active"
		if className:
			self.addClass(className)

		if closeable:
			closeBtn = Button("&times;", self.close, className="item-action")
			closeBtn.removeClass("btn")
			self.popupHeadItem.appendChild(closeBtn)

		if title:
			self.popupHeadline.appendChild(title)

		if icon:
			self.popupIcon.appendChild(icon[0])
		elif title:
			self.popupIcon.appendChild(title[0])
		else:
			self.popupIcon.appendChild("Vi") #fixme!!! this _LIBRARY_ is not only used in the Vi...

		# id can be used to pass information to callbacks
		self.id = id

		#FIXME: Implement a global overlay! One popupOverlay next to a list of popups.
		self.popupOverlay = html5.Div()
		self.popupOverlay["class"] = "popup-overlay is-active"

		self.enableShortcuts = enableShortcuts
		self.onDocumentKeyDownMethod = None

		self.popupOverlay.appendChild(self)
		html5.Body().appendChild(self.popupOverlay)

		#FIXME: Close/Cancel every popup with click on popupCloseBtn without removing the global overlay.

	def onAttach(self):
		super(Popup, self).onAttach()

		if self.enableShortcuts:
			self.onDocumentKeyDownMethod = self.onDocumentKeyDown  # safe reference to method
			html5.document.addEventListener("keydown", self.onDocumentKeyDownMethod)

	def onDetach(self):
		super(Popup, self).onDetach()

		if self.enableShortcuts:
			html5.document.removeEventListener("keydown", self.onDocumentKeyDownMethod)

	def onDocumentKeyDown(self, event):
		if html5.isEscape(event):
			self.close()

	def close(self, *args, **kwargs):
		html5.Body().removeChild(self.popupOverlay)
		self.popupOverlay = None



class InputDialog(Popup):
	def __init__(self, text, value="", successHandler=None, abortHandler=None,
				 	successLbl="OK", abortLbl="Cancel", placeholder="", *args, **kwargs):

		super().__init__(*args, **kwargs)
		self.addClass("popup--inputdialog")

		self.sinkEvent("onKeyDown", "onKeyUp")

		self.successHandler = successHandler
		self.abortHandler = abortHandler

		self.fromHTML(
			"""
			<div class="input-group">
				<label class="label">
					{{text}}
				</label>
				<input class="input" [name]="inputElem" value="{{value}}" placeholder="{{placeholder}}" />
			</div>
			""",
			vars={
				"text": text,
				"value": value,
				"placeholder": placeholder
			}
		)

		# Cancel
		self.popupFoot.appendChild(Button(abortLbl, self.onCancel, className="btn--cancel btn--danger"))

		# Okay
		self.okayBtn = Button(successLbl, self.onOkay, className="btn--okay btn--primary")
		if not value:
			self.okayBtn.disable()

		self.popupFoot.appendChild(self.okayBtn)

		self.inputElem.focus()

	def onKeyDown(self, event):
		if html5.isReturn(event) and self.inputElem["value"]:
			event.stopPropagation()
			event.preventDefault()
			self.onOkay()

	def onKeyUp(self, event):
		if self.inputElem["value"]:
			self.okayBtn.enable()
		else:
			self.okayBtn.disable()

	def onDocumentKeyDown(self, event):
		if html5.isEscape(event):
			event.stopPropagation()
			event.preventDefault()
			self.onCancel()

	def onOkay(self, *args, **kwargs):
		if self.successHandler:
			self.successHandler(self, self.inputElem["value"])
		self.close()

	def onCancel(self, *args, **kwargs):
		if self.abortHandler:
			self.abortHandler(self, self.inputElem["value"])
		self.close()


class Alert(Popup):
	"""
	Just displaying an alerting message box with OK-button.
	"""

	def __init__(self, msg, title=None, className=None, okCallback=None, okLabel="OK", icon="!", closeable=True, *args, **kwargs):
		super().__init__(title, className=None, icon=icon, closeable=closeable, *args, **kwargs)
		self.addClass("popup--alert")

		if className:
			self.addClass(className)

		self.okCallback = okCallback

		message = html5.Span()
		message.addClass("alert-msg")
		self.popupBody.appendChild(message)

		if isinstance(msg, str):
			msg = msg.replace("\n", "<br>")

		message.appendChild(msg, bindTo=False)

		self.sinkEvent("onKeyDown")

		if closeable:
			okBtn = Button(okLabel, callback=self.onOkBtnClick)
			okBtn.addClass("btn--okay btn--primary")
			self.popupFoot.appendChild(okBtn)

			okBtn.focus()

	def drop(self):
		self.okCallback = None
		self.close()

	def onOkBtnClick(self, sender=None):
		if self.okCallback:
			self.okCallback(self)

		self.drop()

	def onKeyDown(self, event):
		if html5.isReturn(event):
			event.stopPropagation()
			event.preventDefault()
			self.onOkBtnClick()


class YesNoDialog(Popup):
	def __init__(self, question, title=None, yesCallback=None, noCallback=None,
	                yesLabel="Yes", noLabel="No", icon="?",
	                    closeable=False, *args, **kwargs):
		super().__init__(title, closeable=closeable, icon=icon, *args, **kwargs)
		self.addClass("popup--yesnodialog")

		self.yesCallback = yesCallback
		self.noCallback = noCallback

		lbl = html5.Span()
		lbl["class"].append("question")
		self.popupBody.appendChild(lbl)

		if isinstance(question, html5.Widget):
			lbl.appendChild(question)
		else:
			utils.textToHtml(lbl, question)

		if len(noLabel):
			btnNo = Button(noLabel, className="btn--no", callback=self.onNoClicked)
			#btnNo["class"].append("btn--no")
			self.popupFoot.appendChild(btnNo)

		btnYes = Button(yesLabel, callback=self.onYesClicked)
		btnYes["class"].append("btn--yes")
		self.popupFoot.appendChild(btnYes)

		self.sinkEvent("onKeyDown")
		btnYes.focus()

	def onKeyDown(self, event):
		if html5.isReturn(event):
			event.stopPropagation()
			event.preventDefault()
			self.onYesClicked()

	def onDocumentKeyDown(self, event):
		if html5.isEscape(event):
			event.stopPropagation()
			event.preventDefault()
			self.onNoClicked()

	def drop(self):
		self.yesCallback = None
		self.noCallback = None
		self.close()

	def onYesClicked(self, *args, **kwargs):
		if self.yesCallback:
			self.yesCallback(self)

		self.drop()

	def onNoClicked(self, *args, **kwargs):
		if self.noCallback:
			self.noCallback(self)

		self.drop()


class SelectDialog(Popup):

	def __init__(self, prompt, items=None, title=None, okBtn="OK", cancelBtn="Cancel", forceSelect=False,
	             callback=None, *args, **kwargs):
		super().__init__(title, *args, **kwargs)
		self["class"].append("popup--selectdialog")

		self.callback = callback
		self.items = items
		assert isinstance(self.items, list)

		# Prompt
		if prompt:
			lbl = html5.Span()
			lbl["class"].append("prompt")

			if isinstance(prompt, html5.Widget):
				lbl.appendChild(prompt)
			else:
				utils.textToHtml(lbl, prompt)

			self.popupBody.appendChild(lbl)

		# Items
		if not forceSelect and len(items) <= 3:
			for idx, item in enumerate(items):
				if isinstance(item, dict):
					title = item.get("title")
					cssc = item.get("class")
				elif isinstance(item, tuple):
					title = item[1]
					cssc = None
				else:
					title = item

				btn = Button(title, callback=self.onAnyBtnClick)
				btn.idx = idx

				if cssc:
					btn.addClass(cssc)

				self.popupBody.appendChild(btn)
		else:
			self.select = html5.Select()
			self.popupBody.appendChild(self.select)

			for idx, item in enumerate(items):
				if isinstance(item, dict):
					title = item.get("title")
				elif isinstance(item, tuple):
					title = item[1]
				else:
					title = item

				opt = html5.Option(title)
				opt["value"] = str(idx)

				self.select.appendChild(opt)

			if okBtn:
				self.popupFoot.appendChild(Button(okBtn, callback=self.onOkClick))

			if cancelBtn:
				self.popupFoot.appendChild(Button(cancelBtn, callback=self.onCancelClick))

	def onAnyBtnClick(self, sender):
		item = self.items[sender.idx]

		if isinstance(item, dict) and item.get("callback") and callable(item["callback"]):
			item["callback"](item)

		if self.callback:
			self.callback(item)

		self.items = None
		self.close()

	def onCancelClick(self, sender=None):
		self.close()

	def onOkClick(self, sender=None):
		assert self.select["selectedIndex"] >= 0
		item = self.items[int(self.select.children(self.select["selectedIndex"])["value"])]

		if isinstance(item, dict) and item.get("callback") and callable(item["callback"]):
			item["callback"](item)

		if self.callback:
			self.callback(item)

		self.items = None
		self.select = None
		self.close()


class TextareaDialog(Popup):
	def __init__(self, text, value="", successHandler=None, abortHandler=None, successLbl="OK", abortLbl="Cancel",
	             *args, **kwargs):
		super().__init__(*args, **kwargs)
		self["class"].append("popup--textareadialog")

		self.successHandler = successHandler
		self.abortHandler = abortHandler

		span = html5.Span()
		span.element.innerHTML = text
		self.popupBody.appendChild(span)

		self.inputElem = html5.Textarea()
		self.inputElem["value"] = value
		self.popupBody.appendChild(self.inputElem)

		okayBtn = Button(successLbl, self.onOkay)
		okayBtn["class"].append("btn--okay")
		self.popupFoot.appendChild(okayBtn)

		cancelBtn = Button(abortLbl, self.onCancel)
		cancelBtn["class"].append("btn--cancel")
		self.popupFoot.appendChild(cancelBtn)

		self.sinkEvent("onKeyDown")

		self.inputElem.focus()

	def onDocumentKeyDown(self, event):
		if html5.isEscape(event):
			event.stopPropagation()
			event.preventDefault()
			self.onCancel()

	def onOkay(self, *args, **kwargs):
		if self.successHandler:
			self.successHandler(self, self.inputElem["value"])
		self.close()

	def onCancel(self, *args, **kwargs):
		if self.abortHandler:
			self.abortHandler(self, self.inputElem["value"])
		self.close()