annotate libervia/web/pages/_browser/tools.py @ 1629:961468588131 default tip

doc (chat): Add documentation for message origin: fix 459
author Goffi <goffi@goffi.org>
date Sun, 08 Jun 2025 17:29:39 +0200
parents 698eaabfca0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1574
4ab4bc3ae462 browser: new `tools` module with a first method to detect touch devices
Goffi <goffi@goffi.org>
parents:
diff changeset
1 """Common useful tools"""
1625
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
2 from browser import window, html
1574
4ab4bc3ae462 browser: new `tools` module with a first method to detect touch devices
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4ab4bc3ae462 browser: new `tools` module with a first method to detect touch devices
Goffi <goffi@goffi.org>
parents:
diff changeset
4
4ab4bc3ae462 browser: new `tools` module with a first method to detect touch devices
Goffi <goffi@goffi.org>
parents:
diff changeset
5 def is_touch_device():
4ab4bc3ae462 browser: new `tools` module with a first method to detect touch devices
Goffi <goffi@goffi.org>
parents:
diff changeset
6 return hasattr(window, 'ontouchstart')
1620
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
7
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
8
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
9 def remove_ids(element):
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
10 """Recursively remove "id" attribute of element and all its descendants."""
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
11 element.removeAttribute('id')
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
12
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
13 for child in element.children:
3a60bf3762ef browser: threads and replies implementation:
Goffi <goffi@goffi.org>
parents: 1574
diff changeset
14 remove_ids(child)
1625
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
15
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
16
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
17 def make_placeholder(original_elt, placeholder_class="visual-placeholder"):
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
18 """Create a visual placeholder that matches an element's appearance.
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
19
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
20 The placeholder will be a clone of the original element but without any ID attributes
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
21 to prevent DOM conflicts.
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
22
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
23 @param original_elt: Element to create placeholder for.
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
24 @param placeholder_class: CSS class to add to placeholder element.
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
25 @return: Placeholder element.
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
26 """
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
27 placeholder = original_elt.cloneNode(True)
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
28 remove_ids(placeholder)
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
29 placeholder.classList.add(placeholder_class)
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
30 placeholder.attrs["aria-hidden"] = "true"
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
31 placeholder.attrs["data-placeholder-for"] = original_elt.id or ""
698eaabfca0e browser (chat): side/panel and keyword handling:
Goffi <goffi@goffi.org>
parents: 1620
diff changeset
32 return placeholder