Skip to content

Commit 771f656

Browse files
committed
add keyable js method
1 parent 1cad00c commit 771f656

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

htagui/all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .containers import VScroll,VScrollPager, View
2020
from .sortables import Sortable
2121
from .swipers import Swiper
22+
from .javascripts import JSKEYABLE
2223
class App(Tag.body):
2324
_ui=None
2425

@@ -33,5 +34,5 @@ def ui(self):
3334

3435
# Swiper not inluded by default !!!!!
3536

36-
ALL=[App,Form,Tabs,Dialog,HSplit,VSplit,IText,ITextarea,IRange,IBool,ISelect,IRadios,FileUpload,Sortable,VScroll,VScrollPager,View]
37+
ALL=[JSKEYABLE, App,Form,Tabs,Dialog,HSplit,VSplit,IText,ITextarea,IRange,IBool,ISelect,IRadios,FileUpload,Sortable,VScroll,VScrollPager,View]
3738
FULL=ALL+[Swiper] # Swiper not inluded by default !!!!! (coz +170ko)

htagui/ifields.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
# #############################################################################
3+
# Copyright (C) 2024 manatlan manatlan[at]gmail(dot)com
4+
#
5+
# MIT licence
6+
#
7+
# https://github.com/manatlan/htag
8+
# #############################################################################
9+
110
import time
211
from htag import Tag,expose
312

htagui/javascripts.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
# #############################################################################
3+
# Copyright (C) 2024 manatlan manatlan[at]gmail(dot)com
4+
#
5+
# MIT licence
6+
#
7+
# https://github.com/manatlan/htag
8+
# #############################################################################
9+
from htag import Tag
10+
11+
""" will contains some htag placeholder objects, to just add some JS at import """
12+
13+
14+
class JSKEYABLE(Tag):
15+
statics = [Tag.script("""function keyable(o) {
16+
o.onkeydown = function(e) {
17+
const ll = [...o.querySelectorAll(".keyable")];
18+
19+
function calcOffset() {
20+
let hh={};
21+
for(let i of ll) {
22+
const h=i.getBoundingClientRect().top;
23+
hh[h]=(!hh[h]) ? 1 : hh[h]+1;
24+
}
25+
return Math.max( ...Object.values(hh) );
26+
}
27+
28+
const current=document.activeElement;
29+
if(ll.indexOf(current)>=0) {
30+
let idx=ll.indexOf(current);
31+
if(e['key']=="ArrowLeft") {
32+
idx = (idx - 1 >= 0) ? idx-1 : 0;
33+
ll[idx].focus();
34+
e.preventDefault();
35+
}
36+
else if(e['key']=="ArrowRight") {
37+
idx = (idx + 1 < ll.length) ? idx+1 : ll.length-1;
38+
ll[idx].focus();
39+
e.preventDefault();
40+
}
41+
else if(e['key']=="ArrowUp") {
42+
const offset = calcOffset();
43+
idx = (idx - offset >= 0) ? idx-offset : 0;
44+
ll[idx].focus();
45+
e.preventDefault();
46+
}
47+
else if(e['key']=="ArrowDown") {
48+
const offset = calcOffset();
49+
idx = (idx + offset < ll.length) ? idx+offset : ll.length-1;
50+
ll[idx].focus();
51+
e.preventDefault();
52+
}
53+
else if(e['key']=="Enter" ) {
54+
ll[idx].click();
55+
e.preventDefault();
56+
}
57+
}
58+
}
59+
60+
const ll = [...o.querySelectorAll(".keyable")];
61+
if(ll) {
62+
for(const i of ll)
63+
i.setAttribute("tabindex",1)
64+
ll[0].focus();
65+
}
66+
else
67+
console.warn( "keyable(element):", "No child elements with 'keyable' class in", o )
68+
}
69+
""")]

manual_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def init(self,x):
263263
self <= Tag.div(_style="height:200px; border:1px solid red;" ) <= ui.VScrollPager([lambda i=i: O(i) for i in range(1,200)])
264264

265265

266+
#=====================================================================
267+
266268
class TestSwiper(Tag.div):
267269
def init(self,root):
268270
self.output=root.output

0 commit comments

Comments
 (0)