Package pyjamas :: Package ui :: Module HTMLPanel
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.HTMLPanel

 1  # Copyright 2006 James Tauber and contributors 
 2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
 3  # 
 4  # Licensed under the Apache License, Version 2.0 (the "License"); 
 5  # you may not use this file except in compliance with the License. 
 6  # You may obtain a copy of the License at 
 7  # 
 8  #     http://www.apache.org/licenses/LICENSE-2.0 
 9  # 
10  # Unless required by applicable law or agreed to in writing, software 
11  # distributed under the License is distributed on an "AS IS" BASIS, 
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13  # See the License for the specific language governing permissions and 
14  # limitations under the License. 
15  from pyjamas import DOM 
16  from pyjamas import Factory 
17  from pyjamas.ui.InnerHTML import InnerHTML 
18   
19  from pyjamas.ui.ComplexPanel import ComplexPanel 
20   
21  HTMLPanel_sUid = 0 
22 23 -def _check_tagname(element, tagname):
24 try: 25 element_tagname = element.nodeName 26 element_tagname = str(element_tagname).lower() 27 except: 28 element_tagname = None 29 if element_tagname is not None and element_tagname == tagname: 30 return element 31 return None
32
33 -def getElementsByTagName(element, tagname):
34 res = [] 35 el = _check_tagname(element, tagname) 36 if el: 37 res.append(el) 38 39 it = DOM.walkChildren(element) 40 while True: 41 try: 42 child = it.next() 43 except StopIteration: 44 break 45 el = _check_tagname(child, tagname) 46 if el: 47 res.append(el) 48 return res
49
50 -def getElementById(element, id):
51 try: 52 element_id = DOM.getAttribute(element, "id") 53 except: 54 element_id = None 55 if element_id is not None and element_id == id: 56 return element 57 58 child = DOM.getFirstChild(element) 59 while child is not None: 60 ret = getElementById(child, id) 61 if ret is not None: 62 return ret 63 child = DOM.getNextSibling(child) 64 65 return None
66
67 68 -class HTMLPanel(ComplexPanel, InnerHTML):
69 - def __init__(self, html, **ka):
70 # NOTE! don't set a default style on this panel, because the 71 # HTML might expect to have one already. Explicitly add a StyleName 72 # if one is needed. 73 #if not ka.has_key('StyleName'): ka['StyleName']="gwt-HTMLPanel" 74 if html: ka['HTML'] = html 75 element = ka.pop('Element', None) or DOM.createDiv() 76 self.setElement(element) 77 ComplexPanel.__init__(self, **ka)
78
79 - def add(self, widget, id):
80 element = getElementById(self.getElement(), id) 81 if element is None: 82 raise Exception("HTMLPanel.add: no element with id '%s'" % str(id)) 83 ComplexPanel.add(self, widget, element)
84
85 - def findTags(self, tagname):
86 return getElementsByTagName(self.getElement(), tagname)
87 88 @staticmethod
89 - def createUniqueId():
90 global HTMLPanel_sUid 91 92 HTMLPanel_sUid += 1 93 return "HTMLPanel_%d" % HTMLPanel_sUid
94 95 Factory.registerClass('pyjamas.ui.HTMLPanel', 'HTMLPanel', HTMLPanel) 96