removed data events from fipamo api, added content api methods, cleaned up front end js

This commit is contained in:
Ro 2021-06-02 10:04:04 -07:00
parent 6cd28c06c0
commit a542b2d884
12 changed files with 267 additions and 187 deletions

View file

@ -29,7 +29,7 @@ class App
$app->add(TwigMiddleware::create($app, $twig)); $app->add(TwigMiddleware::create($app, $twig));
//set up routing //set up routing
$app->get( $app->get(
"/[{first}[/{second}[/{third}[/{fourth}]]]]", "/[{first}[/{second}[/{third}[/{fourth}[/{fifth}]]]]]",
"\RouteControl:get" "\RouteControl:get"
); );
$app->post( $app->post(

View file

@ -6,6 +6,29 @@ class PagesAPI
{ {
} }
public static function getPageContent($request, $args)
{
$task = $args["fourth"];
switch ($task) {
case "published":
$pageNum = $args["fifth"];
$result = (new Book("../content/pages"))->getPages($pageNum, 4, $task);
break;
case "single":
$uuid = $args["fifth"];
$result = (new Book("../content/pages"))->findPageById($uuid);
break;
default:
$result = [
"message" => "Hm, no task. That's unfortunate",
"type" => "TASK_NONE",
];
break;
}
return $result;
}
public static function handlePageTask($request, $args) public static function handlePageTask($request, $args)
{ {
$task = $args["fourth"]; $task = $args["fourth"];

View file

@ -17,9 +17,22 @@ class APIControl
array $args array $args
): ResponseInterface { ): ResponseInterface {
$filename = ""; $filename = "";
switch (isset($args["third"]) ? $args["third"] : "none") { switch (isset($args["third"]) ? $args["third"] : "none") {
case "status": case "status":
$result = AuthAPI::status(); $result = AuthAPI::status();
break;
case "page":
//echo
if (Member::verifyKey($_GET["key"])) {
$result = PagesAPI::getPageContent($request, $args);
} else {
$result = [
"message" => "API access denied, homie",
"type" => "API_ERROR",
];
}
break; break;
case "files": case "files":
if (Session::active()) { if (Session::active()) {

View file

@ -7,6 +7,21 @@ class Member
{ {
} }
public static function verifyKey(string $key)
{
if (isset($key)) {
$folks = (new Settings())->getFolks();
$found = find($folks, ["key" => $key]);
if ($found) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public static function updateData(string $key, string $data, $secret = null) public static function updateData(string $key, string $data, $secret = null)
{ {
$folks = (new Settings())->getFolks(); $folks = (new Settings())->getFolks();

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,5 @@
import FipamoApi from "../libraries/FipamoAPI"; import FipamoApi from "../libraries/FipamoAPI";
import FipamoAdminAPI from "../libraries/FipamoAdminAPI"; //import FipamoAdminAPI from "../libraries/FipamoAdminAPI";
import DataUitls from "./utils/DataUtils"; import DataUitls from "./utils/DataUtils";
import * as DataEvent from "./events/DataEvent"; import * as DataEvent from "./events/DataEvent";
import DashManager from "./controllers/DashManager"; import DashManager from "./controllers/DashManager";
@ -148,7 +148,7 @@ export default class Base {
if (e.target.id == "get-secret-btn") { if (e.target.id == "get-secret-btn") {
let data = { let data = {
email: document.getElementById("email").value, email: document.getElementById("email").value,
task: "retrieveSecret", task: "retrieveSecret"
}; };
this.processing = true; this.processing = true;
api api
@ -174,7 +174,7 @@ export default class Base {
let data = { let data = {
newPass: document.getElementById("new_password").value, newPass: document.getElementById("new_password").value,
newPassConfirm: document.getElementById("new_password2").value, newPassConfirm: document.getElementById("new_password2").value,
secret: document.getElementById("secret").value, secret: document.getElementById("secret").value
}; };
api api
.setNewPass(data) .setNewPass(data)

View file

@ -11,7 +11,7 @@ export default class Mailer {
//-------------------------- //--------------------------
sendMail() { sendMail() {
let mailData = { let mailData = {
content: "This is a test email", content: "This is a test email"
}; };
let admin = new FipamoAdminAPI(); let admin = new FipamoAdminAPI();
admin admin
@ -26,7 +26,7 @@ export default class Mailer {
testMail() { testMail() {
let mailData = { let mailData = {
content: "This is a test email", content: "This is a test email",
mail_task: "TESTING", mail_task: "TESTING"
}; };
let admin = new FipamoAdminAPI(); let admin = new FipamoAdminAPI();
admin admin

View file

@ -15,7 +15,7 @@ export default class NavActions {
id: items[index].id, id: items[index].id,
slug: items[index].getAttribute("data-slug"), slug: items[index].getAttribute("data-slug"),
uuid: items[index].getAttribute("data-uuid"), uuid: items[index].getAttribute("data-uuid"),
path: items[index].getAttribute("data-path"), path: items[index].getAttribute("data-path")
}); });
} }

View file

@ -31,7 +31,7 @@ export default class NavIndex {
} }
}); });
}); });
}, }
}); });
var nav = document.querySelectorAll(".nav-btn"); var nav = document.querySelectorAll(".nav-btn");
for (var i = 0, length = nav.length; i < length; i++) { for (var i = 0, length = nav.length; i < length; i++) {

View file

@ -2,7 +2,7 @@
import FipamoAdminAPI, { import FipamoAdminAPI, {
TASK_PAGE_CREATE, TASK_PAGE_CREATE,
TASK_PAGE_EDIT, TASK_PAGE_EDIT,
TASK_PAGE_DELETE, TASK_PAGE_DELETE
} from "../../libraries/FipamoAdminAPI"; } from "../../libraries/FipamoAdminAPI";
import * as DataEvent from "../events/DataEvent"; import * as DataEvent from "../events/DataEvent";
import PageActions from "../actions/PageActions"; import PageActions from "../actions/PageActions";
@ -235,7 +235,7 @@ export default class PostEditor {
f.target.result, f.target.result,
'" title="', '" title="',
escape(theFile.name), escape(theFile.name),
'"/>', '"/>'
].join(""); ].join("");
document.getElementById("featured-image-drop").innerHTML = ""; document.getElementById("featured-image-drop").innerHTML = "";
document.getElementById("featured-image-drop").appendChild(image); document.getElementById("featured-image-drop").appendChild(image);

View file

@ -1,28 +1,28 @@
import * as DataEvent from '../events/DataEvent'; import * as DataEvent from "../events/DataEvent";
import { position } from 'caret-pos'; import { position } from "caret-pos";
import EventEmitter from '../events/EventEmitter'; import EventEmitter from "../events/EventEmitter";
import * as EditorEvent from '../events/EditorEvent'; import * as EditorEvent from "../events/EditorEvent";
class TextEditor extends EventEmitter { class TextEditor extends EventEmitter {
/** /**
* Text Editor UI Component * Text Editor UI Component
* @constructor * @constructor
* @param {object} textEditor - Text area that will edit text * @param {object} textEditor - Text area that will edit text
* @param {number} scrollLimit - YPos where editor position will become fixed * @param {number} scrollLimit - YPos where editor position will become fixed
*/ */
//-------------------------- //--------------------------
// constructor // constructor
//-------------------------- //--------------------------
constructor(textEditor, scrollLimit) { constructor(textEditor, scrollLimit) {
super(); super();
//hljs.initHighlightingOnLoad(); //hljs.initHighlightingOnLoad();
this.textEditor = textEditor; this.textEditor = textEditor;
this.fixLimit = scrollLimit; this.fixLimit = scrollLimit;
this.caretPos = null; this.caretPos = null;
this.url = ''; this.url = "";
this.setInputs(); this.setInputs();
window.addEventListener('scroll', () => { window.addEventListener("scroll", () => {
//var fixLimit = this.fixLimit; //var fixLimit = this.fixLimit;
/** /**
* edit scroller needs work * edit scroller needs work
if (window.pageYOffset >= fixLimit) { if (window.pageYOffset >= fixLimit) {
document.getElementById('edit-control').style.position = 'fixed'; document.getElementById('edit-control').style.position = 'fixed';
@ -30,129 +30,143 @@ class TextEditor extends EventEmitter {
document.getElementById('edit-control').style.position = 'relative'; document.getElementById('edit-control').style.position = 'relative';
} }
*/ */
}); });
this.refresh(); this.refresh();
} }
//-------------------------- //--------------------------
// methods // methods
//-------------------------- //--------------------------
setInputs() { setInputs() {
let self = this; let self = this;
var editorButtons = document.querySelectorAll('.editor-button'); var editorButtons = document.querySelectorAll(".editor-button");
for (var i = 0, length = editorButtons.length; i < length; i++) { for (var i = 0, length = editorButtons.length; i < length; i++) {
editorButtons[i].addEventListener('click', e => this.handleEditorOption(e), false); editorButtons[i].addEventListener(
} "click",
(e) => this.handleEditorOption(e),
false
);
}
this.textEditor.addEventListener('input', e => { this.textEditor.addEventListener("input", (e) => {
let htmlTagRe = /<[a-z][\s\S]*>/g; let htmlTagRe = /<[a-z][\s\S]*>/g;
let text = this.textEditor.innerText; let text = this.textEditor.innerText;
if (e.inputType == 'insertParagraph') return; //cursor setting gets weird on return, so just back out if (e.inputType == "insertParagraph") return; //cursor setting gets weird on return, so just back out
if (text.search(htmlTagRe) > -1) { if (text.search(htmlTagRe) > -1) {
let caret = position(this.textEditor).pos; let caret = position(this.textEditor).pos;
self.refresh(); self.refresh();
position(this.textEditor, caret); position(this.textEditor, caret);
} }
}); });
} }
refresh() { refresh() {
//var spiffed = hljs.highlight('markdown', this.textEditor.innerText).value; //var spiffed = hljs.highlight('markdown', this.textEditor.innerText).value;
var spiffed = this.textEditor.innerText; var spiffed = this.textEditor.innerText;
spiffed = spiffed.replace(new RegExp('\r?\n', 'g'), '<br>'); spiffed = spiffed.replace(new RegExp("\r?\n", "g"), "<br>");
var temp = document.createElement('div'); var temp = document.createElement("div");
temp.innerText = spiffed; temp.innerText = spiffed;
this.textEditor.innerHTML = temp.innerText; this.textEditor.innerHTML = temp.innerText;
this.textEditor.style.maxWidth = '900px'; this.textEditor.style.maxWidth = "900px";
} }
notify(type, data) { notify(type, data) {
switch (type) { switch (type) {
case DataEvent.POST_UPDATED: case DataEvent.PAGE_UPDATED:
document.getElementById('submit-update').classList.add('icon-hide'); document.getElementById("submit-update").classList.add("icon-hide");
document.getElementById('submit-good').classList.remove('icon-hide'); document.getElementById("submit-good").classList.remove("icon-hide");
document.getElementById('edit-update').classList.remove('submit-start'); document.getElementById("edit-update").classList.remove("submit-start");
document.getElementById('edit-update').classList.add('submit-cool'); document.getElementById("edit-update").classList.add("submit-cool");
setTimeout(() => { setTimeout(() => {
document.getElementById('submit-update').classList.remove('icon-hide'); document
document.getElementById('submit-good').classList.add('icon-hide'); .getElementById("submit-update")
document.getElementById('edit-update').classList.add('submit-start'); .classList.remove("icon-hide");
document.getElementById('edit-update').classList.remove('submit-cool'); document.getElementById("submit-good").classList.add("icon-hide");
}, 2000); document.getElementById("edit-update").classList.add("submit-start");
break; document
case DataEvent.POST_ADDED: .getElementById("edit-update")
// do nothing .classList.remove("submit-cool");
break; }, 2000);
case EditorEvent.EDITOR_UPLOAD_POST_IMAGE: break;
position(this.textEditor, this.caretPos); case DataEvent.PAGE_ADDED:
var sel, range; // do nothing
//var pulled; break;
sel = window.getSelection(); //console.log(sel) case EditorEvent.EDITOR_UPLOAD_POST_IMAGE:
//console.log(note.message) position(this.textEditor, this.caretPos);
if (sel.rangeCount) { var sel, range;
range = sel.getRangeAt(0); //var pulled;
//pulled = sel.getRangeAt(0).toString(); sel = window.getSelection(); //console.log(sel)
range.deleteContents(); //console.log(note.message)
range.insertNode( if (sel.rangeCount) {
document.createTextNode('![image alt text](' + data + " 'image title')") range = sel.getRangeAt(0);
); //pulled = sel.getRangeAt(0).toString();
} range.deleteContents();
this.refresh(); range.insertNode(
break; document.createTextNode(
} "![image alt text](" + data + " 'image title')"
} )
//-------------------------- );
// event handlers }
//-------------------------- this.refresh();
handleEditorOption(e) { break;
e.preventDefault(); }
var sel, range, pulled; }
sel = window.getSelection(); //console.log(sel) //--------------------------
if (sel.rangeCount) { // event handlers
range = sel.getRangeAt(0); //--------------------------
pulled = sel.getRangeAt(0).toString(); handleEditorOption(e) {
range.deleteContents(); e.preventDefault();
switch (e.target.id) { var sel, range, pulled;
case 'edit-bold': sel = window.getSelection(); //console.log(sel)
range.insertNode(document.createTextNode('**' + pulled + '**')); if (sel.rangeCount) {
break; range = sel.getRangeAt(0);
case 'edit-italic': pulled = sel.getRangeAt(0).toString();
range.insertNode(document.createTextNode('*' + pulled + '*')); range.deleteContents();
break; switch (e.target.id) {
case 'edit-strikethrough': case "edit-bold":
range.insertNode(document.createTextNode('<del>' + pulled + '</del>')); range.insertNode(document.createTextNode("**" + pulled + "**"));
break; break;
case 'edit-header1': case "edit-italic":
range.insertNode(document.createTextNode('# ' + pulled)); range.insertNode(document.createTextNode("*" + pulled + "*"));
break; break;
case 'edit-header2': case "edit-strikethrough":
range.insertNode(document.createTextNode('## ' + pulled)); range.insertNode(
break; document.createTextNode("<del>" + pulled + "</del>")
case 'edit-header3': );
range.insertNode(document.createTextNode('### ' + pulled)); break;
break; case "edit-header1":
case 'edit-image': range.insertNode(document.createTextNode("# " + pulled));
this.caretPos = position(this.textEditor).pos; break;
this.emitEvent(EditorEvent.EDITOR_UPLOAD_POST_IMAGE); case "edit-header2":
break; range.insertNode(document.createTextNode("## " + pulled));
case 'submit-save': break;
case 'edit-save': case "edit-header3":
this.emitEvent(EditorEvent.EDITOR_SAVE); range.insertNode(document.createTextNode("### " + pulled));
break; break;
case 'submit-update': case "edit-image":
case 'edit-update': this.caretPos = position(this.textEditor).pos;
this.emitEvent(EditorEvent.EDITOR_UPDATE); this.emitEvent(EditorEvent.EDITOR_UPLOAD_POST_IMAGE);
break; break;
case 'edit-link': case "submit-save":
range.insertNode(document.createTextNode('[' + pulled + '](PASTE URL HERE)')); case "edit-save":
break; this.emitEvent(EditorEvent.EDITOR_SAVE);
case 'edit-delete': break;
this.emitEvent(EditorEvent.EDITOR_DELETE); case "submit-update":
break; case "edit-update":
default: this.emitEvent(EditorEvent.EDITOR_UPDATE);
//range.insertNode(document.createTextNode("[" + self.url + "](PASTE URL HERE)")); break;
break; case "edit-link":
} range.insertNode(
} document.createTextNode("[" + pulled + "](PASTE URL HERE)")
this.refresh(); );
} break;
case "edit-delete":
this.emitEvent(EditorEvent.EDITOR_DELETE);
break;
default:
//range.insertNode(document.createTextNode("[" + self.url + "](PASTE URL HERE)"));
break;
}
}
this.refresh();
}
} }
export default TextEditor; export default TextEditor;

View file

@ -1,9 +1,12 @@
//** REQUEST TYPES **//
export const REQUEST_TYPE_POST = "POST"; export const REQUEST_TYPE_POST = "POST";
export const REQUEST_TYPE_GET = "GET"; export const REQUEST_TYPE_GET = "GET";
export const REQUEST_TYPE_PUT = "PUT"; export const REQUEST_TYPE_PUT = "PUT";
export const REQUEST_TYPE_DELETE = "DELETE"; export const REQUEST_TYPE_DELETE = "DELETE";
//** POST CONTENT TYPES **//
export const CONTENT_TYPE_JSON = "json"; export const CONTENT_TYPE_JSON = "json";
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded"; export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
//** API URLS **//
export const API_STATUS = "/api/v1/status"; export const API_STATUS = "/api/v1/status";
export const API_INIT = "/api/v1/init"; export const API_INIT = "/api/v1/init";
export const API_RESTORE = "/api/v1/restore"; export const API_RESTORE = "/api/v1/restore";
@ -12,20 +15,29 @@ export const API_GET_PAGES = "/api/v1/page/published";
export const API_GET_PAGE = "/api/v1/page/single"; export const API_GET_PAGE = "/api/v1/page/single";
export const API_GET_SECRET = "/api/v1/get-secret"; export const API_GET_SECRET = "/api/v1/get-secret";
export const API_RESET_PASS = "/api/v1/reset-password"; export const API_RESET_PASS = "/api/v1/reset-password";
import * as DataEvent from "../com/events/DataEvent"; //** API TASKS **//
export const AUTH_STATUS = "getAuthStatus";
export const TASK_SITE_INIT = "blogInit";
export const TASK_BACKUP_RESTORE = "downloadBackup";
export const TASK_GET_SECRET = "retrieveSecret";
export const TASK_RESET_PASS = "resetPassword";
export const TASK_GET_CONTENT = "retrieveContent";
export default class FipamoAPI { export default class FipamoAPI {
//-------------------------- //--------------------------
// constructor // constructor
//-------------------------- //--------------------------
constructor() {} constructor() {}
//-------------------------- //--------------------------
// public // methods
//-------------------------- //--------------------------
//** MEMBER AUTH METHODS **//
login(data) { login(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._request( this._request(
API_LOGIN, API_LOGIN,
DataEvent.AUTH_STATUS, AUTH_STATUS,
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
@ -42,7 +54,7 @@ export default class FipamoAPI {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._request( this._request(
API_INIT, API_INIT,
DataEvent.API_INIT, TASK_SITE_INIT,
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
@ -61,7 +73,7 @@ export default class FipamoAPI {
var url, event, method, type, data; var url, event, method, type, data;
url = API_RESTORE; url = API_RESTORE;
event = DataEvent.API_BACKUP_RESTORE; event = TASK_BACKUP_RESTORE;
method = REQUEST_TYPE_POST; method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_FORM; type = CONTENT_TYPE_FORM;
data = new FormData(form); data = new FormData(form);
@ -74,29 +86,12 @@ export default class FipamoAPI {
}); });
}); });
} }
getPages(num) {
let pageNum = num;
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
return new Promise((resolve, reject) => {
this._request(
API_GET_PAGES + "/" + pageNum,
DataEvent.API_GET_PAGES,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
getSecret(data) { getSecret(data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._request( this._request(
API_GET_SECRET, API_GET_SECRET,
DataEvent.API_GET_SECRET, TASK_GET_SECRET,
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
@ -114,7 +109,7 @@ export default class FipamoAPI {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._request( this._request(
API_RESET_PASS, API_RESET_PASS,
DataEvent.API_RESET_PASS, TASK_RESET_PASS,
REQUEST_TYPE_POST, REQUEST_TYPE_POST,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
data data
@ -128,11 +123,31 @@ export default class FipamoAPI {
}); });
} }
getPage(id) { //** CONTENT API METHODS **//
getPages(num, key) {
let pageNum = num;
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._request( this._request(
API_GET_PAGE + "/" + id, API_GET_PAGES + "/" + pageNum + "?key=" + key,
DataEvent.API_GET_PAGES, TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
getPage(id, key) {
return new Promise((resolve, reject) => {
this._request(
API_GET_PAGE + "/" + id + "?key=" + key,
TASK_GET_CONTENT,
REQUEST_TYPE_GET REQUEST_TYPE_GET
) )
.then((result) => { .then((result) => {