]> git.plutz.net Git - serve0/blob - widgets.sh
integrated bookmark manager
[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_tags="$(find "$_DATA/" -path '*/.index/meta' -print0 \
11         | xargs -r0 sed -r '
12           s;^.*\t.*\t.*\ttags=(.*)\tcomment=.*\t.*\r$;\1;;
13           s;,;\n;g;'"$UNSTRING" \
14         | sort -u |HTML |sed 's;&#58\;;:;g; s;&#10\;;\n;g;')"
15 w_tagcategories="$(printf %s "$w_tags" | cut -sd: -f1 | uniq)"
16
17 [ "$ORDER" = Name   ] && w_coname=checked
18 [ "$ORDER" = Date   ] && w_codate=checked
19 [ "$ORDER" = Length ] && w_colength=checked
20
21 w_bookmarks(){
22   local name='' cf='' cs='' bm="$_DATA/.index/bookmarks" proposed_name=''
23   [ ! -d "${bm%/*}" ] && return 0
24   [ ! -f "$bm" ] && touch "$bm"
25
26   [ "$SEARCH" ] && cs="$(STRING "$SEARCH")"
27   [ "$FILTER" ] && cf="$(STRING "$FILTER")"
28
29   name="$(grep -m1 -aF "        search=$cs      filter=$cf${CR}" "$bm" |cut -f1 |UNSTRING)"
30   [ ! "$name" ] && proposed_name="$(
31     printf '%s\t%s' "$SEARCH" "$FILTER" \
32     |sed -r '/^\t$/{  s;\t;All;; q;}
33              /.*\t$/{ s;\t$;;; q;}
34              /^\t.*/{ s;^\t;;;
35                       :x; s;(^|[~^|])([^|^~:]+):;\1;; tx;
36                       s;\^; and ;g; s;\|;,;g; s;~;not ;g; q;}'
37     )"
38
39   printf '[form #bookmarks action=?a=bookmark method=POST
40             [a href="#" x]
41             [hidden "ref" "%s"]
42             [hidden "search" "%s"][hidden "filter" "%s"]
43             [label Name for current page:]
44             [input name="name" value="%s" placeholder="Name" ]
45             [button type="submit" %s]' \
46             "$w_refuri" \
47             "$(HTML "$SEARCH")" "$(HTML "$FILTER")" \
48             "$(HTML "${name:-${proposed_name}}")" \
49             "${name:+Update}${proposed_name:+Add}"
50   [ "$name" ] && printf ' [submit "delete" "delete" Delete]'
51
52   sort "$bm" |while read -r name search filter; do
53     search="${search#search=}" filter="${filter#filter=}" filter="${filter%${CR}}"
54     [ "$search" = "${cs}" -a "$filter" = "${cf}" ] && continue
55
56     name="$(UNSTRING "$name")";
57     search="$(UNSTRING "${search}" |URL)";
58     filter="$(UNSTRING "${filter}" |URL)";
59     printf '[label .link %s]
60             [a .link target=blank href="?o=Name&s=%s&f=%s" by Name]
61             [a .link target=blank href="?o=Date&s=%s&f=%s" by Date]
62             [a .link target=blank href="?o=Length&s=%s&f=%s" by Length]
63             [br]' \
64             "$(HTML "$name")" \
65             "$search" "$filter" \
66             "$search" "$filter" \
67             "$search" "$filter"
68   done
69   printf ']'
70 }
71
72 w_search(){
73   printf '
74   [form #search method=GET action=?
75     [select name=o size=1 
76       [option disabled=disabled Order By]
77       [option value=Name %s Name]
78       [option value=Date %s Date]
79       [option value=Length %s Length]
80     ]
81     [input name=s placeholder=Search value="%s"]
82   ]
83   ' \
84   "$w_coname" "$w_codate" "$w_colength" \
85   "$(HTML "$SEARCH")"
86 }
87
88 w_prefs(){
89   local tm tf
90
91   tm=''; [ "$(COOKIE mode)" = index ] && tm=' '
92   tf=''; [ "$(COOKIE fakemp4)" = yes ] && tf=checked
93
94   printf '
95   [form #prefs method="POST" action="?a=setprefs"
96     [a href="#" x]
97     [hidden "ref" "%s"]
98     [label for=prefs_ps Pagesize]
99     [input #prefs_ps type=number name=pagesize value="%s"][br]
100     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
101     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
102     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
103     [submit "store" "store" Set Cookie]
104   ]
105   ' \
106   "$w_refuri" "$LISTSIZE" \
107   "${tm:-checked}" "${tm:+checked}" "$tf"
108 }
109
110 w_index(){
111   [ -d "$_DATA/$ITEM/.index" ] || printf '
112   [form #index method="POST" action="?a=spawnindex"
113     [hidden "ref" "%s"]
114     [label Set up for Index view: ]
115     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
116     [submit "spawn" "spawn" Set up]
117   ]
118   ' "$w_refuri"
119 }
120
121 w_advsearch(){
122   local n lbid tag category filter f t
123   filter="$(HTML "${FILTER}^" |sed 's;&#126\;;~;g; s;&#94\;;^;g; s;&#124\;;|;g; s;&#58\;;:;g;')"
124
125   printf '[form #advsearch action=?a=advsearch method=POST
126             [a href="#" Hide]
127             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
128             Refine the search further by setting additional search tags using the [strong "+and"] button.]'
129
130   for n in 1 2 3 4 5 6 7 8 9 10; do
131     f="${filter%%^*}"; filter="${filter#*^}"
132
133     t=''; [ "$f" -a ! "${f%%~*}" ] && t=" "
134
135     lbid="cat_${n}_(none)"
136     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
137             ][fieldset .select
138             [radio "pol_%i" "pos" .pol %s #pol_pos_%i"][label for=pol_pos_%i Any]
139             [radio "pol_%i" "neg" .pol %s #pol_neg_%i"][label for=pol_neg_%i None]
140             [label .head Category:]' \
141             $n "${f:+checked}" $n \
142             $n "${t:-checked}" $n $n \
143             $n "${t:+checked}" $n $n
144
145     f="|${f#~}|"
146     printf '*\n%s\n' "$w_tagcategories" \
147     | while read -r category; do
148       lbid="cat_${n}_${category}"
149
150       t=''
151       [ "$category"  = '*' -a   "${f%%|${category}:*}" ] && t=checked
152       [ "$category" != '*' -a ! "${f%%|${category}:*}" ] && t=checked
153
154       printf '[radio "cat_%i" "%s" .cat %s id="%s"][label for="%s" %s]
155               [select name=tag_%s size=10 multiple' \
156               $n "$category" "$t" "$lbid" "$lbid" "$category" $n
157
158       printf '%s\n' "$w_tags" \
159       | { [ "$category" = '*' ] && grep -avF ':' |grep -avxF '' || grep -awF "${category}"; } \
160       | while read -r tag; do
161         t=''; [ ! "${f%%*|${tag}|*}" ] && t=checked
162         printf '[option %s value="%s"\n%s]' "$t" "$tag" "${tag#*:}"
163       done
164       printf '\n]'
165     done
166     printf ']'
167   done
168
169   printf '[fieldset .submit [select name=order
170             [option disabled=disabled Order By]
171             [option value=Name %s Name]
172             [option value=Date %s Date]
173             [option value=Length %s Length]
174           ][button type=submit Apply Filter]]
175           ]' \
176           "$w_coname" \
177           "$w_codate" \
178           "$w_colength"
179 }
180
181 w_tagging(){
182   local tag category
183   printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
184   printf '[a href="#" Hide][br]'
185
186   printf 'Tags\n%s\n' "$w_tagcategories" \
187   | while read -r category; do
188     printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$category"
189     printf %s "$w_tags" \
190     | { [ "$category" = 'Tags' ] && grep -avF ':' | grep -avxF '' || grep -awF "${category}"; } \
191     | while read -r tag; do
192       printf '[option value="%s"\n%s]' "$tag" "${tag#*:}"
193     done
194     printf ']]'
195   done
196
197   printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
198 }