rcpp_framework/web_modules/paged_article/paged_articles.cpp

88 lines
1.7 KiB
C++
Raw Normal View History

2022-02-05 02:45:53 +01:00
#include "paged_articles.h"
#include "core/html/html_builder.h"
#include "core/os/directory.h"
2022-02-05 15:10:56 +01:00
#include "core/html/utils.h"
2022-02-05 02:45:53 +01:00
#include "paged_article.h"
#include <iostream>
void PagedArticles::_handle_request_main(Request *request) {
render_menu(request);
// summary page
request->body += index_page;
2022-02-05 02:45:53 +01:00
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<Directory> dir;
dir.instance();
ERR_FAIL_COND_MSG(dir->open_dir(folder) != OK, "Error opening PagedArticles::folder! folder: " + folder);
while (dir->next()) {
2022-02-05 02:45:53 +01:00
if (dir->current_is_dir()) {
String np = dir->current_get_path();
String fn = dir->current_get_name();
String ff = folder;
ff.append_path(fn);
2022-02-05 02:45:53 +01:00
PagedArticle *p = new PagedArticle();
String seg = dir->current_get_name();
p->articles_folder = ff;
p->set_uri_segment(seg);
add_child(p);
}
}
generate_index_page();
2022-02-05 02:45:53 +01:00
}
void PagedArticles::generate_index_page() {
2022-02-05 02:45:53 +01:00
HTMLBuilder b;
b.div("article_list");
for (int i = 0; i < get_child_count(); ++i) {
PagedArticle *a = Object::cast_to<PagedArticle>(get_child(i));
if (a) {
b.a(a->get_full_uri());
b.div("article_list_entry")->w(a->get_summary())->cdiv();
2022-02-05 02:45:53 +01:00
b.ca();
}
}
b.cdiv();
index_page = b.result;
2022-02-05 02:45:53 +01:00
}
void PagedArticles::_notification(const int what) {
switch (what) {
case NOTIFICATION_ENTER_TREE:
load();
break;
default:
break;
}
WebNode::_notification(what);
2022-02-05 02:45:53 +01:00
}
PagedArticles::PagedArticles() :
WebNode() {
}
PagedArticles::~PagedArticles() {
}