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