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