]> git.plutz.net Git - serve0/blob - widgets.sh
ordering by grouplength
[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             [br]' \
104             "$(HTML "$name" |sed 's;&#44\;;&[wbr];g;')" \
105             "$search" "$filter" \
106             "$search" "$filter" \
107             "$search" "$filter"
108   done
109   printf ']'
110 }
111
112 w_search(){
113   printf '
114   [form #search method=GET action=?
115     [select name=o size=1 
116       [option disabled=disabled Order By]
117       [option value=Name %s Name]
118       [option value=Date %s Date]
119       [option value=Length %s Length]
120       [option value=Group %s Grouplength]
121     ]
122     [input name=s placeholder=Search value="%s"]
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}^" |sed "$w_ascii")"
169
170   printf '[form #advsearch action=?a=advsearch method=POST
171             [a href="#" Hide]
172             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
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' "$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
204       printf '%s\n' "$w_tags" \
205       | { [ "$category" = '*' ] && grep -avF ':' || grep -awF "${category}"; } \
206       | { for n in 1 2 3 4 5 6 7 8 9 0; do
207           read -r line && printf '%s\n' "$line" || break;
208           done;  # pass 10 lines through without modification
209           sort;  # and sort remaining lines
210       } | while read -r tag; do
211         [ "$tag" ] || continue
212         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
213         d="${tag#-}"; d="${d#*:}"
214         printf '[option %s value="%s"\n%s]' "$t" "$tag" "$d"
215       done
216       printf '\n]'
217     done
218     printf ']'
219   done
220
221   printf '[fieldset .submit [select name=order
222             [option disabled=disabled Order By]
223             [option value=Name %s Name]
224             [option value=Date %s Date]
225             [option value=Length %s Length]
226             [option value=Group %s Grouplength]
227           ][button type=submit Apply Filter]]
228           ]' \
229           "$w_coname" "$w_codate" \
230           "$w_colength" "$w_cogroup"
231 }
232
233 w_tagging(){
234   local tag category d
235   printf '[a href="#multitag" Add Tags / Remove Tags]
236           [div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
237   printf '[a href="#" Hide][br]'
238
239   printf 'Tags\n%s\n' "$w_tagcategories" \
240   | while read -r category; do
241     [ "$category" ] || continue
242     printf '[fieldset [legend %s:][select name=tag size=4 multiple\n' "$category"
243     printf %s "$w_tags" \
244     | { [ "$category" = 'Tags' ] && grep -avF ':' || grep -awF "${category}"; } \
245     | { for n in 1 2 3 4 5 6 7 8 9 0; do
246         read -r line && printf '%s\n' "$line" || break
247         done;
248         sort;
249     } | while read -r tag; do
250       [ "$tag" ] || continue
251       d="${tag#-}"; d="${d#*:}"
252       printf '[option value="%s"\n%s]' "$tag" "$d"
253     done
254     printf ']]'
255   done
256
257   printf '[fieldset [legend New:][textarea name=newtag\n]
258           [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
259           ]]'
260 }