You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7 KiB

#include <stdexcept>
#include "Element.h"
#include "../proc/document.cpp"
#include "../proc/string.cpp"
#include "../proc/uuid.cpp"
#include "../class/EventBus.h"
using namespace cjs;
void Element::_identify_and_rebase() {
try {
std::string uuid = this->getUUID();
this->rebase("[data-cjs-uuid='"+uuid+"']");
} catch (std::runtime_error& e) {
std::string new_uuid = uuid::v4();
this->setData("cjs-uuid", new_uuid);
this->rebase("[data-cjs-uuid='"+new_uuid+"']");
}
}
Element::Element(std::string selector, int nth_elem) {
this->_selector_ref = selector;
this->_selector_nth_elem = nth_elem;
this->_identify_and_rebase();
}
Element::Element(Element& parent, std::string innerHTML, std::string selector, int nth_elem) {
this->_selector_ref = selector;
this->_selector_nth_elem = nth_elem;
parent.append(innerHTML);
this->_identify_and_rebase();
}
std::string Element::getInnerHTML() {
cjs_str* html = doc::get_inner_html_of(this->_selector_ref, this->_selector_nth_elem);
std::string return_str = html->to_str();
delete html;
return return_str;
}
std::string Element::getInnerText() {
cjs_str* text = doc::get_inner_text_of(this->_selector_ref, this->_selector_nth_elem);
std::string return_str = text->to_str();
delete text;
return return_str;
}
std::string Element::getID() {
cjs_str* html = doc::get_id_of(this->_selector_ref, this->_selector_nth_elem);
std::string return_str = html->to_str();
delete html;
return return_str;
}
std::string Element::getUUID() {
return this->getData("cjs-uuid");
}
std::string Element::getClassRaw() {
cjs_str* html = doc::get_class_of(this->_selector_ref, this->_selector_nth_elem);
std::string return_str = html->to_str();
delete html;
return return_str;
}
std::vector<std::string> Element::getClasses() {
cjs_str* html = doc::get_class_of(this->_selector_ref, this->_selector_nth_elem);
std::vector<std::string> result = html->split();
delete html;
return result;
}
bool Element::hasClass(std::string cls) {
std::vector<std::string> classes = this->getClasses();
for ( std::string s : classes )
if ( s == cls ) return true;
return false;
}
std::string Element::getAttribute(std::string attr) {
cjs_str* value = doc::_query_select_attribute(attr, this->_selector_ref, this->_selector_nth_elem);
std::string return_str = value->to_str();
delete value;
return return_str;
}
std::string Element::getData(std::string key) {
return this->getAttribute("data-"+key);
}
void Element::setInnerHTML(std::string html) {
doc::set_inner_html_of(this->_selector_ref, html, this->_selector_nth_elem);
}
void Element::setInnerText(std::string text) {
doc::set_inner_text_of(this->_selector_ref, text, this->_selector_nth_elem);
}
void Element::setID(std::string id) {
doc::set_id_of(this->_selector_ref, id, this->_selector_nth_elem);
}
void Element::setClassRaw(std::string cls) {
doc::set_class_of(this->_selector_ref, cls, this->_selector_nth_elem);
}
void Element::addClass(std::string cls) {
if ( !this->hasClass(cls) ) {
std::vector<std::string> classes = this->getClasses();
classes.push_back(cls);
this->setClassRaw(str::join(classes, " "));
}
}
void Element::removeClass(std::string cls) {
if ( this->hasClass(cls) ) {
std::vector<std::string> old_classes = this->getClasses();
std::vector<std::string> new_classes;
for ( std::string maybe_cls : old_classes ) {
if ( maybe_cls != cls )
new_classes.push_back(maybe_cls);
}
this->setClassRaw(str::join(new_classes, " "));
}
}
void Element::toggleClass(std::string cls) {
if ( this->hasClass(cls) ) this->removeClass(cls);
else this->addClass(cls);
}
void Element::setAttribute(std::string attr, std::string value) {
doc::_query_set_attribute(attr, value, this->_selector_ref, this->_selector_nth_elem);
}
void Element::setData(std::string key, std::string value) {
this->setAttribute("data-"+key, value);
}
void Element::append(std::string html) {
this->setInnerHTML(this->getInnerHTML()+html);
}
void Element::addEventListener(std::string dom_event, void(*func)()) {
// Generate the unique function key using the dom_event and uuid
std::string event_key = this->getUUID() + "-" + dom_event;
// Add the function and the key to the event bus
g_event_bus.addHandler(event_key, func);
// Add the event bus link to the element's dom event
doc::link_event_bus(dom_event, event_key, this->_selector_ref, this->_selector_nth_elem);
}
void Element::rebase(std::string query_selector, int nth_elem) {
this->_selector_ref = query_selector;
this->_selector_nth_elem = nth_elem;
}