From 39809a60ec959968905858e306cc4de516945340 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 17 Apr 2021 17:32:45 -0700 Subject: [PATCH] added navigation editor template and methods, moved markdown converstion to DocTools class --- brain/api/v1/SettingsAPI.inc.php | 7 ++++ brain/controller/DashControl.inc.php | 15 +++++++ brain/data/Book.inc.php | 61 ++++++--------------------- brain/data/Settings.inc.php | 50 ++++++++++++++++++++++ brain/utility/DocTools.inc.php | 54 ++++++++++++++++++++++++ brain/views/dash/navigation.twig | 35 ++++++++++++++++ src/com/actions/NavActions.js | 62 ++++++++++++++-------------- src/com/controllers/NavIndex.js | 2 +- 8 files changed, 207 insertions(+), 79 deletions(-) create mode 100644 brain/views/dash/navigation.twig diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index dbc6aab..89d7e09 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -51,6 +51,13 @@ class SettingsAPI "type" => "settingsUpdated", ]; + break; + case "nav-sync": + Settings::navSync($body); + $result = [ + "message" => "Navigation updated. Very slick!", + "type" => "menuUpdated", + ]; break; default: $result = [ diff --git a/brain/controller/DashControl.inc.php b/brain/controller/DashControl.inc.php index f6b0647..e0829c6 100644 --- a/brain/controller/DashControl.inc.php +++ b/brain/controller/DashControl.inc.php @@ -43,6 +43,21 @@ class DashControl die(); } + break; + case "navigation": + if (Session::active()) { + $config = new Settings(); + $settings = $config->getSettings(); + $template = "dash/navigation.twig"; + $pageOptions = [ + "title" => "Edit Dash Navigation", + "status" => Session::active(), + "menu" => $settings["menu"], + ]; + } else { + header("Location: /dashboard"); + die(); + } break; case "pages": if (Session::active()) { diff --git a/brain/data/Book.inc.php b/brain/data/Book.inc.php index efc2f44..f840f4b 100644 --- a/brain/data/Book.inc.php +++ b/brain/data/Book.inc.php @@ -114,54 +114,18 @@ class Book //grab current index from settings and update $id = $task != "create" ? $body["id"] : Settings::getCurrentIndex(); $uuid = $task != "create" ? $body["uuid"] : StringTools::createUUID(); - $write = - "---\n" . - "id: " . - $id . - "\n" . - "uuid: " . - $uuid . - "\n" . - "title: " . - $body["title"] . - "\n" . - "feature: " . - $feature . - "\n" . - "path: " . - $path . - "\n" . - "layout: " . - $body["layout"] . - "\n" . - "tags: " . - $body["tags"] . - "\n" . - "author: " . - $member["handle"] . - "\n" . - "created: " . - $created->format("Y-m-d\TH:i:sP") . - "\n" . - "updated: " . - $updated->format("Y-m-d\TH:i:sP") . - "\n" . - "deleted: " . - $deleted . - "\n" . - "slug: " . - $body["slug"] . - "\n" . - "menu: " . - $body["menu"] . - "\n" . - "published: " . - $body["published"] . - "\n" . - "featured: " . - $body["featured"] . - "\n---\n" . - $body["content"]; + // now that variables are done, set to body object and then convert to markdown to save + + $body["id"] = $id; + $body["uuid"] = $uuid; + $body["feature"] = $feature; + $body["path"] = $path; + $body["author"] = $member["handle"]; + $body["created"] = $created->format("Y-m-d\TH:i:sP"); + $body["updated"] = $updated->format("Y-m-d\TH:i:sP"); + $body["deleted"] = $deleted; + + $write = DocTools::objectToMD($body); // if layout is index, change path to file @@ -270,6 +234,7 @@ class Book } public function getContents() { + //move page collection to utiltiy class $parser = new Parser(); $contents = []; foreach ($this->files as $file) { diff --git a/brain/data/Settings.inc.php b/brain/data/Settings.inc.php index ed26523..137a6cb 100644 --- a/brain/data/Settings.inc.php +++ b/brain/data/Settings.inc.php @@ -49,6 +49,56 @@ class Settings DocTools::writeSettings("../config/settings.json", $settings); } + public static function navSync($data) + { + $settings = self::$settings; + + $remove = $data["remove"]; + //if remove contains id, find nav item page and set menu to false + if ($remove != null || $remove != "") { + $page = (new Book("../content/pages"))->findPageById($remove); + $page["menu"] = "false"; + $page["published"] + ? ($page["published"] = "true") + : ($page["published"] = "false"); + $page["featured"] + ? ($page["featured"] = "true") + : ($page["featured"] = "false"); + $page["deleted"] + ? ($page["deleted"] = "true") + : ($page["deleted"] = "false"); + $updated = new \Moment\Moment(); + $created = new \Moment\Moment($page["rawCreated"]); + $page["created"] = $created->format("Y-m-d\TH:i:sP"); + $page["updated"] = $updated->format("Y-m-d\TH:i:sP"); + + $md = DocTools::objectToMD($page); + + if ($page["layout"] == "index") { + $writePath = "../content/pages/start/index.md"; + } else { + $writePath = + "../content/pages/" . $page["path"] . "/" . $page["slug"] . ".md"; + } + + DocTools::writePages("write", $page["path"], $writePath, $md); + } + + $settings["menu"] = []; + $items = $data["menu"]; + foreach ($items as $item) { + array_push($settings["menu"], [ + "title" => $item["title"], + "id" => $item["id"], + "uuid" => $item["uuid"], + "slug" => $item["slug"], + "path" => $item["path"], + ]); + } + + DocTools::writeSettings("../config/settings.json", $settings); + } + public function getThemes() { return $this->themes; diff --git a/brain/utility/DocTools.inc.php b/brain/utility/DocTools.inc.php index 08876be..cdf98ba 100644 --- a/brain/utility/DocTools.inc.php +++ b/brain/utility/DocTools.inc.php @@ -33,4 +33,58 @@ class DocTools fwrite($new, json_encode($fileContents)); fclose($new); } + + public static function objectToMD($object) + { + $markdown = + "---\n" . + "id: " . + $object["id"] . + "\n" . + "uuid: " . + $object["uuid"] . + "\n" . + "title: " . + $object["title"] . + "\n" . + "feature: " . + $object["feature"] . + "\n" . + "path: " . + $object["path"] . + "\n" . + "layout: " . + $object["layout"] . + "\n" . + "tags: " . + $object["tags"] . + "\n" . + "author: " . + $object["author"] . + "\n" . + "created: " . + $object["created"] . + "\n" . + "updated: " . + $object["updated"] . + "\n" . + "deleted: " . + $object["deleted"] . + "\n" . + "slug: " . + $object["slug"] . + "\n" . + "menu: " . + $object["menu"] . + "\n" . + "published: " . + $object["published"] . + "\n" . + "featured: " . + $object["featured"] . + "\n---\n" . + $object["content"]; + + return $markdown; + } } diff --git a/brain/views/dash/navigation.twig b/brain/views/dash/navigation.twig new file mode 100644 index 0000000..e26a33d --- /dev/null +++ b/brain/views/dash/navigation.twig @@ -0,0 +1,35 @@ +{% extends "dash/_frame.twig" %} + +{% block title %} + {{ title }} +{% endblock %} + +{% block stylesheets %} + + {% endblock %} + + {% block mainContent %} + + {% endblock %} + + {% block javascripts %} + + {% endblock %} \ No newline at end of file diff --git a/src/com/actions/NavActions.js b/src/com/actions/NavActions.js index fe64e52..385dc06 100644 --- a/src/com/actions/NavActions.js +++ b/src/com/actions/NavActions.js @@ -1,35 +1,37 @@ export default class NavActions { - //-------------------------- - // constructor - //-------------------------- - constructor() {} - //-------------------------- - // methods - //-------------------------- - syncMenu() { - let navData = []; - let items = document.getElementById('nav-pages').children; - for (let index = 0; index < items.length; index++) { - navData.push({ - title: items[index].getElementsByTagName('label')[0].innerHTML, - id: items[index].id, - slug: items[index].getAttribute('data-slug'), - uuid: items[index].getAttribute('data-uuid'), - path: items[index].getAttribute('data-path') - }); - } + //-------------------------- + // constructor + //-------------------------- + constructor() {} + //-------------------------- + // methods + //-------------------------- + syncMenu() { + let navData = []; + let items = document.getElementById("nav-pages").children; + for (let index = 0; index < items.length; index++) { + navData.push({ + title: items[index].getElementsByTagName("label")[0].innerHTML, + id: items[index].id, + slug: items[index].getAttribute("data-slug"), + uuid: items[index].getAttribute("data-uuid"), + path: items[index].getAttribute("data-path"), + }); + } - let data = { nav: navData, remove: null }; - return new Promise(function (resolve) { - resolve(data); - }); - } + let data = { menu: navData, remove: null }; + return new Promise(function (resolve) { + resolve(data); + }); + } - removeItem(id) { - document.getElementById('nav-pages').removeChild(document.getElementById(id)); - } + removeItem(id) { + document + .getElementById("nav-pages") + .removeChild(document.getElementById(id)); + } - //-------------------------- - // event handlers - //-------------------------- + //-------------------------- + // event handlers + //-------------------------- } diff --git a/src/com/controllers/NavIndex.js b/src/com/controllers/NavIndex.js index a2e932b..99e1644 100644 --- a/src/com/controllers/NavIndex.js +++ b/src/com/controllers/NavIndex.js @@ -61,7 +61,7 @@ export default class NavIndex { break; case "edit-item": window.location = - "/@/dashboard/page/edit/" + e.target.getAttribute("data-id"); + "/dashboard/page/edit/" + e.target.getAttribute("data-id"); break; } }