]> git.plutz.net Git - serve0/blob - widgets.sh
speed and code optimisations
[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="$(HTML "$REQUEST_URI")"
9
10 w_tags="$(find "$_DATA/" -path '*/.index/meta' -print0 \
11         | xargs -r0 cut -f4 \
12         | cut -d= -f2- |tr , '\n' | sort -u)"
13 w_tags="$(UNSTRING "$w_tags")"
14 w_tagcategories="$(printf %s "$w_tags" |cut -sd: -f1 |sort -u)"
15
16 w_search(){
17   printf '
18   [form #search method=GET action=?
19     [select name=o size=1 
20       [option disabled=disabled Order By]
21       [option value=Name Name]
22       [option value=Date Date]
23       [option value=Length Length]
24     ]
25     [input name=s placeholder=Search value="%s"]
26   ]
27   ' \
28   "$(HTML "$SEARCH")"
29 }
30
31 w_prefs(){
32   local fakemp4 mode
33   fakemp4="$(COOKIE fakemp4)"
34   mode="$(COOKIE mode)"
35   printf '
36   [form #prefs method="POST" action="?a=setprefs"
37     [a href="#" x]
38     [hidden "ref" "%s"]
39     [label for=prefs_ps Pagesize]
40     [input #prefs_ps type=number name=pagesize value="%s"][br]
41     [radio "mode" "browse" %s #prefs_modebrowse] [label for=prefs_modebrowse Browse Folders][br]
42     [radio "mode" "index"  %s #prefs_modeindex ] [label for=prefs_modeindex View Full Index][br]
43     [checkbox "fakemp4" "yes" %s #prefs_fmp4] [label for=prefs_fmp4 Fake .MP4 file ending][br]
44     [submit "store" "store" Set Cookie]
45   ]
46   ' \
47   "$w_refuri" "$LISTSIZE" \
48   "$([ "$mode" = index ] || printf checked)" \
49   "$([ "$mode" = index ] && printf checked)" \
50   "$([ "$fakemp4" = yes    ] && printf checked)"
51 }
52
53 w_index(){
54   [ -d "$_DATA/$ITEM/.index" ] || printf '
55   [form #index method="POST" action="?a=spawnindex"
56     [hidden "ref" "%s"]
57     [label Set up for Index view: ]
58     [checkbox "recursive" "yes" #spawn_recursive] [label for=spawn_recursive Include subdirectories]
59     [submit "spawn" "spawn" Set up]
60   ]
61   ' "$w_refuri"
62 }
63
64 w_advsearch(){
65   local n lbid tag category tn filter f
66   filter="${FILTER}^"
67
68   printf '[form #advsearch action=?a=advsearch method=POST
69             [a href="#" Hide]
70             [p .help Select multiple tags from each category by holding down the [strong Ctrl] key on your keyboard.[br]
71             Refine the search further by setting additional search tags using the [strong "+and"] button.]
72             [hidden "ref" "%s"]
73          ' "$w_refuri"
74
75   for n in 1 2 3 4 5 6 7 8 9 10; do
76     f="${filter%%^*}"; filter="${filter#*^}"
77
78     lbid="$(HTML "cat_${n}_(none)")"
79     printf '[input .and type=checkbox name=and id="and_%i" %s][label for="and_%i" +and
80             ][fieldset .select
81             [input .pol type="radio" name=pol_%i %s id="pol_pos_%i"][label for=pol_pos_%i Any]
82             [input .pol type="radio" name=pol_%i %s id="pol_neg_%i"][label for=pol_neg_%i None]
83             [label .head Category:]' \
84             $n "$([ "$f" ] && printf 'checked="checked"')" $n \
85             $n "$([ "$f" -a ! "${f%%~*}" ] || printf 'checked="checked"')" $n $n \
86             $n "$([ "$f" -a ! "${f%%~*}" ] && printf 'checked="checked"')" $n $n
87     f="|${f#~}|"
88
89     printf '*\n%s\n' "$w_tagcategories" \
90     | while read -r category; do
91       lbid="$(HTML "cat_${n}_${category}")"
92       printf '[input .cat type=radio %s name="cat_%i" id="%s"][label for="%s" %s]
93               [select name=tag_%s size=10 multiple="multiple"' \
94               "$([ ! "${f%%*|${category}:*}" -o ! "${category%\*}" ] && printf 'checked="checked"')" \
95               $n "$lbid" "$lbid" "$(HTML "$category")" $n
96
97       printf %s "$w_tags" \
98       | { [ "$category" = '*' ] && grep -vF ':' || grep -wF "${category}"; } \
99       | while read -r tag; do
100         [ ! "$tag" ] && continue
101         tg="$(HTML "$tag")"; tn="${tg#*:}"
102         printf '[option %s value="%s" %s]' \
103           "$([ ! "${f%%*|${tag}|*}" ] && printf 'selected="selected"')" \
104           "$tg" "$tn"
105       done
106       printf '\n]'
107     done
108     printf ']'
109   done
110
111   printf '[fieldset .submit [select name=order
112             [option disabled=disabled Order By]
113             [option value=Name Name]
114             [option value=Date Date]
115             [option value=Length Length]
116           ][button type=submit Apply Filter]]
117           ]'
118 }
119
120 w_tagging(){
121   local tag category
122   printf '[div #multitag [input type="hidden" name="ref" value="%s"]' "$w_refuri"
123   printf '[a href="#" Hide][br]'
124
125   printf 'Tags\n%s\n' "$w_tagcategories" \
126   | while read -r category; do
127     printf '[fieldset [legend %s:][select name=tag size=4 multiple' "$(HTML "$category")"
128     printf %s "$w_tags" \
129     | { [ "$category" = 'Tags' ] && grep -vF ':' || grep -wF "${category}"; } \
130     | while read -r tag; do
131       [ ! "$tag" ] && continue
132       tag="$(HTML "$tag")"; tn="${tag#*:}"
133       printf '[option value="%s" %s]' "$tag" "$tn"
134     done
135     printf ']]'
136   done
137
138   printf '[fieldset [legend New:][textarea name=newtag\n][button type=Submit Add Tags]]]'
139 }