KMAIN-1827 guardian required set onload
authorBill Erickson <berickxx@gmail.com>
Tue, 20 Oct 2015 16:02:39 +0000 (12:02 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Ensure that the parent/guardian (ident_value2) field gets the correct
value for 'required' on patron load to ensure it's not required for
non-juvenile patrons.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/web/js/ui/default/actor/user/register.js

index 4f52387..c575f23 100644 (file)
@@ -1299,6 +1299,15 @@ function fleshFMRow(row, fmcls, args) {
                     w.attr('disabled', true);
                 }
             }
+            // call the dobLoaded function only after both dob and
+            // guardian widgets have been created.  And then only
+            // if this is an existing patron.
+            if (!patron.isnew()) {
+                if (fmfield == 'ident_value2' || 
+                   (fmfield == 'dob' && findWidget('au', 'ident_value2'))) { 
+                    dobLoaded(ww) ;
+                }
+            }
         }
     );
 
@@ -1429,6 +1438,30 @@ function usePhonePw(newVal) {
     }
 }
 
+function dobIsJuvenile(newDob) {
+    var juvInterval = 
+        orgSettings['global.juvenile_age_threshold'] || '18 years';
+
+    var base = new Date();
+    base.setTime(base.getTime() - 
+        Number(openils.Util.intervalToSeconds(juvInterval) + '000'));
+
+    return newDob > base;
+}
+
+// onload handler for DoB widget.  Sets or clears the 'required' 
+// flag on the parent/guardian (ident_value2) field based on
+// the user's existing DoB value.
+// Param "ww" will contain the ident_value2 widget if it's not 
+// yet globally available (i.e. it's currently being built);
+function dobLoaded(ww) {
+    if (!patron.dob()) return;
+    var guardian = findWidget('au', 'ident_value2') || ww;
+    var newDob = dojo.date.stamp.fromISOString(patron.dob());
+    guardian.widget.attr('required', dobIsJuvenile(newDob));
+}
+
+
 function attachWidgetEvents(fmcls, fmfield, widget) {
 
     dojo.connect(
@@ -1620,20 +1653,14 @@ function attachWidgetEvents(fmcls, fmfield, widget) {
                 dojo.connect(widget.widget, 'onChange',
                     function(newDob) {
                         if(!newDob) return;
+
                         var oldDob = patron.dob();
                         if(dojo.date.stamp.fromISOString(oldDob) == newDob) return;
 
-                        var juvInterval = orgSettings['global.juvenile_age_threshold'] || '18 years';
-                        var base = new Date();
-                        base.setTime(base.getTime() - Number(openils.Util.intervalToSeconds(juvInterval) + '000'));
+                        var attrVal = dobIsJuvenile(newDob);
 
-                        if(newDob <= base) { // older than global.juvenile_age_threshold
-                            findWidget('au', 'juvenile').widget.attr('value', false);
-                            findWidget('au', 'ident_value2').widget.attr('required', false);
-                        } else {
-                            findWidget('au', 'juvenile').widget.attr('value', true);
-                            findWidget('au', 'ident_value2').widget.attr('required', true);
-                        }
+                        findWidget('au', 'juvenile').widget.attr('value', attrVal);
+                        findWidget('au', 'ident_value2').widget.attr('required', attrVal);
                     }
                 );
                 return;