X-Git-Url: https://git.plutz.net/?a=blobdiff_plain;f=widgets.sh;h=8410a0cb92eaa61ab3a469fe6fd3859adc8f1d6f;hb=HEAD;hp=02f7cb2a9fe86f21f05540a49dfee59150158b89;hpb=856d7b3c7823e768e4ad468ff2203fde3838e861;p=webpoll diff --git a/widgets.sh b/widgets.sh index 02f7cb2..8410a0c 100755 --- a/widgets.sh +++ b/widgets.sh @@ -1,22 +1,55 @@ #!/bin/sh +[ -n "$include_widgets" ] && return 0 +include_widgets="$0" + +export MD_HTML="false" +if [ "$(which awk)" ]; then + markdown() { awk -f "$_EXEC/cgilite/markdown.awk"; } +else + markdown() { busybox awk -f "$_EXEC/cgilite/markdown.awk"; } +fi + +dec(){ + local n + for n in "$@"; do + while [ "${n}" != "${n#0}" ]; do n="${n#0}"; done + printf %i\\n "$n" + done +} + 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() { - local month="$1" selected="$2" + # Arguments: + # 1. (optional) select, multiple, submit, 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 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 @@ -28,11 +61,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"; @@ -47,14 +80,49 @@ 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 - printf '[td [label [input type=radio name=date value="%04i-%02i-%02i" %s] %i]]' $Y $m $dom "$(checked $dom $selected)" $dom - [ $dow = 0 ] && printf ']\n' - [ $dow = 0 ] && V=$(( V % 53 + 1)) + date="$(printf "%04i-%02i-%02i" $Y $m $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" + ;; + submit) + [ "$(checked $dom $selected)" ] \ + && printf '[td [submit "%s_remove" "%s" . %i][hidden "%s" "%s"]]' "$input" "$date" "$dom" "$input" "$date" \ + || printf '[td [submit "%s_add" "%s" . %i]]' "$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 -gt 0 ]; then + while [ $dow -le 6 ]; do printf '[td ]'; dow=$((dow + 1)) ; done + printf ']\n' + fi printf ']]' } + +dlist_timeofday() { + local step="${1:-15}" id="${2:-dlist_timeofday}" + printf '[datalist id="%s"\n' $id + for h in $(seq 0 23); do for m in $(seq 0 "$step" 59); do + printf '[option value="%i:%02i"]\n' $h $m + done; done + printf ']\n' +}