Browse Source

move functions out to better homes, somd dead code removal

master
Odilitime 5 years ago
parent
commit
4bc27b9168
  1. 155
      src/graphics/elements/INPUTElement.cpp
  2. 9
      src/graphics/elements/INPUTElement.h

155
src/graphics/elements/INPUTElement.cpp

@ -4,140 +4,13 @@ @@ -4,140 +4,13 @@
#include "../components/DocumentComponent.h"
#include "../../Log.h"
#include "../../html/HTMLParser.h"
#include "../../FormData.h"
#include <ctime>
INPUTElement::INPUTElement() {
isInline = true;
}
void handleResource(WebResource &res, std::string url, DocumentComponent *docComponent) {
if (res.resourceType == ResourceType::INVALID) {
logError() << "Invalid resource type: " << res.raw << std::endl;
return;
}
//std::cout << "body: " << res.raw << std::endl;
//std::cout << "type: " << res.resourceType << std::endl;
// parse HTML
if (res.resourceType == ResourceType::HTML) {
HTMLParser parser;
const std::clock_t begin = clock();
std::shared_ptr<Node> rootNode = parser.parse(res.raw);
const std::clock_t end = clock();
logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
// send NodeTree to window
//this->win->setDOM(rootNode);
//printNode(rootNode, 0);
// we need a way to communicate with our tabComponent
// maybe an event is best
if (docComponent->onBeforeLoad) {
docComponent->onBeforeLoad(url);
}
docComponent->setDOM(rootNode);
} else if (res.resourceType == ResourceType::TXT) {
std::cout << "Rendering text document" << std::endl;
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT);
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>();
tagNode->tag="p";
// bind tag to root
tagNode->parent = rootNode;
rootNode->children.push_back(tagNode);
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>();
textNode->text = res.raw;
// bind text to tag
textNode->parent = tagNode;
tagNode->children.push_back(textNode);
// send NodeTree to window
//this->win->setDOM(rootNode);
docComponent->setDOM(rootNode);
} else {
std::cout << "setWindowContent() - I don't know how to render non-html files" << std::endl;
}
}
std::shared_ptr<Node> findFormNode(std::shared_ptr<Node> node) {
if (!node.get()) {
// found root?
return nullptr;
}
TagNode *tagNode = dynamic_cast<TagNode*>(node.get());
if (tagNode) {
if (tagNode->tag == "form") {
return node;
}
}
return findFormNode(node->parent);
}
std::unique_ptr<std::pair<std::string, std::string>> getTagNodeNameValue(TagNode &tagNode) {
auto namePropIter = tagNode.properties.find("name");
if (namePropIter == tagNode.properties.end()) {
return nullptr;
}
auto valuePropIter = tagNode.properties.find("value");
if (valuePropIter == tagNode.properties.end()) {
return nullptr;
}
return std::make_unique<std::pair<std::string, std::string>>(namePropIter->second, valuePropIter->second);
}
// how do we match up nodes to components to extract the current values out of the components?
// well we link input components back to node (and/or nodes back to components)
// or we create a form UI component
std::unique_ptr<std::map<std::string, std::string>> buildFormData(std::shared_ptr<Node> formNode, std::unique_ptr<std::map<std::string, std::string>> result) {
if (!result) {
result = std::make_unique<std::map<std::string, std::string>>();
}
// check this node
TagNode *tagNode = dynamic_cast<TagNode*>(formNode.get());
if (tagNode) {
auto typePropIter = tagNode->properties.find("type");
bool skip = false;
if (typePropIter != tagNode->properties.end()) {
if (typePropIter->second == "submit") {
skip = true;
}
if (typePropIter->second == "checkbox") {
// FIXME: see if it's checked or not
skip = true;
}
if (typePropIter->second == "radio") {
// FIXME: see if it's checked or not
skip = true;
}
}
auto namePropIter = tagNode->properties.find("name");
if (!skip && namePropIter != tagNode->properties.end()) {
auto valuePropIter = tagNode->properties.find("value");
if (valuePropIter != tagNode->properties.end()) {
auto it = result->find(namePropIter->second);
if (it == result->end()) {
std::cout << "INPUTElement.buildFormData - setting " << namePropIter->second << " to [" << valuePropIter->second << "]" << std::endl;
result->insert(std::pair<std::string, std::string>(namePropIter->second, valuePropIter->second));
} else {
(*result)[namePropIter->second] = valuePropIter->second;
}
}
}
}
// check children noedes
for (auto &child : formNode->children) {
//std::shared_ptr<TagNode> tagNode = std::dynamic_pointer_cast<TagNode>(child);
//if (tagNode) {
result = buildFormData(child, std::move(result));
//}
}
return result;
}
std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &request) {
// const float rawX, const float rawY, const float rawWidth, const float rawHeight, const int windowWidth, const int windowHeight
// what should our default size be?
@ -166,12 +39,14 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re @@ -166,12 +39,14 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
auto propIter3 = tagNode->properties.find("value");
if (propIter3 != tagNode->properties.end()) {
butComponent->value = propIter3->second;
// resize button to text size
butComponent->resizeToTextSize();
} else {
butComponent->value = "submit";
butComponent->value = "Submit";
}
butComponent->onClick=[tagNode, request]() {
// recurse up to find oug form tag
std::shared_ptr<Node> formNode = findFormNode(request.node);
std::shared_ptr<Node> formNode = Node::findTagNodeParent("form", request.node);
if (!formNode) {
std::cout << "INPUTElement::renderer:butComponent->onClick - Can't find form parent for submit" << std::endl;
return;
@ -197,7 +72,7 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re @@ -197,7 +72,7 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
auto formData = buildFormData(formNode, nullptr);
// add our name/value (because we skip all submit buttons, there can only be one)
auto submitButtonNameValue = getTagNodeNameValue(*tagNode);
auto submitButtonNameValue = tagNode->getTagNodeNameValue();
if (submitButtonNameValue) {
formData->insert(*submitButtonNameValue);
}
@ -207,8 +82,8 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re @@ -207,8 +82,8 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
URL uAction = request.docComponent->currentURL.merge(URL(formNodeActionIter->second));
std::cout << "Action URL is " << uAction.toString() << std::endl;
// download URL
WebResource res = postWebResource(uAction, std::move(formData));
handleResource(res, uAction.toString(), request.docComponent);
WebResource res = getOnlineWebResource(uAction, std::move(formData));
request.docComponent->handleResource(res, uAction.toString());
} else {
// need documentComponent
@ -232,8 +107,8 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re @@ -232,8 +107,8 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
URL uAction = request.docComponent->currentURL.merge(URL(formNodeActionIter->second+"?"+queryString));
std::cout << "Action URL is " << uAction.toString() << std::endl;
// download URL
WebResource res = postWebResource(uAction, nullptr);
handleResource(res, uAction.toString(), request.docComponent);
WebResource res = getOnlineWebResource(uAction, nullptr);
request.docComponent->handleResource(res, uAction.toString());
}
};
@ -245,15 +120,5 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re @@ -245,15 +120,5 @@ std::unique_ptr<Component> INPUTElement::renderer(const ElementRenderRequest &re
//std::cout << "INPUTElement::renderer - ignoring input is of type: " << type << std::endl;
}
}
/*
TextNode *textNode = dynamic_cast<TextNode*>(node.get());
if (textNode) {
if (node->parent->children.size() == 1) {
std::unique_ptr<Component> component = std::make_unique<InputComponent>(textNode->text, x, y, 12, false, 0x000000FF, windowWidth, windowHeight);
return component;
}
}
*/
return nullptr;
}

9
src/graphics/elements/INPUTElement.h

@ -3,15 +3,6 @@ @@ -3,15 +3,6 @@
#include "Element.h"
#include "../components/Component.h"
#include "../../html/TextNode.h"
#include "../../WebResource.h"
class DocumentComponent;
std::shared_ptr<Node> findFormNode(std::shared_ptr<Node> node);
std::unique_ptr<std::pair<std::string, std::string>> getTagNodeNameValue(TagNode &tagNode);
std::unique_ptr<std::map<std::string, std::string>> buildFormData(std::shared_ptr<Node> formNode, std::unique_ptr<std::map<std::string, std::string>> result);
void handleResource(WebResource &res, std::string url, DocumentComponent *docComponent);
class INPUTElement : public Element {
public:

Loading…
Cancel
Save