From 6f89906afb09e115a106e759536cc4e58c8ddcd7 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 23 2019 09:22:33 +0000
Subject: [PATCH 1/3] retain old search pattern in web ui


Fixes: https://pagure.io/koji/issue/1130

---

diff --git a/www/kojiweb/includes/header.chtml b/www/kojiweb/includes/header.chtml
index 0e086e6..adc519f 100644
--- a/www/kojiweb/includes/header.chtml
+++ b/www/kojiweb/includes/header.chtml
@@ -1,4 +1,5 @@
 #encoding UTF-8
+#import cgi
 #import koji
 #from kojiweb import util
 #from koji_cli.lib import greetings
@@ -49,7 +50,12 @@ $localnav
               <option value="win">Windows Artifacts</option>
               #end if
             </select>
-            <input type="text" name="terms" title="You can use glob expressions here (e.g. 'bash-*')"/>
+            #try
+                #set $old_terms = cgi.escape($terms)
+            #except
+                #set $old_terms = ""
+            #end try
+            <input type="text" name="terms" title="You can use glob expressions here (e.g. 'bash-*')" value="$old_terms"/>
             <input type="submit" value="Search"/>
           </form>
         </div><!-- end header -->

From 1a13c15534f71e0e8a2e09ba7a1e163e86a57404 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 23 2019 09:22:33 +0000
Subject: [PATCH 2/3] retain search type


---

diff --git a/www/kojiweb/includes/header.chtml b/www/kojiweb/includes/header.chtml
index adc519f..d008f4e 100644
--- a/www/kojiweb/includes/header.chtml
+++ b/www/kojiweb/includes/header.chtml
@@ -36,18 +36,18 @@ $localnav
           <form action="search" id="headerSearch">
             <input type="hidden" name="match" value="glob"/>
             <select name="type">
-              <option value="package">Packages</option>
-              <option value="build">Builds</option>
-              <option value="tag">Tags</option>
-              <option value="target">Build Targets</option>
-              <option value="user">Users</option>
-              <option value="host">Hosts</option>
-              <option value="rpm">RPMs</option>
+              <option $util.toggleSelected($self, $type, "package") value="package">Packages</option>
+              <option $util.toggleSelected($self, $type, "build") value="build">Builds</option>
+              <option $util.toggleSelected($self, $type, "tag") value="tag">Tags</option>
+              <option $util.toggleSelected($self, $type, "target") value="target">Build Targets</option>
+              <option $util.toggleSelected($self, $type, "user") value="user">Users</option>
+              <option $util.toggleSelected($self, $type, "host") value="host">Hosts</option>
+              <option $util.toggleSelected($self, $type, "rpm") value="rpm">RPMs</option>
               #if $mavenEnabled
-              <option value="maven">Maven Artifacts</option>
+              <option $util.toggleSelected($self, $type, "maven") value="maven">Maven Artifacts</option>
               #end if
               #if $winEnabled
-              <option value="win">Windows Artifacts</option>
+              <option $util.toggleSelected($self, $type, "win") value="win">Windows Artifacts</option>
               #end if
             </select>
             #try

From a8da193f3cdbf20a3f56d3d949f29ce0764a3d12 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: May 23 2019 09:43:51 +0000
Subject: [PATCH 3/3] retain form values in search page


---

diff --git a/www/kojiweb/search.chtml b/www/kojiweb/search.chtml
index 8749dad..0b9a67f 100644
--- a/www/kojiweb/search.chtml
+++ b/www/kojiweb/search.chtml
@@ -1,9 +1,9 @@
+#import cgi
 #from kojiweb import util
 
 #include "includes/header.chtml"
 
   <h4>Search</h4>
-
   <form action="search">
     <table>
       <tr>
@@ -11,21 +11,26 @@
         <tr><td colspan="3" class="error">$error</td></tr>
         #end if
         <th>Search</th>
-        <td><input type="text" name="terms"/></td>
+        #try
+            #set $old_terms = cgi.escape($terms)
+        #except
+            #set $old_terms = ""
+        #end try
+        <td><input type="text" name="terms" value="$old_terms"/></td>
         <td>
           <select name="type">
-            <option value="package">Packages</option>
-            <option value="build">Builds</option>
-            <option value="tag">Tags</option>
-            <option value="target">Build Targets</option>
-            <option value="user">Users</option>
-            <option value="host">Hosts</option>
-            <option value="rpm">RPMs</option>
+            <option $util.toggleSelected($self, $type, "package") value="package">Packages</option>
+            <option $util.toggleSelected($self, $type, "build") value="build">Builds</option>
+            <option $util.toggleSelected($self, $type, "tag") value="tag">Tags</option>
+            <option $util.toggleSelected($self, $type, "target") value="target">Build Targets</option>
+            <option $util.toggleSelected($self, $type, "user") value="user">Users</option>
+            <option $util.toggleSelected($self, $type, "host") value="host">Hosts</option>
+            <option $util.toggleSelected($self, $type, "rpm") value="rpm">RPMs</option>
             #if $mavenEnabled
-            <option value="maven">Maven Artifacts</option>
+            <option $util.toggleSelected($self, $type, "maven") value="maven">Maven Artifacts</option>
             #end if
             #if $winEnabled
-            <option value="win">Windows Artifacts</option>
+            <option $util.toggleSelected($self, $type, "win") value="win">Windows Artifacts</option>
             #end if
           </select>
         </td>
@@ -33,9 +38,12 @@
       <tr>
         <th>&nbsp;</th>
         <td colspan="2">
-          <input type="radio" name="match" value="glob" id="radioglob" checked="checked"/><abbr title="? will match any single character, * will match any sequence of zero or more characters" id="abbrglob">glob</abbr>
-          <input type="radio" name="match" value="regexp" id="radioregexp"/><abbr title="full POSIX regular expressions" id="abbrregexp">regexp</abbr>
-          <input type="radio" name="match" value="exact" id="radioexact"/><abbr title="exact matches only" id="abbrexact">exact</abbr>
+          #if not $varExists('match')
+             #set $match='glob'
+          #end if
+          <input type="radio" name="match" value="glob" $util.toggleSelected($self, $match, "glob", True) id="radioglob"/><abbr title="? will match any single character, * will match any sequence of zero or more characters" id="abbrglob">glob</abbr>
+          <input type="radio" name="match" value="regexp" $util.toggleSelected($self, $match, "regexp", True) id="radioregexp"/><abbr title="full POSIX regular expressions" id="abbrregexp">regexp</abbr>
+          <input type="radio" name="match" value="exact" $util.toggleSelected($self, $match, "exact", True) id="radioexact"/><abbr title="exact matches only" id="abbrexact">exact</abbr>
         </td>
       </tr>
       <tr>
diff --git a/www/lib/kojiweb/util.py b/www/lib/kojiweb/util.py
index abca32d..d5b5cbb 100644
--- a/www/lib/kojiweb/util.py
+++ b/www/lib/kojiweb/util.py
@@ -189,14 +189,18 @@ def toggleOrder(template, sortKey, orderVar='order'):
     else:
         return sortKey
 
-def toggleSelected(template, var, option):
+def toggleSelected(template, var, option, checked=False):
     """
     If the passed in variable var equals the literal value in option,
-    return 'selected="selected"', otherwise return ''.
-    Used for setting the selected option in select boxes.
+    return 'selected="selected"', otherwise return ''. If checked is True,
+    '"checked="checked"' string is returned
+    Used for setting the selected option in select and radio boxes.
     """
     if var == option:
-        return 'selected="selected"'
+        if checked:
+            return 'checked="checked"'
+        else:
+            return 'selected="selected"'
     else:
         return ''
 

