luci-base: ui.js: implement AbstractElement.setPlaceholder()
authorJo-Philipp Wich <jo@mein.io>
Mon, 3 Aug 2020 13:17:40 +0000 (15:17 +0200)
committerJo-Philipp Wich <jo@mein.io>
Wed, 5 Aug 2020 11:51:16 +0000 (13:51 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/ui.js

index 91a93b9e9019e01d9147cc75052106563b8367a7..7823182f04980ced6e1e36bae3ff03b1b1dc73f3 100644 (file)
@@ -101,6 +101,32 @@ var UIElement = baseclass.extend(/** @lends LuCI.ui.AbstractElement.prototype */
                        this.node.value = value;
        },
 
+       /**
+        * Set the current placeholder value of the input widget.
+        *
+        * @instance
+        * @memberof LuCI.ui.AbstractElement
+        * @param {string|string[]|null} value
+        * The placeholder to set for the input element. Only applicable to text
+        * inputs, not to radio buttons, selects or similar.
+        */
+       setPlaceholder: function(value) {
+               var node = this.node ? this.node.querySelector('input,textarea') : null;
+               if (node) {
+                       switch (node.getAttribute('type') || 'text') {
+                       case 'password':
+                       case 'search':
+                       case 'tel':
+                       case 'text':
+                       case 'url':
+                               if (value != null && value != '')
+                                       node.setAttribute('placeholder', value);
+                               else
+                                       node.removeAttribute('placeholder');
+                       }
+               }
+       },
+
        /**
         * Check whether the current input value is valid.
         *