def __init__(self, container=document.body, parent=None, default_css=True): """Create a new menu, inserted inside the container. For the top level menu, parent is None, otherwise it is a SPAN element (top menu) or a TR element (submenu).""" self.container = container self.parent = parent if default_css: # Insert default CSS stylesheet if not already loaded for stylesheet in document.styleSheets: if stylesheet.ownerNode.id == "brython-menu": break else: document <= html.STYLE(style_sheet, id="brython-menu") self.default_css = default_css if parent: parent.submenu = html.TABLE(Class="brython-menu-submenu") parent.submenu.style.position = "absolute" parent.submenu.style.display = "none" self.container <= parent.submenu parent.bind("click", self.unfold) if not hasattr(self.container, "bind_document"): # Click on the document outside of the menu removes all submenus document.bind("click", self.hide_menus) self.container.bind_document = True
def __init__(self, title="", *, top=None, left=None, ok_cancel=False, default_css=True): if default_css: for stylesheet in document.styleSheets: if stylesheet.ownerNode.id == "brython-dialog": break else: document <= html.STYLE(style_sheet, id="brython-dialog") html.DIV.__init__(self, style=dict(position="absolute"), Class="brython-dialog-main") #set_style(self, "dialog-main") self.title_bar = html.DIV(html.SPAN(title), Class="brython-dialog-title") self <= self.title_bar self.close_button = html.SPAN("×", Class="brython-dialog-close") self.title_bar <= self.close_button self.close_button.bind("click", self.close) self.panel = html.DIV(Class="brython-dialog-panel") self <= self.panel if ok_cancel: ok_cancel_zone = html.DIV(style={"text-align": "center"}) ok, cancel = "Ok", "Cancel" if isinstance(ok_cancel, (list, tuple)): if not len(ok_cancel) == 2: raise ValueError( f"ok_cancel expects 2 elements, got {len(ok_cancel)}") ok, cancel = ok_cancel self.ok_button = html.BUTTON(ok, Class="brython-dialog-button") self.cancel_button = html.BUTTON(cancel, Class="brython-dialog-button") self.cancel_button.bind("click", self.close) ok_cancel_zone <= self.ok_button + self.cancel_button self <= ok_cancel_zone document <= self cstyle = window.getComputedStyle(self) # Center horizontally and vertically if left is None: width = round(float(cstyle.width[:-2]) + 0.5) left = int((window.innerWidth - width) / 2) self.left = left self.style.left = f'{left}px' if top is None: height = round(float(cstyle.height[:-2]) + 0.5) top = int((window.innerHeight - height) / 2) # top is relative to document scrollTop top += document.scrollingElement.scrollTop self.top = top self.style.top = f'{top}px' self.title_bar.bind("mousedown", self.mousedown) self.title_bar.bind("touchstart", self.mousedown) self.title_bar.bind("mouseup", self.mouseup) self.title_bar.bind("touchend", self.mouseup) self.bind("leave", self.mouseup) self.is_moving = False
def __init__(self, elt_id=None, title="Interactive Interpreter", globals=None, locals=None, history=None, rows=30, cols=84, default_css=True, clear_zone=True, banner=True): """ Create the interpreter. - "elt_id" is the id of a textarea in the document. If not set, a new popup window is added with a textarea. - "globals" and "locals" are the namespaces the RPEL runs in - "history", if set, must be a list of strings """ if default_css: # Insert default CSS stylesheet if not already loaded for stylesheet in document.styleSheets: if stylesheet.ownerNode.id == "brython-interpreter": break else: document <= html.STYLE(style_sheet, id="brython-interpreter") if elt_id is None: self.dialog = Dialog(title=title, top=10, left=10, default_css=default_css) self.zone = html.TEXTAREA(rows=rows, cols=cols, Class="brython-interpreter") self.dialog.panel <= self.zone else: if isinstance(elt_id, str): try: elt = document[elt_id] if elt.tagName != "TEXTAREA": raise ValueError( f"element {elt_id} is a {elt.tagName}, " + "not a TEXTAREA") self.zone = elt except KeyError: raise KeyError(f"no element with id '{elt_id}'") elif isinstance(elt_id, DOMNode): if elt_id.tagName == "TEXTAREA": self.zone = elt_id else: raise ValueError("element is not a TEXTAREA") else: raise ValueError( "element should be a string or " + f"a TEXTAREA, got '{elt_id.__class__.__name__}'") v = sys.implementation.version if clear_zone: self.zone.value = '' if banner: self.zone.value += ( f"Brython {v[0]}.{v[1]}.{v[2]} on " f"{window.navigator.appName} {window.navigator.appVersion}" "\n") self.zone.value += ">>> " self.cursor_to_end() self._status = "main" self.history = history or [] self.current = len(self.history) self.globals = {} if globals is None else globals self.globals.update(editor_ns) self.locals = self.globals if locals is None else locals self.buffer = '' sys.stdout.write = sys.stderr.write = self.write sys.stdout.__len__ = sys.stderr.__len__ = lambda: len(self.buffer) self.zone.bind('keypress', self.keypress) self.zone.bind('keydown', self.keydown) self.zone.bind('mouseup', self.mouseup) self.zone.focus()
# -*- coding: utf-8 -*- from browser import document, html, webcomponent from .router import Router from .vdom import VirtualDOM, VirtualEventer EventStore: dict = {} MapsiCompStyle = html.STYLE() MapsiCompStyle.clear() MapsiCompStyle.setAttribute("class", "mapsi-comp") document.head <= MapsiCompStyle class Component: template: str = "" el: str = "" def __init__(self): template_replaced = self.template.strip() for comp in Mapsi.components: if not comp in ("comp-main-app", self.el): comp_tag = comp[5:-4].lower() dom_comp_tag = comp[5:] template_replaced = template_replaced.replace( f"<{comp_tag} ", f"<{dom_comp_tag} ").replace( f"<{comp_tag}>", f"<{dom_comp_tag}>").replace(f"</{comp_tag}>", f"</{dom_comp_tag}>") if "<content></content>" in template_replaced:
def __setup__(): document.head <= html.STYLE(CSS, type="text/css", media="screen") Popup(Cena())
button.nice{ margin-right: 15%; color: #fff; background: #7ae; border-width: 2px; border-style: solid; border-radius: 5px; border-color: #45b; text-align: center; font-size: 15px; padding: 6px; } """ document.body <= html.STYLE(css) state = "off" div_style = {"position": "absolute", "left": int(width * 0.5), "paddingLeft": "1em", "padding-right": "1em", "backgroundColor": "#ccc", "borderStyle": "solid", "borderColor" : "#888", "borderWidth": "0px 0px 0px 3px", "color": "#113", "font-size": "12px" }
from browser import ( ajax, document, html, ) main = document['main'] name = "Max" age = 26 main.textContent = f"Hello {name}, you are {age:04d}!" main.style = dict(backgroundColor='blue', color='white') main.style = dict(color='green', notattr=10) # Style main <= html.STYLE(".huge { font-size: 50px; }") # Toggle the class button = html.BUTTON("Toggle Largess") button.bind('click', lambda e: main.classList.toggle("huge")) main <= button # My favorite things favorites = [ "Programming", "Sweets", "Westworld", ] ul = html.UL([html.LI(fav) for fav in favorites])