forms: Add a reset/return button to separate-page forms
authorTero Marttila <terom@fixme.fi>
Fri, 07 Jan 2011 03:45:19 +0200
changeset 36 d7a159024912
parent 35 13b5dd3a7a5f
child 37 eabea2857143
forms: Add a reset/return button to separate-page forms
static/forms.css
static/js/forms.js
svv/forms.py
svv/orders.py
--- a/static/forms.css	Fri Jan 07 03:19:37 2011 +0200
+++ b/static/forms.css	Fri Jan 07 03:45:19 2011 +0200
@@ -118,6 +118,15 @@
     border: auto;
 }
 
+form input[type=reset]
+{
+    width: auto;
+
+    font-size: x-small;
+
+    float: right;
+}
+
 /* Field's descriptive text */
 fieldset p
 {
--- a/static/js/forms.js	Fri Jan 07 03:19:37 2011 +0200
+++ b/static/js/forms.js	Fri Jan 07 03:45:19 2011 +0200
@@ -17,6 +17,9 @@
             this.removeAttr("disabled");
     }
 
+    /**
+     * Query or set form field checked state
+     */
     $.fn.checked = function (flag) {
         if (flag == undefined)
             // XXX: jQuery returns true here?
@@ -29,6 +32,18 @@
             this.removeAttr("checked");
     }
 
+    /**
+     * Redirect browser to another URL
+     *
+     * XXX: Can I do a global function like this?
+     */
+    $.redirect = function (url) {
+        window.location.href = url;
+
+        return false;
+    }
+
+
     /*
      * The given checkbox acts as an enable/disable toggle for this form control
      */
--- a/svv/forms.py	Fri Jan 07 03:19:37 2011 +0200
+++ b/svv/forms.py	Fri Jan 07 03:45:19 2011 +0200
@@ -364,6 +364,20 @@
             tags.script("$(document).ready(function () { $('#" + name + "').formEnabledBy($('#" + checkbox_name + "')); });")
         )
 
+    def render_reset_button (self, value, return_url=None) :
+        """
+            Render HTML for a <input type="reset"> that abandons the form and returns the user to a page when pressed.
+
+                value                   - button title
+                return_url              - (optional) URL to redirect user back to when button is pressed
+        """
+
+        return tags.input(type='reset', value=value, 
+            onclick = (
+                "$.redirect('" + return_url + "')"
+            ) if return_url else None,
+        )
+
 
     def render_form_field (self, name, title, description, inputs) :
         """
--- a/svv/orders.py	Fri Jan 07 03:19:37 2011 +0200
+++ b/svv/orders.py	Fri Jan 07 03:45:19 2011 +0200
@@ -366,12 +366,13 @@
 
         )
 
-    def render (self, action, submit=u"Tallenna") :
+    def render (self, action, submit=u"Tallenna", return_url=None) :
         """
             Render the entire <form>, using any loaded/processed values.
 
                 action          - the target URL for the form to POST to
                 submit          - label for the submit button
+                return_url      - URL to return to if form is abandoned
         """
 
         return tags.form(action=action, method='POST')(
@@ -433,6 +434,7 @@
             ),
 
             tags.input(type='submit', value=submit),
+            self.render_reset_button(u"Unohda koko juttu...", return_url) if return_url else None,
         )
 
 class OrderContractForm (BaseForm) :
@@ -496,7 +498,11 @@
 
         self.contract_text = self.DEFAULT_TEXT
 
-    def render (self, action, submit=u"Tulosta vuokrasopimus") :
+    def render (self, action, return_url=None, edit_button=False) :
+        """
+                return_ulr              - display a "Return to order" link/button TO THIS URL
+                edit_button             - display an "Edit further" button, hiding extra fields until pressed
+        """
         
         # self.url_for(OrderContractDocument, id=id)
         return tags.form(action=action, method='POST')(
@@ -517,11 +523,13 @@
                             self.render_text_input('contract_text', self.contract_text, multiline=True, autoscale=True)
                         )),
 
-                    ) if self.edit else None,
+                    ) if not edit_button or self.edit else None,
 
                     tags.li(
-                        tags.input(type='submit', value=submit),
-                        tags.input(type='submit', name='edit', value=u"Muokkaa vuokrasopimusta"),
+                        tags.input(type='submit', value=u"Tulosta vuokrasopimus"),
+                        tags.input(type='submit', name='edit', value=u"Muokkaa vuokrasopimusta") if edit_button else None,
+
+                        self.render_reset_button(u"Unohda koko juttu...", return_url) if return_url else None,
                     ),
                 ),
             ),
@@ -763,8 +771,8 @@
             # feed form POST data
             form.process(self.POST)
 
-        # render
-        return form.render(action=self.url_for(OrderContractDocument, id=id))
+        # render, with edit button
+        return form.render(action=self.url_for(OrderContractDocument, id=id), edit_button=True)
 
 
     def render_order (self, order) :
@@ -883,7 +891,7 @@
         return (
             tags.h1(u"Tilaus #%d" % (id, )),
 
-            self.form.render(action=self.url_for(EditOrderView, id=id)),
+            self.form.render(action=self.url_for(EditOrderView, id=id), return_url=self.url_for(OrderView, id=id)),
         )
 
 
@@ -1002,7 +1010,7 @@
         
         return (
             tags.h1(u"Vuokrasopimus"),
-            self.form.render(action=self.url_for(OrderContractDocument, id=id)),
+            self.form.render(action=self.url_for(OrderContractDocument, id=id), return_url=self.url_for(OrderView, id=id)),
         )
     
     def generate_document (self, id) :