From 084db952ce2aa302a42f35fa34866f34cb06ba99 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Mon, 4 Aug 2008 17:16:27 +0000 Subject: [PATCH] libs/cbi: Added value function to luci.cbi.Value to create Comboboxes --- i18n/english/luasrc/i18n/cbi.en.lua | 2 + i18n/german/luasrc/i18n/cbi.de.lua | 4 +- libs/cbi/htdocs/luci-static/resources/cbi.js | 73 ++++++++++++++++++++++++++++ libs/cbi/luasrc/cbi.lua | 20 ++++---- libs/cbi/luasrc/view/cbi/value.htm | 13 +++++ 5 files changed, 101 insertions(+), 11 deletions(-) diff --git a/i18n/english/luasrc/i18n/cbi.en.lua b/i18n/english/luasrc/i18n/cbi.en.lua index 14153989f..ca1285d0a 100644 --- a/i18n/english/luasrc/i18n/cbi.en.lua +++ b/i18n/english/luasrc/i18n/cbi.en.lua @@ -4,3 +4,5 @@ cbi_invalid = "Error: Invalid input value" cbi_addopt = "-- Additional Field --" cbi_optional = " (optional)" cbi_sectempty = "This section contains no values yet" +cbi_manual = "-- manual --" +cbi_select = "-- Please choose --" diff --git a/i18n/german/luasrc/i18n/cbi.de.lua b/i18n/german/luasrc/i18n/cbi.de.lua index 1ca36305f..f34cb59b5 100644 --- a/i18n/german/luasrc/i18n/cbi.de.lua +++ b/i18n/german/luasrc/i18n/cbi.de.lua @@ -2,4 +2,6 @@ cbi_add = "Eintrag hinzufügen" cbi_del = "Eintrag entfernen" cbi_invalid = "Error: Ungültige Eingabe" cbi_addopt = "-- Zusätzliches Feld --" -cbi_sectempty = "Diese Sektion enthält noch keine Einträge" \ No newline at end of file +cbi_sectempty = "Diese Sektion enthält noch keine Einträge" +cbi_manual = "-- manuell --" +cbi_select = "-- Bitte auswählen --" \ No newline at end of file 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%> /> + <% if #self.keylist > 0 then -%> + + <% end -%> <%+cbi/valuefooter%> -- 2.11.0