]> git.plutz.net Git - serve0/commitdiff
split up functions in filter box generator
authorpaul <paul@plutz.net>
Wed, 29 Jul 2015 18:16:23 +0000 (18:16 +0000)
committerpaul <paul@plutz.net>
Wed, 29 Jul 2015 18:16:23 +0000 (18:16 +0000)
svn path=/trunk/; revision=65

templates/advfilter.html.sh

index 80ec4c268bc3e66d98e6dc200db541a95db6512d..f63d4dc7f328c3a4168594203f1426b18f733bda 100755 (executable)
 # You should have received a copy of the GNU Affero General Public License
 # along with Serve0  If not, see <http://www.gnu.org/licenses/>. 
 
+quicklinks(){
+  output=''
+  tac ${_DATA}/meta/recent |while [ "$(echo "$output" |wc -l)" -lt 10 ] && read line; do
+    line="<a href="$line">$line</a>"
+    echo "$output" |grep -qF "$line" || output="$output$line\n"
+  done
+  echo $output
+}
+
+list_categories(){
+  echo none
+  printf %s "$taglist" |sed -rn 's;^([^:]+):.*$;\1;p' |sort -u
+}
+
+category_selected(){
+  cat="$1"
+  filter="$2"
+
+  printf %s "$taglist" \
+  | sed -r 's;^('"$filter"')$;1 &;;t;s;^;0 ;' \
+  | sed -rn 's;^(0|1) '"$cat"'([^:]+)$;\1 \2;p' \
+  | sed 's;^0 ;<option>;;s;^1 ;<option selected>;;s;$;</option>;'
+}
+
+category_tabs(){
+  for cat in "$@"; do
+  cat <<EOF
+    <div class="tab">
+    <input class="tabhandle" type="radio" name="category$n" id="cat${n}_$cat" value="$cat" $([ "$cat" = "$fcat" ] && echo checked)>
+    <label class="category tabhandle" for="cat${n}_$cat">$([ "$cat" = none ] && printf %s general || printf %s "$cat")</label>
+    <select class="category tabcontent" class="input tagfilter" name="cfilter${n}_" size="12" multiple>
+      $([ "$cat" = none ] && category_selected "" "$filter" || category_selected "${cat}:" "$filter")
+    </select>
+    </div>
+EOF
+  done
+}
+
+filterbox(){
+  n=$1
+  filter="$(printf '%s^' "$filters" |cut -d^ -f$(($n+1)))"
+  fneg="${filter%%[a-zA-Z]*}"
+  filter="${filter#!}"
+  fcat="${filter%%:*}"
+  [ "$fcat" = "$filter" ] && fcat=none
+  
+  debug "fneg: $fneg   fcat: $fcat     filter: $filter"
+  
+  cat <<EOF
+  <input class="tabhandle" type="checkbox" id="filter$n" $([ $n = 0 -o -n "$filter" ] && echo checked) >
+  <label class="tabhandle filter" for="filter$n">+ and</label>
+  <div class="tabcontent filter">
+    <input type="radio" name="type$n" value="pos" id="pos$n" ${fneg:-checked} ><label for="pos$n" class="conjunction">Any</label>
+    <input type="radio" name="type$n" value="neg" id="neg$n" ${fneg:+checked} ><label for="neg$n" class="conjunction">None</label>
+    <span class="help">of the items selected here:</span>
+  
+    <label class="category">Category:</label>
+    <div class="category tabcontainer">
+      $(category_tabs $(list_categories) )
+    </div>
+  </div>
+EOF
+}
+
 cat <<EOF
   <form class="panel" id="advfilter"
         action="?action=filter" method="post" accept-charset="UTF-8">
@@ -32,56 +96,7 @@ cat <<EOF
     <hr>
 
     <p class="help">Up to <strong>10</strong> filter boxes will appear as you start selecting tags. You can select <strong>multiple</strong> tags in each box by holding down the <strong>Ctrl</strong>-key. Click the "<strong>Filter!</strong>" button when you are ready.</p>
-
-  $(for n in {0..9}; do
-    filter="$(printf '%s^' "$filters" |cut -d^ -f$(($n+1)))"
-    fneg="${filter%%[a-zA-Z]*}"
-    filter="${filter#!}"
-    fcat="${filter%%:*}"
-    [ "$fcat" = "$filter" ] && fcat=none
-
-    debug "fneg: $fneg fcat: $fcat     filter: $filter"
-    
-    cat <<-EOF
-       <input class="tabhandle" type="checkbox" id="filter$n" $([ $n = 0 -o -n "$filter" ] && echo checked) >
-       <label class="tabhandle filter" for="filter$n">+ and</label>
-       <div class="tabcontent filter">
-         <input type="radio" name="type$n" value="pos" id="pos$n" ${fneg:-checked} ><label for="pos$n" class="conjunction">Any</label>
-         <input type="radio" name="type$n" value="neg" id="neg$n" ${fneg:+checked} ><label for="neg$n" class="conjunction">None</label>
-         <span class="help">of the items selected here:</span>
-
-         <label class="category">Category:</label>
-          <div class="category tabcontainer">
-           <div class="tab">
-           <input class="tabhandle" type="radio" name="category$n" id="cat${n}_none" value="none" $([ "none" = "$fcat" ] && echo checked)>
-           <label class="category tabhandle" for="cat${n}_none">general</label>
-           <select class="category tabcontent" class="input tagfilter" name="cfilter${n}_" size="12" multiple>
-             $(echo "$taglist" |grep -v : |while read item; do
-                 echo -E "$item" |egrep -q "^($filter)$" && \
-                    echo "<option selected >$item</option>" \
-                 || echo "<option>$item</option>"
-              done)
-           </select>
-           </div>
-         $( echo "$taglist" |sed -rn 's;^([^:]+):.*$;\1;p' |sort -u \
-             |while read cat; do echo "
-           <div class="tab">
-           <input class="tabhandle" type="radio" name="category$n" id="cat${n}_$cat" value="$cat" $([ "$cat" = "$fcat" ] && echo checked)>
-           <label class=\"category tabhandle\" for="cat${n}_$cat">$cat</label>
-           <select class=\"category tabcontent\" class=\"input tagfilter\" name="cfilter${n}_" size="12" multiple>
-             $(echo "$taglist" |sed -rn "s;^$cat:;;p" | while read item; do
-                echo -E "${cat}:${item}" |egrep -q "^($filter)$" && \
-                    echo "<option selected >$item</option>" \
-                 || echo "<option>$item</option>"
-              done)
-           </select>
-           </div>
-         "
-          done)
-          </div>
-       </div>
-       EOF
-  done)
+    $(for n in {0..9}; do filterbox "$n"; done)
 
     <p class="filter final">
       <label for="o3" class="order">Order by:</label>
@@ -95,13 +110,7 @@ cat <<EOF
 
     <p class="quicklinks">
       <label class="quicklinks">Most recent</label>
-      $(output=''
-        tac ${_DATA}/meta/recent |while [ "$(echo "$output" |wc -l)" -lt 10 ] && read line; do
-          line="<a href="$line">$line</a>"
-          echo "$output" |grep -qF "$line" || output="$output$line\n"
-        done
-        echo $output
-      )
+      $(quicklinks)
     </p>
   </form>
 EOF