diff options
Diffstat (limited to 'src/core/dom/Node.cpp')
-rw-r--r-- | src/core/dom/Node.cpp | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/src/core/dom/Node.cpp b/src/core/dom/Node.cpp index a65fc7f..c9651fb 100644 --- a/src/core/dom/Node.cpp +++ b/src/core/dom/Node.cpp @@ -23,6 +23,16 @@ namespace dom { /* Class Node */ +void Node::setName(std::string name) +{ + // Call the name change event + NameChangeEvent ev{this->name, name}; + triggerEvent(ev); + + // Set the new name + this->name = std::move(name); +} + void Node::path(std::vector<std::string> &p) const { if (!isRoot()) { @@ -46,9 +56,9 @@ void Node::doResolve(std::vector<Rooted<Node>> &res, } int Node::resolve(std::vector<Rooted<Node>> &res, - const std::vector<std::string> &path, Filter filter, - void *filterData, unsigned idx, VisitorSet &visited, - const std::string *alias) + const std::vector<std::string> &path, Filter filter, + void *filterData, unsigned idx, VisitorSet &visited, + const std::string *alias) { // Abort if this node was already visited for this path index std::pair<const Node *, int> recKey = std::make_pair(this, idx); @@ -88,6 +98,48 @@ std::vector<Rooted<Node>> Node::resolve(const std::vector<std::string> &path, return res; } +int Node::registerEventHandler(EventType type, EventHandler handler, + Handle<Managed> owner, + bool includeChildren) +{ + const int id = handlerIdCounter++; + handlers.insert(std::make_pair( + type, + EventHandlerDescriptor{id, handler, owner, this, includeChildren})); + return id; +} + +bool Node::unregisterEventHandler(int id) { + for (auto it = handlers.begin(); it != handlers.end(); it++) { + if (it->second.id == id) { + handlers.erase(it); + return true; + } + } + return false; +} + +bool Node::triggerEvent(Event &event, bool fromChild) { + bool res = false; + // Iterate over all event handlers + const auto range = handlers.equal_range(event.type); + for (auto it = range.first; it != range.second; it++) { + // Fetch a reference to the descriptor, check whether it should be + // called for bubbled events + EventHandlerDescriptor descr = it->second; + if (!fromChild || descr.includeChildren) { + descr.handler(event, descr.owner); + res = true; + } + } + + // If possible, let the event bubble up to the parent node + if (event.canBubble() && !parent.isNull()) { + res = parent->triggerEvent(event, true) | res; + } + return res; +} + } } |