X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=widgets.sh;h=5e7132e169945579f4a3a88154dda4ad6fe99bd7;hb=57b8376ada25742ac4d1078997bdeeeb20c56911;hp=248749535423255dba66c7144dffb1d6f9061f8b;hpb=2cfdff52842b5c1ac1a4c42289f5bc67328d01d1;p=webpoll diff --git a/widgets.sh b/widgets.sh index 2487495..5e7132e 100755 --- a/widgets.sh +++ b/widgets.sh @@ -1,27 +1,37 @@ #!/bin/sh checked(){ - if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then - printf 'checked="checked"' - fi 2>/dev/null + local check="$1"; shift 1; + for comp in "$@"; do + if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then + printf 'checked="checked"' + break; + fi 2>/dev/null + done } selected(){ - if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then - printf 'selected="selected"' - fi 2>/dev/null + local check="$1"; shift 1; + for comp in "$@"; do + if [ "$check" = "$comp" ] || [ "$check" -eq "$comp" ]; then + printf 'selected="selected"' + break; + fi 2>/dev/null + done } w_month() { # Arguments: - # 1. (optional) Name of select box, default "date" - # 2. (optional) Month to display in format: YYYY-MM, default current month - # 3. (optional) Day to preselect in format: DD, default none + # 1. (optional) select, multiple, none - default: select + # 2. (optional) Name of form field - default: "date" + # 3. (optional) Month to display in format: YYYY-MM - default: current month + # 4. (optional, multiple) Days to preselect in format: DD - default: none - local input="${1:-date}" month="$2" selected="$3" + local type="${1:-select}" input="${2:-date}" month="$3" + shift 3; local selected="$*" local dow dom days n=1 Y m d V w B if [ $month ]; then read Y m d V w B<<-EOF - $(date -d "${month}-01" +"%Y %m %d %V %w %B") + $(date -d "${month}-01" +"%_Y %_m %_d %_V %w %B") EOF else read Y m d V w <<-EOF @@ -33,11 +43,11 @@ w_month() { fi case $m in - 0[13578]|10|12) + [13578]|10|12) 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";; - 0[469]|11) + [469]|11) 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";; - 02) if [ $(( Y / 400 )) = 0 ]; then + 2) if [ $(( Y / 400 )) = 0 ]; then 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"; elif [ $(( Y / 100 )) = 0 ]; then 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"; @@ -52,18 +62,33 @@ w_month() { [tr .monthname [th colspan=8 . %s]] [tr .weekday [th][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s][th . %s]] ][tbody - ' "$month" "$B $Y" Mo Di Mi Do Fr Sa So + ' "$month" "$B $Y" Mo Tu We Th Fr Sa Su for dom in $days; do - dow=$(( (w - d + 35 + dom ) % 7)) + dow=$(( ( w - d + 35 + dom ) % 7)) [ $dow = 1 -o $dom = 1 ] && printf '[tr [th .weekno . %i]' $V [ $dom = 1 ] && while [ $n -lt $(( ($dow + 6) % 7 + 1)) ]; do printf '[td ]'; n=$((n + 1)); done date="$(printf "%04i-%02i-%02i" $Y $m $dom)" - printf '[td [input type=radio id="%s_%s" name="%s" value="%s" %s][label for="%s_%s" . %i]]' \ - "$input" "$date" "$input" "$date" "$(checked $dom $selected)" "$input" "$date" "$dom" - [ $dow = 0 ] && printf ']\n' - [ $dow = 0 ] && V=$(( V % 53 + 1)) + case $type in + none) + printf '[td [label %s . %i]]' \ + "$(checked $dom $selected)" "$dom" + ;; + multiple) + printf '[td [input type=checkbox id="%s_%s" name="%s" value="%s" %s][label for="%s_%s" . %i]]' \ + "$input" "$date" "$input" "$date" "$(checked $dom $selected)" "$input" "$date" "$dom" + ;; + select|*) + printf '[td [input type=radio id="%s_%s" name="%s" value="%s" %s][label for="%s_%s" . %i]]' \ + "$input" "$date" "$input" "$date" "$(checked $dom $selected)" "$input" "$date" "$dom" + ;; + esac + if [ $dow = 0 ]; then + printf ']\n' + V=$((V + 1)) + [ $m = 1 -a $V -ge 53 ] && V=1 + fi done - if [ $dow -le 6 ]; then + if [ $dow -gt 0 ]; then while [ $dow -le 6 ]; do printf '[td ]'; dow=$((dow + 1)) ; done printf ']\n' fi