URL: https://github.com/freeipa/freeipa/pull/713
Author: pvomacka
 Title: #713: WebUI: fix showing required asterisk '*'
Action: opened

PR body:
"""
There was a bug that when user switch between two facets where is
required field and in one of them is writable and in second one
is not writable, then the asterisk which marks required field is
not shown. i.e. admin vs. user details page or global_passwd_policy
vs. other_passwd_policy details page.

That was caused by incorrect evaluation of required state of field.
Evaluation works that way: evaluate old required state, then evaluate
current required state and if states has changed then emit change event.
The evaluation depends on writable and read_only state of field.
Those two states are set before evaluation of required state, but
their old values (for evaluating previous required stated) were
not stored anywhere.

This commit adds two attributes which stores old writable
and read_only states. The required asterisk is then shown correctly.

https://pagure.io/freeipa/issue/6849
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/713/head:pr713
git checkout pr713
From dabf168e700efbe86c7c3788f4f8962852f183e3 Mon Sep 17 00:00:00 2001
From: Pavel Vomacka <pvoma...@redhat.com>
Date: Thu, 13 Apr 2017 17:15:16 +0200
Subject: [PATCH] WebUI: fix showing required asterisk '*'

There was a bug that when user switch between two facets where is
required field and in one of them is writable and in second one
is not writable, then the asterisk which marks required field is
not shown. i.e. admin vs. user details page or global_passwd_policy
vs. other_passwd_policy details page.

That was caused by incorrect evaluation of required state of field.
Evaluation works that way: evaluate old required state, then evaluate
current required state and if states has changed then emit change event.
The evaluation depends on writable and read_only state of field.
Those two states are set before evaluation of required state, but
their old values (for evaluating previous required stated) were
not stored anywhere.

This commit adds two attributes which stores old writable
and read_only states. The required asterisk is then shown correctly.

https://pagure.io/freeipa/issue/6849
---
 install/ui/src/freeipa/field.js | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/install/ui/src/freeipa/field.js b/install/ui/src/freeipa/field.js
index 76ce253..f9836e2 100644
--- a/install/ui/src/freeipa/field.js
+++ b/install/ui/src/freeipa/field.js
@@ -221,6 +221,13 @@ field.field = IPA.field = function(spec) {
     that.read_only = spec.read_only;
 
     /**
+     * Attribute for storing previous value of read_only attribute.
+     * It is set during changing read_only attribute.
+     * @property {boolean}
+     */
+    that.old_read_only = spec.read_only;
+
+    /**
      * Writable is set during load
      * @readonly
      * @property {boolean}
@@ -228,6 +235,13 @@ field.field = IPA.field = function(spec) {
     that.writable = true;
 
     /**
+     * Attribute for storing previous value of writable attribute.
+     * It is set during changing writable attribute.
+     * @property {boolean}
+     */
+    that.old_writable = true;
+
+    /**
      * Enabled
      * @readonly
      * @property {boolean}
@@ -352,9 +366,14 @@ field.field = IPA.field = function(spec) {
      * Evaluate if field has to have some value
      * @return {boolean}
      */
-    that.is_required = function() {
-        if (that.read_only) return false;
-        if (!that.writable) return false;
+    that.is_required = function(old) {
+        if (old) {
+            if (that.old_read_only) return false;
+            if (!that.old_writable) return false;
+        } else {
+            if (that.read_only) return false;
+            if (!that.writable) return false;
+        }
 
         if (that.required !== undefined) return that.required;
         return that.metadata && that.metadata.required;
@@ -369,9 +388,9 @@ field.field = IPA.field = function(spec) {
      * @param {boolean} required
      */
     that.set_required = function(required) {
-        var old = that.is_required();
+        var old = that.is_required(true);
         that.required = required;
-        var current = that.is_required();
+        var current = that.is_required(false);
 
         if (current !== old) {
             that.emit('require-change', { source: that, required: current });
@@ -570,9 +589,9 @@ field.field = IPA.field = function(spec) {
      */
     that.set_writable = function(writable) {
 
-        var old = !!that.writable;
+        that.old_writable = !!that.writable;
         that.writable = writable;
-        if (old !== writable) {
+        if (that.old_writable !== writable) {
             that.emit('writable-change', { source: that, writable: writable });
         }
 
@@ -586,11 +605,12 @@ field.field = IPA.field = function(spec) {
      */
     that.set_read_only = function(read_only) {
 
-        var old = !!that.read_only;
+        that.old_read_only = !!that.read_only;
         that.read_only = read_only;
-        if (old !== read_only) {
+        if (that.old_read_only !== read_only) {
             that.emit('readonly-change', { source: that, readonly: read_only });
         }
+
         that.set_required(that.required); // force update of required
     };
 
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to