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