change py to js convert algorithm

master
inpos 2018-08-08 22:15:37 +03:00
parent 3286b15441
commit feebd05e99
3 changed files with 72 additions and 27 deletions

View File

@ -16,10 +16,11 @@ class MyApplication(cp.ExtApplication):
wnd = Ext.create('widget.window', {'title': 'My Window', 'width': 300, 'height': 250, wnd = Ext.create('widget.window', {'title': 'My Window', 'width': 300, 'height': 250,
'items': [{'xtype': 'button', 'text': 'Click Here', 'handler': button_click}], 'items': [{'xtype': 'button', 'text': 'Click Here', 'handler': button_click}],
'buttons': [ 'buttons': [
{'text': 'OK', 'handler': js.FuncWithParams(ok_click, {'arg1': 1, 'arg2': 'val2'})}, {'text': 'OK', 'handler': js.FuncWithParams(ok_click, {'arg1': 1, 'arg2': 'val2', 'arg3': js.cli.this.id})},
{'text': 'Close', 'handler': str(js.function('this.up(\'window\').close()'))}]}) {'text': 'Close', 'handler': js.function('this.up(\'window\').close()')}]})
wnd.show() wnd.show()
wnd.setHeight(200) wnd.setHeight(200)
print(repr(js.live_methods))
cp.THEME = 'classic' cp.THEME = 'classic'

View File

@ -1,14 +1,13 @@
import json
from . import js from . import js
import re
__all__ = ['create', 'createByAlias', 'Component'] __all__ = ['create', 'createByAlias', 'Component']
def js_ajax(fn, arg_dict = {}): def js_ajax(fn, arg_dict = {}):
i = id(fn) i = id(fn)
js.live_methods[i] = fn js.live_methods[i] = fn
func_args = ', '.join(['\'{k}\': {v}'.format( k = k,v = '\'%s\'' % v if type(v) is str else v ) for k,v in arg_dict.items()]) func_args = ', '.join(['\'{k}\': {v}'.format( k = k,v = js.encode(v) ) for k,v in arg_dict.items()])
if func_args != '': func_args = ', ' + func_args if func_args != '': func_args = ', ' + func_args
return "%s({'url': '%s', 'method': 'GET', 'params': { 'fn': %d, 'id_': %s %s}, 'success': %s })"\ return "%s({'url': '%s', 'method': 'GET', 'params': { 'fn': %d, 'id_': %s %s}, 'success': %s })"\
% (js.client.Ext.Ajax.request, js.AJAX_URL, i, js.client.this.id, func_args, js.function('eval(arguments[0].responseText);')) % (js.client.Ext.Ajax.request, js.AJAX_URL, i, js.client.this.id, func_args, js.function('eval(arguments[0].responseText);'))
@ -59,5 +58,4 @@ class Component(js.JsObject):
pass pass
def __str__(self): def __str__(self):
s = json.dumps(self._js, default=js._encoder, indent=4) return js.dict2extjs(self._js)
return re.sub(r'("(handler|renderTo)":\s+)("([^"]+)")', r'\1\4', s)

View File

@ -8,38 +8,84 @@ js_ajax = None
live_methods = {} live_methods = {}
class FuncWithParams(object): class FuncWithParams:
def __init__(self, func, params): def __init__(self, func, params):
self.func = func self.func = func
self.params = params self.params = params
def _encoder(o): def list2extjs(l):
if isinstance(o, JsObject): s = '[ %s ]'
return o._js ss = []
elif isinstance(o, JsNode): for v in l:
return block(str(o)) if isinstance(v, int):
elif isinstance(o, types.FunctionType) and js_ajax: ss.append('%d' % v)
return str(function(js_ajax(o))) elif isinstance(v, str):
elif isinstance(o, FuncWithParams): ss.append('\'%s\'' % v)
return str(function(js_ajax(o.func, o.params))) elif isinstance(v, types.FunctionType):
ss.append(str(function(js_ajax(v))))
elif isinstance(v, FuncWithParams):
ss.append(str(function(js_ajax(v.func, v.params))))
elif isinstance(v, JsFunction):
ss.append(str(v))
elif isinstance(v, JsNode):
ss.append(str(v))
elif isinstance(v, dict):
ss.append(dict2extjs(v))
elif isinstance(v, (list, tuple)):
ss.append(list2extjs(v))
return s % ', '.join(ss)
def dict2extjs(d):
s = '{ %s }'
ss = []
for k, v in d.items():
if isinstance(v, int):
ss.append('%s: %d' % (k,v))
elif isinstance(v, str):
ss.append('%s: \'%s\'' % (k, v))
elif isinstance(v, types.FunctionType):
ss.append('%s: %s' % (k, str(function(js_ajax(v)))))
elif isinstance(v, FuncWithParams):
ss.append('%s: %s' % (k, str(function(js_ajax(v.func, v.params)))))
elif isinstance(v, JsFunction):
ss.append('%s: %s' % (k, str(v)))
elif isinstance(v, JsNode):
ss.append('%s: %s' % (k, str(v)))
elif isinstance(v, dict):
ss.append('%s: %s' % (k, dict2extjs(v)))
elif isinstance(v, (list, tuple)):
ss.append('%s: %s' % (k, list2extjs(v)))
return s % ', '.join(ss)
def encode(o): def encode(o):
if isinstance(o, JsNode): if isinstance(o, JsNode):
return str(o) return str(o)
elif isinstance(o, (list, tuple)): elif isinstance(o, (list, tuple)):
return '[%s]' % ','.join([encode(i) for i in o]) return list2extjs(o)
elif isinstance(o, int):
return '%d' % o
elif isinstance(o, str):
return '\'%s\'' % o
elif isinstance(o, types.FunctionType):
return str(function(js_ajax(o)))
elif isinstance(o, FuncWithParams):
return str(function(js_ajax(o.func, o.params)))
elif isinstance(o, JsFunction):
return str(o)
elif isinstance(o, JsNode):
return str(o)
elif isinstance(o, dict):
return dict2extjs(o)
else: else:
return json.dumps(o, default=_encoder) return o
# trick json serialize javascript block class JsBlock:
class JsBlock(str): def __init__(self, *args, **kwargs):
def __new__(cls, *args, **kwargs): self.code = args[0]
obj = super(JsBlock, cls).__new__(cls, '%s' % args[0])
obj.code = args[0]
return obj
def __str__(cls): def __str__(self):
return cls.code return self.code
class JsFunction(JsBlock): class JsFunction(JsBlock):
def __str__(cls): def __str__(cls):