def getProperty(obj, propertyName): """ Reads an object given a property and returns it as a JavaScriptObject @param object @param propertyName @return the object """ DOM.getAttribute(obj, propertyName)
def pygwt_processMetas(): from pyjamas import DOM metas = doc().getElementsByTagName("meta") for i in range(metas.length): meta = metas.item(i) name = DOM.getAttribute(meta, "name") if name == "pygwt:module": content = DOM.getAttribute(meta, "content") if content: pygwt_moduleNames.append(content) return pygwt_moduleNames
def createWidgetOnElement(element): fc = DOM.getAttribute(element, 'id') lbr = fc.find("(") klsname = fc[:lbr] txtargs = fc[lbr+1:-1] args = [] kwargs = {} for arg in txtargs.split(','): findeq = arg.find('=') if findeq == -1: args.append(arg) else: k = arg[:findeq] v = arg[findeq+1:] if ((v[0] == "'" and v[-1] == "'") or (v[0] == '"' and v[-1] == '"')): # string - strip quotes v = v[1:-1] else: # assume it's an int v = int(v) kwargs[k] = v kwargs['Element'] = element return lookupClass(klsname)(*args, **kwargs)
def identify(self, element): if element.nodeType != 1: return False if str(string.lower(element.tagName)) != 'span': return False style = DOM.getAttribute(element, "className") return style and style.startswith("editor-")
def setStyleName(element, style, add): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: oldStyle = "" idx = oldStyle.find(style) # Calculate matching index lastPos = len(oldStyle) while idx != -1: if idx == 0 or (oldStyle[idx - 1] == " "): last = idx + len(style) if (last == lastPos) or ((last < lastPos) and (oldStyle[last] == " ")): break idx = oldStyle.find(style, idx + 1) if add: if idx == -1: DOM.setAttribute(element, "className", oldStyle + " " + style) else: if idx != -1: if idx == 0: begin = '' else: begin = oldStyle[:idx-1] end = oldStyle[idx + len(style):] DOM.setAttribute(element, "className", begin + end)
def createWidgetOnElement(element): fc = DOM.getAttribute(element, 'id') lbr = fc.find("(") klsname = fc[:lbr] txtargs = fc[lbr + 1:-1] args = [] kwargs = {} for arg in txtargs.split(','): findeq = arg.find('=') if findeq == -1: args.append(arg) else: k = arg[:findeq] v = arg[findeq + 1:] if ((v[0] == "'" and v[-1] == "'") or (v[0] == '"' and v[-1] == '"')): # string - strip quotes v = v[1:-1] else: # assume it's an int v = int(v) kwargs[k] = v kwargs['Element'] = element return lookupClass(klsname)(*args, **kwargs)
def setStyleName(element, style, add): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: oldStyle = "" idx = oldStyle.find(style) # Calculate matching index lastPos = len(oldStyle) while idx != -1: if idx == 0 or (oldStyle[idx - 1] == " "): last = idx + len(style) if (last == lastPos) or ((last < lastPos) and (oldStyle[last] == " ")): break idx = oldStyle.find(style, idx + 1) if add: if idx == -1: DOM.setAttribute(element, "className", oldStyle + " " + style) else: if idx != -1: if idx == 0: begin = '' else: begin = oldStyle[:idx - 1] end = oldStyle[idx + len(style):] DOM.setAttribute(element, "className", begin + end)
def drawImage(self, img, *args): # create an image element: # <image xlink:href="firefox.jpg" x="0" y="0" height="50px" width="50px"/> # The way to do this is to create a clipping container and place the image inside that, # using scaling and positioning to approximate some of the effect of sourceXY and destXY # for now, we ignore all this # # get the image size and URL - image may be a Image Widget or an <img> Element if isinstance(img, Widget): # Image widget img_elem = img.getElement() url = img.getUrl() else: # <img> element img_elem = img url = DOM.getAttribute(img, "src") # determine all parms sourceXY = None sourceWH = None destXY = None destWH = None # get the parms that are specified if len(args) == 8: # all parms specified sourceXY = (args[0], args[1]) sourceWH = (args[2], args[3]) destXY = (args[4], args[5]) destWH = (args[6], args[7]) elif len(args) == 4: # only destXY and destWH are specified destXY = (args[0], args[1]) destWH = (args[2], args[3]) elif len(args) == 2: # we just get destXY destXY = (args[0], args[1]) # by default we keep the same size destWH = (img_elem.width, img_elem.height) else: raise TypeError("Wrong number of arguments for SVGCanvas.drawImage") # sourceWH and destWH determine the scaling # sourceXY and scaling determine where to place the image inside the clipping container # destXY determines where to place the clipping container # for now just create an SVG image element: # <image xlink:href="firefox.jpg" x="0" y="0" height="50px" width="50px"/> # print 'Create: <image xlink:href="'+url+'" x="'+str(destXY[0])+'" y="'+str(destXY[1])+'" height="'+str(destWH[1])+'px" width="'+str(destWH[0])+'px"/>' image = self._createElementSVG("image") # set the URL image.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", url); # set the pos/dimensions DOM.setElemAttribute(image, "x", str(int(destXY[0]))) DOM.setElemAttribute(image, "y", str(int(destXY[1]))) DOM.setElemAttribute(image, "width", str(int(destWH[0]))) DOM.setElemAttribute(image, "height", str(int(destWH[1]))) # add the element to the canvas self._addElementSVG(image)
def getEventTargetCell(self, event): td = DOM.eventGetTarget(event) while td is not None: if DOM.getAttribute(td, "tagName").lower() == "td": tr = DOM.getParent(td) body = DOM.getParent(tr) if DOM.compare(body, self.bodyElem): return td if DOM.compare(td, self.bodyElem): return None td = DOM.getParent(td) return None
def getElementById(element, id): try: element_id = DOM.getAttribute(element, "id") except: element_id = None if element_id is not None and element_id == id: return element child = DOM.getFirstChild(element) while child is not None: ret = getElementById(child, id) if ret is not None: return ret child = DOM.getNextSibling(child) return None
def findStyleName(element, style): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: return -1 idx = oldStyle.find(style) # Calculate matching index lastPos = len(oldStyle) while idx != -1: if idx == 0 or (oldStyle[idx - 1] == " "): last = idx + len(style) if (last == lastPos) or ((last < lastPos) and (oldStyle[last] == " ")): break idx = oldStyle.find(style, idx + 1) return idx
def setDragImage(self, element, x, y): position_absolute = DOM.getStyleAttribute(element, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = x + DOM.getAbsoluteLeft(element.offsetParent) self.dragTopOffset = y + DOM.getAbsoluteTop(element.offsetParent) else: self.dragLeftOffset = x self.dragTopOffset = y if element.tagName.lower().endswith('img'): src = DOM.getAttribute(element, 'src') element = DOM.createElement('img') DOM.setAttribute(element, 'src', src) if not self.draggingImage: self.createDraggingImage(element) else: self.draggingImage.setImage(element)
def findStyleName(element, style): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: return -1 idx = oldStyle.find(style) # Calculate matching index lastPos = len(oldStyle) while idx != -1: if idx == 0 or (oldStyle[idx - 1] == " "): last = idx + len(style) if (last == lastPos) or ((last < lastPos) and \ (oldStyle[last] == " ")): break idx = oldStyle.find(style, idx + 1) return idx
def setDragImage(self, element, x, y): position_absolute = DOM.getStyleAttribute(element, 'position') == 'absolute' if position_absolute: self.dragLeftOffset = x + DOM.getAbsoluteLeft( element.offsetParent) self.dragTopOffset = y + DOM.getAbsoluteTop( element.offsetParent) else: self.dragLeftOffset = x self.dragTopOffset = y if element.tagName.lower().endswith('img'): src = DOM.getAttribute(element,'src') element = DOM.createElement('img') DOM.setAttribute(element, 'src', src) if not self.draggingImage: self.createDraggingImage(element) else: self.draggingImage.setImage(element)
def onDragStart(self, event): dt = event.dataTransfer target = DOM.eventGetTarget(event) target = Widget(Element=target) try: id = target.getID() except: id = '' if id == 'datadrag0': dt.setData('text/plain', 'Hello World!') elif id == 'datadrag1': logo = doc().getElementById('logo') logo_parent_element = DOM.getParent(logo) text = DOM.getInnerText(logo_parent_element) html = DOM.getInnerHTML(logo_parent_element) uri = DOM.getAttribute(logo, 'src') dt.setData('text/plain', text) dt.setData('text/html', html) dt.setData('text/uri-list', uri) elif id == 'datadrag2': dt.setData('x-star-trek/tribble', 'I am a tribble')
def setStyleName(element, style, add): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: oldStyle = "" idx = findStyleName(element, style) if add: if idx == -1: DOM.setAttribute(element, "className", oldStyle + " " + style) return if idx == -1: return if idx == 0: begin = '' else: begin = oldStyle[:idx-1] end = oldStyle[idx + len(style):] DOM.setAttribute(element, "className", begin + end)
def setStyleName(element, style, add): oldStyle = DOM.getAttribute(element, "className") if oldStyle is None: oldStyle = "" idx = findStyleName(element, style) if add: if idx == -1: DOM.setAttribute(element, "className", oldStyle + " " + style) return if idx == -1: return if idx == 0: begin = '' else: begin = oldStyle[:idx - 1] end = oldStyle[idx + len(style):] DOM.setAttribute(element, "className", begin + end)
def getStyleName(self, row, column): return DOM.getAttribute(self.getElement(row, column), "className")
def getStyleName(self): return DOM.getAttribute(self.element, "className")
def getMin(self): return DOM.getAttribute(self.getElement(), "min")
def getAttr(self, row, column, attr): elem = self.getElement(row, column) return DOM.getAttribute(elem, attr)
def drawImage(self, img, *args): if isinstance(img, Widget): img = img.getElement() fullWidth = img.width fullHeight = img.height if len(args) == 8: sourceX = args[0] sourceY = args[1] sourceWidth = args[2] sourceHeight = args[3] destX = args[4] destY = args[5] destWidth = args[6] destHeight = args[7] elif len(args) == 4: sourceX = 0 sourceY = 0 sourceWidth = fullWidth sourceHeight = fullHeight destX = args[0] destY = args[1] destWidth = args[2] destHeight = args[3] elif len(args) == 2: sourceX = 0 sourceY = 0 sourceWidth = fullWidth sourceHeight = fullHeight destX = args[0] destY = args[1] destWidth = fullWidth destHeight = fullHeight vmlStr = [] # JSOStack.getScratchArray() vmlStr.append("<v:group style=\"position:absolute;width:10;height:10;") dX = self.getCoordX(self.matrix, destX, destY) dY = self.getCoordY(self.matrix, destX, destY) # If we have a transformation matrix with rotation/scale, we # apply a filter if self.context.matrix[0] != 1 or self.context.matrix[1] != 0: # We create a padding bounding box to prevent clipping. vmlStr.append("padding-right:") vmlStr.append(str(self.parentWidth) + "px;") vmlStr.append("padding-bottom:") vmlStr.append(str(self.parentHeight) + "px;") vmlStr.append( "filter:progid:DXImageTransform.Microsoft.Matrix(M11='") vmlStr.append("" + str(self.matrix[0])) vmlStr.append("',") vmlStr.append("M12='") vmlStr.append("" + str(self.matrix[1])) vmlStr.append("',") vmlStr.append("M21='") vmlStr.append(str(self.matrix[3])) vmlStr.append("',") vmlStr.append("M22='") vmlStr.append(str(self.matrix[4])) vmlStr.append("',") vmlStr.append("Dx='") vmlStr.append(str(math.floor(((dX / 10))))) vmlStr.append("',") vmlStr.append("Dy='") vmlStr.append(str(math.floor(((dY / 10))))) vmlStr.append("', SizingMethod='clip');") else: vmlStr.append("left:") vmlStr.append("%dpx;" % int(dX / 10)) vmlStr.append("top:") vmlStr.append("%dpx" % int(dY / 10)) vmlStr.append( "\" coordsize=\"100,100\" coordorigin=\"0,0\"><v:image src=\"") vmlStr.append(DOM.getAttribute(img, "src")) vmlStr.append("\" style=\"") vmlStr.append("width:") vmlStr.append(str(int(destWidth * 10))) vmlStr.append(";height:") vmlStr.append(str(int(destHeight * 10))) vmlStr.append(";\" cropleft=\"") vmlStr.append(str(sourceX / fullWidth)) vmlStr.append("\" croptop=\"") vmlStr.append(str(sourceY / fullHeight)) vmlStr.append("\" cropright=\"") vmlStr.append(str((fullWidth - sourceX - sourceWidth) / fullWidth)) vmlStr.append("\" cropbottom=\"") vmlStr.append(str((fullHeight - sourceY - sourceHeight) / fullHeight)) vmlStr.append("\"/></v:group>") self.insert("BeforeEnd", ''.join(vmlStr))
def getPlaceholder(self): return DOM.getAttribute(self.getElement(), "placeholder")
def getKind(self): return DOM.getAttribute(self.getElement(), "type")
def getDefaultValue(self): return DOM.getAttribute(self.getElement(), "defaultValue")
def getText(self): return DOM.getAttribute(self.getElement(), "value")
def getUrl(self): return DOM.getAttribute(self.getElement(), "src")
def getFilename(self): return DOM.getAttribute(self.getElement(), "value")
def getTitle(self): return DOM.getAttribute(self.element, "title")
def getStyleName(self, row): return DOM.getAttribute(self.getElement(row), "className")
def getID(self): """Get the id attribute of the associated DOM element.""" return DOM.getAttribute(self.getElement(), "id")
def drawImage(self, img, *args): """ Draws an input image at a given position on the canvas. Resizes image according to specified width and height and samples from the specified sourceY and sourceX. We recommend that the pixel and coordinate spaces be the same to provide consistent positioning and scaling results option 1: @param img the image to be drawn @param destX x coord of the top left corner in the destination space @param destY y coord of the top left corner in the destination space option 2: @param img The image to be drawn @param destX x coord of the top left corner in the destination space @param destY y coord of the top left corner in the destination space @param destWidth the width of drawn image in the destination @param destHeight the height of the drawn image in the destination option 3: @param img the image to be drawn @param sourceX the start X position in the source image @param sourceY the start Y position in the source image @param sourceWidth the width in the source image you want to sample @param sourceHeight the height in the source image you want to sample @param destX x coord of the top left corner in the destination space @param destY y coord of the top left corner in the destination space @param destWidth the width of drawn image in the destination @param destHeight the height of the drawn image in the destination """ # create an image element: # <image xlink:href="firefox.jpg" x="0" y="0" height="50px" width="50px"/> # The way to do this is to create a clipping container and place the image inside that, # using scaling and positioning to approximate some of the effect of sourceXY and destXY # for now, we ignore all this # # get the image size and URL - image may be a Image Widget or an <img> Element if isinstance(img, Widget): # Image widget img_elem = img.getElement() url = img.getUrl() else: # <img> element img_elem = img url = DOM.getAttribute(img, "src") # determine all parms sourceXY = None sourceWH = None destXY = None destWH = None # get the parms that are specified if len(args) == 8: # all parms specified sourceXY = (args[0], args[1]) sourceWH = (args[2], args[3]) destXY = (args[4], args[5]) destWH = (args[6], args[7]) elif len(args) == 4: # only destXY and destWH are specified destXY = (args[0], args[1]) destWH = (args[2], args[3]) elif len(args) == 2: # we just get destXY destXY = (args[0], args[1]) # by default we keep the same size destWH = (img_elem.width, img_elem.height) else: raise TypeError("Wrong number of arguments for SVGCanvas.drawImage") # sourceWH and destWH determine the scaling # sourceXY and scaling determine where to place the image inside the clipping container # destXY determines where to place the clipping container # for now just create an SVG image element: # <image xlink:href="firefox.jpg" x="0" y="0" height="50px" width="50px"/> # print 'Create: <image xlink:href="'+url+'" x="'+str(destXY[0])+'" y="'+str(destXY[1])+'" height="'+str(destWH[1])+'px" width="'+str(destWH[0])+'px"/>' image = self._createElementSVG("image") # set the URL image.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", url); # set the pos/dimensions DOM.setElemAttribute(image, "x", str(int(destXY[0]))) DOM.setElemAttribute(image, "y", str(int(destXY[1]))) DOM.setElemAttribute(image, "width", str(int(destWH[0]))) DOM.setElemAttribute(image, "height", str(int(destWH[1]))) # add the element to the canvas self._addElementSVG(image)
def getMax(self): return DOM.getAttribute(self.getElement(), "max")
def getSrc(self): return DOM.getAttribute(self.getElement(), 'src')
def getName(self): return DOM.getAttribute(self.inputElem, "name")
def getAction(self): return DOM.getAttribute(self.getElement(), "action")
def getStep(self): return DOM.getAttribute(self.getElement(), "step")
def getName(self): return DOM.getAttribute(self.getElement(), "name")
def getValue(self, index): self.checkIndex(index) option = DOM.getChild(self.getElement(), index) return DOM.getAttribute(option, "value")
def drawImage(self, img, *args): if isinstance(img, Widget): img = img.getElement() fullWidth = img.width fullHeight = img.height if len(args) == 8: sourceX = args[0] sourceY = args[1] sourceWidth = args[2] sourceHeight = args[3] destX = args[4] destY = args[5] destWidth = args[6] destHeight = args[7] elif len(args) == 4: sourceX = 0 sourceY = 0 sourceWidth = fullWidth sourceHeight = fullHeight destX = args[0] destY = args[1] destWidth = args[2] destHeight = args[3] elif len(args) == 2: sourceX = 0 sourceY = 0 sourceWidth = fullWidth sourceHeight = fullHeight destX = args[0] destY = args[1] destWidth = fullWidth destHeight = fullHeight vmlStr = [] # JSOStack.getScratchArray() vmlStr.append("<v:group style=\"position:absolute;width:10;height:10;") dX = self.getCoordX(self.matrix, destX, destY) dY = self.getCoordY(self.matrix, destX, destY) # If we have a transformation matrix with rotation/scale, we # apply a filter if self.context.matrix[0] != 1 or self.context.matrix[1] != 0: # We create a padding bounding box to prevent clipping. vmlStr.append("padding-right:") vmlStr.append(str(self.parentWidth) + "px;") vmlStr.append("padding-bottom:") vmlStr.append(str(self.parentHeight) + "px;") vmlStr.append("filter:progid:DXImageTransform.Microsoft.Matrix(M11='") vmlStr.append("" + str(self.matrix[0])) vmlStr.append("',") vmlStr.append("M12='") vmlStr.append("" + str(self.matrix[1])) vmlStr.append("',") vmlStr.append("M21='") vmlStr.append(str(self.matrix[3])) vmlStr.append("',") vmlStr.append("M22='") vmlStr.append(str(self.matrix[4])) vmlStr.append("',") vmlStr.append("Dx='") vmlStr.append(str(math.floor(((dX / 10))))) vmlStr.append("',") vmlStr.append("Dy='") vmlStr.append(str(math.floor(((dY / 10))))) vmlStr.append("', SizingMethod='clip');") else: vmlStr.append("left:") vmlStr.append("%dpx;" % int(dX / 10)) vmlStr.append("top:") vmlStr.append("%dpx" % int(dY / 10)) vmlStr.append("\" coordsize=\"100,100\" coordorigin=\"0,0\"><v:image src=\"") vmlStr.append(DOM.getAttribute(img, "src")) vmlStr.append("\" style=\"") vmlStr.append("width:") vmlStr.append(str(int(destWidth * 10))) vmlStr.append(";height:") vmlStr.append(str(int(destHeight * 10))) vmlStr.append(";\" cropleft=\"") vmlStr.append(str(sourceX / fullWidth)) vmlStr.append("\" croptop=\"") vmlStr.append(str(sourceY / fullHeight)) vmlStr.append("\" cropright=\"") vmlStr.append(str((fullWidth - sourceX - sourceWidth) / fullWidth)) vmlStr.append("\" cropbottom=\"") vmlStr.append(str((fullHeight - sourceY - sourceHeight) / fullHeight)) vmlStr.append("\"/></v:group>") self.insert("BeforeEnd", ''.join(vmlStr))
def get(self): "Get the value" return DOM.getAttribute(self.element, self.attribute)