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