]> git.plutz.net Git - serve0/blob - widgets.sh
Merge branch 'master' of git.plutz.net:serve0
[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="$( local cn=1
18     find "$_DATA/" -path '*/.index/meta' -print0 \
19     | xargs -r0 sed -r '
20       s;^.*\t.*\t.*\ttags=(.*)\tcomment=.*\t.*\r$;\1;;
21       s;,;\n;g;'"$UNSTRING" \
22     | { sort; printf '\n'; } \
23     | while read -r tag; do
24       [ "$tag" = "$otag" ] \
25       && cn=$((cn + 1)) \
26       || {
27         printf "%i      %s\n" "$cn" "$otag"
28         cn=1
29       }
30       otag="$tag"
31     done \
32     |sort -rn |cut -f2- |HTML \
33     |sed "s-
-\n-g; s;\n\n;\n;g;"
34   )"
35   w_tagcategories="$(printf %s "$w_tags" | sed -rn '/:/s;^-?([^:]+):.*$;\1;p' |sort -u )"
36   printf %s "$w_tags" >"$c_tags"
37   printf %s "$w_tagcategories" >"$c_tagcategories"
38 else
39   w_tags="$(cat "$c_tags")"
40   w_tagcategories="$(cat "$c_tagcategories")"
41 fi
42
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   printf '[form #bookmarks action=?a=bookmark method=POST
78             [a href="#" x]
79             [hidden "ref" "%s"]
80             [hidden "search" "%s"][hidden "filter" "%s"]
81             [label Name for current page:]
82             [input name="name" value="%s" placeholder="Name" ]
83             [button type="submit" %s]' \
84             "$w_refuri" \
85             "$(HTML "$SEARCH")" "$(HTML "$FILTER")" \
86             "$(w_bmname)" "${name}"
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     printf '[label .link %s]
97             [a .link target=blank href="/?o=Name&s=%s&f=%s" by Name]
98             [a .link target=blank href="/?o=Date&s=%s&f=%s" by Date]
99             [a .link target=blank href="/?o=Length&s=%s&f=%s" by Length]
100             [a .link target=blank href="/?o=Group&s=%s&f=%s" by Group]
101             ' \
102             "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
103             "$search" "$filter" \
104             "$search" "$filter" \
105             "$search" "$filter" \
106             "$search" "$filter"
107   done
108   printf ']'
109 }
110
111 w_search(){
112   printf '
113   [form #search method=GET action=./?
114     [select name=o size=1 
115       [option disabled=disabled Order By]
116       [option value=Name %s Name]
117       [option value=Date %s Date]
118       [option value=Length %s Length]
119       [option value=Group %s Group]
120     ]
121     [input name=s placeholder=Search value="%s"]
122     [a #t_avsearch href="#advsearch" Advanced]
123   ]
124   ' \
125   "$w_coname" "$w_codate" "$w_colength" "$w_cogroup" \
126   "$(HTML "$SEARCH")"
127 }
128
129 w_prefs(){
130   local tm tf td
131
132   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
133   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
134   td=''; [ "$(COOKIE downscale)" = yes ] && td=checked
135
136   printf '
137   [form #prefs method="POST" action="?a=setprefs"
138     [a href="#" x]
139     [hidden "ref" "%s"]
140     [label for=prefs_ps Pagesize]
141     [input #prefs_ps type=number name=pagesize value="%s"][br]
142     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
143     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
144     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file type][br]
145     [checkbox "downscale" "yes" %s #prefs_downscale] [label for=prefs_downscale Prefer downscale to 480p][br]
146     [submit "index" "update" Force Index Update][br]
147     [submit "store" "store" Set Cookie]
148   ]
149   ' \
150   "$w_refuri" "$LISTSIZE" \
151   "${tm:-checked}" "${tm:+checked}" "$tf" "$td"
152 }
153
154 w_index(){
155   printf '
156   [form #index method="POST" action="?a=spawnindex"
157     [hidden "ref" "%s"]
158     [label Set up for Index view: ]
159     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
160     [submit "spawn" "spawn" Set up]
161   ]
162   ' "$w_refuri"
163   return 0
164 }
165
166 w_advsearch(){
167   local n lbid tag category filter f t d
168   filter="$(HTML "${FILTER}^")"
169
170   printf '[form #advsearch action=./?a=advsearch method=POST
171             [a href="#" X]
172             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.
173             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
174
175   for n in 1 2 3 4 5 6 7 8 9 10; do
176     f="${filter%%^*}"; filter="${filter#*^}"
177
178     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
179
180     lbid="cat_${n}_(none)"
181     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
182             ][fieldset .select
183             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
184             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
185             [label .head Category:]' \
186             $n "${f:+checked}" $n \
187             $n "${t:-checked}" $n $n \
188             $n "${t:+checked}" $n $n
189
190     f="|${f#\~}|"
191     printf '*\n%s\n$\n' "$w_tagcategories" \
192     | while read -r category; do
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       #         [select name=tag_%s size=10 multiple' \
202       #         $n "$category" "$t" "$lbid" "$lbid" "$category" $n
203       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
204               [div .catselect\n' \
205               $n "$category" "$t" "$lbid" "$lbid" "$category"
206
207       printf '%s\n' "$w_tags" \
208       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
209       | { for n in 1 2 3 4 5 6 7 8 9 0; do
210           read -r line && printf '%s\n' "$line" || break;
211           done;  # pass 10 lines through without modification
212           sort;  # and sort remaining lines
213       } | while read -r tag; do
214         [ "$tag" ] || continue
215         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
216         d="${tag#-}"; d="${d#*:}"
217         # printf '[option %s value="%s"\n%s]' "$t" "$tag" "$d"
218         printf '[label [checkbox "tag_%s" "%s" %s] %s]' "$n" "$tag" "$t" "$d"
219       done
220       d="${f##*\$:}" d="${d%%\|*}"
221       [ "$category" = \$ ] && printf '[input name="tag_%i" value="%s"]' "$n" "$(HTML "$d")"
222       printf '\n]'
223     done
224     printf ']'
225   done
226
227   printf '[fieldset .submit [select name=order
228             [option disabled=disabled Order By]
229             [option value=Name %s Name]
230             [option value=Date %s Date]
231             [option value=Length %s Length]
232             [option value=Group %s Group]
233           ][button type=submit Apply Filter]]
234           ]' \
235           "$w_coname" "$w_codate" \
236           "$w_colength" "$w_cogroup"
237 }
238
239 w_delete(){
240   printf '[a href="#multitag" Add Tags / Remove Tags]
241           [div #multitag [input type="hidden" name="ref" value="%s"]
242           [a href="#" X]
243           [fieldset [legend New:]
244           [submit "op" "filedelete" Delete Files]
245           ]]' "$w_refuri"
246 }
247
248 w_tagging(){
249   local tag category d
250   printf '[a href="#multitag" Add Tags / Remove Tags]
251           [div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
252   printf '[a href="#" X]'
253
254   printf 'Tags\n%s\n' "$w_tagcategories" \
255   | while read -r category; do
256     [ "$category" ] || continue
257     # printf '[fieldset [legend %s:][select name=tag size=4 multiple\n' "$category"
258     printf '[fieldset [legend %s:][div .tagselect\n' "$category"
259     printf %s "$w_tags" \
260     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
261     | { for n in 1 2 3 4 5 6 7 8 9 0; do
262         read -r line && printf '%s\n' "$line" || break
263         done;
264         sort;
265     } | while read -r tag; do
266       [ "$tag" ] || continue
267       d="${tag#-}"; d="${d#*:}"
268       # printf '[option value="%s"\n%s]' "$tag" "$d"
269       printf '[label [checkbox "tag" "%s"] %s]\n' "$tag" "$d"
270     done
271     printf ']]'
272   done
273
274   printf '[fieldset [legend New:][textarea name=newtag\n]
275           [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
276           ]]'
277 }