annotate browser_side/nativedom.py @ 216:9827cda1a6b0

browser_side: added key listener to login and register panels - pressing enter set the focus to the next field or validate the form fix bug 29
author souliane <souliane@mailoo.org>
date Sun, 08 Sep 2013 13:40:01 +0200
parents 9763dec220ed
children 43881c3dda9d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
165
9763dec220ed dates update
Goffi <goffi@goffi.org>
parents: 142
diff changeset
6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org>
142
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 This class provide basic DOM parsing based on native javascript parser
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 __init__ code comes from Tim Down at http://stackoverflow.com/a/8412989
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 """
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from __pyjamas__ import JS
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class Node():
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self._node = js_node
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35 def _jsNodesList2List(self, js_nodes_list):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 ret=[]
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 for i in range(len(js_nodes_list)):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 #ret.append(Element(js_nodes_list.item(i)))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 ret.append(self.__class__(js_nodes_list.item(i))) # XXX: Ugly, but used to word around a Pyjamas's bug
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return ret
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 def nodeName(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return self._node.nodeName
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def childNodes(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return self._jsNodesList2List(self._node.childNodes)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def getAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return self._node.getAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def hasAttribute(self, attr):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 return self._node.hasAttribute(attr)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 class Element(Node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, js_node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60 Node.__init__(self, js_node)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def getElementsByTagName(self, tagName):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 return self._jsNodesList2List(self._node.getElementsByTagName(tagName))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 class Document(Node):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, js_document):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 Node.__init__(self, js_document)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 @property
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def documentElement(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return Element(self._node.documentElement)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 class NativeDOM:
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def __init__(self):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 JS("""
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if (typeof window.DOMParser != "undefined") {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 } else if (typeof window.ActiveXObject != "undefined" &&
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 new window.ActiveXObject("Microsoft.XMLDOM")) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 this.parseXml = function(xmlStr) {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 xmlDoc.async = "false";
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 xmlDoc.loadXML(xmlStr);
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return xmlDoc;
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 };
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 } else {
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 throw new Error("No XML parser found");
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 }
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 """)
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def parseString(self, xml):
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return Document(self.parseXml(xml))
f6aeeb753c06 browser side: ultra-minimalist native DOM implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98