summaryrefslogtreecommitdiffhomepage
path: root/libs/cbi
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2008-08-04 17:16:27 +0000
committerSteven Barth <steven@midlink.org>2008-08-04 17:16:27 +0000
commit084db952ce2aa302a42f35fa34866f34cb06ba99 (patch)
tree41bcdef71918816ff78de209455edf1162fc8a9f /libs/cbi
parent513e1cbba9e8ba98cc0b4f667aa89233b72b4dd1 (diff)
libs/cbi: Added value function to luci.cbi.Value to create Comboboxes
Diffstat (limited to 'libs/cbi')
-rw-r--r--libs/cbi/htdocs/luci-static/resources/cbi.js73
-rw-r--r--libs/cbi/luasrc/cbi.lua20
-rw-r--r--libs/cbi/luasrc/view/cbi/value.htm13
3 files changed, 96 insertions, 10 deletions
diff --git a/libs/cbi/htdocs/luci-static/resources/cbi.js b/libs/cbi/htdocs/luci-static/resources/cbi.js
index 3abc3a9f5..38e27d8f4 100644
--- a/libs/cbi/htdocs/luci-static/resources/cbi.js
+++ b/libs/cbi/htdocs/luci-static/resources/cbi.js
@@ -35,4 +35,77 @@ function cbi_d_init() {
for (var x in cbi_d) {
cbi_d_update(x);
}
+}
+
+function cbi_bind(obj, type, callback, mode) {
+ if (typeof mode == "undefined") {
+ mode = false;
+ }
+ if (!obj.addEventListener) {
+ ieCallback = function(){
+ var e = window.event;
+ if (!e.target && e.srcElement) {
+ e.target = e.srcElement;
+ };
+ e.target['_eCB' + type + callback] = callback;
+ e.target['_eCB' + type + callback](e);
+ e.target['_eCB' + type + callback] = null;
+ };
+ obj.attachEvent('on' + type, ieCallback);
+ } else {
+ obj.addEventListener(type, callback, mode);
+ }
+ return obj;
+}
+
+function cbi_combobox(id, values, def, man) {
+ var obj = document.getElementById(id)
+ if (obj.value == "" || values[obj.value]) {
+ var sel = document.createElement("select")
+ obj.parentNode.appendChild(sel)
+
+ if (obj.value == "") {
+ var optdef = document.createElement("option")
+ optdef.value = ""
+ optdef.appendChild(document.createTextNode(def))
+ sel.appendChild(optdef)
+ }
+
+ for (var i in values) {
+ var opt = document.createElement("option")
+ opt.value = i
+
+ if (obj.value == i) {
+ opt.selected = "selected"
+ }
+
+ opt.appendChild(document.createTextNode(values[i]))
+ sel.appendChild(opt)
+ }
+
+ var optman = document.createElement("option")
+ optman.value = ""
+ optman.appendChild(document.createTextNode(man))
+ sel.appendChild(optman)
+
+ obj.style.display = "none"
+
+ cbi_bind(sel, "change", function() {
+ obj.value = sel.options[sel.selectedIndex].value
+
+ if (sel.selectedIndex == sel.options.length - 1) {
+ obj.style.display = "inline"
+ sel.parentNode.removeChild(sel)
+ obj.focus()
+ }
+ })
+ }
+}
+
+function cbi_combobox_init(id, values, def, man) {
+ var obj = document.getElementById(id)
+ cbi_bind(obj, "change", function() {
+ cbi_combobox(id, values, def, man)
+ })
+ cbi_combobox(id, values, def, man)
} \ No newline at end of file
diff --git a/libs/cbi/luasrc/cbi.lua b/libs/cbi/luasrc/cbi.lua
index fe99f0246..8c8f68b24 100644
--- a/libs/cbi/luasrc/cbi.lua
+++ b/libs/cbi/luasrc/cbi.lua
@@ -595,7 +595,10 @@ function AbstractValue.render(self, s, scope)
if cond then
return string.format(
' %s="%s"', tostring(key),
- tostring( val or scope[key] or self[key] or "" )
+ tostring( val
+ or scope[key]
+ or (type(self[key]) ~= "function" and self[key])
+ or "" )
)
else
return ''
@@ -642,17 +645,14 @@ Value = class(AbstractValue)
function Value.__init__(self, ...)
AbstractValue.__init__(self, ...)
self.template = "cbi/value"
-
- self.maxlength = nil
+ self.keylist = {}
+ self.vallist = {}
end
--- This validation is a bit more complex
-function Value.validate(self, val)
- if self.maxlength and tostring(val):len() > self.maxlength then
- val = nil
- end
-
- return val
+function Value.value(self, key, val)
+ val = val or key
+ table.insert(self.keylist, tostring(key))
+ table.insert(self.vallist, tostring(val))
end
diff --git a/libs/cbi/luasrc/view/cbi/value.htm b/libs/cbi/luasrc/view/cbi/value.htm
index 5a7339d83..4d473bf5e 100644
--- a/libs/cbi/luasrc/view/cbi/value.htm
+++ b/libs/cbi/luasrc/view/cbi/value.htm
@@ -14,4 +14,17 @@ $Id$
-%>
<%+cbi/valueheader%>
<input type="text" onchange="cbi_d_update(this.id)"<%= attr("name", cbid) .. attr("id", cbid) .. attr("value", self:cfgvalue(section)) .. ifattr(self.size, "size") .. ifattr(self.maxlength, "maxlength") %> />
+ <% if #self.keylist > 0 then -%>
+ <script type="text/javascript">
+ cbi_combobox_init('<%=cbid%>', {
+ <%-
+ for i, k in ipairs(self.keylist) do
+ -%>
+ <%-=string.format("%q", k) .. ":" .. string.format("%q", self.vallist[i])-%>,
+ <%-
+ end
+ -%>
+ }, '<%:cbi_select%>', '<%:cbi_manual%>');
+ </script>
+ <% end -%>
<%+cbi/valuefooter%>