Module: check_mk
Branch: master
Commit: 5aa063fc3a61d50e278036fd7329deaf08edc2e7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=5aa063fc3a61d5…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Jan 10 09:54:33 2012 +0100
New ValueSpec for list of strings
---
web/htdocs/js/checkmk.js | 36 ++++++++++++++++++++++++++++++++
web/htdocs/pages.css | 4 +-
web/htdocs/valuespec.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/web/htdocs/js/checkmk.js b/web/htdocs/js/checkmk.js
index 3c6382c..e908f9e 100644
--- a/web/htdocs/js/checkmk.js
+++ b/web/htdocs/js/checkmk.js
@@ -1383,3 +1383,39 @@ function valuespec_toggle_dropdownn(oDropdown, divid) {
oDiv = null;
}
+function list_of_strings_init(tableid) {
+ var oTable = document.getElementById(tableid);
+ var oTBody = oTable.childNodes[0];
+ var oTr = oTBody.childNodes[oTBody.childNodes.length - 1];
+ var oTd = oTr.childNodes[0];
+ for (var j in oTd.childNodes) {
+ var o = oTd.childNodes[j];
+ if (o.tagName == "INPUT") {
+ o.onfocus = function(e) { return list_of_strings_extend(this); };
+ }
+ }
+}
+
+function list_of_strings_extend(oInput, j) {
+ var oldName = oInput.name;
+ // Transform e.g extra_emails_12 -> extra_emails_13
+ var splitted = oldName.split("_");
+ var num = 1 + parseInt(splitted[splitted.length-1]);
+ splitted[splitted.length-1] = "" + num;
+ var newName = splitted.join("_");
+
+ var oTr = oInput.parentNode.parentNode;
+ var oTBody = oTr.parentNode;
+ var oNewTr = document.createElement("TR");
+ oNewTr.innerHTML = oTr.innerHTML.replace('"' + oldName +
'"', '"' + newName + '"');
+ oTBody.appendChild(oNewTr);
+ var oNewTd = oNewTr.childNodes[0];
+ for (var j in oNewTd.childNodes) {
+ var o = oNewTd.childNodes[j];
+ if (o.tagName == "INPUT") {
+ o.onfocus = function(e) { return list_of_strings_extend(this); };
+ }
+ }
+ // Remove handle from old last element
+ oInput.onfocus = null;
+}
diff --git a/web/htdocs/pages.css b/web/htdocs/pages.css
index d682e79..d2bf545 100644
--- a/web/htdocs/pages.css
+++ b/web/htdocs/pages.css
@@ -374,10 +374,10 @@ table.form.nomargin td.title {
border-top-style: solid;
}
-table.form td {
+table.form > tbody > tr > td {
padding: 4px 10px;
}
-table.form th {
+table.form > tbody > tr > th {
border-width: 1px;
border-color: #fff;
border-style: solid solid none solid;
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index aab449a..e0bfcef 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -329,6 +329,57 @@ class Filename(TextAscii):
# permissions and the file might be created with Nagios permissions (on OMD this
# is the same, but for others not)
+class ListOfStrings(ValueSpec):
+ def __init__(self, **kwargs):
+ ValueSpec.__init__(self, **kwargs)
+ self._valuespec = kwargs.get("valuespec", TextAscii())
+
+ def render_input(self, vp, value):
+ # Form already submitted?
+ if html.has_var(vp + "_0"):
+ value = self.from_html_vars(vp)
+ # Remove variables from URL, so that they do not appear
+ # in hidden_fields()
+ nr = 0
+ while html.has_var(vp + "_%d" % nr):
+ html.del_var(vp + "_%d" % nr)
+ nr += 1
+ html.write('<table border=0 cellspacing=0 cellpadding=0
id="%s">' % vp)
+
+ for nr, s in enumerate(value + [""]):
+ html.write('<tr><td>')
+ self._valuespec.render_input(vp + "_%d" % nr, s)
+ html.write('</td></tr>')
+
+ html.write('</table>')
+ html.javascript("list_of_strings_init('%s');" % vp);
+
+ def canonical_value(self):
+ return []
+
+ def value_to_text(self, value):
+ return ", ".join([self._valuespec.value_to_text(v) for v in value])
+
+ def from_html_vars(self, vp):
+ value = []
+ nr = 0
+ while html.has_var(vp + "_%d" % nr):
+ s = html.var(vp + "_%d" % nr).strip()
+ if s:
+ value.append(s)
+ nr += 1
+ return value
+
+ def validate_datatype(self, value, vp):
+ if type(value) != list:
+ raise MKUserError(varprefix, _("Expected data type is "
+ "list, but your type is %s." % type(value)))
+ for nr, s in enumerate(value):
+ self._valuespec.validate_datatype(s, vp + "_%d" % nr)
+
+ def validate_value(self, value, vp):
+ for nr, s in enumerate(value):
+ self._valuespec.validate_value(s, vp + "_%d" % nr)
# Same but for floating point values
class Float(Integer):