def jsConvertData(jsData, jsFnc): """ Description: ------------ Generic conversion function for any data in the internal framework. This will convert to String any data coming from the Javascript Python interface. Any pure Python object will be converted using the json function to be then written as a string to the resulting page :param jsData: The Python Javascript data :param jsFnc: Optional. The conversion function :return: """ if not hasattr(jsData, 'varData') and not hasattr(jsData, 'fncName'): if hasattr(jsData, 'toStr'): return jsData.toStr() else: try: return JsObject.JsObject(json.dumps(jsData)) except TypeError as err: return str(jsData) except Exception as err: if isinstance(jsData, range): return JsObject.JsObject(json.dumps(list(jsData))) raise return jsData
def jsConvertData(js_data: Union[str, primitives.JsDataModel, float, dict, list], js_funcs: Optional[Union[list, str]], depth: bool = False) -> str: """ Description: ------------ Generic conversion function for any data in the internal framework. This will convert to String any data coming from the Javascript Python interface. Any pure Python object will be converted using the json function to be then written as a string to the resulting page. Attributes: ---------- :param Union[str, primitives.JsDataModel, float, dict, list] js_data: The Python Javascript data. :param Optional[Union[list, str]] js_funcs: Optional. The conversion function (not used). :param bool depth: Optional. Set to true of it is a nested object. """ if not hasattr(js_data, 'varData') and not hasattr(js_data, 'fncName'): if hasattr(js_data, 'toStr'): return js_data.toStr() else: try: if depth: if isinstance(js_data, dict): result = [] for k, v in js_data.items(): result.append( "%s: %s" % (k, jsConvertData(v, js_funcs, depth=depth))) return "{%s}" % ", ".join(result) else: result = [ jsConvertData(v, js_funcs, depth=depth) for v in js_data ] return "[%s]" % ", ".join(result) return JsObject.JsObject(json.dumps(js_data)) except TypeError as err: return str(js_data) except Exception as err: if isinstance(js_data, range): return JsObject.JsObject(json.dumps(list(js_data))) raise return js_data
def __getitem__(self, index: int): if not isinstance(index, int): return JsObject.JsObject("%s[%s]" % (self.varId, index), page=self.page) if index < 0: return JsObject.JsObject("%s[%s %s]" % (self.varId, self.length, index), page=self.page) return JsObject.JsObject("%s[%s]" % (self.varId, index), page=self.page)
def __getitem__(self, index): if not isinstance(index, int): return JsObject.JsObject("%s[%s]" % (self.varId, index), report=self._report) if index < 0: return JsObject.JsObject("%s[%s %s]" % (self.varId, self.length, index), report=self._report) return JsObject.JsObject("%s[%s]" % (self.varId, index), report=self._report)
def href(self, href: Union[str, primitives.JsDataModel] = None, secured: bool = False): """ Description: ------------ The href property sets or returns the entire URL of the current component. Usage:: page.js.location.href("https://www.w3schools.com/howto/howto_js_fullscreen.asp") Related Pages: https://www.w3schools.com/jsref/prop_loc_href.asp Attributes: ---------- :param Union[str, primitives.JsDataModel] href: Optional. Set the href property. :param bool secured: Optional. :return: A String, representing the entire URL of the page, including the protocol (like http://). """ if href is None: return JsString.JsString("location.href", is_py_data=False) if not hasattr(href, 'toStr') and href.startswith("www."): href = r"http:\\%s" % href if not secured else r"https:\\%s" % href return JsObject.JsObject("location.href = %s" % JsUtils.jsConvertData(href, None))
def href(self, href=None, secured=False): """ Description: ------------ The href property sets or returns the entire URL of the current p Usage:: rptObj.js.location.href("https://www.w3schools.com/howto/howto_js_fullscreen.asp") Related Pages: https://www.w3schools.com/jsref/prop_loc_href.asp Attributes: ---------- :param href: Set the href property :return: A String, representing the entire URL of the page, including the protocol (like http://) """ if href is None: return JsString.JsString("location.href", isPyData=False) if not hasattr(href, 'toStr') and href.startswith("www."): href = r"http:\\%s" % href if not secured else r"https:\\%s" % href return JsObject.JsObject("location.href = %s" % JsUtils.jsConvertData(href, None))
def toArgs(self): """ Description: ----------- """ return JsObject.JsObject("...%s" % self.varId)
def null(self): """ Description: ----------- Javascript null reference """ return JsObject.JsObject("null", is_py_data=False)
def setItem(self, key: Union[primitives.JsDataModel, str], data: Any): """ Description: ------------ Syntax for SAVING data to localStorage. The localStorage object stores data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year. Usage:: jsObj.localStorage.getItem("lastname", "test") Related Pages: https://www.w3schools.com/jsref/met_storage_setitem.asp Attributes: ---------- :param Union[primitives.JsDataModel, str] key: A String specifying the name of the key you want to set the value of. :param Aby data: A String specifying the value of the key you want to set the value of. :return: A String, representing the inserted value. """ key = JsUtils.jsConvertData(key, None) data = JsUtils.jsConvertData(data, None) return JsObject.JsObject("localStorage.setItem(%s, %s)" % (key, data))
def key(self, i: Union[primitives.JsDataModel, int]): """ Description: ------------ The key() method returns name of the key with the specified index. The localStorage object stores data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year. Usage:: jsObj.localStorage.key(0) Related Pages: https://www.w3schools.com/jsref/met_storage_key.asp Attributes: ---------- :param Union[primitives.JsDataModel, int] i: A Number representing the index of the key you want to get the name of. :return: A String, representing the name of the specified key """ i = JsUtils.jsConvertData(i, None) return JsObject.JsObject("localStorage.key(%s)" % i)
def setItem(self, key: Union[primitives.JsDataModel, str], data: Any): """ Description: ------------ Syntax for SAVING data to sessionStorage. The sessionStorage object stores data for only one session (the data is deleted when the browser tab is closed). Usage:: jsObj.sessionStorage.setItem("lastname", "Smith") jsObj.sessionStorage.setItem("lastname", jsObj.objects.get("bin")), Related Pages: https://www.w3schools.com/Jsref/prop_win_sessionstorage.asp Attributes: ---------- :param Union[primitives.JsDataModel, str] key: The key used to store the data in the session cache. :param Any data: """ key = JsUtils.jsConvertData(key, None) data = JsUtils.jsConvertData(data, None) return JsObject.JsObject("sessionStorage.setItem(%s, %s)" % (key, data))
def incr(self, incr): """ Description: ----------- :param incr: """ return JsObject.JsObject("%s++" % incr)
def function(self, args, returns, eval=False): params, values = [], [] for i, v in enumerate(args): if i > 0: params.append("x%s" % i) else: params.append("x") values.append(str(v)) if eval: return JsObject.JsObject("(function(%s){ return eval(%s) })(%s)" % (", ".join(params), returns, ", ".join(values)))
def toDict(self, header): """ Description: ----------- :param header: """ return JsObject.JsObject( "(function(r, h){var rec = {}; h.forEach(function(c, i){rec[c] = r[i]}); return rec})(%s, %s)" % (self.varId, header))
def undefined(self): """ Similar as the None in Python Documentation https://www.w3schools.com/jsref/jsref_undefined.asp :return: A Python Js undefined object """ return JsObject.JsObject("undefined", isPyData=False)
def null(self): """ Similar as None in Python Documentation https://www.w3schools.com/js/js_datatypes.asp :return: A Python Js Null object """ return JsObject.JsObject("null")
def unpack(self, jsId, column=None): """ :param jsId: :param column: """ if not jsId in self._js_ids: column = JsUtils.jsConvertData(column, None) self._js_frg.append("%s.push(row[%s])" % (jsId, column)) self._js_ids.add(jsId) return JsObject.JsObject(jsId, isPyData=False)
class DataReduce(object): """ Description: ----------- rVal : val : index : """ rVal, val, index = JsObject.JsObject("r"), JsNumber.JsNumber( "o", isPyData=False), JsNumber.JsNumber("i", isPyData=False)
class DataEach: """ Description: ----------- Data Class for the Jquery each loop index : index data : element """ index, data = JsNumber.JsNumber( "index", is_py_data=False), JsObject.JsObject("data", is_py_data=False)
def incr(self, incr: str): """ Description: ----------- Increment a counter. Attributes: ---------- :param str incr: the variable name used to store the counter. """ return JsObject.JsObject("%s++" % incr)
class DataReduce: """ Description: ----------- rVal : val : index : """ rVal, val = JsObject.JsObject("r"), JsNumber.JsNumber("o", is_py_data=False) index = JsNumber.JsNumber("i", is_py_data=False)
def key(self, i: Union[primitives.JsDataModel, int]): """ Description: ------------ The sessionStorage object stores data for only one session (the data is deleted when the browser tab is closed). Attributes: ---------- :param Union[primitives.JsDataModel, int] i: The key number. """ i = JsUtils.jsConvertData(i, None) return JsObject.JsObject("sessionStorage.key(%s)" % i)
def root(self): """ Description: ------------ The IntersectionObserver interface's read-only root property identifies the Element whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the root is null, then the bounds of the actual document viewport are used. Related Pages: https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/root """ return JsObject.JsObject("%s.root", isPyData=False)
def null(self): """ Description: ----------- Similar as None in Python. Related Pages: https://www.w3schools.com/js/js_datatypes.asp :return: A Python Js Null object. """ return JsObject.JsObject("null")
class DataLoop: """ Description: ----------- Data Class used for all the loop and map in the Javascript side. This will get the below attributes val : The current value in the loop index : The index item arr : The full array (only available in case of arrays, map, filter, every ) """ val, index, arr = JsObject.JsObject("value"), JsNumber.JsNumber( "index", is_py_data=False), JsArray.JsArray("arr")
def undefined(self): """ Description: ----------- Similar as the None in Python. Related Pages: https://www.w3schools.com/jsref/jsref_undefined.asp :return: A Python Js undefined object. """ return JsObject.JsObject("undefined", is_py_data=False)
def clearWatch(self, watchId): """ Stops the watchPosition() method Related Pages: https://www.w3schools.com/html/html5_geolocation.asp :param watchId: The ID number returned by the Geolocation.watchPosition() method when installing the handler you wish to remove. """ watchId = JsUtils.jsConvertData(watchId, None) return JsObject.JsObject("navigator.geolocation.clearWatch(%s)" % watchId, isPyData=False)
def shift(self): """ The shift() method removes the first item of an array. Example jsObj.console.log(jsObj.objects.array.new([2, 5, 12, -3], "MyArray").shift()), jsObj.console.log(jsObj.objects.array.get("MyArray")), Documentation https://www.w3schools.com/jsref/jsref_shift.asp :return: Any type*, representing the removed array item. *An array item can be a string, a number, an array, a boolean, or any other object types that are allowed in an array. """ return JsObject.JsObject("%s.shift()" % self.varId, isPyData=False)
def pop(self): """ The pop() method removes the last element of an array, and returns that element. Example jsObj.objects.array.new([2, 5, 12, -3], "MyArray") jsObj.objects.array.get("MyArray").pop() Documentation: https://www.w3schools.com/js/js_array_methods.asp :return: Any type*, representing the removed array item. *An array item can be a string, a number, an array, a boolean, or any other object types that are allowed in an array. """ return JsObject.JsObject("%s.pop()" % self.varId, isPyData=False)
def unpack(self, js_code: str, column: Union[str, primitives.JsDataModel] = None): """ Description: ----------- Attributes: ---------- :param str js_code: The variable id to store the records. :param Union[str, primitives.JsDataModel] column: The column name. """ if js_code not in self._js_ids: column = JsUtils.jsConvertData(column, None) self._js_frg.append("%s.push(row[%s])" % (js_code, column)) self._js_ids.add(js_code) return JsObject.JsObject(js_code, is_py_data=False)