]> git.plutz.net Git - webpoll/blob - widgets.sh
fix android: display arrow buttons in top row of calendar
[webpoll] / widgets.sh
1 #!/bin/sh
2
3 checked(){
4   local check="$1"; shift 1;
5   for comp in "$@"; do
6     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
7       printf 'checked="checked"'
8       break;
9     fi 2>/dev/null
10   done
11 }
12 selected(){
13   local check="$1"; shift 1;
14   for comp in "$@"; do
15     if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then
16       printf 'selected="selected"'
17       break;
18     fi 2>/dev/null
19   done
20 }
21
22 w_month() {
23   # Arguments:
24   # 1. (optional) select, multiple, submit, none - default: select
25   # 2. (optional) Name of form field - default: "date"
26   # 3. (optional) Month to display in format: YYYY-MM - default: current month
27   # 4. (optional, multiple) Days to preselect in format: DD - default: none
28
29   local type="${1:-select}" input="${2:-date}" month="$3"
30   shift 3; local selected="$*"
31   local dow dom days n=1 Y m d V w B
32   if [ $month ]; then
33     read Y m d V w B<<-EOF
34         $(date -d "${month}-01" +"%_Y %_m %_d %_V %w %B")
35         EOF
36   else
37     read Y m d V w <<-EOF
38         $(date +"%Y %m %d %V %w")
39         EOF
40     month="$Y-$m"
41     V="$((V - d / 7))"
42     [ $V -lt 1 ] && V=$((V + 53))
43   fi
44
45   case $m in
46     [13578]|10|12)
47       days="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";;
48     [469]|11)
49       days="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";;
50     2) if [ $(( Y / 400 )) = 0 ]; then
51         days="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";
52       elif [ $(( Y / 100 )) = 0 ]; then
53         days="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";
54       elif [ $(( Y /   4 )) = 0 ]; then
55         days="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";
56       else
57         days="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";
58       fi;;
59   esac
60
61   printf '[table .calendar month=%s [thead
62             [tr .monthname [th colspan=8 . %s]]
63             [tr   .weekday [th][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s]]
64           ][tbody
65          ' "$month" "$B $Y" Mo Tu We Th Fr Sa Su
66   for dom in $days; do
67     dow=$(( ( w - d + 35 + dom ) % 7))
68     [ $dow = 1 -o $dom = 1 ] && printf '[tr [th .weekno . %i]' $V
69     [ $dom = 1 ] && while [ $n -lt $(( ($dow + 6) % 7 + 1)) ]; do printf '[td ]'; n=$((n + 1)); done
70     date="$(printf "%04i-%02i-%02i" $Y $m $dom)"
71     case $type in
72       none)
73         printf '[td [label %s . %i]]' \
74                "$(checked $dom $selected)" "$dom"
75         ;;
76       multiple)
77         printf '[td [input type=checkbox id="%s_%s" name="%s" value="%s" %s][label for="%s_%s" . %i]]' \
78                "$input" "$date" "$input" "$date" "$(checked $dom $selected)" "$input" "$date" "$dom"
79         ;;
80       submit)
81         [ "$(checked $dom $selected)" ] \
82         && printf '[td [submit "%s_remove" "%s" . %i][hidden "%s" "%s"]]' "$input" "$date" "$dom" "$input" "$date" \
83         || printf '[td [submit "%s_add"    "%s" . %i]]' "$input" "$date" "$dom"
84         ;;
85       select|*)
86         printf '[td [input type=radio id="%s_%s" name="%s" value="%s" %s][label for="%s_%s" . %i]]' \
87                "$input" "$date" "$input" "$date" "$(checked $dom $selected)" "$input" "$date" "$dom"
88         ;;
89     esac
90     if [ $dow = 0 ]; then
91       printf ']\n'
92       V=$((V + 1))
93       [ $m = 1 -a $V -ge 53 ] && V=1
94     fi
95   done
96   if [ $dow -gt 0 ]; then
97     while [ $dow -le 6 ]; do printf '[td ]'; dow=$((dow + 1)) ; done
98     printf ']\n'
99   fi
100   printf ']]'
101 }
102
103 dlist_timeofday() {
104   local step="${1:-15}" id="${2:-dlist_timeofday}"
105   printf '[datalist id="%s"\n' $id
106     for h in $(seq 0 23); do for m in $(seq 0 "$step" 59); do
107       printf '[option value="%i:%02i"]\n' $h $m
108     done; done
109   printf ']\n'
110 }