The Dirty dialogs have been combined into IPA.dirty_dialog. It
provides the Update and Reset buttons with customizable callback.

Previously the widget's dirty status is computed by comparing the
old values with the new values. This method is sometimes inaccurate,
so the is_dirty() method has been modified to simply return a flag
which is set to true if the widget is changed.

Ticket #896.

--
Endi S. Dewata
From 826122a193737641e24b6bcd1ff2ba3e21353aba Mon Sep 17 00:00:00 2001
From: Endi S. Dewata <edew...@redhat.com>
Date: Thu, 26 May 2011 17:57:39 -0500
Subject: [PATCH] Added Update and Reset buttons into Dirty dialog.

The Dirty dialogs have been combined into IPA.dirty_dialog. It
provides the Update and Reset buttons with customizable callback.

Previously the widget's dirty status is computed by comparing the
old values with the new values. This method is sometimes inaccurate,
so the is_dirty() method has been modified to simply return a flag
which is set to true if the widget is changed.

Ticket #896.
---
 install/ui/aci.js               |    7 +-
 install/ui/associate.js         |   41 ++++++------
 install/ui/details.js           |    8 +--
 install/ui/dns.js               |    6 --
 install/ui/entity.js            |    5 +-
 install/ui/hbac.js              |    5 +-
 install/ui/ipa.js               |   68 +++++++++++---------
 install/ui/navigation.js        |   22 ++++++-
 install/ui/sudo.js              |    3 +-
 install/ui/test/widget_tests.js |    9 ++-
 install/ui/widget.js            |  134 +++++++++++++++------------------------
 11 files changed, 142 insertions(+), 166 deletions(-)

diff --git a/install/ui/aci.js b/install/ui/aci.js
index 336a965fc5d236c91802e9abae019d0b5eea6c1b..d507e2d07b85809d1e45bddbbe6cc3f59faffd02 100644
--- a/install/ui/aci.js
+++ b/install/ui/aci.js
@@ -233,7 +233,7 @@ IPA.attributes_widget = function(spec) {
                 click: function(){
                     $('.aci-attribute', that.table).
                         attr('checked', $(this).attr('checked'));
-                    that.show_undo();
+                    that.set_dirty(true);
                 }
             })
         })).append($('<th/>', {
@@ -245,7 +245,6 @@ IPA.attributes_widget = function(spec) {
             that.create_undo(container);
             that.get_undo().click(function(){
                 that.reset();
-                that.hide_undo();
             });
         }
 
@@ -298,7 +297,7 @@ IPA.attributes_widget = function(spec) {
                 value: value,
                 'class': 'aci-attribute',
                 click: function() {
-                    that.show_undo();
+                    that.set_dirty(true);
                 }
             }));
             td =  $('<td/>').appendTo(aci_tr);
@@ -335,7 +334,7 @@ IPA.attributes_widget = function(spec) {
                     value: value,
                     'class': 'aci-attribute',
                     change: function() {
-                        that.show_undo();
+                        that.set_dirty(true);
                     }
                 }));
 
diff --git a/install/ui/associate.js b/install/ui/associate.js
index 3ba510f10caf502cfa3323137b6a4eba27afbc72..5eb84260eee57ef556db13cf4e04eeb9c430f52a 100644
--- a/install/ui/associate.js
+++ b/install/ui/associate.js
@@ -348,7 +348,6 @@ IPA.association_table_widget = function (spec) {
 
     that.create = function(container) {
 
-        var entity = IPA.get_entity(that.entity_name);
         var column;
 
         // create a column if none defined
@@ -395,21 +394,6 @@ IPA.association_table_widget = function (spec) {
 
         that.table_setup(container);
 
-        var dialog = IPA.dialog({
-            title: IPA.messages.dialogs.dirty_title,
-            width: '20em'
-        });
-
-        dialog.create = function() {
-            dialog.container.append(IPA.messages.dialogs.dirty_message);
-        };
-
-        dialog.add_button(IPA.messages.buttons.ok, function() {
-            dialog.close();
-        });
-
-        dialog.init();
-
         var entity = IPA.get_entity(that.entity_name);
         var facet_name = IPA.current_facet(entity);
         var facet = entity.get_facet(facet_name);
@@ -424,7 +408,17 @@ IPA.association_table_widget = function (spec) {
                 }
 
                 if (facet.is_dirty()) {
+                    var dialog = IPA.dirty_dialog({
+                        facet: facet
+                    });
+
+                    dialog.callback = function() {
+                        that.show_remove_dialog();
+                    };
+
+                    dialog.init();
                     dialog.open(that.container);
+
                 } else {
                     that.show_remove_dialog();
                 }
@@ -443,7 +437,17 @@ IPA.association_table_widget = function (spec) {
                 }
 
                 if (facet.is_dirty()) {
+                    var dialog = IPA.dirty_dialog({
+                        facet: facet
+                    });
+
+                    dialog.callback = function() {
+                        that.show_add_dialog();
+                    };
+
+                    dialog.init();
                     dialog.open(that.container);
+
                 } else {
                     that.show_add_dialog();
                 }
@@ -809,11 +813,6 @@ IPA.association_facet = function (spec) {
         that.table.init();
     };
 
-    that.is_dirty = function() {
-        var pkey = $.bbq.getState(that.entity_name+'-pkey');
-        return pkey != that.pkey;
-    };
-
     that.create_header = function(container) {
 
         that.facet_create_header(container);
diff --git a/install/ui/details.js b/install/ui/details.js
index 4af837e8ec5f78e6bce33885813abe884559f463..4aa864fed7d572a34fc254d205b32700379966e5 100644
--- a/install/ui/details.js
+++ b/install/ui/details.js
@@ -496,14 +496,11 @@ IPA.details_facet = function(spec) {
 
 
     that.is_dirty = function() {
-
-        var i;
-        for ( i =0; i <   that.sections.length; i +=1 ){
-            if (that.sections[i].is_dirty()){
+        for (var i=0; i<that.sections.length; i++) {
+            if (that.sections[i].is_dirty()) {
                 return true;
             }
         }
-
         return false;
     };
 
@@ -558,7 +555,6 @@ IPA.details_facet = function(spec) {
             for (var j=0; j<section_fields.length; j++) {
                 var field = section_fields[j];
 
-                var span = $('span[name='+field.name+']', section.container).first();
                 values = field.save();
                 if (!values) continue;
 
diff --git a/install/ui/dns.js b/install/ui/dns.js
index 1c0ff6eeff457855dc8ec751f1f5981168da13cd..ff429452480120810f83b9ef098ccceab25ba388 100644
--- a/install/ui/dns.js
+++ b/install/ui/dns.js
@@ -329,12 +329,6 @@ IPA.records_facet = function(spec) {
         dialog.open(that.container);
     };
 
-    that.is_dirty = function() {
-        var pkey = $.bbq.getState(that.entity_name+'-pkey');
-        var record = $.bbq.getState(that.entity_name+'-record');
-        return pkey != that.pkey || record != that.record;
-    };
-
     that.create_header = function(container) {
 
         that.facet_create_header(container);
diff --git a/install/ui/entity.js b/install/ui/entity.js
index c855da1b5863dc9335de483650442c4f2af835e0..4b6ce1d0ebfefacb501210a13942166b62315793 100644
--- a/install/ui/entity.js
+++ b/install/ui/entity.js
@@ -122,7 +122,7 @@ IPA.facet = function (spec) {
     that.load = function() {
     };
 
-    that.is_dirty = function (){
+    that.is_dirty = function() {
         return false;
     };
 
@@ -536,10 +536,7 @@ IPA.entity_header = function(spec) {
                 }
 
                 var pkey = $.bbq.getState(that.entity.name+'-pkey');
-
                 IPA.nav.show_page(that.entity.name, other_facet.name, pkey);
-                $('a', that.facet_tabs).removeClass('selected');
-                $('a', li).addClass('selected');
 
                 return false;
             }
diff --git a/install/ui/hbac.js b/install/ui/hbac.js
index aa11879b8321438ab36ca9342b3e97ad9aa76cf7..4d46a18f53c5ed821a776deceb2e5a1346394b73 100644
--- a/install/ui/hbac.js
+++ b/install/ui/hbac.js
@@ -438,7 +438,6 @@ IPA.hbacrule_details_facet = function (spec) {
             for (var j=0; j<section_fields.length; j++) {
                 var field = section_fields[j];
 
-                var span = $('span[name='+field.name+']', section.container).first();
                 var values = field.save();
                 if (!values) continue;
 
@@ -461,7 +460,7 @@ IPA.hbacrule_details_facet = function (spec) {
                 }
 
                 // skip unchanged field
-                if (!field.is_dirty(span)) continue;
+                if (!field.is_dirty()) continue;
 
                 // check enable/disable
                 if (field.name == 'ipaenabledflag') {
@@ -795,7 +794,7 @@ IPA.hbacrule_accesstime_widget = function (spec) {
 
         var input = $('input[name="'+that.name+'"]', that.container);
         input.change(function() {
-            that.show_undo();
+            that.set_dirty(true);
         });
 
         var undo = that.get_undo();
diff --git a/install/ui/ipa.js b/install/ui/ipa.js
index 0266e3499e1e4f7329fc344cd48b9532a585ef93..7fd784b5426b6aa2dadff245536ca88168b365a1 100644
--- a/install/ui/ipa.js
+++ b/install/ui/ipa.js
@@ -155,37 +155,6 @@ var IPA = ( function () {
         that.entities.remove(name);
     };
 
-    that.test_dirty = function(){
-        if (IPA.current_entity){
-            var facet_name =   IPA.current_facet(IPA.current_entity);
-            var facet = IPA.current_entity.get_facet(facet_name);
-            if (!facet) return false;
-
-            if (facet.is_dirty()){
-
-                var dialog = IPA.dialog({
-                    title: IPA.messages.dialogs.dirty_title,
-                    width: '20em'
-                });
-
-                dialog.create = function() {
-                    dialog.container.append(IPA.messages.dialogs.dirty_message);
-                };
-
-                dialog.add_button(IPA.messages.buttons.ok, function() {
-                    dialog.close();
-                });
-
-                dialog.init();
-
-                dialog.open($('#navigation'));
-
-                return false;
-            }
-        }
-        return true;
-    };
-
     that.display_activity_icon = function() {
         that.network_call_count++;
         $('.network-activity-indicator').css('visibility', 'visible');
@@ -578,3 +547,40 @@ IPA.create_network_spinner = function(){
         'class':'network-activity-indicator',
         html: '<img src="spinner_small.gif" />'});
 };
+
+IPA.dirty_dialog = function(spec) {
+
+    spec = spec || {};
+    spec.title = spec.title || IPA.messages.dialogs.dirty_title;
+    spec.width = spec.width || '25em';
+
+    var that = IPA.dialog(spec);
+    that.facet = spec.facet;
+    that.message = spec.message || IPA.messages.dialogs.dirty_message;
+
+    that.create = function() {
+        that.container.append(that.message);
+    };
+
+    that.add_button(IPA.messages.buttons.update, function() {
+        that.facet.update(function() {
+            that.close();
+            that.callback();
+        });
+    });
+
+    that.add_button(IPA.messages.buttons.reset, function() {
+        that.facet.reset();
+        that.close();
+        that.callback();
+    });
+
+    that.add_button(IPA.messages.buttons.cancel, function() {
+        that.close();
+    });
+
+    that.callback = function() {
+    };
+
+    return that;
+};
\ No newline at end of file
diff --git a/install/ui/navigation.js b/install/ui/navigation.js
index 379573910d262328cdbe921e59b5a013ed7369f8..11520ff91de5bf0e26183b2006a4c356f8a2a2ab 100644
--- a/install/ui/navigation.js
+++ b/install/ui/navigation.js
@@ -80,9 +80,27 @@ IPA.navigation = function(spec) {
     };
 
     that.push_state = function(params) {
-        if (!IPA.test_dirty()) {
-            return false;
+
+        if (IPA.current_entity) {
+            var facet_name = IPA.current_facet(IPA.current_entity);
+            var facet = IPA.current_entity.get_facet(facet_name);
+
+            if (facet.is_dirty()) {
+                var dialog = IPA.dirty_dialog({
+                    facet: facet
+                });
+
+                dialog.callback = function() {
+                    $.bbq.pushState(params);
+                };
+
+                dialog.init();
+                dialog.open($('#navigation'));
+
+                return false;
+            }
         }
+
         $.bbq.pushState(params);
         return true;
     };
diff --git a/install/ui/sudo.js b/install/ui/sudo.js
index 7bd6f4500c6593c08d14878d1d7193ee4c318073..89b7101bd04d10ea837752946064edb94cee2647 100644
--- a/install/ui/sudo.js
+++ b/install/ui/sudo.js
@@ -454,7 +454,6 @@ IPA.sudorule_details_facet = function (spec) {
             for (var j=0; j<section_fields.length; j++) {
                 var field = section_fields[j];
 
-                var span = $('span[name='+field.name+']', section.container).first();
                 var values = field.save();
                 if (!values) continue;
 
@@ -477,7 +476,7 @@ IPA.sudorule_details_facet = function (spec) {
                 }
 
                 // skip unchanged field
-                if (!field.is_dirty(span)) continue;
+                if (!field.is_dirty()) continue;
 
                 // check enable/disable
                 if (field.name == 'ipaenabledflag') {
diff --git a/install/ui/test/widget_tests.js b/install/ui/test/widget_tests.js
index f4281e38fa173de50fe5390c0f476781a2868377..f323f969718521c0de9928561b1479ec9ef09c0f 100644
--- a/install/ui/test/widget_tests.js
+++ b/install/ui/test/widget_tests.js
@@ -185,13 +185,14 @@ test("IPA.table_widget" ,function(){
 test("Testing base widget.", function() {
     var update_called = false;
     var spec = {
-        name:'title',
-        update:function(){
-            update_called = true;
-        }
+        name:'title'
     };
 
     var widget = IPA.widget(spec);
+    widget.update = function() {
+        update_called = true;
+    };
+
     base_widget_test(widget,'user','test_value');
     widget_string_test(widget);
     ok (update_called, 'Update called');
diff --git a/install/ui/widget.js b/install/ui/widget.js
index 6b211d6f46701ef4e3ddf47327d181e2618568a9..66dedbdfe12ea223aa42f5db8e9b35a24b7752dc 100644
--- a/install/ui/widget.js
+++ b/install/ui/widget.js
@@ -51,17 +51,11 @@ IPA.widget = function(spec) {
     that.undo = typeof spec.undo == 'undefined' ? true : spec.undo;
     that.join = spec.join;
 
-    that.init = spec.init || init;
-    that.create = spec.create || create;
-    that.setup = spec.setup || setup;
-    that.load = spec.load || load;
-    that.save = spec.save || save;
-    that.update = spec.update || update;
-
     that.param_info = spec.param_info;
     that.metadata = spec.metadata;
 
     that.values = [];
+    that.dirty = false;
     that.valid = true;
 
     that.__defineGetter__("entity_name", function(){
@@ -128,7 +122,7 @@ IPA.widget = function(spec) {
         }
     };
 
-    function init() {
+    that.init = function() {
         if (that.entity_name) {
             that.param_info = IPA.get_entity_param(that.entity_name, that.name);
 
@@ -143,20 +137,20 @@ IPA.widget = function(spec) {
                 }
             }
         }
-    }
+    };
 
-    function create(container) {
-    }
+    that.create = function(container) {
+    };
 
-    function setup(container) {
+    that.setup = function(container) {
         that.container = container;
-    }
+    };
 
     /**
      * This function stores the entire record and the values
      * of the field, then invoke reset() to update the UI.
      */
-    function load(record) {
+    that.load = function(record) {
         that.record = record;
 
         var value = record[that.name];
@@ -186,24 +180,24 @@ IPA.widget = function(spec) {
         }
 
         that.reset();
-    }
+    };
 
     that.reset = function() {
-        that.hide_undo();
+        that.set_dirty(false);
         that.update();
     };
 
-    function update() {
-    }
+    that.update = function() {
+    };
 
     /**
      * This function saves the values entered in the UI.
      * It returns the values in an array, or null if
      * the field should not be saved.
      */
-    function save() {
+    that.save = function() {
         return that.values;
-    }
+    };
 
     /**
      * This function compares the original values and the
@@ -211,45 +205,7 @@ IPA.widget = function(spec) {
      * it will return true.
      */
     that.is_dirty = function() {
-
-        if (that.read_only) {
-            return false;
-        }
-
-        var values = that.save();
-
-        if (!values) { // ignore null values
-            return false;
-        }
-
-        if (!that.values) {
-
-            if (values instanceof Array) {
-
-                if ((values.length === 0) ||
-                    (values.length === 1) &&
-                    (values[0] === '')) {
-                    return false;
-                }
-            }
-
-            return true;
-        }
-
-        if (values.length != that.values.length) {
-            return true;
-        }
-
-        values.sort();
-        that.values.sort();
-
-        for (var i=0; i<values.length; i++) {
-            if (values[i] != that.values[i]) {
-                return true;
-            }
-        }
-
-        return false;
+        return that.dirty;
     };
 
     that.create_undo = function(container) {
@@ -262,6 +218,17 @@ IPA.widget = function(spec) {
             }).appendTo(container);
     };
 
+    that.set_dirty = function(dirty) {
+        that.dirty = dirty;
+        if (that.undo) {
+            if (dirty) {
+                that.show_undo();
+            } else {
+                that.hide_undo();
+            }
+        }
+    };
+
     that.get_undo = function() {
         return $(that.undo_span);
     };
@@ -302,6 +269,7 @@ IPA.widget = function(spec) {
     that.widget_load = that.load;
     that.widget_reset = that.reset;
     that.widget_save = that.save;
+    that.widget_set_dirty = that.set_dirty;
 
     return that;
 };
@@ -352,9 +320,7 @@ IPA.text_widget = function(spec) {
 
         var input = $('input[name="'+that.name+'"]', that.container);
         input.keyup(function() {
-            if (that.undo) {
-                that.show_undo();
-            }
+            that.set_dirty(true);
             that.validate();
         });
 
@@ -418,6 +384,17 @@ IPA.multivalued_text_widget = function(spec) {
         }
     };
 
+    that.set_dirty = function(dirty, index) {
+        that.widget_set_dirty(dirty);
+        if (that.undo) {
+            if (dirty) {
+                that.show_undo(index);
+            } else {
+                that.hide_undo(index);
+            }
+        }
+    };
+
     that.show_undo = function(index) {
         var undo = that.get_undo(index);
         undo.css('display', 'inline');
@@ -550,9 +527,8 @@ IPA.multivalued_text_widget = function(spec) {
             var index = that.row_index(row);
             if (index >= that.values.length) {
                 // show undo/remove link for new value
+                that.set_dirty(true, index);
                 if (that.undo) {
-                    that.show_undo(index);
-                    that.show_undo();
                     remove_link.css('display', 'none');
                 } else {
                     remove_link.css('display', 'inline');
@@ -563,9 +539,8 @@ IPA.multivalued_text_widget = function(spec) {
                 var index = that.row_index(row);
                 // uncross removed value
                 input.removeClass('strikethrough');
+                that.set_dirty(true, index);
                 if (that.undo) {
-                    that.show_undo(index);
-                    that.show_undo();
                     if (index < that.values.length) {
                         remove_link.css('display', 'inline');
                     }
@@ -579,10 +554,7 @@ IPA.multivalued_text_widget = function(spec) {
                     // restore old value then cross it out
                     that.update(index);
                     input.addClass('strikethrough');
-                    if (that.undo) {
-                        that.show_undo(index);
-                        that.show_undo();
-                    }
+                    that.set_dirty(true, index);
                     remove_link.css('display', 'none');
                 } else {
                     // remove new value
@@ -623,7 +595,7 @@ IPA.multivalued_text_widget = function(spec) {
     };
 
     that.reset = function(index) {
-        that.hide_undo(index);
+        that.set_dirty(false, index);
         that.update(index);
     };
 
@@ -685,7 +657,7 @@ IPA.checkbox_widget = function (spec) {
 
         var input = $('input[name="'+that.name+'"]', that.container);
         input.change(function() {
-            that.show_undo();
+            that.set_dirty(true);
         });
 
         var undo = that.get_undo();
@@ -759,7 +731,7 @@ IPA.checkboxes_widget = function (spec) {
 
         var input = $('input[name="'+that.name+'"]', that.container);
         input.change(function() {
-            that.show_undo();
+            that.set_dirty(true);
         });
 
         var undo = that.get_undo();
@@ -837,7 +809,7 @@ IPA.radio_widget = function(spec) {
 
         var input = $('input[name="'+that.name+'"]', that.container);
         input.change(function() {
-            that.show_undo();
+            that.set_dirty(true);
         });
 
         var undo = that.get_undo();
@@ -913,7 +885,7 @@ IPA.select_widget = function(spec) {
 
         that.select = $('select[name="'+that.name+'"]', that.container);
         that.select.change(function() {
-            that.show_undo();
+            that.set_dirty(true);
         });
 
         var undo = that.get_undo();
@@ -994,7 +966,7 @@ IPA.textarea_widget = function (spec) {
 
         var input = $('textarea[name="'+that.name+'"]', that.container);
         input.keyup(function() {
-            that.show_undo();
+            that.set_dirty(true);
             that.validate();
 
         });
@@ -1439,7 +1411,7 @@ IPA.entity_select_widget = function(spec) {
         that.entity_select = $('<select/>', {
             id: that.name + '-entity-select',
             change: function(){
-                that.show_undo();
+                that.set_dirty(true);
             }
         }).appendTo(container);
 
@@ -1450,7 +1422,7 @@ IPA.entity_select_widget = function(spec) {
             style: 'display: none;',
             keyup: function(){
                 populate_select();
-                that.show_undo();
+                that.set_dirty(true);
             }
         }).appendTo(container);
 
@@ -1477,14 +1449,10 @@ IPA.entity_select_widget = function(spec) {
 
     that.reset = function() {
         that.entity_filter.val(that.values[0]);
-        that.hide_undo();
+        that.set_dirty(false);
         populate_select(that.values[0]);
     };
 
-    that.is_dirty = function() {
-        return (that.save()[0] !== that.values[0]);
-    };
-
     that.load = function(record) {
         var value = record[that.name];
         if (value instanceof Array) {
-- 
1.7.4

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

Reply via email to