diff --git a/weex_core/Source/core/data_render/parser.cc b/weex_core/Source/core/data_render/parser.cc index fb9935d252..ba1feca9de 100644 --- a/weex_core/Source/core/data_render/parser.cc +++ b/weex_core/Source/core/data_render/parser.cc @@ -69,6 +69,14 @@ struct ASTParser final { block->PushExpression(call_func); } + inline void AddEventStatement(std::vector> args, + Handle& block) { + Handle set_attr_func_id = factory_->NewIdentifier("addEvent"); + Handle call_func = + factory_->NewCallExpression(set_attr_func_id, args); + block->PushExpression(call_func); + } + void AddAppendChildCall(json11::Json& json, Handle& parent_identifier, Handle& child_identifier) { Handle statement = stacks_[stacks_.size() - 1]; @@ -113,6 +121,35 @@ struct ASTParser final { } } + void AddEventForVNode( + const json11::Json& events, + Handle child_identifier) { + if (!events.is_array()) return; + for (auto it = events.array_items().begin(); + it != events.array_items().end(); it++) { + auto event = *it; + if (event.is_object()) { + auto event_type = event["type"]; + auto params = event["params"]; + Handle event_constant = + factory_->NewStringConstant(event_type.string_value()); + std::vector> args{child_identifier, event_constant}; + if (params.is_array()) { + for (auto jt = params.array_items().begin(); + jt != params.array_items().end(); jt++) { + args.push_back(ParseExpression(*jt)); + } + } + AddEventStatement(args, stacks_[stacks_.size() - 1]); + } else if (event.is_string()) { + Handle event_constant = + factory_->NewStringConstant(event.string_value()); + AddEventStatement({child_identifier, event_constant}, + stacks_[stacks_.size() - 1]); + } + } + } + ParseResult Parse() { ASTParseError error = UNKOWN_ERROR; do { @@ -258,6 +295,14 @@ struct ASTParser final { return exprs; } + inline Handle ParseExpression(const json11::Json& json) { + if (json.is_string()) { + return factory_->NewStringConstant(json.string_value()); + } else { + return ParseBindingExpression(json); + } + } + Handle ParseBindingExpression(const json11::Json& json) { const json11::Json& exp_str = json["@binding"]; if (exp_str.is_string()) { @@ -682,6 +727,9 @@ struct ASTParser final { } } + // addEvent + AddEventForVNode(json["event"], child_identifier); + // new block for appending grandson // { // var parent = child; diff --git a/weex_core/Source/core/data_render/vnode/vnode.cc b/weex_core/Source/core/data_render/vnode/vnode.cc index 96ae608d2c..e61ee45094 100644 --- a/weex_core/Source/core/data_render/vnode/vnode.cc +++ b/weex_core/Source/core/data_render/vnode/vnode.cc @@ -25,7 +25,7 @@ namespace core { namespace data_render { VNode::VNode(const std::string &tag_name, const std::string &node_id, - const std::string &ref) { + const std::string &ref) { ref_ = ref; tag_name_ = tag_name; node_id_ = node_id; @@ -33,6 +33,7 @@ VNode::VNode(const std::string &tag_name, const std::string &node_id, styles_ = new std::map(); attributes_ = new std::map(); events_ = new std::map(); + event_params_map_.reset(new EventParamsMap); } VNode::~VNode() { @@ -60,6 +61,11 @@ VNode::~VNode() { } } +const VNode::ParamsList &VNode::GetParamsList(const std::string &event) { + auto it = event_params_map_->find(event); + return it != event_params_map_->end() ? it->second : ParamsList(); +} + void VNode::SetStyle(const std::string &key, const std::string &value) { MapInsertOrAssign(styles_, key, value); } @@ -98,11 +104,16 @@ VNode *VNode::FindNode(const std::string &render_object_ref) { return node; } -void VNode::AddEvent(const std::string &event, const std::string &function, +void VNode::AddEvent(const std::string &event, const std::vector ¶ms) { - //todo + auto it = event_params_map_->find(event); + if (it != event_params_map_->end()) { + it->second.push_back(params); + } else { + event_params_map_->insert({event, {params}}); + } } - + void VNode::AddChild(VNode *child) { child->parent_ = this; child->component_ = component_; diff --git a/weex_core/Source/core/data_render/vnode/vnode.h b/weex_core/Source/core/data_render/vnode/vnode.h index de7c8c844a..e626c9841d 100644 --- a/weex_core/Source/core/data_render/vnode/vnode.h +++ b/weex_core/Source/core/data_render/vnode/vnode.h @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include "core/render/node/render_object.h" namespace weex { @@ -33,6 +35,10 @@ class Value; class VNode { public: + typedef std::vector Params; + typedef std::vector ParamsList; + typedef std::unordered_map EventParamsMap; + VNode(const std::string &tag_name, const std::string &node_id, const std::string &ref); @@ -42,7 +48,7 @@ class VNode { void SetAttribute(const std::string &key, const std::string &value); - void AddEvent(const std::string &event, const std::string &function, + void AddEvent(const std::string &event, const std::vector ¶ms); void AddEvent(const std::string &event, void *func, void *inst); @@ -82,6 +88,12 @@ class VNode { return events_; } + inline EventParamsMap *event_params_map() const { + return event_params_map_.get(); + } + + const ParamsList& GetParamsList(const std::string& event); + inline bool HasChildren() { return !child_list_.empty(); } inline void set_component(VComponent* c) { @@ -111,6 +123,7 @@ class VNode { std::map *styles_; std::map *attributes_; std::map *events_; + std::unique_ptr event_params_map_; void MapInsertOrAssign(std::map *target_map, const std::string &key, const std::string &value);