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