browser.py :  » Ajax » pyjamas » src » pygtkweb » library » 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 » browser.py
from __pyjamas__ import JS,doc,wnd,get_main_frame

listeners = {}

def mash_attrib(name, joiner='-'):
    return name

def get_listener(item):
    pass

def set_listener(item, listener):
    pass

def round_val(val, digits):
    return JS('val.toFixed(digits);')

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

    def append(self, element):
        JS('''
        this.element.appendChild(element.element);
        ''')

    def prepend(self, element):
        JS('''
        this.element.insertBefore(element.element, self.element.firstChild);
        ''')

    def getX(self):
        JS('''
        var obj = this.element;
        var curleft = 0;
        if (obj.offsetParent) {
            curleft = obj.offsetLeft
            while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
            }
        }
        return curleft;
        ''')

    def getY(self):
        JS('''
        var obj = this.element;
        var curtop = 0;
        if (obj.offsetParent) {
            curtop = obj.offsetTop
            while (obj = obj.offsetParent) {
                curtop += obj.offsetTop
            }
        }
        return curtop;
        ''')

    def getWidth(self):
        JS('''
        return this.element.offsetWidth;
        ''')

    def getHeight(self):
        JS('''
        return this.element.offsetHeight;
        ''')

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

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

    def setStyle(self, property, value):
        JS('''
        this.element.style[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):
        JS('''
        return this.element.style[property];
        ''')

    def setProperty(self, property, value):
        JS('''
        //this.element.setAttribute(property,value);
        this.element[property] = value;
        ''')

    def getProperty(self, property):
        JS('''
        //return this.element.getAttribute(property);
        return this.element[property];
        ''')

    def setHTML(self, content):
        JS('''
        this.element.innerHTML = content;
        ''')

    def getHTML(self):
        JS('''
        return this.element.innerHTML;
        ''')

    def on_browser_event(self, view, e, ignore):
        pass

    def catchEvents(self, name, object):
        JS('''
        var tmp = function(e) {
            if (!e) var e = $wnd.event;
            if (e.target) targ = e.target;
            else if (e.srcElement) targ = e.srcElement;
            if (targ.nodeType == 3) targ = targ.parentNode;
            if (targ.__ref)
                object.dom_event(e, targ.__ref);
            else
                object.dom_event(e, null);
        };
        ''')
        name = name[0]
        self.activeEvents.append((name, object))
        JS('''
        var old_callback = this.element['on'+name];
        this.element['on'+name] = function(e){if(old_callback){old_callback(e);}tmp(e);};
        ''')

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

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

    @staticmethod
    def setContent(message):
        JS('''
        $doc.body.innerHTML = message;
        ''')

    @staticmethod
    def setTitle(title):
        JS('''
        $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.