def generate_setup_code(self, registry, parent, closure, ident_n=1): r = [] ident = "\t" * ident_n for target, value in self.assignments.items(): if target == "id": continue t = type(value) #print self.name, target, value target_owner, target_lvalue, target_prop = self.get_lvalue( registry, parent, target) if isinstance(value, (str, basestring)): value = replace_enums(value, self, registry) r.append('//assigning %s to %s' % (target, value)) value, deps = parse_deps( parent, value, partial(self.transform_root, registry, None)) if deps: undep = [] for idx, _dep in enumerate(deps): path, dep = _dep undep.append(path) undep.append("'%s'" % dep) r.append( "%s%s._replaceUpdater('%s', function() { %s = %s }, [%s])" % (ident, target_owner, target_prop, target_lvalue, value, ",".join(undep))) else: r.append("%s%s._removeUpdater('%s'); %s = %s;" % (ident, target_owner, target_prop, target_lvalue, value)) elif t is component_generator: if target == "delegate": continue var = "%s$%s" % (escape(parent), escape(target)) r.append( self.call_setup(registry, ident_n, var, value, closure)) else: raise Exception("skip assignment %s = %s" % (target, value)) def put_in_instance(handler): path, name = handler return path or not self.prototype code_index = 0 def next_codevar(lines, code, index): var = "$code$%d" % index code = '%svar %s = %s' % (ident, var, code) lines.append(code) return var if not self.prototype: for code, methods in self.transform_handlers( registry, self.methods): if len(methods) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(methods): path = path_or_parent( path, parent, partial(self.transform_root, registry, None)) code = code.replace( '@super.', self.get_base_type(registry, mangle=True) + '.prototype.') r.append("%s%s.%s = %s.bind(%s)" % (ident, path, name, code, parent)) for code, handlers in self.transform_handlers(registry, self.signal_handlers): handlers = list(filter(put_in_instance, handlers)) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): has_path = bool(path) path = path_or_parent( path, parent, partial(self.transform_root, registry, parent)) if has_path: r.append("%sif (%s) %s.on('%s', %s.bind(%s))" % (ident, path, path, name, code, parent)) #fixme: remove me? else: r.append("%s%s.on('%s', %s.bind(%s))" % (ident, path, name, code, parent)) for code, handlers in self.transform_handlers(registry, self.changed_handlers): handlers = list(filter(put_in_instance, handlers)) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): has_path = bool(path) path = path_or_parent( path, parent, partial(self.transform_root, registry, parent)) if has_path: r.append("%sif (%s) %s.onChanged('%s', %s.bind(%s))" % (ident, path, path, name, code, parent)) #fixme: remove me? else: r.append("%s%s.onChanged('%s', %s.bind(%s))" % (ident, path, name, code, parent)) for code, handlers in self.transform_handlers(registry, self.key_handlers): handlers = list(filter(put_in_instance, handlers)) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): path = path_or_parent( path, parent, partial(self.transform_root, registry, parent)) r.append("%s%s.onPressed('%s', %s.bind(%s))" % (ident, path, name, code, parent)) for idx, value in enumerate(self.children): var = '%s$child%d' % (escape(parent), idx) r.append(self.call_setup(registry, ident_n, var, value, closure)) if self.elements: r.append( "%s%s.assign(%s)" % (ident, parent, json.dumps(self.elements, sort_keys=True))) # put all implicit assignment r.append("%s%s._implicit=[%s]" % (ident, parent, ', '.join( ["'" + x + "'" for x in self.implicit_assignments]))) r.append(self.generate_animations(registry, parent)) r.append('%s%s.completed()' % (ident, parent)) return "\n".join(r)
def generate_setup_code(self, registry, parent, closure, ident_n=1): r = [] ident = "\t" * ident_n for target, value in self.assignments.iteritems(): if target == "id": continue t = type(value) #print self.name, target, value target_owner, target_lvalue, target_prop = self.get_lvalue( registry, parent, target) if t is str: value = replace_enums(value, self, registry) r.append('//assigning %s to %s' % (target, value)) value, deps = parse_deps( parent, value, partial(self.transform_root, registry)) if deps: var = "update$%s$%s" % (escape(parent), escape(target)) r.append("%svar %s = function() { %s = %s; }" % (ident, var, target_lvalue, value)) undep = [] for idx, _dep in enumerate(deps): path, dep = _dep undep.append("[%s, '%s']" % (path, dep)) r.append("%s%s._replaceUpdater('%s', %s, [%s])" % (ident, target_owner, target_prop, var, ",".join(undep))) else: r.append("%s%s._removeUpdater('%s'); %s = %s;" % (ident, target_owner, target_prop, target_lvalue, value)) elif t is component_generator: if target == "delegate": continue var = "%s$%s" % (escape(parent), escape(target)) r.append( self.call_setup(registry, ident_n, var, value, closure)) else: raise Exception("skip assignment %s = %s" % (target, value)) def put_in_instance(handler): path, name = handler return path or not self.prototype code_index = 0 def next_codevar(lines, code, index): var = "$code$%d" % index code = '%svar %s = %s' % (ident, var, code) lines.append(code) return var if not self.prototype: for code, methods in self.transform_handlers( registry, self.methods): if len(methods) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(methods): path = path_or_parent( path, parent, partial(self.transform_root, registry)) r.append("%s%s.%s = %s.bind(%s)" % (ident, path, name, code, path)) for code, handlers in self.transform_handlers(registry, self.signal_handlers): handlers = filter( lambda h: put_in_instance(h) or h[1] == 'completed', handlers) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): path = path_or_parent(path, parent, partial(self.transform_root, registry)) if name != "completed": r.append("%s%s.on('%s', %s.bind(%s))" % (ident, path, name, code, path)) else: r.append("%s%s._context._onCompleted(%s, %s)" % (ident, path, path, code)) for code, handlers in self.transform_handlers(registry, self.changed_handlers): handlers = filter(put_in_instance, handlers) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): path = path_or_parent(path, parent, partial(self.transform_root, registry)) r.append("%s%s.onChanged('%s', %s.bind(%s))" % (ident, path, name, code, path)) for code, handlers in self.transform_handlers(registry, self.key_handlers): handlers = filter(put_in_instance, handlers) if not handlers: continue if len(handlers) > 1: code = next_codevar(r, code, code_index) code_index += 1 for path, name in sorted(handlers): path = path_or_parent(path, parent, partial(self.transform_root, registry)) r.append("%s%s.onPressed('%s', %s.bind(%s))" % (ident, path, name, code, path)) for idx, value in enumerate(self.children): var = '%s$child%d' % (escape(parent), idx) r.append(self.call_setup(registry, ident_n, var, value, closure)) if self.elements: r.append("\t%s.assign(%s)" % (parent, json.dumps(self.elements, sort_keys=True))) r.append(self.generate_animations(registry, parent)) return "\n".join(r)
def generate_setup_code(self, registry, parent, closure, ident_n = 1): r = [] ident = "\t" * ident_n for target, value in self.assignments.iteritems(): if target == "id": continue t = type(value) #print self.name, target, value target_lvalue = self.get_lvalue(parent, target) if t is str: value = replace_enums(value, self, registry) deps = parse_deps(parent, value) if deps: var = "update$%s$%s" %(escape(parent), escape(target)) r.append('//assigning %s to %s' %(target, value)) r.append("%svar %s = (function() { %s = (%s); }).bind(%s)" %(ident, var, target_lvalue, value, parent)) undep = [] for idx, _dep in enumerate(deps): path, dep = _dep depvar = "dep$%s$%s$%d" %(escape(parent), escape(target), idx) r.append('%svar %s = %s' %(ident, depvar, path)) r.append("%s%s.connectOnChanged(%s, '%s', %s)" %(ident, parent, depvar, dep, var)) undep.append("[%s, '%s', %s]" %(depvar, dep, var)) r.append("%s%s._removeUpdater('%s', [%s])" %(ident, parent, target, ",".join(undep))) r.append("%s%s();" %(ident, var)) else: r.append('//assigning %s to %s' %(target, value)) r.append("%s%s._removeUpdater('%s'); %s = (%s);" %(ident, parent, target, target_lvalue, value)) elif t is component_generator: if target == "delegate": continue var = "%s$%s" %(escape(parent), escape(target)) r.append(self.call_setup(registry, ident_n, var, value, closure)) else: raise Exception("skip assignment %s = %s" %(target, value)) if not self.prototype: for _name, argscode in self.methods.iteritems(): path, name = _name args, code = argscode path = path_or_parent(path, parent) code = process(code, self, registry) r.append("%s%s.%s = (function(%s) %s ).bind(%s)" %(ident, path, name, ",".join(args), code, path)) for _name, argscode in self.signal_handlers.iteritems(): path, name = _name if not path and self.prototype and name != 'completed': #sync with condition above continue args, code = argscode code = process(code, self, registry) path = path_or_parent(path, parent) if name != "completed": r.append("%s%s.on('%s', (function(%s) %s ).bind(%s))" %(ident, path, name, ",".join(args), code, path)) else: r.append("%s%s._context._onCompleted((function() %s ).bind(%s))" %(ident, path, code, path)) for _name, code in self.changed_handlers.iteritems(): path, name = _name if not path and self.prototype: #sync with condition above continue code = process(code, self, registry) path = path_or_parent(path, parent) r.append("%s%s.onChanged('%s', (function(value) %s ).bind(%s))" %(ident, path, name, code, path)) for _name, code in self.key_handlers.iteritems(): path, name = _name if not path and self.prototype: #sync with condition above continue code = process(code, self, registry) path = path_or_parent(path, parent) r.append("%s%s.onPressed('%s', (function(key, event) %s ).bind(%s))" %(ident, path, name, code, path)) r.append(self.generate_animations(registry, parent)) for idx, value in enumerate(self.children): var = '%s$child%d' %(escape(parent), idx) r.append(self.call_setup(registry, ident_n, var, value, closure)) r.append("%s%s.addChild(%s)" %(ident, parent, var)); if self.elements: r.append("\t%s.assign(%s)" %(parent, json.dumps(self.elements))) return "\n".join(r)