Module: check_mk
Branch: master
Commit: 245229011f79f1f2a3707a7b3ad9c0990f91b1d8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=245229011f79f1…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Mar 21 09:24:20 2013 +0100
htmllib: move keybindings from TextArea to htmllib
---
web/htdocs/htmllib.py | 22 +++++++++++++++++++
web/htdocs/js/checkmk.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++
web/htdocs/valuespec.py | 47 +----------------------------------------
3 files changed, 75 insertions(+), 46 deletions(-)
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index f762f35..7c3361b 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -189,6 +189,7 @@ class html:
self.auto_id = 0
self.have_help = False
self.plugged = False
+ self.keybindings = []
def set_buffering(self, b):
self.buffering = b
@@ -911,6 +912,11 @@ class html:
def body_end(self):
if self.have_help:
self.javascript("help_enable();")
+ if self.keybindings:
+ self.javascript("""var keybindings = %r;\n
+document.body.onkeydown = keybindings_keydown;
+document.body.onkeyup = keybindings_keyup;
+""" % self.keybindings)
if self.final_javascript_code:
self.javascript(self.final_javascript_code);
self.write("</body></html>\n")
@@ -1269,3 +1275,19 @@ class html:
def del_cookie(self, varname):
self.set_cookie(varname, '', time.time() - 60)
+ # Keyboard control
+
+ RETURN = 13
+ SHIFT = 16
+ CTRL = 17
+ ALT = 18
+ BACKSPACE = 8
+
+ def add_keybinding(self, keylist, jscode):
+ self.keybindings.append([keylist, jscode])
+
+ def add_keybindings(self, bindings):
+ self.keybindings += bindings
+
+
+
diff --git a/web/htdocs/js/checkmk.js b/web/htdocs/js/checkmk.js
index 48226d4..82f17b4 100644
--- a/web/htdocs/js/checkmk.js
+++ b/web/htdocs/js/checkmk.js
@@ -1923,3 +1923,55 @@ function timeline_hover(row_nr, onoff)
}
}
+// .--Keybindings---------------------------------------------------------.
+// | _ __ _ _ _ _ |
+// | | |/ /___ _ _| |__ (_)_ __ __| (_)_ __ __ _ ___ |
+// | | ' // _ \ | | | '_ \| | '_ \ / _` | | '_ \ / _` / __|
|
+// | | . \ __/ |_| | |_) | | | | | (_| | | | | | (_| \__ \ |
+// | |_|\_\___|\__, |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___/ |
+// | |___/ |___/ |
+// +----------------------------------------------------------------------+
+
+// var keybindings will be defined dynamically by htmllib.py
+var keybindings_pressedkeys = [];
+
+function keybindings_keydown(e) {
+ if (!e) e = window.event;
+ var keyCode = e.which || e.keyCode;
+ keybindings_pressedkeys.push(keyCode);
+ keybindings_check(e);
+}
+
+function keybindings_keyup(e) {
+ if (!e) e = window.event;
+ var keyCode = e.which || e.keyCode;
+ for (var i in keybindings_pressedkeys) {
+ if (keybindings_pressedkeys[i] == keyCode) {
+ keybindings_pressedkeys.splice(i, 1);
+ break;
+ }
+ }
+}
+
+function keybindings_check(e) {
+ for (var i in keybindings) {
+ var keylist = keybindings[i][0];
+ var jscode = keybindings[i][1];
+ if (keybindings_check_keylist(keylist)) {
+ if (e.stopPropagation)
+ e.stopPropagation();
+ e.cancelBubble = true;
+ eval(jscode);
+ return;
+ }
+ }
+}
+
+function keybindings_check_keylist(keylist)
+{
+ for (var i in keylist) {
+ if (keybindings_pressedkeys.indexOf(keylist[i]) < 0)
+ return false;
+ }
+ return true;
+}
diff --git a/web/htdocs/valuespec.py b/web/htdocs/valuespec.py
index 2d91a9f..8b805ca 100644
--- a/web/htdocs/valuespec.py
+++ b/web/htdocs/valuespec.py
@@ -502,19 +502,12 @@ class HTTPUrl(TextAscii):
(self._target and 'target="%s" ' % self._target or
""),
url, text)
-RETURN = 13
-SHIFT = 16
-CTRL = 17
-ALT = 18
-BACKSPACE = 8
-
class TextAreaUnicode(TextUnicode):
def __init__(self, **kwargs):
TextUnicode.__init__(self, **kwargs)
self._cols = kwargs.get("cols", 60)
self._rows = kwargs.get("rows", 20) # Allowed: "auto" ->
Auto resizing
self._minrows = kwargs.get("minrows", 0) # Minimum number of initial
rows when "auto"
- self._submit_keys = kwargs.get("submit_keys", [])
def value_to_text(self, value):
return "<pre class=ve_textarea>%s</pre>" % value
@@ -532,45 +525,7 @@ class TextAreaUnicode(TextUnicode):
else:
attrs = {}
rows = self._rows
- if self._submit_keys:
- attrs['id'] = "textarea_%s" % varprefix
- commit_code = ""
- for keylist, code in self._submit_keys:
- commit_code += " textarea_%s_trysubmit(e, %r, %s);\n" % (
- varprefix, keylist, repr(code))
-
- html.final_javascript("""
-textarea_text_keys = [];
-function textarea_%s_keydown(e) {
- if (!e) e = window.event;
- var keyCode = e.which || e.keyCode;
- textarea_%s_keys.push(keyCode);
-%s}
-function textarea_%s_keyup(e) {
- if (!e) e = window.event;
- var keyCode = e.which || e.keyCode;
- for (var i in textarea_%s_keys) {
- if (textarea_%s_keys[i] == keyCode) {
- textarea_%s_keys.splice(i, 1);
- break;
- }
- }
-}
-function textarea_%s_trysubmit(e, keylist, code) {
- for (var i in keylist) {
- if (textarea_%s_keys.indexOf(keylist[i]) < 0) {
- return;
- }
- }
- if (e.stopPropagation)
- e.stopPropagation();
- e.cancelBubble = true;
- eval(code);
-}
-var t = document.getElementById("textarea_%s");
-t.onkeydown = function(e) { return textarea_%s_keydown(e); }
-t.onkeyup = function(e) { return textarea_%s_keyup(e); }
-""" % ((varprefix, varprefix, commit_code) + (varprefix,)*9))
+
html.text_area(varprefix, value, rows=rows, cols=self._cols, attrs = attrs)