From 2de0398fb095f35ccae8b3adb23ea90462b13608 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 5 Feb 2022 02:45:53 +0100 Subject: [PATCH] Added a new PagedArticles class. --- modules/paged_article/paged_articles.cpp | 98 ++++++++++++++++++++++++ modules/paged_article/paged_articles.h | 34 ++++++++ 2 files changed, 132 insertions(+) create mode 100644 modules/paged_article/paged_articles.cpp create mode 100644 modules/paged_article/paged_articles.h diff --git a/modules/paged_article/paged_articles.cpp b/modules/paged_article/paged_articles.cpp new file mode 100644 index 0000000..625a11c --- /dev/null +++ b/modules/paged_article/paged_articles.cpp @@ -0,0 +1,98 @@ +#include "paged_articles.h" + +#include "core/html/html_builder.h" + +#include "core/os/directory.h" +#include "core/utils.h" +#include "paged_article.h" + +#include + +void PagedArticles::_handle_request_main(Request *request) { + render_menu(request); + + // summary page + request->body += summary_page; + + request->compile_and_send_body(); +} + +void PagedArticles::load() { + ERR_FAIL_COND_MSG(folder == "", "Error: PagedArticles::load called, but a folder is not set!"); + + if (folder.size() > 0 && folder[folder.size() - 1] == '/') { + folder.pop_back(); + } + + Ref dir; + dir.instance(); + + ERR_FAIL_COND_MSG(dir->open_dir(folder) != OK, "Error opening PagedArticles::folder! folder: " + folder); + + while (dir->has_next()) { + if (!dir->read()) { + dir->next(); + continue; + } + + if (dir->current_is_dir()) { + if (dir->current_is_special_dir()) { + dir->next(); + continue; + } + + String np = dir->current_get_path(); + String fn = dir->current_get_name(); + + String ff = folder + "/" + fn; + + PagedArticle *p = new PagedArticle(); + String seg = dir->current_get_name(); + p->articles_folder = ff; + seg.print(); + p->set_uri_segment(seg); + add_child(p); + } + + dir->next(); + } + + generate_summary(); +} + +void PagedArticles::generate_summary() { + HTMLBuilder b; + + b.div("article_list"); + + for (int i = 0; i < get_child_count(); ++i) { + PagedArticle *a = Object::cast_to(get_child(i)); + + if (a) { + b.a(a->get_full_uri()); + b.div("article_list_entry")->w(a->get_summary())->cdiv(); + b.ca(); + } + } + + b.cdiv(); + + summary_page = b.result; +} + +void PagedArticles::_notification(const int what) { + switch (what) { + case NOTIFICATION_ENTER_TREE: + load(); + break; + default: + break; + } +} + +PagedArticles::PagedArticles() : + WebNode() { +} + +PagedArticles::~PagedArticles() { +} \ No newline at end of file diff --git a/modules/paged_article/paged_articles.h b/modules/paged_article/paged_articles.h new file mode 100644 index 0000000..0ddc75f --- /dev/null +++ b/modules/paged_article/paged_articles.h @@ -0,0 +1,34 @@ +#ifndef PAGED_ARTICLES_H +#define PAGED_ARTICLES_H + +#include "core/containers/vector.h" +#include "core/string.h" + +#include "core/file_cache.h" +#include "core/http/web_node.h" + +#include "core/http/request.h" + +//todo pagination + +class PagedArticles : public WebNode { + RCPP_OBJECT(PagedArticles, WebNode); + +public: + void _handle_request_main(Request *request); + + void load(); + void generate_summary(); + + void _notification(const int what); + + PagedArticles(); + ~PagedArticles(); + + String folder; + +protected: + String summary_page; +}; + +#endif \ No newline at end of file