From: Paul Hänsch Date: Tue, 20 Jul 2021 19:03:14 +0000 (+0200) Subject: allow different select modes X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=af2bd954840f47026ffe47752f513cb71a40281b;p=webpoll allow different select modes --- diff --git a/widgets.css b/widgets.css index 6763d40..245597c 100644 --- a/widgets.css +++ b/widgets.css @@ -34,17 +34,19 @@ table.calendar tbody tr th.weekno { color: #888; } -table.calendar input[type=radio] { +table.calendar input[type=radio], +table.calendar input[type=checkbox] { display: none; } -table.calendar td input + label { +table.calendar td label { display: inline-block; width: 2em; margin: 0; padding: .25em; text-align: right; line-height: 1em; } -table.calendar td input:checked + label { +table.calendar td input:checked + label, +table.calendar td label[checked] { font-weight: bold; line-height: .75em; border: .125em solid; 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