browserhulahop.py :  » Ajax » pyjamas » src » pygtkweb » library » platform » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Ajax » pyjamas 
pyjamas » src » pygtkweb » library » platform » browserhulahop.py
def mash_attrib(name, joiner='-'):
    res = ''
    for c in name:
        if c.isupper():
            res += joiner + c.lower()
        else:
            res += c
    return res

def round_val(val, digits):
    return round(val, digits)

def get_listener(eventtype, item):
    #print "get_listener", eventtype, item
    #print "listeners", listeners
    if item is None:
        return None
    if hasattr(item, "__instance__"):
        ret = listeners.get(eventtype, {}).get(item.__instance__)
    else:
        ret = listeners.get(eventtype, {}).get(hash(item))
    return ret

def set_listener(eventtype, item, listener):
    #print "set_listener", eventtype, item, listener
    if not listeners.has_key(eventtype):
        listeners[eventtype] = {}
    if hasattr(item, "__instance__"):
        listeners[eventtype][item.__instance__] = listener
    else:
        listeners[eventtype][hash(item)] = listener

class Element:
    def __init__(self, tag=None, element=None):
        if tag is not None:
            self.element = doc().createElement(tag)
        elif element is not None:
            self.element = element
        else:
            raise Exception("Cannot create Element without tag or element")
        
        self.activeEvents = []

    def append(self, element):
        self.element.appendChild(element.element)

    def prepend(self, element):
        self.element.insertBefore(element.element, self.element.firstChild)

    def getX(self):
        obj = self.element
        curleft = 0
        if (obj.offsetParent):
            curleft = obj.offsetLeft
            while obj.offsetParent:
                curleft += obj.offsetParent.offsetLeft
                obj = obj.offsetParent
        return curleft

    def getY(self):
        obj = self.element
        curtop = 0
        if (obj.offsetParent):
            curtop = obj.offsetTop
            while obj.offsetParent:
                curtop += obj.offsetParent.offsetTop
                obj = obj.offsetParent
        return curtop

    def getWidth(self):
        return self.element.offsetWidth

    def getHeight(self):
        return self.element.offsetHeight

    def setWidth(self, width):
        self.setStyle('width',width)

    def setHeight(self, height):
        self.setStyle('height',height)

    def setStyle(self, property, value):
        self.element.style.setProperty(mash_attrib(property), value, "")

    def setPxStyle(self, property, value):
        self.setStyle(property, "%dpx" % value)

    def setPercentStyle(self, property, value):
        self.setStyle(property, "%d%%" % value)

    def getStyle(self, property):
        return this.element.style.getProperty(mash_attrib(property))

    def setProperty(self, property, value):
        self.element.setAttribute(mash_attrib(property), value)

    def getProperty(self, property):
        return this.element.getAttribute(mash_attrib(property))

    def setHTML(self, content):
        self.element.innerHTML = content

    def getHTML(self):
        return self.element.innerHTML

    def on_browser_event(self, view, e, ignore):
        #print "on_browser_event", view, e, ignore
        if not e:
            e = wnd().event
        if e.target:
            targ = e.target
        elif e.srcElement:
            targ = e.srcElement
        while targ and not get_listener(e.type, targ):
            #print "no parent listener", curElem, getParent(curElem)
            targ = targ.parentNode
        if targ and targ.nodeType != 1:
            targ = targ.parentNode

        listener = get_listener(e.type, targ)
        if listener:
            listener.dom_event(e, self)

    def catchEvents(self, name, object):
        name = name[0]
        self.activeEvents.append((name, object))
        if not get_listener(name, self.element):
            set_listener(name, self.element, object)
            mf = get_main_frame()
            mf.addEventListener(self.element, name, self.on_browser_event)

class Document:
    window = Element(element= wnd())
    document = Element(element= doc())
    body = Element(element= doc().body)

    @staticmethod
    def createElement(tag):
        return Element(tag)
    
    @staticmethod
    def append(element):
        doc().body.appendChild(element.element)

    @staticmethod
    def setContent(message):
        doc().body.innerHTML = message

    @staticmethod
    def setTitle(title):
        doc().title = title

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.