]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/source/class/org/argeo/ria/util/Element.js
Better way to handle commands'submenus, to be able to duplicate them in context menus.
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / source / class / org / argeo / ria / util / Element.js
1 /**
2 * Cross browser XML Element API
3 *
4 * Overrides the Qooxdoo qx.xml.Element to handle the namespace prefixes
5 *
6 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/81f3de54-3b79-46dc-8e01-73ca2d94cdb5.asp
7 * http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML
8 */
9 qx.Class.define("org.argeo.ria.util.Element",
10 {
11
12 statics :
13 {
14 /**
15 * Selects the first XmlNode that matches the XPath expression.
16 *
17 * @param element {Element | Document} root element for the search
18 * @param query {String} XPath query
19 * @param NSMap (Object) A map matching namespace prefixes to namespace URIS;
20 * @return {Element} first matching element
21 * @signature function(element, query, NSMap)
22 */
23 selectSingleNode : qx.core.Variant.select("qx.client",
24 {
25 "mshtml|opera": function(element, query, NSMap) {
26 if(NSMap){
27 var namespaces = [];
28 var i=0;
29 for(var prefix in NSMap){
30 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
31 i++;
32 }
33 var doc = element.ownerDocument || element;
34 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
35 }
36 try{
37 return element.selectSingleNode(query);
38 }catch(err){}
39 },
40
41 "default": function(element, query, NSMap)
42 {
43 if(!this.__xpe) {
44 this.__xpe = new XPathEvaluator();
45 }
46
47 var xpe = this.__xpe;
48
49 try {
50 var resolver;
51 if(NSMap){
52 resolver = function(prefix){
53 return NSMap[prefix] || null;
54 }
55 }else{
56 resolver = xpe.createNSResolver(element);
57 }
58 //return xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
59 return xpe.evaluate(query, element, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
60 } catch(err) {
61 throw new Error("selectSingleNode: query: " + query + ", element: " + element + ", error: " + err);
62 }
63 }
64 }),
65
66
67 /**
68 * Selects a list of nodes matching the XPath expression.
69 *
70 * @param element {Element | Document} root element for the search
71 * @param query {String} XPath query
72 * @param NSMap {Map} Mapping between namespaces prefixes and URI.
73 * @return {Element[]} List of matching elements
74 * @signature function(element, query, NSMap)
75 */
76 selectNodes : qx.core.Variant.select("qx.client",
77 {
78 "mshtml|opera": function(element, query, NSMap) {
79 if(NSMap){
80 var namespaces = [];
81 var i=0;
82 for(var prefix in NSMap){
83 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
84 i++;
85 }
86 var doc = element.ownerDocument || element;
87 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
88 }
89 return element.selectNodes(query);
90 },
91
92 "default": function(element, query, NSMap)
93 {
94 var xpe = this.__xpe;
95
96 if(!xpe) {
97 this.__xpe = xpe = new XPathEvaluator();
98 }
99
100 try {
101 var resolver;
102 if(NSMap){
103 resolver = function(prefix){
104 return NSMap[prefix] || null;
105 }
106 }else{
107 resolver = xpe.createNSResolver(element);
108 }
109 //var result = xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
110 var result = xpe.evaluate(query, element, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
111 } catch(err) {
112 throw new Error("selectNodes: query: " + query + ", element: " + element + ", error: " + err);
113 }
114
115 var nodes = [];
116 for (var i=0; i<result.snapshotLength; i++) {
117 nodes[i] = result.snapshotItem(i);
118 }
119
120 return nodes;
121 }
122 }),
123
124
125 /**
126 * Returns a list of elements with the given tag name belonging to the given namespace (http://developer.mozilla.org/en/docs/DOM:element.getElementsByTagNameNS).
127 *
128 * @param element {Element | Document} the element from where the search should start.
129 * Note that only the descendants of this element are included in the search, not the node itself.
130 * @param namespaceURI {var} is the namespace URI of elements to look for . For example, if you need to look
131 * for XHTML elements, use the XHTML namespace URI, <tt>http://www.w3.org/1999/xhtml</tt>.
132 * @param tagname {String} the tagname to look for
133 * @return {Element[]} a list of found elements in the order they appear in the tree.
134 * @signature function(element, namespaceURI, tagname)
135 */
136 getElementsByTagNameNS : qx.core.Variant.select("qx.client",
137 {
138 "mshtml": function(element, namespaceURI, tagname)
139 {
140 var doc = element.ownerDocument || element;
141
142 doc.setProperty("SelectionLanguage", "XPath");
143 doc.setProperty("SelectionNamespaces", "xmlns:ns='" + namespaceURI + "'");
144
145 return qx.xml.Element.selectNodes(element, 'descendant-or-self::ns:' + tagname);
146 },
147
148 "default": function(element, namespaceURI, tagname) {
149 return element.getElementsByTagNameNS(namespaceURI, tagname);
150 }
151 }),
152
153
154 /**
155 * Selects the first XmlNode that matches the XPath expression and returns the text content of the element
156 *
157 * @param element {Element|Document} root element for the search
158 * @param query {String} XPath query
159 * @param NSMap {Object} Mapping between NS prefix / uri
160 * @return {String} the joined text content of the found element or null if not appropriate.
161 * @signature function(element, query)
162 */
163 getSingleNodeText : function(element, query, NSMap)
164 {
165 var node = this.selectSingleNode(element, query, NSMap);
166 return qx.dom.Node.getText(node);
167 }
168 }
169 });