Alert addresses for patron registration : UI
authorBill Erickson <berick@esilibrary.com>
Fri, 9 Dec 2011 20:52:32 +0000 (15:52 -0500)
committerThomas Berezansky <tsbere@mvlc.org>
Wed, 14 Dec 2011 15:10:40 +0000 (10:10 -0500)
Add support for comparing user addresses to alert addresses.  When an
address is found, the address in question is styled (the header row
turns red) and the configured alert message is shown along the
top-right, where other warnings appear.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Thomas Berezansky <tsbere@mvlc.org>
Open-ILS/src/templates/actor/user/register.tt2
Open-ILS/web/css/skin/default/register.css
Open-ILS/web/js/ui/default/actor/user/register.js

index 379405f..4e7c7fe 100644 (file)
     <div><a href='javascript:void(0);' id='uedit-dupe-address-link'></a></div>
 </div>
 
+<div id='uedit-address-alert' class='hidden'>
+    <div id='uedit-address-alert-header'>[% l('Address Alert') %]</div>
+    <div id='uedit-address-alert-message'></div>
+</div>
+
 <div class='hidden'>
     <span id='true' style='color:green;'>&#x2713;</span>
     <span id='false' style='color:red;'>&#x2717;</span>
index 2868ef4..582975b 100644 (file)
     padding: 20px;
 }
 
+#uedit-address-alert {
+    position: fixed;
+    top:140px;
+    right:30px;
+    width:300px;
+    border:2px dashed #d9e8f9;
+    -moz-border-radius: 10px;
+    font-weight: bold;
+    padding: 10px;
+    text-align: center;
+}
+#uedit-address-alert-header { 
+    color: red; 
+    text-decoration: italic
+    text-align: center;
+}
+
+.uedit-address-alert-divider  {
+    border: 2px dashed red;
+}
+
 .uedit-help {
     width: 25px;
     border:1px solid #e0e0e0;
index 56b7261..fffe460 100644 (file)
@@ -48,10 +48,17 @@ var uEditCloneCopyAddr; // if true, copy addrs on clone instead of link
 var homeOuTypes = {};
 var cardPerms = {};
 var editCard;
+var prevBillingAddress;
+var prevMailingAddress;
 
 var dupeUsrname = false;
 var dupeBarcode = false;
 
+// allow for a pause after typing before sending address alert queries
+var addressAlertTimeout = 2000; 
+var addressAlertFields = 
+    ['street1', 'street2', 'city', 'state', 'county', 'country', 'post_code'];
+
 if(!window.xulG) var xulG = null;
 var lock_ready = false;
 var already_locked = false;
@@ -1261,50 +1268,131 @@ function attachWidgetEvents(fmcls, fmfield, widget) {
     }
 
     if(fmclass = 'aua') {
-        switch(fmfield) {
-            case 'post_code':
-                dojo.connect(widget.widget, 'onChange',
-                    function(e) { 
-                        fieldmapper.standardRequest(
-                            ['open-ils.search', 'open-ils.search.zip'],
-                            {   async: true,
-                                params: [e],
-                                oncomplete : function(r) {
-                                    var res = openils.Util.readResponse(r);
-                                    if(!res) return;
-                                    var callback = function(w) { return w._addr == widget._addr; };
-                                    if(res.city) findWidget('aua', 'city', callback).widget.attr('value', res.city);
-                                    if(res.state) findWidget('aua', 'state', callback).widget.attr('value', res.state);
-                                    if(res.county) findWidget('aua', 'county', callback).widget.attr('value', res.county);
-                                    if(res.alert) alert(res.alert);
-                                }
+
+        // map post code to city, state, and county
+        if (fmfield == 'post_code') {
+            dojo.connect(widget.widget, 'onChange',
+                function(e) { 
+                    fieldmapper.standardRequest(
+                        ['open-ils.search', 'open-ils.search.zip'],
+                        {   async: true,
+                            params: [e],
+                            oncomplete : function(r) {
+                                var res = openils.Util.readResponse(r);
+                                if(!res) return;
+                                var callback = function(w) { return w._addr == widget._addr; };
+                                if(res.city) findWidget('aua', 'city', callback).widget.attr('value', res.city);
+                                if(res.state) findWidget('aua', 'state', callback).widget.attr('value', res.state);
+                                if(res.county) findWidget('aua', 'county', callback).widget.attr('value', res.county);
+                                if(res.alert) alert(res.alert);
                             }
-                        );
-                    }
-                );
-                return;
+                        }
+                    );
+                }
+            );
+        }
 
-            case 'street1':
-            case 'street2':
-            case 'city':
-                dojo.connect(widget.widget, 'onChange',
-                    function(e) {
-                        var callback = function(w) { return w._addr == widget._addr; };
-                        var args = {
-                            street1 : findWidget('aua', 'street1', callback).widget.attr('value'),
-                            street2 : findWidget('aua', 'street2', callback).widget.attr('value'),
-                            city : findWidget('aua', 'city', callback).widget.attr('value'),
-                            post_code : findWidget('aua', 'post_code', callback).widget.attr('value')
-                        };
-                        if(args.street1 && args.city && args.post_code)
-                            uEditDupeSearch('address', args); 
-                    }
-                );
-                return;
+        // duplicate address search
+        if (['street1', 'street2', 'city'].indexOf(fmfield) > -1) {
+            dojo.connect(widget.widget, 'onChange',
+                function(e) {
+                    var callback = function(w) { return w._addr == widget._addr; };
+                    var args = {
+                        street1 : findWidget('aua', 'street1', callback).widget.attr('value'),
+                        street2 : findWidget('aua', 'street2', callback).widget.attr('value'),
+                        city : findWidget('aua', 'city', callback).widget.attr('value'),
+                        post_code : findWidget('aua', 'post_code', callback).widget.attr('value')
+                    };
+                    if(args.street1 && args.city && args.post_code)
+                        uEditDupeSearch('address', args); 
+                }
+            ); 
+        }
+
+        if (addressAlertFields.indexOf(fmfield) > -1) {
+            dojo.connect(
+                widget.widget, 'onChange', 
+                function() { uEditAddressAlertMarshal(widget._addr) }
+            );
         }
     }
 }
 
+function uEditAddressAlertMarshal(addrId, changeBilling, changeMailing) {
+
+    if (changeBilling) {
+        uEditAddressAlertMarshal(prevBillingAddress);
+        prevBillingAddress = addrId;
+    }
+    
+    if (changeMailing) {
+        uEditAddressAlertMarshal(prevMailingAddress);
+        prevMailingAddress = addrId;
+    }
+
+    var callback = function(w) { return w._addr == addrId; };
+    var args = {};
+    dojo.forEach(addressAlertFields,
+        function(field) {
+            args[field] = findWidget('aua', field, callback).widget.attr('value')
+        }
+    );
+    args.mailing_address = dojo.byId('uedit-mailing-address-' + addrId).checked;
+    args.billing_address = dojo.byId('uedit-billing-address-' + addrId).checked;
+    uEditAddressAlertSearch(args, addrId);
+}
+
+var _addrAlertTimeout = {};
+function uEditAddressAlertSearch(args, addrId) {
+
+    _addrAlertTimeout[addrId] = setTimeout(
+        function() {
+            if (_addrAlertTimeout[addrId]) 
+                clearTimeout(_addrAlertTimeout[addrId]);
+
+            console.log('creating addr alert search for ' + addrId);
+
+            fieldmapper.standardRequest(
+                ['open-ils.actor', 'open-ils.actor.address_alert.test'],
+                {   async: true,
+                    params: [openils.User.authtoken, staff.ws_ou(), args],
+                    oncomplete : function(r) {
+                        var alerts = openils.Util.readResponse(r);
+                        var msgNode = dojo.byId('uedit-address-alert-message');
+                        var headerRow = dojo.filter(
+                            dojo.query('[name=uedit-addr-divider]'),
+                            function(row) { return row.getAttribute('addr') == addrId })[0]
+
+                        msgNode.innerHTML = '';
+
+                        if (alerts.length) {
+
+                            // show the alert box
+                            openils.Util.hide('uedit-help-div');
+                            openils.Util.hide('uedit-dupe-div');
+                            openils.Util.show('uedit-address-alert');
+
+                            // style the address header row
+                            openils.Util.addCSSClass(headerRow, 'uedit-address-alert-divider');
+
+                            dojo.forEach(alerts,
+                                function(addr) {
+                                    msgNode.innerHTML += addr.alert_message() + '<br/>';
+                                }
+                            );
+
+                        } else { 
+                            openils.Util.hide('uedit-address-alert');
+                            openils.Util.removeCSSClass(headerRow, 'uedit-address-alert-divider');
+                        }
+                    }
+                }
+            );
+        }, 
+        addressAlertTimeout
+    );
+}
+
 function uEditDupeSearch(type, value) {
     if(!value) return;
     var search;
@@ -1360,6 +1448,7 @@ function uEditDupeSearch(type, value) {
                 if(resp && resp.length > 0) {
 
                     openils.Util.hide('uedit-help-div');
+                    openils.Util.hide('uedit-address-alert');
                     openils.Util.show('uedit-dupe-div');
                     var link;
 
@@ -1407,6 +1496,7 @@ function getByName(node, name) {
 
 function ueLoadContextHelp(fmcls, fmfield) {
     openils.Util.hide('uedit-dupe-div');
+    openils.Util.hide('uedit-dupe-div');
     openils.Util.show('uedit-help-div');
     dojo.byId('uedit-help-field').innerHTML = fieldmapper.IDL.fmclasses[fmcls].field_map[fmfield].label;
     dojo.byId('uedit-help-text').innerHTML = fieldDoc[fmcls][fmfield].string();
@@ -1720,15 +1810,22 @@ function uEditNewAddr(evt, id, mkLinks) {
                 // billing address
                 var ba = getByName(row, 'billing_address');
                 ba.id = 'uedit-billing-address-' + id;
-                if(mkLinks || (patron.billing_address() && patron.billing_address().id() == id))
+                if(mkLinks || (patron.billing_address() && patron.billing_address().id() == id)) {
                     ba.checked = true;
+                    prevBillingAddress = id;
+                }
 
                 // mailing address
                 var ma = getByName(row, 'mailing_address');
                 ma.id = 'uedit-mailing-address-' + id;
-                if(mkLinks || (patron.mailing_address() && patron.mailing_address().id() == id))
+                if(mkLinks || (patron.mailing_address() && patron.mailing_address().id() == id)) {
                     ma.checked = true;
+                    prevMailingAddress = id;
+                }
 
+                ba.onclick = function() { console.log('ba.onchange ' + id); uEditAddressAlertMarshal(id, true) };
+                ma.onclick = function() { uEditAddressAlertMarshal(id, false, true) };
+                
                 var btn = dojo.query('[name=delete-button]', row)[0];
                 if(btn) btn.onclick = function(){ uEditDeleteAddr(id) };
             }