order: Implement correct form preset values for phone/email
authorTero Marttila <terom@fixme.fi>
Thu, 23 Dec 2010 22:31:52 +0200
changeset 18 5d06a3658699
parent 17 820c46308e45
child 19 23007836528e
order: Implement correct form preset values for phone/email
static/js/forms.js
svv/orders.py
--- a/static/js/forms.js	Thu Dec 23 21:20:41 2010 +0200
+++ b/static/js/forms.js	Thu Dec 23 22:31:52 2010 +0200
@@ -53,7 +53,7 @@
     /*
      * When non-zero <select>/<option> is selected, apply that option as pre-filled values for other form items
      */
-    $.fn.formSelectPreset = function (opts) {
+    $.fn.formSelectsPreset = function (opts) {
         opts = $.extend({
             // which option value is the "default", i.e. 'create new'
             valueDefault: 0,
@@ -63,6 +63,9 @@
 
             // which element to apply selected option text to
             textTarget: null,
+
+            // list of targets from external lookup maps
+            mapTargets: null
         }, opts);
 
         function update () {
@@ -73,35 +76,50 @@
             text = $.trim($(this).find("option:selected").text());
 
             // fields to set
-            field_values = [
-                [ opts.valueTarget, value ],
-                [ opts.textTarget, text ]
-            ];
-
-            if (value == opts.valueDefault) {
-                // clear and re-enable fields
-                if (opts.valueTarget && (opts.valueTarget.disabled() || opts.valueTarget.empty())) {
-                    opts.valueTarget.disabled(false);
-                    opts.valueTarget.val("");
-                }
+            field_values = [];
 
-                if (opts.textTarget && (opts.textTarget.disabled() || opts.textTarget.empty())) {
-                    opts.textTarget.disabled(false);
-                    opts.textTarget.val("");
-                }
-
-                return;
-            }
+            if (opts.valueTarget)
+                field_values.push([opts.valueTarget, value]);
 
-            // set field values
-            if (opts.valueTarget) {
-                opts.valueTarget.disabled(true);
-                opts.valueTarget.val(value);
+            if (opts.textTarget)
+                field_values.push([opts.textTarget, text]);
+
+            if (opts.mapTargets) {
+                $.each(opts.mapTargets, function (index, entry) {
+                    var field = entry[0], map = entry[1];
+
+                    // entry
+                    field_values.push([field, map[value]]);
+                });
             }
+            
+            // apply new value
+            if (value == opts.valueDefault) {
+                // Re-enable all the fields that we set preset values for earlier
+                $.each(field_values, function (index, entry) {
+                    var field = entry[0], value = entry[1];
 
-            if (opts.textTarget) {
-                opts.textTarget.disabled(true);
-                opts.textTarget.val(text);
+                    if (!field.disabled())
+                        // if we didn't lock it down, we shouldn't be resetting it..
+                        // this happens mainly at page load
+                        return;
+
+                    // reset
+                    field.val("");
+                    field.disabled(false);
+                });
+
+            } else {
+                // Set preset value and lock down the target fields
+                $.each(field_values, function (index, entry) {
+                    var field = entry[0], value = entry[1];
+
+                    // contant value
+                    field.disabled(true);
+                    
+                    // display the read-only value
+                    field.val(value);
+                });
             }
         }
         
--- a/svv/orders.py	Thu Dec 23 21:20:41 2010 +0200
+++ b/svv/orders.py	Thu Dec 23 22:31:52 2010 +0200
@@ -12,6 +12,14 @@
 import logging
 import collections
 
+try :
+    # Python2.6 stdlib
+    import json
+
+except ImportError :
+    # simplejson, API should be similar
+    import simplejson as json
+
 log = logging.getLogger('svv.orders')
 
 class FormError (Exception) :
@@ -522,7 +530,7 @@
             self.render_select_input('customer_id', [(0, u"Luo uusi")] + customers, self.customer_id),
             self.render_text_input('customer_name', self.customer_name),
 
-            tags.script(r"$(document).ready(function () { $('#customer_id').formSelectPreset({textTarget: $('#customer_name')}); });"),
+            tags.script(r"$(document).ready(function () { $('#customer_id').formSelectsPreset({textTarget: $('#customer_name')}); });"),
         )
 
     def render_contact_input (self) :
@@ -530,13 +538,30 @@
             Render HTML for contact name field <input>s
         """
         # recommended contacts for selected customer, if known
-        contacts = self.build_contact_list(self.customer_id)
+        contacts = list(self.build_contact_list(self.customer_id))
 
         return (
             self.render_select_input('contact_id', [(0, u"Luo uusi")] + [(id, name) for id, name, phone, email in contacts], self.contact_id),
             self.render_text_input('contact_name', self.contact_name),
 
-            tags.script(r"$(document).ready(function () { $('#contact_id').formSelectPreset({textTarget: $('#contact_name')}); });"),
+            tags.script("""\
+$(document).ready(function () { 
+    contact_phones = %(phones)s;
+    contact_emails = %(emails)s;
+
+    $('#contact_id').formSelectsPreset({
+        textTarget: $('#contact_name'),
+
+        mapTargets: [
+            [$('#contact_phone'), contact_phones],
+            [$('#contact_email'), contact_emails]
+        ]
+    });
+});
+""" %       dict(
+                phones  = json.dumps(dict((row[db.contacts.c.id], row[db.contacts.c.phone]) for row in contacts)),
+                emails  = json.dumps(dict((row[db.contacts.c.id], row[db.contacts.c.email]) for row in contacts)),
+            )),
         )
 
     def render_event_input (self) :
@@ -610,11 +635,11 @@
                         self.render_contact_input()
                     )),
 
-                    self.render_form_field('contact_phone', u"Puhelin", u"Yhteyshenkilön puhelinnumero", (
+                    self.render_form_field('contact_phone', u"Puhelinnumero", u"Yhteyshenkilön puhelinnumero", (
                         self.render_text_input('contact_phone', self.contact_phone)
                     )),
 
-                    self.render_form_field('contact_email', u"Sähköposti", u"Yhteyshenkilön sähköpostiosoite", (
+                    self.render_form_field('contact_email', u"Sähköpostiosoite", u"Yhteyshenkilön sähköpostiosoite", (
                         self.render_text_input('contact_email', self.contact_email)
                     )),
                 ),