]> git.plutz.net Git - serve0/blob - widgets.sh
Bugfix: keep 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 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   printf '[form #bookmarks action=?a=bookmark method=POST
70             [a href="#" x]
71             [hidden "ref" "%s"]
72             [hidden "search" "%s"][hidden "filter" "%s"]
73             [label Name for current page:]
74             [input name="name" value="%s" placeholder="Name" ]
75             [button type="submit" %s]' \
76             "$w_refuri" \
77             "$(HTML "$SEARCH")" "$(HTML "$FILTER")" \
78             "$(w_bmname)" "${name}"
79   [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
80
81   sort "$bm" |while read -r name search filter; do
82     search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
83     [ "$search" = "${w_str_s}" -a "$filter" = "${w_str_f}" ] && continue
84
85     name="$(UNSTRING "$name")";
86     search="$(UNSTRING "${search}" |URL)";
87     filter="$(UNSTRING "${filter}" |URL)";
88     printf '[label .link %s]
89             [a .link target=blank href="/?o=Name&s=%s&f=%s" by Name]
90             [a .link target=blank href="/?o=Date&s=%s&f=%s" by Date]
91             [a .link target=blank href="/?o=Length&s=%s&f=%s" by Length]
92             [a .link target=blank href="/?o=Group&s=%s&f=%s" by Group]
93             ' \
94             "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
95             "$search" "$filter" \
96             "$search" "$filter" \
97             "$search" "$filter" \
98             "$search" "$filter"
99   done
100   printf ']'
101 }
102
103 w_search(){
104   printf '
105   [form #search method=GET action=./?
106     [select name=o size=1 
107       [option disabled=disabled Order By]
108       [option value=Name %s Name]
109       [option value=Date %s Date]
110       [option value=Length %s Length]
111       [option value=Group %s Group]
112     ]
113     [input name=s placeholder=Search value="%s"]
114     [a #t_avsearch href="#advsearch" Advanced]
115   ]
116   ' \
117   "$w_coname" "$w_codate" "$w_colength" "$w_cogroup" \
118   "$(HTML "$SEARCH")"
119 }
120
121 w_prefs(){
122   local tm tf td
123
124   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
125   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
126   td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
127
128   printf '
129   [form #prefs method="POST" action="?a=setprefs"
130     [a href="#" x]
131     [hidden "ref" "%s"]
132     [label for=prefs_ps Pagesize]
133     [input #prefs_ps type=number name=pagesize value="%s"][br]
134     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
135     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
136     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
137     [checkbox "downscale" "yes" %s #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
138     [submit "index" "update" Force Index Update][br]
139     [submit "store" "store" Set Cookie]
140   ]
141   ' \
142   "$w_refuri" "$LISTSIZE" \
143   "${tm:-checked}" "${tm:+checked}" "$tf" "$td"
144 }
145
146 w_index(){
147   printf '
148   [form #index method="POST" action="?a=spawnindex"
149     [hidden "ref" "%s"]
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   ' "$w_refuri"
155   return 0
156 }
157
158 w_advsearch(){
159   local n lbid tag category filter f t d
160   filter="$(HTML "${FILTER}^")"
161
162   printf '[form #advsearch action=./?a=advsearch method=POST
163             [a href="#" X]
164             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.
165             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
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     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
174             ][fieldset .select
175             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
176             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
177             [label .head Category:]' \
178             $n "${f:+checked}" $n \
179             $n "${t:-checked}" $n $n \
180             $n "${t:+checked}" $n $n
181
182     f="|${f#\~}|"
183     printf '*\n%s\n$\n' "$w_tagcategories" \
184     | while read -r category; do
185       lbid="cat_${n}_${category}"
186
187       t=''
188       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
189       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
190       [ "$category" != '*' -a ! "${f%%|-${category}:*}" ] && t=checked
191
192       # printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
193       #         [select name=tag_%s size=10 multiple' \
194       #         $n "$category" "$t" "$lbid" "$lbid" "$category" $n
195       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
196               [div .catselect\n' \
197               $n "$category" "$t" "$lbid" "$lbid" "$category"
198
199       printf '%s\n' "$w_tags" \
200       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
201       | { for n in 1 2 3 4 5 6 7 8 9 0; do
202           read -r line && printf '%s\n' "$line" || break;
203           done;  # pass 10 lines through without modification
204           sort;  # and sort remaining lines
205       } | while read -r tag; do
206         [ "$tag" ] || continue
207         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
208         d="${tag#-}"; d="${d#*:}"
209         # printf '[option %s value="%s"\n%s]' "$t" "$tag" "$d"
210         printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tag" "$t" "$d"
211       done
212       d="${f##*\$:}" d="${d%%\|*}"
213       [ "$category" = \$ ] && printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$d")"
214       printf '\n]'
215     done
216     printf ']'
217   done
218
219   printf '[fieldset .submit [select name=order
220             [option disabled=disabled Order By]
221             [option value=Name %s Name]
222             [option value=Date %s Date]
223             [option value=Length %s Length]
224             [option value=Group %s Group]
225           ][button type=submit Apply Filter]]
226           ]' \
227           "$w_coname" "$w_codate" \
228           "$w_colength" "$w_cogroup"
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 tag category d
242   printf '[a href="#multitag" Add Tags / Remove Tags]
243           [div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
244   printf '[a href="#" X]'
245
246   printf 'Tags\n%s\n' "$w_tagcategories" \
247   | while read -r category; do
248     [ "$category" ] || continue
249     # printf '[fieldset [legend %s:][select name=tag size=4 multiple\n' "$category"
250     printf '[fieldset [legend %s:][div .tagselect\n' "$category"
251     printf %s "$w_tags" \
252     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
253     | { for n in 1 2 3 4 5 6 7 8 9 0; do
254         read -r line && printf '%s\n' "$line" || break
255         done;
256         sort;
257     } | while read -r tag; do
258       [ "$tag" ] || continue
259       d="${tag#-}"; d="${d#*:}"
260       # printf '[option value="%s"\n%s]' "$tag" "$d"
261       printf '[label [checkbox "tag" "%s"] %s]\n' "$tag" "$d"
262     done
263     printf ']]'
264   done
265
266   printf '[fieldset [legend New:][textarea name=newtag\n]
267           [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
268           ]]'
269 }