]> git.plutz.net Git - serve0/blob - widgets.sh
Mixed tag sorting: most common first, then alphabetically
[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" -quit)" ]
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 \
37     | while read 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     printf "%i  %s\n" "$cn" "$otag"
47   } |sort -rn |cut -f2- |HTML |sed "$w_ascii s-
-\n-g;" |tee "$w_tags" )"
48   w_tagcategories="$(printf %s "$w_tags" \
49                      | cut -sd: -f1 |sort -u \
50                      | tee "$w_tagcategories" )"
51 else
52   w_tags="$(cat "$w_tags")"
53   w_tagcategories="$(cat "$w_tagcategories")"
54 fi
55
56
57 [ "$ORDER" = Name   ] && w_coname=checked
58 [ "$ORDER" = Date   ] && w_codate=checked
59 [ "$ORDER" = Length ] && w_colength=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     ]
121     [input name=s placeholder=Search value="%s"]
122   ]
123   ' \
124   "$w_coname" "$w_codate" "$w_colength" \
125   "$(HTML "$SEARCH")"
126 }
127
128 w_prefs(){
129   local tm tf
130
131   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
132   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
133
134   printf '
135   [form #prefs method="POST" action="?a=setprefs"
136     [a href="#" x]
137     [hidden "ref" "%s"]
138     [label for=prefs_ps Pagesize]
139     [input #prefs_ps type=number name=pagesize value="%s"][br]
140     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
141     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
142     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
143     [submit "store" "store" Set Cookie]
144   ]
145   ' \
146   "$w_refuri" "$LISTSIZE" \
147   "${tm:-checked}" "${tm:+checked}" "$tf"
148 }
149
150 w_index(){
151   [ -d "$_DATA/$ITEM/.index" ] || printf '
152   [form #index method="POST" action="?a=spawnindex"
153     [hidden "ref" "%s"]
154     [label Set up for Index view: ]
155     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
156     [submit "spawn" "spawn" Set up]
157   ]
158   ' "$w_refuri"
159 }
160
161 w_advsearch(){
162   local n lbid tag category filter f t
163   filter="$(HTML "${FILTER}^" |sed "$w_ascii")"
164
165   printf '[form #advsearch action=?a=advsearch method=POST
166             [a href="#" Hide]
167             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
168             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
169
170   for n in 1 2 3 4 5 6 7 8 9 10; do
171     f="${filter%%^*}"; filter="${filter#*^}"
172
173     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
174
175     lbid="cat_${n}_(none)"
176     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
177             ][fieldset .select
178             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
179             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
180             [label .head Category:]' \
181             $n "${f:+checked}" $n \
182             $n "${t:-checked}" $n $n \
183             $n "${t:+checked}" $n $n
184
185     f="|${f#~}|"
186     printf '*\n%s\n' "$w_tagcategories" \
187     | while read -r category; do
188       lbid="cat_${n}_${category}"
189
190       t=''
191       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
192       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
193
194       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
195               [select name=tag_%s size=10 multiple' \
196               $n "$category" "$t" "$lbid" "$lbid" "$category" $n
197
198       printf '%s\n' "$w_tags" \
199       | { [ "$category" = '*' ] && grep -avF ':' |grep -avxF '' || grep -awF "${category}"; } \
200       | { for n in 1 2 3 4 5 6 7 8 9 0; do line; done; sort; } \
201       | while read -r tag; do
202         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
203         printf '[option %s value="%s"\n%s]' "$t" "$tag" "${tag#*:}"
204       done
205       printf '\n]'
206     done
207     printf ']'
208   done
209
210   printf '[fieldset .submit [select name=order
211             [option disabled=disabled Order By]
212             [option value=Name %s Name]
213             [option value=Date %s Date]
214             [option value=Length %s Length]
215           ][button type=submit Apply Filter]]
216           ]' \
217           "$w_coname" \
218           "$w_codate" \
219           "$w_colength"
220 }
221
222 w_tagging(){
223   local tag category
224   printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
225   printf '[a href="#" Hide][br]'
226
227   printf 'Tags\n%s\n' "$w_tagcategories" \
228   | while read -r category; do
229     printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$category"
230     printf %s "$w_tags" \
231     | { [ "$category" = 'Tags' ] && grep -avF ':' | grep -avxF '' || grep -awF "${category}"; } \
232     | { for n in 1 2 3 4 5 6 7 8 9 0; do line; done; sort; } \
233     | while read -r tag; do
234       printf '[option value="%s"\n%s]' "$tag" "${tag#*:}"
235     done
236     printf ']]'
237   done
238
239   printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
240 }