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