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