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.
117 lines
5.2 KiB
117 lines
5.2 KiB
#ifndef CJS_PROC_DOCUMENT
|
|
#define CJS_PROC_DOCUMENT
|
|
|
|
#include <string>
|
|
#include <stdexcept>
|
|
#include "string.cpp"
|
|
|
|
namespace cjs {
|
|
namespace doc {
|
|
#include <emscripten.h>
|
|
|
|
cjs_str* _query_select_property(std::string method, std::string query_selector, int nth_elem = 0) {
|
|
int method_pass = cjs::str::pass(cjs::str::cpp_to_c(method), method.length());
|
|
int query_pass = cjs::str::pass(cjs::str::cpp_to_c(query_selector), query_selector.length());
|
|
|
|
int return_str_pass = EM_ASM_INT({
|
|
try {
|
|
return cjs.string_pass(document.querySelectorAll(cjs.receive($1))[$2][cjs.receive($0)]);
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}, method_pass, query_pass, nth_elem);
|
|
|
|
if ( return_str_pass == 0 ) {
|
|
throw std::runtime_error("The property or element is undefined.");
|
|
}
|
|
|
|
cjs_str* return_str = cjs::str::receive(return_str_pass);
|
|
return return_str;
|
|
}
|
|
|
|
cjs_str* _query_select_attribute(std::string attribute, std::string query_selector, int nth_elem = 0) {
|
|
int attribute_pass = cjs::str::pass(cjs::str::cpp_to_c(attribute), attribute.length());
|
|
int query_pass = cjs::str::pass(cjs::str::cpp_to_c(query_selector), query_selector.length());
|
|
|
|
int return_str_pass = EM_ASM_INT({
|
|
try {
|
|
return cjs.string_pass(document.querySelectorAll(cjs.receive($1))[$2].getAttribute(cjs.receive($0)));
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}, attribute_pass, query_pass, nth_elem);
|
|
|
|
if ( return_str_pass == 0 ) {
|
|
throw std::runtime_error("The attribute or element is undefined.");
|
|
}
|
|
|
|
cjs_str* return_str = cjs::str::receive(return_str_pass);
|
|
return return_str;
|
|
}
|
|
|
|
void _query_set_property(std::string property, std::string value, std::string query_selector, int nth_elem = 0) {
|
|
int property_pass = cjs::str::pass(cjs::str::cpp_to_c(property), property.length());
|
|
int value_pass = cjs::str::pass(cjs::str::cpp_to_c(value), value.length());
|
|
int query_pass = cjs::str::pass(cjs::str::cpp_to_c(query_selector), query_selector.length());
|
|
|
|
EM_ASM({
|
|
document.querySelectorAll(cjs.receive($2))[$3][cjs.receive($0)] = cjs.receive($1);
|
|
}, property_pass, value_pass, query_pass, nth_elem);
|
|
}
|
|
|
|
void _query_set_attribute(std::string attribute, std::string value, std::string query_selector, int nth_elem = 0) {
|
|
int attribute_pass = cjs::str::pass(cjs::str::cpp_to_c(attribute), attribute.length());
|
|
int value_pass = cjs::str::pass(cjs::str::cpp_to_c(value), value.length());
|
|
int query_pass = cjs::str::pass(cjs::str::cpp_to_c(query_selector), query_selector.length());
|
|
|
|
EM_ASM({
|
|
document.querySelectorAll(cjs.receive($2))[$3].setAttribute(cjs.receive($0), cjs.receive($1));
|
|
}, attribute_pass, value_pass, query_pass, nth_elem);
|
|
}
|
|
|
|
void link_event_bus(std::string dom_event, std::string event_bus_key, std::string query_selector, int nth_elem = 0) {
|
|
int dom_event_pass = cjs::str::pass(cjs::str::cpp_to_c(dom_event), dom_event.length());
|
|
int event_bus_key_pass = cjs::str::pass(cjs::str::cpp_to_c(event_bus_key), event_bus_key.length());
|
|
int query_pass = cjs::str::pass(cjs::str::cpp_to_c(query_selector), query_selector.length());
|
|
|
|
EM_ASM({
|
|
document.querySelectorAll(cjs.receive($2))[$3].addEventListener(cjs.receive($0), () => cjs.fire_event_bus(cjs.receive($1)));
|
|
}, dom_event_pass, event_bus_key_pass, query_pass, nth_elem);
|
|
}
|
|
|
|
cjs_str* get_inner_html_of(std::string query_selector, int nth_elem = 0) {
|
|
return _query_select_property("innerHTML", query_selector, nth_elem);
|
|
}
|
|
|
|
void set_inner_html_of(std::string query_selector, std::string value, int nth_elem = 0) {
|
|
_query_set_property("innerHTML", value, query_selector, nth_elem);
|
|
}
|
|
|
|
cjs_str* get_inner_text_of(std::string query_selector, int nth_elem = 0) {
|
|
return _query_select_property("innerText", query_selector, nth_elem);
|
|
}
|
|
|
|
void set_inner_text_of(std::string query_selector, std::string value, int nth_elem = 0) {
|
|
_query_set_property("innerText", value, query_selector, nth_elem);
|
|
}
|
|
|
|
cjs_str* get_id_of(std::string query_selector, int nth_elem = 0) {
|
|
return _query_select_property("id", query_selector, nth_elem);
|
|
}
|
|
|
|
void set_id_of(std::string query_selector, std::string value, int nth_elem = 0) {
|
|
_query_set_property("id", value, query_selector, nth_elem);
|
|
}
|
|
|
|
cjs_str* get_class_of(std::string query_selector, int nth_elem = 0) {
|
|
return _query_select_attribute("class", query_selector, nth_elem);
|
|
}
|
|
|
|
void set_class_of(std::string query_selector, std::string cls, int nth_elem = 0) {
|
|
return _query_set_attribute("class", cls, query_selector, nth_elem);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif //CJS_PROC_DOCUMENT
|