luci2: Properly handle empty values in ListValue widgets
[project/luci2/ui.git] / luci2 / htdocs / luci2 / luci2.js
index 6e66128..45584e7 100644 (file)
@@ -5240,7 +5240,7 @@ function LuCI2()
                        return (i.disabled || i.error.text() == '');
                },
 
-               depends: function(d, v)
+               depends: function(d, v, add)
                {
                        var dep;
 
@@ -5285,7 +5285,11 @@ function LuCI2()
                        if ($.isEmptyObject(dep))
                                return this;
 
-                       this.dependencies.push(dep);
+                       if (!add || !this.dependencies.length)
+                               this.dependencies.push(dep);
+                       else
+                               for (var i = 0; i < this.dependencies.length; i++)
+                                       $.extend(this.dependencies[i], dep);
 
                        return this;
                },
@@ -5485,7 +5489,7 @@ function LuCI2()
                        var s = $('<select />')
                                .addClass('form-control');
 
-                       if (this.options.optional)
+                       if (this.options.optional && !this.has_empty)
                                $('<option />')
                                        .attr('value', '')
                                        .text(_luci2.tr('-- Please choose --'))
@@ -5508,6 +5512,9 @@ function LuCI2()
                        if (!this.choices)
                                this.choices = [ ];
 
+                       if (k == '')
+                               this.has_empty = true;
+
                        this.choices.push([k, v || k]);
                        return this;
                }
@@ -5582,10 +5589,7 @@ function LuCI2()
                        {
                                ev.data.select.hide();
                                ev.data.input.show().focus();
-
-                               var v = ev.data.input.val();
-                               ev.data.input.val(' ');
-                               ev.data.input.val(v);
+                               ev.data.input.val('');
                        }
                        else if (self.options.optional && s.selectedIndex == 0)
                        {
@@ -5595,6 +5599,8 @@ function LuCI2()
                        {
                                ev.data.input.val(ev.data.select.val());
                        }
+
+                       ev.stopPropagation();
                },
 
                _blur: function(ev)
@@ -5605,7 +5611,7 @@ function LuCI2()
 
                        ev.data.select.empty();
 
-                       if (self.options.optional)
+                       if (self.options.optional && !self.has_empty)
                                $('<option />')
                                        .attr('value', '')
                                        .text(_luci2.tr('-- please choose --'))
@@ -5635,7 +5641,7 @@ function LuCI2()
                                .appendTo(ev.data.select);
 
                        ev.data.input.hide();
-                       ev.data.select.val(val).show().focus();
+                       ev.data.select.val(val).show().blur();
                },
 
                _enter: function(ev)
@@ -5682,6 +5688,9 @@ function LuCI2()
                        if (!this.choices)
                                this.choices = [ ];
 
+                       if (k == '')
+                               this.has_empty = true;
+
                        this.choices.push([k, v || k]);
                        return this;
                },