[Freeipa-devel] [PATCH] 014 Code cleanup: widget creation

2011-09-13 Thread Petr Vobornik

https://fedorahosted.org/freeipa/ticket/1788

Removed code duplication of undo links.
Simplified code of widget creation to be more readable.
--
Petr Vobornik
From c1e47469cd394c8934e0a6bf3cc84e88b5a6bb5a Mon Sep 17 00:00:00 2001
From: Petr Vobornik pvobo...@redhat.com
Date: Tue, 13 Sep 2011 13:53:54 +0200
Subject: [PATCH] Code cleanup: widget creation

https://fedorahosted.org/freeipa/ticket/1788

Removed code duplication of undo links.
Simplified code of widget creation to be more readable.
---
 install/ui/aci.js|3 -
 install/ui/widget.js |  182 +++---
 2 files changed, 70 insertions(+), 115 deletions(-)

diff --git a/install/ui/aci.js b/install/ui/aci.js
index a0399f501aaff6cd89e08e03c1de865b03222077..15219e7fef9372e2bbd75b8d8da47e8aa60d4b34 100644
--- a/install/ui/aci.js
+++ b/install/ui/aci.js
@@ -261,9 +261,6 @@ IPA.attributes_widget = function(spec) {
 
 if (that.undo) {
 that.create_undo(container);
-that.get_undo().click(function(){
-that.reset();
-});
 }
 
 if (that.object_type){
diff --git a/install/ui/widget.js b/install/ui/widget.js
index 12a6b966c999d598576d53132c4f37fff685d3b1..58698486894ce9e72842ea1cf011a5fb75286421 100644
--- a/install/ui/widget.js
+++ b/install/ui/widget.js
@@ -287,7 +287,15 @@ IPA.widget = function(spec) {
 return that.dirty;
 };
 
-that.create_undo = function(container) {
+/**
+ * This function creates an undo link in the container.
+ * On_undo is a link click callback. It can be specified to custom
+ * callback. If a callback isn't set, default callback is used. If
+ * spefified to value other than a function, no callback is registered.
+ */
+that.create_undo = function(container, on_undo) {
+container.append(' ');
+
 that.undo_span =
 $('span/', {
 name: 'undo',
@@ -295,6 +303,16 @@ IPA.widget = function(spec) {
 'class': 'ui-state-highlight ui-corner-all undo',
 html: 'undo'
 }).appendTo(container);
+
+if(on_undo === undefined) {
+on_undo = function() {
+that.reset();
+};
+}
+
+if(typeof on_undo === 'function') {
+that.undo_span.click(on_undo);
+}
 };
 
 that.set_dirty = function(dirty) {
@@ -389,61 +407,48 @@ IPA.text_widget = function(spec) {
 IPA.select_range(that.input, start, end);
 };
 
-
 that.create = function(container) {
 
 that.widget_create(container);
 
 container.addClass('text-widget');
 
-$('label/', {
+that.display_control = $('label/', {
 name: that.name,
 style: 'display: none;'
 }).appendTo(container);
 
-$('input/', {
+that.input = $('input/', {
 type: that.type,
 name: that.name,
 disabled: that.disabled,
 size: that.size,
-title: that.tooltip
+title: that.tooltip,
+keyup: function() {
+that.set_dirty(that.test_dirty());
+that.validate();
+}
 }).appendTo(container);
 
 if (that.undo) {
-container.append(' ');
 that.create_undo(container);
 }
 
 that.create_error_link(container);
-
-var input = $('input[name='+that.name+']', that.container);
-input.keyup(function() {
-that.set_dirty(that.test_dirty());
-that.validate();
-});
-
-var undo = that.get_undo();
-undo.click(function() {
-that.reset();
-});
-that.input = input;
 };
 
 that.update = function() {
 var value = that.values  that.values.length ? that.values[0] : '';
 
-var label = $('label[name='+that.name+']', that.container);
-var input = $('input[name='+that.name+']', that.container);
-
 if (that.read_only || !that.writable) {
-label.html(value);
-label.css('display', 'inline');
-input.css('display', 'none');
+that.display_control.html(value);
+that.display_control.css('display', 'inline');
+that.input.css('display', 'none');
 
 } else {
-$('input[name='+that.name+']', that.container).val(value);
-label.css('display', 'none');
-input.css('display', 'inline');
+that.input.val(value);
+that.display_control.css('display', 'none');
+that.input.css('display', 'inline');
 }
 };
 
@@ -452,8 +457,7 @@ IPA.text_widget = function(spec) {
 return null;
 
 } else {
-var input = $('input[name='+that.name+']', that.container);
-var value = input.val();
+var value = that.input.val();
 return value === '' ? [] : [value];
 

Re: [Freeipa-devel] [PATCH] 014 Code cleanup: widget creation

2011-09-13 Thread Endi Sukma Dewata

On 9/13/2011 7:57 AM, Petr Vobornik wrote:

https://fedorahosted.org/freeipa/ticket/1788

Removed code duplication of undo links.
Simplified code of widget creation to be more readable.


ACK and pushed to master and ipa-2-1.

One little thing though, the create_undo() will always append a space 
before the undo element. Right now it's not a problem, but suppose 
there's a custom widget that doesn't want a space before the undo 
element we can't use the standard method. We'll deal with that if it 
becomes a problem.


--
Endi S. Dewata

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