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.
This commit is contained in:
Endi S. Dewata 2011-05-26 17:57:39 -05:00 committed by Adam Young
parent 17c3f9e84e
commit aa29a8a769
11 changed files with 142 additions and 166 deletions

View File

@ -233,7 +233,7 @@ IPA.attributes_widget = function(spec) {
click: function(){ click: function(){
$('.aci-attribute', that.table). $('.aci-attribute', that.table).
attr('checked', $(this).attr('checked')); attr('checked', $(this).attr('checked'));
that.show_undo(); that.set_dirty(true);
} }
}) })
})).append($('<th/>', { })).append($('<th/>', {
@ -245,7 +245,6 @@ IPA.attributes_widget = function(spec) {
that.create_undo(container); that.create_undo(container);
that.get_undo().click(function(){ that.get_undo().click(function(){
that.reset(); that.reset();
that.hide_undo();
}); });
} }
@ -298,7 +297,7 @@ IPA.attributes_widget = function(spec) {
value: value, value: value,
'class': 'aci-attribute', 'class': 'aci-attribute',
click: function() { click: function() {
that.show_undo(); that.set_dirty(true);
} }
})); }));
td = $('<td/>').appendTo(aci_tr); td = $('<td/>').appendTo(aci_tr);
@ -335,7 +334,7 @@ IPA.attributes_widget = function(spec) {
value: value, value: value,
'class': 'aci-attribute', 'class': 'aci-attribute',
change: function() { change: function() {
that.show_undo(); that.set_dirty(true);
} }
})); }));

View File

@ -348,7 +348,6 @@ IPA.association_table_widget = function (spec) {
that.create = function(container) { that.create = function(container) {
var entity = IPA.get_entity(that.entity_name);
var column; var column;
// create a column if none defined // create a column if none defined
@ -395,21 +394,6 @@ IPA.association_table_widget = function (spec) {
that.table_setup(container); 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 entity = IPA.get_entity(that.entity_name);
var facet_name = IPA.current_facet(entity); var facet_name = IPA.current_facet(entity);
var facet = entity.get_facet(facet_name); var facet = entity.get_facet(facet_name);
@ -424,7 +408,17 @@ IPA.association_table_widget = function (spec) {
} }
if (facet.is_dirty()) { if (facet.is_dirty()) {
var dialog = IPA.dirty_dialog({
facet: facet
});
dialog.callback = function() {
that.show_remove_dialog();
};
dialog.init();
dialog.open(that.container); dialog.open(that.container);
} else { } else {
that.show_remove_dialog(); that.show_remove_dialog();
} }
@ -443,7 +437,17 @@ IPA.association_table_widget = function (spec) {
} }
if (facet.is_dirty()) { if (facet.is_dirty()) {
var dialog = IPA.dirty_dialog({
facet: facet
});
dialog.callback = function() {
that.show_add_dialog();
};
dialog.init();
dialog.open(that.container); dialog.open(that.container);
} else { } else {
that.show_add_dialog(); that.show_add_dialog();
} }
@ -809,11 +813,6 @@ IPA.association_facet = function (spec) {
that.table.init(); 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.create_header = function(container) {
that.facet_create_header(container); that.facet_create_header(container);

View File

@ -496,14 +496,11 @@ IPA.details_facet = function(spec) {
that.is_dirty = function() { that.is_dirty = function() {
for (var i=0; i<that.sections.length; i++) {
var i; if (that.sections[i].is_dirty()) {
for ( i =0; i < that.sections.length; i +=1 ){
if (that.sections[i].is_dirty()){
return true; return true;
} }
} }
return false; return false;
}; };
@ -558,7 +555,6 @@ IPA.details_facet = function(spec) {
for (var j=0; j<section_fields.length; j++) { for (var j=0; j<section_fields.length; j++) {
var field = section_fields[j]; var field = section_fields[j];
var span = $('span[name='+field.name+']', section.container).first();
values = field.save(); values = field.save();
if (!values) continue; if (!values) continue;

View File

@ -329,12 +329,6 @@ IPA.records_facet = function(spec) {
dialog.open(that.container); 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.create_header = function(container) {
that.facet_create_header(container); that.facet_create_header(container);

View File

@ -122,7 +122,7 @@ IPA.facet = function (spec) {
that.load = function() { that.load = function() {
}; };
that.is_dirty = function (){ that.is_dirty = function() {
return false; return false;
}; };
@ -536,10 +536,7 @@ IPA.entity_header = function(spec) {
} }
var pkey = $.bbq.getState(that.entity.name+'-pkey'); var pkey = $.bbq.getState(that.entity.name+'-pkey');
IPA.nav.show_page(that.entity.name, other_facet.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; return false;
} }

View File

@ -438,7 +438,6 @@ IPA.hbacrule_details_facet = function (spec) {
for (var j=0; j<section_fields.length; j++) { for (var j=0; j<section_fields.length; j++) {
var field = section_fields[j]; var field = section_fields[j];
var span = $('span[name='+field.name+']', section.container).first();
var values = field.save(); var values = field.save();
if (!values) continue; if (!values) continue;
@ -461,7 +460,7 @@ IPA.hbacrule_details_facet = function (spec) {
} }
// skip unchanged field // skip unchanged field
if (!field.is_dirty(span)) continue; if (!field.is_dirty()) continue;
// check enable/disable // check enable/disable
if (field.name == 'ipaenabledflag') { if (field.name == 'ipaenabledflag') {
@ -795,7 +794,7 @@ IPA.hbacrule_accesstime_widget = function (spec) {
var input = $('input[name="'+that.name+'"]', that.container); var input = $('input[name="'+that.name+'"]', that.container);
input.change(function() { input.change(function() {
that.show_undo(); that.set_dirty(true);
}); });
var undo = that.get_undo(); var undo = that.get_undo();

View File

@ -155,37 +155,6 @@ var IPA = ( function () {
that.entities.remove(name); 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.display_activity_icon = function() {
that.network_call_count++; that.network_call_count++;
$('.network-activity-indicator').css('visibility', 'visible'); $('.network-activity-indicator').css('visibility', 'visible');
@ -578,3 +547,40 @@ IPA.create_network_spinner = function(){
'class':'network-activity-indicator', 'class':'network-activity-indicator',
html: '<img src="spinner_small.gif" />'}); 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;
};

View File

@ -80,9 +80,27 @@ IPA.navigation = function(spec) {
}; };
that.push_state = function(params) { 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); $.bbq.pushState(params);
return true; return true;
}; };

View File

@ -454,7 +454,6 @@ IPA.sudorule_details_facet = function (spec) {
for (var j=0; j<section_fields.length; j++) { for (var j=0; j<section_fields.length; j++) {
var field = section_fields[j]; var field = section_fields[j];
var span = $('span[name='+field.name+']', section.container).first();
var values = field.save(); var values = field.save();
if (!values) continue; if (!values) continue;
@ -477,7 +476,7 @@ IPA.sudorule_details_facet = function (spec) {
} }
// skip unchanged field // skip unchanged field
if (!field.is_dirty(span)) continue; if (!field.is_dirty()) continue;
// check enable/disable // check enable/disable
if (field.name == 'ipaenabledflag') { if (field.name == 'ipaenabledflag') {

View File

@ -185,13 +185,14 @@ test("IPA.table_widget" ,function(){
test("Testing base widget.", function() { test("Testing base widget.", function() {
var update_called = false; var update_called = false;
var spec = { var spec = {
name:'title', name:'title'
update:function(){
update_called = true;
}
}; };
var widget = IPA.widget(spec); var widget = IPA.widget(spec);
widget.update = function() {
update_called = true;
};
base_widget_test(widget,'user','test_value'); base_widget_test(widget,'user','test_value');
widget_string_test(widget); widget_string_test(widget);
ok (update_called, 'Update called'); ok (update_called, 'Update called');

View File

@ -51,17 +51,11 @@ IPA.widget = function(spec) {
that.undo = typeof spec.undo == 'undefined' ? true : spec.undo; that.undo = typeof spec.undo == 'undefined' ? true : spec.undo;
that.join = spec.join; 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.param_info = spec.param_info;
that.metadata = spec.metadata; that.metadata = spec.metadata;
that.values = []; that.values = [];
that.dirty = false;
that.valid = true; that.valid = true;
that.__defineGetter__("entity_name", function(){ that.__defineGetter__("entity_name", function(){
@ -128,7 +122,7 @@ IPA.widget = function(spec) {
} }
}; };
function init() { that.init = function() {
if (that.entity_name) { if (that.entity_name) {
that.param_info = IPA.get_entity_param(that.entity_name, that.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; that.container = container;
} };
/** /**
* This function stores the entire record and the values * This function stores the entire record and the values
* of the field, then invoke reset() to update the UI. * of the field, then invoke reset() to update the UI.
*/ */
function load(record) { that.load = function(record) {
that.record = record; that.record = record;
var value = record[that.name]; var value = record[that.name];
@ -186,24 +180,24 @@ IPA.widget = function(spec) {
} }
that.reset(); that.reset();
} };
that.reset = function() { that.reset = function() {
that.hide_undo(); that.set_dirty(false);
that.update(); that.update();
}; };
function update() { that.update = function() {
} };
/** /**
* This function saves the values entered in the UI. * This function saves the values entered in the UI.
* It returns the values in an array, or null if * It returns the values in an array, or null if
* the field should not be saved. * the field should not be saved.
*/ */
function save() { that.save = function() {
return that.values; return that.values;
} };
/** /**
* This function compares the original values and the * This function compares the original values and the
@ -211,45 +205,7 @@ IPA.widget = function(spec) {
* it will return true. * it will return true.
*/ */
that.is_dirty = function() { that.is_dirty = function() {
return that.dirty;
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;
}; };
that.create_undo = function(container) { that.create_undo = function(container) {
@ -262,6 +218,17 @@ IPA.widget = function(spec) {
}).appendTo(container); }).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() { that.get_undo = function() {
return $(that.undo_span); return $(that.undo_span);
}; };
@ -302,6 +269,7 @@ IPA.widget = function(spec) {
that.widget_load = that.load; that.widget_load = that.load;
that.widget_reset = that.reset; that.widget_reset = that.reset;
that.widget_save = that.save; that.widget_save = that.save;
that.widget_set_dirty = that.set_dirty;
return that; return that;
}; };
@ -352,9 +320,7 @@ IPA.text_widget = function(spec) {
var input = $('input[name="'+that.name+'"]', that.container); var input = $('input[name="'+that.name+'"]', that.container);
input.keyup(function() { input.keyup(function() {
if (that.undo) { that.set_dirty(true);
that.show_undo();
}
that.validate(); 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) { that.show_undo = function(index) {
var undo = that.get_undo(index); var undo = that.get_undo(index);
undo.css('display', 'inline'); undo.css('display', 'inline');
@ -550,9 +527,8 @@ IPA.multivalued_text_widget = function(spec) {
var index = that.row_index(row); var index = that.row_index(row);
if (index >= that.values.length) { if (index >= that.values.length) {
// show undo/remove link for new value // show undo/remove link for new value
that.set_dirty(true, index);
if (that.undo) { if (that.undo) {
that.show_undo(index);
that.show_undo();
remove_link.css('display', 'none'); remove_link.css('display', 'none');
} else { } else {
remove_link.css('display', 'inline'); remove_link.css('display', 'inline');
@ -563,9 +539,8 @@ IPA.multivalued_text_widget = function(spec) {
var index = that.row_index(row); var index = that.row_index(row);
// uncross removed value // uncross removed value
input.removeClass('strikethrough'); input.removeClass('strikethrough');
that.set_dirty(true, index);
if (that.undo) { if (that.undo) {
that.show_undo(index);
that.show_undo();
if (index < that.values.length) { if (index < that.values.length) {
remove_link.css('display', 'inline'); remove_link.css('display', 'inline');
} }
@ -579,10 +554,7 @@ IPA.multivalued_text_widget = function(spec) {
// restore old value then cross it out // restore old value then cross it out
that.update(index); that.update(index);
input.addClass('strikethrough'); input.addClass('strikethrough');
if (that.undo) { that.set_dirty(true, index);
that.show_undo(index);
that.show_undo();
}
remove_link.css('display', 'none'); remove_link.css('display', 'none');
} else { } else {
// remove new value // remove new value
@ -623,7 +595,7 @@ IPA.multivalued_text_widget = function(spec) {
}; };
that.reset = function(index) { that.reset = function(index) {
that.hide_undo(index); that.set_dirty(false, index);
that.update(index); that.update(index);
}; };
@ -685,7 +657,7 @@ IPA.checkbox_widget = function (spec) {
var input = $('input[name="'+that.name+'"]', that.container); var input = $('input[name="'+that.name+'"]', that.container);
input.change(function() { input.change(function() {
that.show_undo(); that.set_dirty(true);
}); });
var undo = that.get_undo(); var undo = that.get_undo();
@ -759,7 +731,7 @@ IPA.checkboxes_widget = function (spec) {
var input = $('input[name="'+that.name+'"]', that.container); var input = $('input[name="'+that.name+'"]', that.container);
input.change(function() { input.change(function() {
that.show_undo(); that.set_dirty(true);
}); });
var undo = that.get_undo(); var undo = that.get_undo();
@ -837,7 +809,7 @@ IPA.radio_widget = function(spec) {
var input = $('input[name="'+that.name+'"]', that.container); var input = $('input[name="'+that.name+'"]', that.container);
input.change(function() { input.change(function() {
that.show_undo(); that.set_dirty(true);
}); });
var undo = that.get_undo(); var undo = that.get_undo();
@ -913,7 +885,7 @@ IPA.select_widget = function(spec) {
that.select = $('select[name="'+that.name+'"]', that.container); that.select = $('select[name="'+that.name+'"]', that.container);
that.select.change(function() { that.select.change(function() {
that.show_undo(); that.set_dirty(true);
}); });
var undo = that.get_undo(); var undo = that.get_undo();
@ -994,7 +966,7 @@ IPA.textarea_widget = function (spec) {
var input = $('textarea[name="'+that.name+'"]', that.container); var input = $('textarea[name="'+that.name+'"]', that.container);
input.keyup(function() { input.keyup(function() {
that.show_undo(); that.set_dirty(true);
that.validate(); that.validate();
}); });
@ -1439,7 +1411,7 @@ IPA.entity_select_widget = function(spec) {
that.entity_select = $('<select/>', { that.entity_select = $('<select/>', {
id: that.name + '-entity-select', id: that.name + '-entity-select',
change: function(){ change: function(){
that.show_undo(); that.set_dirty(true);
} }
}).appendTo(container); }).appendTo(container);
@ -1450,7 +1422,7 @@ IPA.entity_select_widget = function(spec) {
style: 'display: none;', style: 'display: none;',
keyup: function(){ keyup: function(){
populate_select(); populate_select();
that.show_undo(); that.set_dirty(true);
} }
}).appendTo(container); }).appendTo(container);
@ -1477,14 +1449,10 @@ IPA.entity_select_widget = function(spec) {
that.reset = function() { that.reset = function() {
that.entity_filter.val(that.values[0]); that.entity_filter.val(that.values[0]);
that.hide_undo(); that.set_dirty(false);
populate_select(that.values[0]); populate_select(that.values[0]);
}; };
that.is_dirty = function() {
return (that.save()[0] !== that.values[0]);
};
that.load = function(record) { that.load = function(record) {
var value = record[that.name]; var value = record[that.name];
if (value instanceof Array) { if (value instanceof Array) {