Module: check_mk
Branch: master
Commit: 3e197a28b0984a9d12d5f1b8a31fe60a974e4367
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=3e197a28b0984a…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Sat Dec 31 18:51:15 2011 +0100
Improved ValueSpecs
---
web/htdocs/check_mk.css | 9 +++++++++
web/htdocs/forms.py | 4 ++--
web/htdocs/htmllib.py | 13 +++++++++++++
web/htdocs/pages.css | 3 +++
web/htdocs/valuespec.py | 31 +++++++++++++++++++++++--------
web/htdocs/wato.py | 13 +++++++++++++
6 files changed, 63 insertions(+), 10 deletions(-)
diff --git a/web/htdocs/check_mk.css b/web/htdocs/check_mk.css
index e8b8e48..57802d8 100644
--- a/web/htdocs/check_mk.css
+++ b/web/htdocs/check_mk.css
@@ -77,6 +77,15 @@ input:hover, select:hover, textarea:hover {
background-color: #EDECEB;
}
+/* Select element with icons */
+select.icon option {
+ background-repeat: no-repeat;
+ background-position: bottom left;
+ background-size: 18px;
+ padding-left:22px;
+ height: 18px;
+}
+
input.buttonlink {
padding: 1px;
diff --git a/web/htdocs/forms.py b/web/htdocs/forms.py
index aab1955..c39dc64 100644
--- a/web/htdocs/forms.py
+++ b/web/htdocs/forms.py
@@ -27,7 +27,7 @@
from lib import *
-def edit_dictionary(entries, value):
+def edit_dictionary(entries, value, focus=None):
new_value = value.copy()
if html.check_transaction():
messages = []
@@ -57,7 +57,7 @@ def edit_dictionary(entries, value):
else:
v = vs.default_value()
vs.render_input(name, v)
- if first:
+ if (not focus and first) or (name == focus):
vs.set_focus(name)
first = False
html.write("<tr><td class=buttons colspan=2>")
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index 80d47d8..f7546b4 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -461,6 +461,19 @@ class html:
if varname:
self.form_vars.append(varname)
+ def icon_select(self, varname, options, deflt=""):
+ current = self.var(varname, deflt)
+ self.write("<select class=icon name=\"%s\" id=\"%s\"
size=\"1\">\n" %
+ (varname, varname))
+ for value, text, icon in options:
+ if value == None: value = ""
+ sel = value == current and " selected" or ""
+ self.write('<option
style="background-image:url(images/icon_%s.png);" '
+ 'value=\"%s\"%s>%s</option>\n' % (icon,
value, sel, text))
+ self.write("</select>\n")
+ if varname:
+ self.form_vars.append(varname)
+
def begin_radio_group(self, horizontal=False):
if self.mobile:
if horizontal:
diff --git a/web/htdocs/pages.css b/web/htdocs/pages.css
index f0cb41f..d682e79 100644
--- a/web/htdocs/pages.css
+++ b/web/htdocs/pages.css
@@ -400,6 +400,7 @@ table.form td.legend, table.form td.title {
color: #274750;
border: 1px solid #fff;
padding-top: 10px;
+ padding-bottom: 10px;
}
/* Bottom row with action buttons, right justified */
@@ -434,6 +435,8 @@ table.form td.content {
padding-right: 2ex;
border: 1px solid #fff;
border-left-style: none;
+ padding-top: 10px;
+ padding-bottom: 10px;
}
td.legend, td.content {
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 13e03c8..a288aac 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -24,7 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-import math, os
+import math, os, time
from lib import *
# Abstract base class of all value declaration classes.
@@ -239,7 +239,7 @@ class TextAscii(ValueSpec):
raise MKUserError(varprefix, _("The value must be of type str, but it
has type %s") % type(value))
def validate_value(self, value, varprefix):
- if not self._allow_empty and value == "":
+ if not self._allow_empty and value.strip() == "":
raise MKUserError(varprefix, _("An empty value is not allowed
here."))
class TextUnicode(TextAscii):
@@ -366,6 +366,8 @@ class Checkbox(ValueSpec):
# help_separator: if you set this to a character, e.g. "-", then
# value_to_text will omit texts from the character up to the end of
# a choices name.
+# Note: The list of choices may contain 2-tuples or 3-tuples.
+# The format is (value, text {, icon} )
class DropdownChoice(ValueSpec):
def __init__(self, **kwargs):
ValueSpec.__init__(self, **kwargs)
@@ -379,14 +381,18 @@ class DropdownChoice(ValueSpec):
# Convert values from choices to keys
defval = "0"
options = []
- for n, (val, title) in enumerate(self._choices):
- options.append((str(n), title))
- if val == value:
+ for n, entry in enumerate(self._choices):
+ options.append((str(n),) + entry[1:])
+ if entry[0] == value:
defval = str(n)
- html.select(varprefix, options, defval)
+ if len(options[0]) == 3:
+ html.icon_select(varprefix, options, defval)
+ else:
+ html.select(varprefix, options, defval)
def value_to_text(self, value):
- for val, title in self._choices:
+ for entry in self._choices:
+ val, title = entry[:2]
if value == val:
if self._help_separator:
return title.split(self._help_separator, 1)[0].strip()
@@ -395,7 +401,8 @@ class DropdownChoice(ValueSpec):
def from_html_vars(self, varprefix):
sel = html.var(varprefix)
- for n, (val, title) in enumerate(self._choices):
+ for n, entry in enumerate(self._choices):
+ val = entry[0]
if sel == str(n):
return val
return self._choices[0][0] # can only happen if user garbled URL
@@ -858,3 +865,11 @@ class ElementSelection(ValueSpec):
if type(value) != str:
raise MKUserError(varprefix, _("The datatype must be str (string), but
is %s") % type(value))
+
+class AutoTimestamp(FixedValue):
+ def __init__(self, **kwargs):
+ ValueSpec.__init__(self, **kwargs)
+ self._value = time.time()
+
+ def value_to_text(self, value):
+ return time.strftime("%F %T", time.localtime(value))
diff --git a/web/htdocs/wato.py b/web/htdocs/wato.py
index bfcde17..40220ab 100644
--- a/web/htdocs/wato.py
+++ b/web/htdocs/wato.py
@@ -6930,6 +6930,19 @@ def save_users(profiles):
# Call the users_saved hook
call_hook(call_hook_users_saved, users)
+# Dropdown for choosing a multisite user
+class UserSelection(ElementSelection):
+ def __init__(self, **kwargs):
+ ElementSelection.__init__(self, **kwargs)
+
+ def get_elements(self):
+ users = load_users()
+ elements = dict([ (name, "%s - %s" % (name, us.get("alias",
name))) for (name, us) in users.items() ])
+ return elements
+
+ def value_to_text(self, value):
+ return self._elements.get(value, value).split(" - ")[-1]
+
#.
# .-Roles----------------------------------------------------------------.
# | ____ _ |