]> git.plutz.net Git - serve0/blob - widgets.sh
allow manual grouping
[serve0] / widgets.sh
1 #!/bin/sh
2
3 [ -n "$include_widgets" ] && return 0
4 include_widgets="$0"
5
6 . "$_EXEC/cgilite/storage.sh"
7 . "$_EXEC/db_meta.sh"
8
9 w_refuri="$(URL "$PATH_INFO")?$(HTML "$QUERY_STRING")"
10
11 w_str_s="$(STRING "$SEARCH")"
12 w_str_f="$(STRING "$FILTER")"
13
14 c_tags="$_DATA/.index/tags.cache"; c_tagcategories="$_DATA/.index/tagcategories.cache"
15 if [ ! -s "$c_tags" -o ! -s "$c_tagcategories" ] \
16    || [ "$(find "$_DATA/" -path '*/.index/meta' -newer "$c_tags")" ]
17   then
18   w_tags="$(
19     list_meta |sed -E '
20       s;^.*\ttags=([^\t]*)\t.*$;\1;;
21       s;,;\n;g;
22     ' \
23     | sort |uniq -c |sort -rn |sed -E 's;^ *[0-9]+ ;;;' \
24     | UNSTRING | HTML \
25     | sed -E 's;&#x0A\;;\n;g; s;\n+;\n;g;'
26   )"
27   w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
28   printf %s "$w_tags" >"$c_tags"
29   printf %s "$w_tagcategories" >"$c_tagcategories"
30 else
31   w_tags="$(cat "$c_tags")"
32   w_tagcategories="$(cat "$c_tagcategories")"
33 fi
34
35
36 [ "$ORDER" = Name   ] && w_coname=checked
37 [ "$ORDER" = Date   ] && w_codate=checked
38 [ "$ORDER" = Length ] && w_colength=checked
39 [ "$ORDER" = Group  ] && w_cogroup=checked
40
41 w_bmname=
42 w_bmname(){
43   [ "$w_bmname" ] || w_bmname="$(
44     bm="$_DATA/.index/bookmarks"
45     name="$(grep -m1 -aF "      search=${w_str_s}       filter=${w_str_f}${CR}" "$bm" 2>&-)"
46
47     if [ "$name" ]; then
48       printf '%s' "$name" |cut -f1 |UNSTRING |HTML
49     else
50       printf '%s\t%s' "$SEARCH" "$FILTER" \
51       | sed -r '/^\t$/{  s;\t;All;; q;}
52                 /.*\t$/{ s;\t$;;; q;}
53                 /^\t.*/{ s;^\t;;;
54                          :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
55                          s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}' \
56       | HTML
57     fi
58   )"
59   printf '%s' "$w_bmname"
60 }
61
62 w_bookmarks(){
63   local bm="$_DATA/.index/bookmarks" name='' search='' filter=''
64   [ ! -d "${bm%/*}" ] && return 0
65   [ ! -f "$bm" ] && touch "$bm"
66
67   grep -qaF "   search=$w_str_s filter=${w_str_f}${CR}" "$bm" && name=Update || name=Add
68
69   cat <<-EOF
70         [form #bookmarks action=?a=bookmark method=POST
71           [a href="#" x]
72           [hidden "ref" "${w_refuri}"]
73           [hidden "search" "$(HTML "$SEARCH")"][hidden "filter" "$(HTML "$FILTER")"]
74           [label Name for current page:]
75           [input name="name" value="$(w_bmname)" placeholder="Name" ]
76           [button type="submit" . ${name}]
77         EOF
78   [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
79
80   sort "$bm" |while read -r name search filter; do
81     search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
82     [ "$search" = "${w_str_s}" -a "$filter" = "${w_str_f}" ] && continue
83
84     name="$(UNSTRING "$name")";
85     search="$(UNSTRING "${search}" |URL)";
86     filter="$(UNSTRING "${filter}" |URL)";
87     cat <<-EOF
88         [label .link . $(HTML "$name" |sed 's;&#44\;;&[wbr];g;')]
89         [a .link target=blank href="/?o=Name&s=${search}&f=${filter}" by Name]
90         [a .link target=blank href="/?o=Date&s=${search}&f=${filter}" by Date]
91         [a .link target=blank href="/?o=Length&s=${search}&f=${filter}" by Length]
92         [a .link target=blank href="/?o=Group&s=${search}&f=${filter}" by Group]
93         EOF
94   done
95   printf ']'
96 }
97
98 w_search(){
99   cat <<-EOF
100         [form #search method=GET action=./?
101           [select name=o size=1
102             [option disabled=disabled Order By]
103             [option value=Name ${w_coname} Name]
104             [option value=Date ${w_codate} Date]
105             [option value=Length ${w_colength} Length]
106             [option value=Group ${w_cogroup} Group]
107           ]
108           [input name=s placeholder=Search value="$(HTML "$SEARCH")"]
109           [a #t_avsearch href="#advsearch" Advanced]
110         ]
111         EOF
112 }
113
114 w_prefs(){
115   local tm tf td
116
117   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
118   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
119   td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
120
121   cat <<-EOF
122         [form #prefs method="POST" action="?a=setprefs"
123           [a href="#" x]
124           [hidden "ref" "${w_refuri}"]
125           [label for=prefs_ps Pagesize]
126           [input #prefs_ps type=number name=pagesize value="${LISTSIZE}"][br]
127           [radio "mode" "browse" ${tm:-checked=checked} #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
128           [radio "mode" "index" ${tm:+checked=checked} #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
129           [checkbox "fakemp4" "yes" ${tf} #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
130           [checkbox "downscale" "yes" ${td} #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
131           [submit "index" "update" Force Index Update][br]
132           [submit "store" "store" Set Cookie]
133         ]
134         EOF
135 }
136
137 w_index(){
138   cat <<-EOF
139         [form #index method="POST" action="?a=spawnindex"
140           [hidden "ref" "${w_refuri}"]
141           [label Set up for Index view: ]
142           [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
143           [submit "spawn" "spawn" Set up]
144         ]
145         EOF
146   return 0
147 }
148
149 w_advsearch(){
150   local n lbid tag category filter f t d
151   filter="$(HTML "${FILTER}^")"
152
153   cat <<-EOF
154         [form #advsearch action=./?a=advsearch method=POST
155           [a href="#" X]
156           [p .help Refine the search further by setting additional search tags using the [strong "+and"] button.]
157         EOF
158
159   for n in 1 2 3 4 5 6 7 8 9 10; do
160     f="${filter%%^*}"; filter="${filter#*^}"
161
162     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
163
164     lbid="cat_${n}_(none)"
165     cat <<-EOF
166         [input .and type=checkbox name=and id="and_${n}" ${f:+checked=checked}][label for="and_${n}" +and
167         ][fieldset .select
168         [radio "pol_${n}" "pos" .pol ${t:-checked=checked} #pol_pos_${n}"][label for=pol_pos_${n} Any]
169         [radio "pol_${n}" "neg" .pol ${t:+checked=checked} #pol_neg_${n}"][label for=pol_neg_${n} None]
170         [label .head Category:]
171         EOF
172
173     f="|${f#\~}|"
174     printf '*\n%s\n$\n' "$w_tagcategories" \
175     | while read -r category; do
176       lbid="cat_${n}_${category}"
177
178       t=''
179       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
180       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
181       [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
182
183       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
184               [div .catselect\n' \
185               $n "$category" "$t" "$lbid" "$lbid" "$category"
186
187       printf '%s\n' "$w_tags" \
188       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
189       | { sed -u 10q; sort; } \
190       | while read -r tag; do
191         [ "$tag" ] || continue
192         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
193         d="${tag#-}"; d="${d#*:}"
194         printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tag" "$t" "$d"
195       done
196       d="${f##*\$:}" d="${d%%\|*}"
197       [ "$category" = \$ ] && printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$d")"
198       printf '\n]'
199     done
200     printf ']'
201   done
202
203   cat <<-EOF
204           [fieldset .submit [select name=order
205             [option disabled=disabled Order By]
206             [option value=Name ${w_coname} Name]
207             [option value=Date ${w_codate} Date]
208             [option value=Length ${w_colength} Length]
209             [option value=Group ${w_cogroup} Group]
210           ][button type=submit Apply Filter]]
211         ]
212         EOF
213 }
214
215 w_delete(){
216   printf '[a href="#multitag" Add Tags / Remove Tags]
217           [div #multitag [input type="hidden" name="ref" value="%s"]
218           [a href="#" X]
219           [fieldset [legend New:]
220           [submit "op" "filedelete" Delete Files]
221           ]]' "$w_refuri"
222 }
223
224 w_tagging(){
225   local tag category d
226
227   cat <<-EOF
228         [a href="#multitag" Add Tags / Remove Tags]
229         [div #multitag [input type="hidden" name="ref" value="${w_refuri}"]
230           [a href="#" X]
231         EOF
232
233   printf 'Tags\n%s\n' "$w_tagcategories" \
234   | while read -r category; do
235     [ "$category" ] || continue
236     printf '[fieldset [legend %s:][div .tagselect\n' "$category"
237     printf %s "$w_tags" \
238     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
239     | { sed -u 10q; sort; } \
240     | while read -r tag; do
241       [ "$tag" ] || continue
242       d="${tag#-}"; d="${d#*:}"
243       printf '[label [checkbox "tag" "%s"] %s]\n' "$tag" "$d"
244     done
245     printf ']]'
246   done
247
248   cat <<-EOF
249           [fieldset [legend New:][textarea name=newtag\n]
250           [label [checkbox "makegroup" "true"] Join selected into group]
251           [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
252         ]]
253         EOF
254 }