Initial cleanup pass on Paginator and ListPage.

This commit is contained in:
Relintai 2022-07-02 21:56:10 +02:00
parent fa2ac74a7c
commit 549eb0b39b
5 changed files with 128 additions and 73 deletions

View File

@ -1,59 +1,59 @@
#include "paginator.h"
#include "core/math/math.h"
#include "core/math/math_funcs.h"
#include "html_builder.h"
int Paginator::get_item_count() const {
int HTMLPaginator::get_item_count() const {
return _item_count;
}
void Paginator::set_item_count(const int val) {
void HTMLPaginator::set_item_count(const int val) {
_item_count = val;
_page_count = Math::floorf_int(Math::divf(val, _max_visible_links));
}
int Paginator::get_page_count() const {
int HTMLPaginator::get_page_count() const {
return _page_count;
}
void Paginator::set_page_count(const int val) {
void HTMLPaginator::set_page_count(const int val) {
_page_count = val;
_item_count = val * _max_visible_links;
}
int Paginator::get_max_visible_links() const {
int HTMLPaginator::get_max_visible_links() const {
return _max_visible_links;
}
void Paginator::set_max_visible_links(const int val) {
void HTMLPaginator::set_max_visible_links(const int val) {
_max_visible_links = val;
set_item_count(_item_count);
}
void Paginator::start() {
void HTMLPaginator::start() {
_current_page_index = 0;
}
String Paginator::next() {
String HTMLPaginator::next() {
++_current_page_index;
return get_current();
}
String Paginator::get_current() {
String HTMLPaginator::get_current() {
return get_pagination_for_indx(_current_page_index);
}
String Paginator::get_pagination_for_indx(const int page_index) {
String HTMLPaginator::get_pagination_for_indx(const int page_index) {
if (use_links_array) {
return render_links(renderer, page_index);
} else {
return render_indexed(renderer, page_index);
}
}
String Paginator::get_pagination_for_num(const int page_num) {
String HTMLPaginator::get_pagination_for_num(const int page_num) {
return get_pagination_for_indx(page_num - 1);
}
String Paginator::render_indexed(Ref<Paginator> target, const int page_index) {
String HTMLPaginator::render_indexed(Ref<HTMLPaginator> target, const int page_index) {
if (!target.is_valid()) {
target = Ref<Paginator>(this);
target = Ref<HTMLPaginator>(this);
}
String s = target->base_url;
@ -95,7 +95,7 @@ String Paginator::render_indexed(Ref<Paginator> target, const int page_index) {
if (page_count != 0 && page_index != 0) {
b.li()->clsse(tclass_enabled_li);
{
b.a()->href(s + std::to_string(page_index - 1))->rel_prev()->f()->w(ttext_prev_link)->ca();
b.a()->href(s + itos(page_index - 1))->rel_prev()->f()->w(ttext_prev_link)->ca();
}
b.cli();
} else {
@ -107,21 +107,21 @@ String Paginator::render_indexed(Ref<Paginator> target, const int page_index) {
if (i != page_index) {
b.li()->clsse(tclass_enabled_li);
{
b.a()->href(s + std::to_string(i + 1))->f()->w(std::to_string(i + 1))->ca();
b.a()->href(s + itos(i + 1))->f()->w(itos(i + 1))->ca();
}
b.cli();
} else {
b.li()->clsse(tclass_disabled_li)->f()->w(std::to_string(i + 1))->cli();
b.li()->clsse(tclass_disabled_li)->f()->w(itos(i + 1))->cli();
}
}
} else {
b.li()->clsse(tclass_disabled_li)->f()->w(std::to_string(1))->cli();
b.li()->clsse(tclass_disabled_li)->f()->w(itos(1))->cli();
}
if (page_count != 0 && page_index < page_count - 1) {
b.li()->clsse(tclass_enabled_li);
{
b.a()->href(s + std::to_string(page_index + 2))->rel_next()->f()->w(ttext_next_link)->ca();
b.a()->href(s + itos(page_index + 2))->rel_next()->f()->w(ttext_next_link)->ca();
}
b.cli();
} else {
@ -132,16 +132,15 @@ String Paginator::render_indexed(Ref<Paginator> target, const int page_index) {
return b.result;
}
String Paginator::render_links(Ref<Paginator> target, const int page_index) {
String HTMLPaginator::render_links(Ref<HTMLPaginator> target, const int page_index) {
if (!target.is_valid()) {
target = Ref<Paginator>(this);
target = Ref<HTMLPaginator>(this);
}
if (page_index < 0) {
return "";
}
String s = target->base_url;
if (s.size() > 0 && s[s.size() - 1] != '/') {
s += '/';
@ -149,9 +148,8 @@ String Paginator::render_links(Ref<Paginator> target, const int page_index) {
uint32_t max = target->links.size();
int max_visible_links = target->_max_visible_links;
int page_count = target->_page_count;
//int page_count = target->_page_count;
const String &tclass_main_ul = target->class_main_ul;
const String &tclass_enabled_li = target->class_enabled_li;
const String &tclass_disabled_li = target->class_disabled_li;
@ -196,15 +194,15 @@ String Paginator::render_links(Ref<Paginator> target, const int page_index) {
if (i != page_index) {
b.li()->clsse(tclass_enabled_li);
{
b.a()->href(s + target->links[i])->f()->w(std::to_string(i + 1))->ca();
b.a()->href(s + target->links[i])->f()->w(itos(i + 1))->ca();
}
b.cli();
} else {
b.li()->clsse(tclass_disabled_li)->f()->w(std::to_string(i + 1))->cli();
b.li()->clsse(tclass_disabled_li)->f()->w(itos(i + 1))->cli();
}
}
} else {
b.li()->clsse(tclass_disabled_li)->f()->w(std::to_string(1))->cli();
b.li()->clsse(tclass_disabled_li)->f()->w(itos(1))->cli();
}
if (max != 0 && page_index < max - 1) {
@ -222,7 +220,7 @@ String Paginator::render_links(Ref<Paginator> target, const int page_index) {
return b.result;
}
Paginator::Paginator() {
HTMLPaginator::HTMLPaginator() {
use_links_array = false;
hide_if_one_page = false;
@ -238,6 +236,6 @@ Paginator::Paginator() {
text_prev_link = "previous";
}
Paginator::~Paginator() {
HTMLPaginator::~HTMLPaginator() {
renderer.unref();
}
}

View File

@ -1,13 +1,13 @@
#ifndef PAGINATOR_H
#define PAGINATOR_H
#ifndef HTML_PAGINATOR_H
#define HTML_PAGINATOR_H
#include "core/string.h"
#include "core/containers/vector.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "core/reference.h"
class Paginator : public Reference {
RCPP_OBJECT(Paginator, Reference);
class HTMLPaginator : public Reference {
GDCLASS(HTMLPaginator, Reference);
public:
//settint the item count will update page count and vice versa
@ -39,13 +39,13 @@ public:
String get_pagination_for_indx(const int page_index);
String get_pagination_for_num(const int page_num);
virtual String render_indexed(Ref<Paginator> target, const int page_index);
virtual String render_links(Ref<Paginator> target, const int page_index);
virtual String render_indexed(Ref<HTMLPaginator> target, const int page_index);
virtual String render_links(Ref<HTMLPaginator> target, const int page_index);
Ref<Paginator> renderer;
Ref<HTMLPaginator> renderer;
Paginator();
~Paginator();
HTMLPaginator();
~HTMLPaginator();
protected:
int _item_count;
@ -54,4 +54,4 @@ protected:
int _current_page_index;
};
#endif
#endif

View File

@ -1,17 +1,15 @@
#ifndef UTILS_H
#define UTILS_H
#ifndef HTML_UTILS_H
#define HTML_UTILS_H
#include "core/string.h"
#include "core/containers/vector.h"
#include "core/ustring.h"
#include "core/vector.h"
class Utils {
class HTMLUtils {
public:
static void markdown_to_html(String *str);
static String get_pagination(const String &base_url, const uint32_t max, const uint32_t current_index, const uint32_t max_visible_links = 10);
static String get_pagination_links(const String &base_url, const Vector<String> &links, const uint32_t current_index, const uint32_t max_visible_links = 10);
protected:
};
#endif
#endif

View File

@ -1,14 +1,13 @@
#include "list_page.h"
#include "core/math/math.h"
#include "web/html/html_builder.h"
#include "../../html/html_builder.h"
#include "../../http/web_permission.h"
#include "web/html/utils.h"
#include "web/http/web_permission.h"
#include <tinydir/tinydir.h>
#include <iostream>
void ListPage::handle_request_main(Request *request) {
void ListPage::_handle_request_main(Ref<WebServerRequest> request) {
if (_web_permission.is_valid()) {
if (_web_permission->activate(request)) {
return;
@ -22,7 +21,7 @@ void ListPage::handle_request_main(Request *request) {
return;
}
const String &cs = request->get_current_path_segment();
String cs = request->get_current_path_segment();
if (cs == "") {
render_menu(request);
@ -50,10 +49,10 @@ void ListPage::handle_request_main(Request *request) {
request->compile_and_send_body();
}
void ListPage::render_index(Request *request) {
void ListPage::_render_index(Ref<WebServerRequest> request) {
request->body += _pages[0];
}
void ListPage::render_preview(Request *request) {
void ListPage::_render_preview(Ref<WebServerRequest> request) {
}
void ListPage::load() {
@ -88,7 +87,7 @@ void ListPage::load() {
tinydir_close(&dir);
files.sort_inc();
files.sort();
Vector<String> list_entries;
@ -184,7 +183,6 @@ void ListPage::_notification(const int what) {
ListPage::ListPage() :
WebNode() {
max_visible_navigation_links = 6;
entry_per_page = 4;
main_div_class = "list_page";
@ -194,4 +192,40 @@ ListPage::ListPage() :
}
ListPage::~ListPage() {
}
}
void ListPage::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_paginate"), &ListPage::get_paginate);
ClassDB::bind_method(D_METHOD("set_paginate", "val"), &ListPage::set_paginate);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paginate"), "set_paginate", "get_paginate");
ClassDB::bind_method(D_METHOD("get_max_visible_navigation_links"), &ListPage::get_max_visible_navigation_links);
ClassDB::bind_method(D_METHOD("set_max_visible_navigation_links", "val"), &ListPage::set_max_visible_navigation_links);
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_visible_navigation_links"), "set_max_visible_navigation_links", "get_max_visible_navigation_links");
ClassDB::bind_method(D_METHOD("get_entry_per_page"), &ListPage::get_entry_per_page);
ClassDB::bind_method(D_METHOD("set_entry_per_page", "val"), &ListPage::set_entry_per_page);
ADD_PROPERTY(PropertyInfo(Variant::INT, "entry_per_page"), "set_entry_per_page", "get_entry_per_page");
ClassDB::bind_method(D_METHOD("get_folder"), &ListPage::get_folder);
ClassDB::bind_method(D_METHOD("set_folder", "val"), &ListPage::set_folder);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "folder"), "set_folder", "get_folder");
ClassDB::bind_method(D_METHOD("get_main_div_class"), &ListPage::get_main_div_class);
ClassDB::bind_method(D_METHOD("set_main_div_class", "val"), &ListPage::set_main_div_class);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "main_div_class"), "set_main_div_class", "get_main_div_class");
ClassDB::bind_method(D_METHOD("get_entry_div_class"), &ListPage::get_entry_div_class);
ClassDB::bind_method(D_METHOD("set_entry_div_class", "val"), &ListPage::set_entry_div_class);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "entry_div_class"), "set_entry_div_class", "get_entry_div_class");
ClassDB::bind_method(D_METHOD("get_empty_div_class"), &ListPage::get_empty_div_class);
ClassDB::bind_method(D_METHOD("set_empty_div_class", "val"), &ListPage::set_empty_div_class);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "empty_div_class"), "set_empty_div_class", "get_empty_div_class");
ClassDB::bind_method(D_METHOD("get_placeholder_text"), &ListPage::get_placeholder_text);
ClassDB::bind_method(D_METHOD("set_placeholder_text", "val"), &ListPage::set_placeholder_text);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder_text", "get_placeholder_text");
ClassDB::bind_method(D_METHOD("load"), &ListPage::load);
}

View File

@ -1,12 +1,10 @@
#ifndef LIST_PAGE_H
#define LIST_PAGE_H
#include "core/containers/vector.h"
#include "core/string.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "web/http/web_node.h"
#include "web/http/request.h"
#include "../../http/web_node.h"
// This class will load and generate pages from a folder of md files. It supports pagination,
// it will put entry_per_page md files per page. It generates html on enter tree, and caches everything.
@ -27,14 +25,40 @@
// <div class="list_entry_empty">No content yet!</div> // Set the class via the empty_div_class property, text via placeholder_text property
// </div>
class WebServerRequest;
class ListPage : public WebNode {
RCPP_OBJECT(ListPage, WebNode);
GDCLASS(ListPage, WebNode);
public:
void handle_request_main(Request *request);
bool get_paginate();
void set_paginate(const bool &val);
void render_index(Request *request);
void render_preview(Request *request);
int get_max_visible_navigation_links();
void set_max_visible_navigation_links(const int &val);
int get_entry_per_page();
void set_entry_per_page(const int &val);
String get_folder();
void set_folder(const String &val);
String get_main_div_class();
void set_main_div_class(const String &val);
String get_entry_div_class();
void set_entry_div_class(const String &val);
String get_empty_div_class();
void set_empty_div_class(const String &val);
String get_placeholder_text();
void set_placeholder_text(const String &val);
void _handle_request_main(Ref<WebServerRequest> request);
void _render_index(Ref<WebServerRequest> request);
void _render_preview(Ref<WebServerRequest> request);
void load();
@ -43,11 +67,13 @@ public:
virtual String render_entry(const String &list_entry);
virtual void render_no_entries_response();
void _notification(const int what);
ListPage();
~ListPage();
protected:
void _notification(const int what);
static void _bind_methods();
bool paginate;
int max_visible_navigation_links;
int entry_per_page;
@ -58,9 +84,8 @@ public:
String empty_div_class;
String placeholder_text;
protected:
Vector<String> _pages;
String _no_entries_response;
};
#endif
#endif