def __urlStringFromDict__(parameterDict): params = [] for key, value in parameterDict.iteritems(): if type(value) in (list, set, tuple): for instance in value: params.append("%s=%s" % (key, urllib.quote(interpretAsString(instance)))) else: params.append("%s=%s" % (key, urllib.quote(interpretAsString(value)))) return "&".join(params)
def __urlStringFromDict__(parameterDict): params = [] for key, value in parameterDict.iteritems(): if type(value) in (list, set, tuple): for instance in value: params.append( "%s=%s" % (key, urllib.quote(interpretAsString(instance)))) else: params.append("%s=%s" % (key, urllib.quote(interpretAsString(value)))) return "&".join(params)
def startTag(self): """ Returns the elements html start tag, for example '<span class="whateverclass">' """ if not self._tagName: return u'' nativeAttributes = (('name', self.fullName()), ('id', self.fullId()), ('class', self._classes), ('style', self._style),) startTag = "<" + self._tagName + " " attributes = nativeAttributes if self._attributes is not None: attributes = chain(attributes, self.attributes.iteritems()) for key, value in attributes: value = interpretAsString(value) if value: if value == '_BLANK_': value = "" if value == '_EMPTY_': startTag += key + " " else: startTag += key + '="' + value.replace('"', '"') + '" ' if self._tagSelfCloses: startTag += '/' else: startTag = startTag[:-1] startTag += '>' return unicode(startTag)
def startTag(self): """ Returns the elements html start tag, for example '<span class="whateverclass">' """ if not self.tagName: return u'' self.attributes['name'] = self.fullName() self.attributes['id'] = self.fullId() self.attributes['class'] = self.classes self.attributes['style'] = self.style startTag = "<" + self.tagName + " " for key, value in self.attributes.iteritems(): value = interpretAsString(value) if value: if value == '<BLANK>': value = "" if value == '<EMPTY>': startTag += key + " " else: startTag += key + '="' + value.replace('"', '"') + '" ' if self.tagSelfCloses: startTag += '/' else: startTag = startTag[:-1] startTag += '>' return unicode(startTag)
def update(self, silent=True, parameters=None, instanceId=None, timeout=0): """ Returns the javascript that will update this control """ if parameters == None: parameters = {} return "ajaxUpdate('%s', %s, '%s', %d);" % ( self.jsId(instanceId), interpretAsString(silent), self.__urlStringFromDict__(parameters), timeout)
def submit(self, silent=False, parameters=None, instanceId=None, timeout=0): """ Returns the javascript that will submit this control """ if parameters == None: parameters = {} return "ajaxSubmit('%s', %s, '%s', %d);" % (self.jsId(instanceId), interpretAsString(silent), self.__urlStringFromDict__(parameters), timeout)
def addOption(self, key, value=None, displayKeys=True): """ Adds options based on a key-value dictionary """ newOption = Option() if not value: value = key key = interpretAsString(key) value = interpretAsString(value) if displayKeys: newOption.setValue(value) newOption.setText(key) else: newOption.setValue(key) newOption.setText(value) newOption.connect('selected', None, self, 'emit', 'selectionChanged') return self.addChildElement(newOption)
def jsFunctionAsString(self, jsFunction): """ Creates and returns a jsFunction implementation based on a pased in python function representation """ methodName = jsFunction.__name__ attributes = list(jsFunction.func_code.co_varnames) attributes.reverse() if jsFunction.func_defaults: methodDefaults = list(jsFunction.func_defaults) else: methodDefaults = [] methodDefaults.reverse() defaults = {} for index, default in enumerate(methodDefaults): if type(default) in types.StringTypes: default = "'" + default + "'" else: default = interpretAsString(default) defaults[attributes[index]] = default attributeValues = {} for attribute in attributes: if defaults.get(attribute, None) == None: attributeValues[attribute] = None attributes.reverse() script = ["\nfunction %s(%s)" % (methodName, ', '.join(attributes))] script.append("{") for var, default in defaults.iteritems(): script.append("\tif(%s == null) var %s = %s;" % (var, var, default)) for var in attributes: if var.startswith("element"): script.append('\tvar %s = JUGetElement(%s);' % (var, var)) script.append(interpretAsString(jsFunction(**attributeValues))) script.append("}\n") return "\n".join(script)
def content(self, formatted=False, *args, **kwargs): """ Overrides the base content method to return the javascript associated with the scriptcontainer """ scriptContent = ";".join([interpretAsString(script) for script in self.scripts()]) for objectType in self.usedObjects: for function in objectType.jsFunctions: scriptContent += self.jsFunctionAsString(getattr(objectType, function)) return scriptContent
def setValue(self, value): """ Selects a child select option """ strValue = interpretAsString(value) for obj in self.childElements: if strValue and ((obj.fullId() == strValue) or (str(obj.value()) == strValue) or (obj._textNode.text() == strValue)): obj.select() else: obj.unselect()
def jsFunctionAsString(self, jsFunction): """ Creates and returns a jsFunction implementation based on a pased in python function representation """ methodName = jsFunction.__name__ attributes = list(jsFunction.func_code.co_varnames) attributes.reverse() if jsFunction.func_defaults: methodDefaults = list(jsFunction.func_defaults) else: methodDefaults = [] methodDefaults.reverse() defaults = {} for index, default in enumerate(methodDefaults): if type(default) in types.StringTypes: default = "'" + default + "'" else: default = interpretAsString(default) defaults[attributes[index]] = default attributeValues = {} for attribute in attributes: if defaults.get(attribute, None) == None: attributeValues[attribute] = None attributes.reverse() script = ["\nfunction %s(%s)" % (methodName, ", ".join(attributes))] script.append("{") for var, default in defaults.iteritems(): script.append("\tif(%s == null) var %s = %s;" % (var, var, default)) for var in attributes: if var.startswith("element"): script.append("\tvar %s = JUGetElement(%s);" % (var, var)) script.append(interpretAsString(jsFunction(**attributeValues))) script.append("}\n") return "\n".join(script)
def setValue(self, value): """ Selects a child select option """ strValue = interpretAsString(value) for obj in self.childElements: if strValue and ((obj.fullId() == strValue) or (str(obj.value()) == strValue) or (obj.textBeforeChildren == strValue)): obj.select() else: obj.unselect()
def updateControls(controls, silent=True, parameters=None, timeout=0): """ Returns the javascript to update a list of controls """ if parameters == None: parameters = {} for index, control in enumerate(controls): if isinstance(control, Base.WebElement): controls[index] = control.jsId() return "ajaxUpdate(%s, %s, '%s', %d);" % (str(controls), interpretAsString(silent), AjaxController.__urlStringFromDict__(parameters), timeout)
def updateControls(controls, silent=True, parameters=None, timeout=0): """ Returns the javascript to update a list of controls """ if parameters == None: parameters = {} for index, control in enumerate(controls): if isinstance(control, Base.WebElement): controls[index] = control.jsId() return "ajaxUpdate(%s, %s, '%s', %d);" % ( str(controls), interpretAsString(silent), AjaxController.__urlStringFromDict__(parameters), timeout)
def content(self, variableDict=None, formatted=False): """ Overrides the base content method to return the javascript associated with the scriptcontainer """ scriptContent = "" for script in self.scripts(): scriptContent += interpretAsString(script) + "\n" for objectType in self.usedObjects: for function in objectType.jsFunctions: scriptContent += self.jsFunctionAsString(getattr(objectType, function)) return scriptContent
def content(self, variableDict=None, formatted=False): """ Overrides the base content method to return the javascript associated with the scriptcontainer """ scriptContent = "" for script in self.scripts(): scriptContent += interpretAsString(script) + "\n" for objectType in self.usedObjects: for function in objectType.jsFunctions: scriptContent += self.jsFunctionAsString( getattr(objectType, function)) return scriptContent
def addOption(self, key, value=None, displayKeys=True): """ Adds a new option that can be selected """ newOption = Buttons.Link() newOption.setDestination("#Link") if not self.options: newOption.addClass('selected') if not value: value = key key = interpretAsString(key) value = interpretAsString(value) if displayKeys: newOption.name = value newOption.setText(key) else: newOption.name = key newOption.setText(value) newOption.addJavascriptEvent('onclick', 'selectUnrolledOption(this);') self.options.append(newOption) return self.addChildElement(newOption)
def jsAddFilter(self, filterType, toggledOn): """ Return the javascript to add a filter to the filter list clientside """ return "javascriptAddFilter(this, '" + filterType + "', " + \ interpretAsString(toggledOn) + ");"
def do(self, silent=True, parameters=None, instanceId=None): if parameters == None: parameters = {} return "ajaxDo('%s', %s, '%s');" % ( self.jsId(instanceId), interpretAsString(silent), self.__urlStringFromDict__(parameters))
def javascriptEvent(self, event): """ Returns the action associated with a particular client-side event: event - the name of the client side event """ return interpretAsString(self.attributes.get(event, None))
def do(self, silent=True, parameters=None, instanceId=None): if parameters == None: parameters = {} return "ajaxDo('%s', %s, '%s');" % (self.jsId(instanceId), interpretAsString(silent), self.__urlStringFromDict__(parameters))
def jsAddFilter(self, filterType, toggledOn): """ Return the javascript to add a filter to the filter list clientside """ return "javascriptAddFilter(this, '" + filterType + "', " + interpretAsString(toggledOn) + ");"