2013-04-30 09:49:34 +04:00
|
|
|
import types
|
|
|
|
import json
|
|
|
|
|
|
|
|
AJAX_URL = '/ajax_callback'
|
2018-08-16 17:49:28 +03:00
|
|
|
AJAX_FUNC_URL = '/ajax_func_callback'
|
2013-04-30 09:49:34 +04:00
|
|
|
js_manager = None
|
|
|
|
js_ajax = None
|
|
|
|
|
|
|
|
live_methods = {}
|
|
|
|
|
2018-08-14 15:04:52 +03:00
|
|
|
class RE:
|
|
|
|
def __init__(self, re):
|
|
|
|
self.re = re
|
2018-08-09 14:45:14 +03:00
|
|
|
|
2018-08-14 15:07:18 +03:00
|
|
|
def re(pattern):
|
|
|
|
return RE(pattern)
|
|
|
|
|
2018-08-16 17:49:28 +03:00
|
|
|
def js_procedure(func_id, ajax_args=''):
|
|
|
|
return '''
|
|
|
|
Ext.Ajax.request(
|
|
|
|
{
|
|
|
|
url: %s,
|
|
|
|
method: 'GET',
|
|
|
|
params: {
|
|
|
|
fn: %d%s
|
|
|
|
},
|
|
|
|
success: function () { eval(arguments[0].responseText); }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
''' % (
|
|
|
|
AJAX_URL,
|
|
|
|
func_id,
|
|
|
|
ajax_args
|
|
|
|
)
|
|
|
|
|
|
|
|
def js_function(func_id, ajax_args=''):
|
|
|
|
return '''
|
|
|
|
var ajax_result;
|
|
|
|
Ext.Ajax.request(
|
|
|
|
{
|
|
|
|
url: %s,
|
|
|
|
method: 'GET',
|
|
|
|
params: {
|
|
|
|
fn: %d%s
|
|
|
|
},
|
|
|
|
success: function () { ajax_result = JSON.parse(arguments[0].responseText); }
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return ajax_result.data;
|
|
|
|
''' % (
|
|
|
|
const.AJAX_URL_PROC,
|
|
|
|
func_id,
|
|
|
|
ajax_args
|
|
|
|
)
|
|
|
|
|
|
|
|
def js_ajax(fn, arg_dict = {}, f_type=js_procedure):
|
|
|
|
i = id(fn)
|
|
|
|
gvars.functions[i] = fn
|
|
|
|
func_args = ',\n'.join(['\'{k}\': {v}'.format( k = k,v = encode(v) ) for k,v in arg_dict.items()])
|
|
|
|
if func_args != '':
|
|
|
|
func_args = ',\n' + func_args
|
|
|
|
print(func_args)
|
|
|
|
return f_type(i, ajax_args=func_args)
|
2018-08-08 12:53:19 +03:00
|
|
|
|
2018-08-08 22:15:37 +03:00
|
|
|
def list2extjs(l):
|
2018-08-09 16:49:32 +03:00
|
|
|
return '[ %s ]' % ', '.join([encode(v) for v in l])
|
2018-08-08 22:15:37 +03:00
|
|
|
|
|
|
|
def dict2extjs(d):
|
2018-08-09 16:49:32 +03:00
|
|
|
return '{ %s }' % ', '.join('%s: %s' % (k, encode(v)) for k,v in d.items())
|
2018-08-08 22:15:37 +03:00
|
|
|
|
2018-08-16 17:49:28 +03:00
|
|
|
class JsProcedure:
|
|
|
|
def __init__(self, *a, **kw):
|
|
|
|
self.func = a[0]
|
|
|
|
self.params = kw.get('params', {})
|
|
|
|
self.args = kw.get('args', [])
|
|
|
|
@property
|
|
|
|
def js(self):
|
|
|
|
return 'function (%s) { %s }' % (', '.join(self.args), js_ajax(self, arg_dict=self.params, f_type=js_procedure))
|
|
|
|
|
|
|
|
class JsFunction(JsProcedure):
|
|
|
|
@property
|
|
|
|
def js(self):
|
|
|
|
return 'function (%s) { %s }' % (', '.join(self.args), js_ajax(self, arg_dict=self.params, f_type=js_function))
|
|
|
|
|
|
|
|
class JsStrFunction:
|
|
|
|
def __init__(self, *a, **kw):
|
|
|
|
self.code = a[0]
|
|
|
|
self.args = kw.get('args', [])
|
|
|
|
@property
|
|
|
|
def js(self):
|
|
|
|
return 'function (%s) { %s }' % (', '.join(self.args), self.code)
|
|
|
|
|
|
|
|
function = JsFunction
|
|
|
|
procedure = JsProcedure
|
|
|
|
strfunction = JsStrFunction
|
2013-04-30 09:49:34 +04:00
|
|
|
|
|
|
|
def encode(o):
|
|
|
|
if isinstance(o, JsNode):
|
|
|
|
return str(o)
|
2018-08-14 15:04:52 +03:00
|
|
|
if isinstance(o, RE):
|
|
|
|
return o.re
|
2013-04-30 09:49:34 +04:00
|
|
|
elif isinstance(o, (list, tuple)):
|
2018-08-08 22:15:37 +03:00
|
|
|
return list2extjs(o)
|
2018-08-09 20:41:21 +03:00
|
|
|
elif isinstance(o, bool):
|
|
|
|
return str(o).lower()
|
2018-08-08 22:15:37 +03:00
|
|
|
elif isinstance(o, int):
|
2018-08-09 20:41:21 +03:00
|
|
|
return str(o)
|
|
|
|
elif isinstance(o, float):
|
|
|
|
return str(o)
|
2018-08-08 22:15:37 +03:00
|
|
|
elif isinstance(o, str):
|
|
|
|
return '\'%s\'' % o
|
2018-08-16 17:49:28 +03:00
|
|
|
elif isinstance(o, (function, procedure, strfunction)):
|
|
|
|
return o.js
|
2018-08-08 22:15:37 +03:00
|
|
|
elif isinstance(o, dict):
|
|
|
|
return dict2extjs(o)
|
2018-08-09 15:59:57 +03:00
|
|
|
elif isinstance(o, JsObject):
|
|
|
|
return o._id
|
2013-04-30 09:49:34 +04:00
|
|
|
else:
|
2018-08-08 22:15:37 +03:00
|
|
|
return o
|
2013-04-30 09:49:34 +04:00
|
|
|
|
2018-08-08 22:15:37 +03:00
|
|
|
class JsBlock:
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.code = args[0]
|
2018-08-11 16:25:59 +03:00
|
|
|
self.args = args[1] if len(args) > 1 else []
|
2013-04-30 09:49:34 +04:00
|
|
|
|
2018-08-08 22:15:37 +03:00
|
|
|
def __str__(self):
|
|
|
|
return self.code
|
2013-04-30 09:49:34 +04:00
|
|
|
|
|
|
|
def write(code):
|
|
|
|
if js_manager:
|
|
|
|
js_manager.write(str(code))
|
|
|
|
|
|
|
|
def __lshift__(self, value):
|
|
|
|
print('test')
|
|
|
|
|
|
|
|
class JsManager(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.output = []
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self.output.append(data)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
output = self.output[:]
|
2018-08-09 08:57:18 +03:00
|
|
|
s = '\n'.join(output)
|
|
|
|
|
2013-04-30 09:49:34 +04:00
|
|
|
return s
|
|
|
|
|
|
|
|
class JsNode(object):
|
|
|
|
def __init__(self, name='', parent=None):
|
|
|
|
if parent and parent.name:
|
|
|
|
self.name = parent.name + '.' + name
|
|
|
|
else:
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return JsNode(attr, self)
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
if attr == 'name':
|
|
|
|
super(JsNode, self).__setattr__(attr, value)
|
|
|
|
else:
|
|
|
|
value = encode(value)
|
|
|
|
if self is client.var:
|
|
|
|
s = 'var %s = %s' % (attr, value)
|
|
|
|
else:
|
|
|
|
name = self.name + '.' if self.name else ''
|
|
|
|
s = '%s%s = %s' % (name, attr, value)
|
|
|
|
write(s)
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
return JsNode('%s + %s' % (encode(self), encode(other)))
|
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
return JsNode('%s - %s' % (encode(self), encode(other)))
|
|
|
|
|
|
|
|
def __mul__(self, other):
|
|
|
|
return JsNode('%s * %s' % (encode(self), encode(other)))
|
|
|
|
|
|
|
|
def __truediv__(self, other):
|
|
|
|
return JsNode('%s / %s' % (encode(self), encode(other)))
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
l = []
|
|
|
|
d = []
|
|
|
|
for arg in args:
|
|
|
|
l.append(encode(arg))
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
d.append('%s=%s' % (k, encode(v)))
|
|
|
|
_args = []
|
|
|
|
if l:
|
|
|
|
_args.extend(l)
|
|
|
|
if d:
|
|
|
|
_args.extend(d)
|
|
|
|
s = '%s(%s)' % (self.name, ','.join(_args))
|
|
|
|
self.name = s
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class JsClient(JsNode):
|
|
|
|
def __init__(self, name='', parent=None):
|
|
|
|
if parent and parent.name:
|
|
|
|
self.name = parent.name + '.' + name
|
|
|
|
else:
|
|
|
|
self.name = name
|
|
|
|
self.__dict__['var'] = JsNode('var')
|
|
|
|
|
|
|
|
def __lshift__(self, other):
|
|
|
|
write(other)
|
|
|
|
|
|
|
|
class JsObjectNode(JsNode):
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
super(JsObjectNode, self).__call__(*args, **kwargs)
|
|
|
|
write(str(self))
|
|
|
|
|
2013-05-11 09:24:06 +04:00
|
|
|
class JsOutput(object):
|
|
|
|
def __init__(self, manager=True):
|
|
|
|
self.body = []
|
|
|
|
if manager:
|
|
|
|
js_manager = self
|
|
|
|
|
|
|
|
def __lshift__(self, other):
|
|
|
|
self.write(other)
|
|
|
|
|
|
|
|
def write(self, code):
|
|
|
|
self.body.append(code)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = ';\n'.join(self.body)
|
|
|
|
return s
|
|
|
|
|
|
|
|
out = output = JsOutput
|
|
|
|
|
2013-04-30 09:49:34 +04:00
|
|
|
class JsObject(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._loading = True
|
|
|
|
self._id = 'js_%s' % id(self)
|
|
|
|
self._create()
|
|
|
|
self._js = kwargs
|
|
|
|
self._loading = False
|
|
|
|
|
|
|
|
def _create(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _update(self, config):
|
|
|
|
self._js.update(config)
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if not self.__dict__.get('_loading', True):
|
|
|
|
if attr in self._js:
|
|
|
|
return self._js.get(attr)
|
|
|
|
else:
|
|
|
|
return JsObjectNode(attr, JsNode(self._id))
|
|
|
|
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
if '_js' in self.__dict__ and not attr in self.__dict__:
|
|
|
|
self[attr] = value
|
|
|
|
else:
|
|
|
|
super(JsObject, self).__setattr__(attr, value)
|
|
|
|
|
|
|
|
def __setitem__(self, attr, value):
|
|
|
|
if not self._loading:
|
|
|
|
write('%s.%s = %s' % (self._id, attr, json.dumps(value)))
|
|
|
|
self._js[attr] = value
|
2013-05-11 09:24:06 +04:00
|
|
|
|
2013-04-30 10:48:44 +04:00
|
|
|
def alert(msg):
|
|
|
|
write(client.alert(msg))
|
|
|
|
|
2013-04-30 09:49:34 +04:00
|
|
|
def load(filename, klass=JsObject):
|
|
|
|
return klass(**json.load(open(filename)))
|
|
|
|
|
|
|
|
cli = client = JsClient()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
class MyManager(JsManager):
|
|
|
|
def write(self, code):
|
|
|
|
print(code)
|
|
|
|
js_manager = MyManager()
|
|
|
|
write(client.console.log('test'))
|
|
|
|
n = JsNode('console')
|
|
|
|
write(n.print(n.log({'id': 'item id'})))
|
|
|
|
client.var.x = 1
|
|
|
|
client.x.y = client.window.open('http://www.google.com')
|
|
|
|
client << client.x.y()([client.x])
|
|
|
|
client << client.Ext.create('window', {'left': 10})
|
|
|
|
client << client.x
|
|
|
|
|
|
|
|
# test block
|
2018-08-09 08:57:18 +03:00
|
|
|
print(encode({'click': 'call'}))
|