X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=widgets.sh;h=472318d01a8f02d0af4d09ac94f2f8560db210d5;hb=af2bd954840f47026ffe47752f513cb71a40281b;hp=248749535423255dba66c7144dffb1d6f9061f8b;hpb=2cfdff52842b5c1ac1a4c42289f5bc67328d01d1;p=webpoll diff --git a/widgets.sh b/widgets.sh index 2487495..472318d 100755 --- a/widgets.sh +++ b/widgets.sh @@ -1,23 +1,33 @@ #!/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 @@ -52,14 +62,26 @@ 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" + 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 [ $dow = 0 ] && printf ']\n' [ $dow = 0 ] && V=$(( V % 53 + 1)) done