Skip to content

Commit

Permalink
[core][data_render] add event support
Browse files Browse the repository at this point in the history
  • Loading branch information
yxping committed Nov 6, 2018
1 parent 75db0d1 commit 4da4a58
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
48 changes: 48 additions & 0 deletions weex_core/Source/core/data_render/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ struct ASTParser final {
block->PushExpression(call_func);
}

inline void AddEventStatement(std::vector<Handle<Expression>> args,
Handle<BlockStatement>& block) {
Handle<Expression> set_attr_func_id = factory_->NewIdentifier("addEvent");
Handle<CallExpression> call_func =
factory_->NewCallExpression(set_attr_func_id, args);
block->PushExpression(call_func);
}

void AddAppendChildCall(json11::Json& json, Handle<Identifier>& parent_identifier,
Handle<Identifier>& child_identifier) {
Handle<BlockStatement> statement = stacks_[stacks_.size() - 1];
Expand Down Expand Up @@ -113,6 +121,35 @@ struct ASTParser final {
}
}

void AddEventForVNode(
const json11::Json& events,
Handle<weex::core::data_render::Identifier> 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<StringConstant> event_constant =
factory_->NewStringConstant(event_type.string_value());
std::vector<Handle<Expression>> 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<StringConstant> event_constant =
factory_->NewStringConstant(event.string_value());
AddEventStatement({child_identifier, event_constant},
stacks_[stacks_.size() - 1]);
}
}
}

ParseResult Parse() {
ASTParseError error = UNKOWN_ERROR;
do {
Expand Down Expand Up @@ -258,6 +295,14 @@ struct ASTParser final {
return exprs;
}

inline Handle<Expression> ParseExpression(const json11::Json& json) {
if (json.is_string()) {
return factory_->NewStringConstant(json.string_value());
} else {
return ParseBindingExpression(json);
}
}

Handle<Expression> ParseBindingExpression(const json11::Json& json) {
const json11::Json& exp_str = json["@binding"];
if (exp_str.is_string()) {
Expand Down Expand Up @@ -682,6 +727,9 @@ struct ASTParser final {
}
}

// addEvent
AddEventForVNode(json["event"], child_identifier);

// new block for appending grandson
// {
// var parent = child;
Expand Down
19 changes: 15 additions & 4 deletions weex_core/Source/core/data_render/vnode/vnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ 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;

styles_ = new std::map<std::string, std::string>();
attributes_ = new std::map<std::string, std::string>();
events_ = new std::map<std::string, void *>();
event_params_map_.reset(new EventParamsMap);
}

VNode::~VNode() {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Value> &params) {
//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_;
Expand Down
15 changes: 14 additions & 1 deletion weex_core/Source/core/data_render/vnode/vnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include "core/render/node/render_object.h"

namespace weex {
Expand All @@ -33,6 +35,10 @@ class Value;

class VNode {
public:
typedef std::vector<Value> Params;
typedef std::vector<Params> ParamsList;
typedef std::unordered_map<std::string, ParamsList> EventParamsMap;

VNode(const std::string &tag_name, const std::string &node_id,
const std::string &ref);

Expand All @@ -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<Value> &params);
void AddEvent(const std::string &event, void *func, void *inst);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -111,6 +123,7 @@ class VNode {
std::map<std::string, std::string> *styles_;
std::map<std::string, std::string> *attributes_;
std::map<std::string, void *> *events_;
std::unique_ptr<EventParamsMap> event_params_map_;

void MapInsertOrAssign(std::map<std::string, std::string> *target_map,
const std::string &key, const std::string &value);
Expand Down

0 comments on commit 4da4a58

Please sign in to comment.