Problem:
When value in checkbox is modified twice in a row (so it is at its original value) an 'undo' button is still visible even when it shouldn't be.


Cause:
IPA server sends boolean values as 'TRUE' or 'FALSE' (strings). Checkbox_widget converts them to JavaScript? boolean (true, false). Save method in checkbox_widget is returning array with a boolean. So test_dirty method always evaluates to dirty because 'FALSE' != false.

This patch is fixing the problem.

Note (future enhancements):
As we were talking before about making fields less dependent on widget types. The dependency comes from the fact that dirty evaluation is in field. I plan to move the core to widget. I'm thinking about something like:
Widget would have:
  * input value parser - ie for parsing strings to booleans - configurable
  * original value (parsed)
  * inner state (inner_save())
  * is_dirty() - compare inner state with original value
* output formatter - can make boolean back to strings (just example) - configurable
  * save() would return array of formatted values

Field:
  * load(record) would pick values from record as now
* is_dirty - needed for facets. Would be: dirty = 'one of associated widgets is dirty' * save() - merge associated widgets values - usually only on array from one widget

Maybe input and output formatters should be in field.

https://fedorahosted.org/freeipa/ticket/2494
--
Petr Vobornik
From 2b800fb59a7d04be9a67a5216e20168bec6aae8e Mon Sep 17 00:00:00 2001
From: Petr Vobornik <pvobo...@redhat.com>
Date: Fri, 9 Mar 2012 15:52:05 +0100
Subject: [PATCH] Fixed evaluating checkbox dirty status

Problem:
When value in checkbox is modified twice in a row (so it is at its original value) an 'undo' button is still visible even when it shouldn't be.

Cause:
IPA server sends boolean values as 'TRUE' or 'FALSE' (strings). Checkbox_widget converts them to JavaScript? boolean (true, false). Save method in checkbox_widget is returning array with a boolean. So test_dirty method always evaluates to dirty because 'FALSE' != false.

This patch is fixing the problem.

https://fedorahosted.org/freeipa/ticket/2494
---
 install/ui/field.js             |   24 +++++++++++++++++-------
 install/ui/group.js             |    2 +-
 install/ui/test/widget_tests.js |    4 +++-
 install/ui/widget.js            |   12 +++---------
 4 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/install/ui/field.js b/install/ui/field.js
index 5f073705d665b1b1af72e6e21686af545e4f55ab..162ec81ba96a740a6267d4a5f3241279eb69f131 100644
--- a/install/ui/field.js
+++ b/install/ui/field.js
@@ -487,6 +487,23 @@ IPA.checkbox_field = function(spec) {
     var that = IPA.field(spec);
 
     that.checked = spec.checked || false;
+    that.boolean_formatter = IPA.boolean_formatter();
+
+    that.load = function(record) {
+
+        that.record = record;
+
+        that.values = that.get_value(record, that.param);
+
+        var value = that.boolean_formatter.parse(that.values);
+        if (value === '') value = that.widget.checked; //default value
+
+        that.values = [value];
+
+        that.load_writable(record);
+
+        that.reset();
+    };
 
     that.widgets_created = function() {
 
@@ -510,13 +527,6 @@ IPA.checkboxes_field = function(spec) {
 
     var that = IPA.field(spec);
 
-    that.checkbox_load = that.load;
-/*
-    // a checkbox will always have a value, so it's never required
-    that.is_required = function() {
-        return false;
-    };
-*/
     return that;
 };
 
diff --git a/install/ui/group.js b/install/ui/group.js
index 93b2fe0fff62579fab7bb4f3e8b54c44099f37de..5ad9ba1f1d887c1297573a90c209ace00e3f3a86 100644
--- a/install/ui/group.js
+++ b/install/ui/group.js
@@ -140,7 +140,7 @@ IPA.group_nonposix_checkbox_widget = function (spec) {
 };
 
 IPA.widget_factories['nonposix_checkbox'] = IPA.group_nonposix_checkbox_widget;
-IPA.field_factories['nonposix_checkbox'] = IPA.checkbox_fields;
+IPA.field_factories['nonposix_checkbox'] = IPA.checkbox_field;
 
 IPA.group_adder_dialog = function(spec) {
 
diff --git a/install/ui/test/widget_tests.js b/install/ui/test/widget_tests.js
index 951f943a10d8e560b10a73b658972a29147107d7..489572c2c21077216f65dc47ac84919b85e71176 100644
--- a/install/ui/test/widget_tests.js
+++ b/install/ui/test/widget_tests.js
@@ -223,7 +223,9 @@ test("Testing checkbox widget.", function() {
     spec = {name:'title'};
     base_widget_test('test_value');
 
-    var mock_record = { 'title': 'TRUE' };
+    //Changing mock record from 'TRUE' to true. Value normalization is field's
+    //job. Checkbox should work with booleans values.
+     var mock_record = { 'title': [true] };
 
     widget.update(mock_record.title);
     same(widget.save(),[true], "Checkbox is set");
diff --git a/install/ui/widget.js b/install/ui/widget.js
index f906d165c6202b3e2b54b9299cfcafed9bccb0e4..83f07594a1eea19bcd18e770ebc3bf20c7b9a214 100644
--- a/install/ui/widget.js
+++ b/install/ui/widget.js
@@ -632,20 +632,14 @@ IPA.checkbox_widget = function (spec) {
         var value;
 
         if (values && values.length) {
-            // use loaded value
             value = values[0];
-        } else {
+        }
+
+        if (typeof value !== 'boolean') {
             // use default value
             value = that.checked;
         }
 
-        // convert string into boolean
-        if (value === 'TRUE') {
-            value = true;
-        } else if (value === 'FALSE') {
-            value = false;
-        }
-
         that.input.attr('checked', value);
     };
 
-- 
1.7.7.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to