Author: michiel
Date: 2010-02-05 14:52:03 +0100 (Fri, 05 Feb 2010)
New Revision: 40878

Modified:
   mmbase/branches/MMBase-1_8/html/mmbase/validation/Widgets.js
Log:
backported labelsToInputs

Modified: mmbase/branches/MMBase-1_8/html/mmbase/validation/Widgets.js
===================================================================
--- mmbase/branches/MMBase-1_8/html/mmbase/validation/Widgets.js        
2010-02-05 13:33:41 UTC (rev 40877)
+++ mmbase/branches/MMBase-1_8/html/mmbase/validation/Widgets.js        
2010-02-05 13:52:03 UTC (rev 40878)
@@ -305,3 +305,82 @@
 
     });
 };
+
+
+Widgets.prototype.labelsToInputs = function(selector, options) {
+    var emptyisuntouched = options && options['emptyisuntouched'];
+    //var ignornon         = options && options['emptyisuntouched'];
+    $(document).ready(function() {
+        $(selector).each(function() {
+            var labelText = $(this).text();
+            var labelFor = $(this).attr("for");
+            var input = $("#" + labelFor);
+            if (input.val() == "") {
+                if (input.attr("type") == 'password') {
+                    try {
+                        input.attr("type", "text");
+                    } catch (e) {
+                        // happens in text/html FF, never mind...
+                        var i = $("<input type='text' value='' id='" + 
input.attr("id") + " name='" + input.attr('name') + " class='" + 
input.attr("class") + "' />");
+                        input.before(i);
+                        input.hide();
+                        i[0].realInput = input;
+                        input = i;
+
+                    }
+                    input.addClass("password");
+                }
+                input.val(labelText);
+                input.addClass("untouched");
+                $(this).css("display", "none");
+                var focus = function() {
+                    // if entered for the first time, remove the label value
+                    if ($(this).hasClass("untouched")) {
+                        if (emptyisuntouched) {
+                            $(this).removeClass("untouched");
+                        }
+                        this.value = "";
+                        if ($(this).hasClass("password")) {
+                            try {
+                                $(this).attr("type", "password");
+                            } catch (e) {
+                                $(this.realInput).show().focus();
+                                $(this).hide();
+                                // happens in text/html FF, never mind...
+
+                            }
+                        }
+                    }
+                };
+                input.focus(focus);
+                input.select(focus);
+                input.blur(function() {
+                    // if leaving, the value is empty, and empty is equivalent 
to 'untouched', put the label back in.
+                    if ($(this).val() == "") {
+                        if (emptyisuntouched) {
+                            $(this).addClass("untouched");
+                        }
+                        if ($(this).hasClass("untouched")) {
+                            $(this).val(labelText);
+                            if ($(this).hasClass("password")) {
+                                try {
+                                    $(this).attr("type", "text");
+                                } catch (e) {
+                                    // happens in text/html FF, never mind...
+                                }
+                            }
+                        }
+                    }
+                });
+                if (! emptyisuntouched) {
+                    input.keyup(function() {
+                        $(this).removeClass("untouched");
+                    });
+                }
+            } else {
+                // value is not empty, so cant use it for the label
+            }
+
+        });
+    });
+}

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to