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));
//set up routing
$app->get(
"/[{first}[/{second}[/{third}[/{fourth}]]]]",
"/[{first}[/{second}[/{third}[/{fourth}[/{fifth}]]]]]",
"\RouteControl:get"
);
$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)
{
$task = $args["fourth"];

View file

@ -17,9 +17,22 @@ class APIControl
array $args
): ResponseInterface {
$filename = "";
switch (isset($args["third"]) ? $args["third"] : "none") {
case "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;
case "files":
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)
{
$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 FipamoAdminAPI from "../libraries/FipamoAdminAPI";
//import FipamoAdminAPI from "../libraries/FipamoAdminAPI";
import DataUitls from "./utils/DataUtils";
import * as DataEvent from "./events/DataEvent";
import DashManager from "./controllers/DashManager";
@ -148,7 +148,7 @@ export default class Base {
if (e.target.id == "get-secret-btn") {
let data = {
email: document.getElementById("email").value,
task: "retrieveSecret",
task: "retrieveSecret"
};
this.processing = true;
api
@ -174,7 +174,7 @@ export default class Base {
let data = {
newPass: document.getElementById("new_password").value,
newPassConfirm: document.getElementById("new_password2").value,
secret: document.getElementById("secret").value,
secret: document.getElementById("secret").value
};
api
.setNewPass(data)

View file

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

View file

@ -15,7 +15,7 @@ export default class NavActions {
id: items[index].id,
slug: items[index].getAttribute("data-slug"),
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");
for (var i = 0, length = nav.length; i < length; i++) {

View file

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

View file

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

View file

@ -1,9 +1,12 @@
//** REQUEST TYPES **//
export const REQUEST_TYPE_POST = "POST";
export const REQUEST_TYPE_GET = "GET";
export const REQUEST_TYPE_PUT = "PUT";
export const REQUEST_TYPE_DELETE = "DELETE";
//** POST CONTENT TYPES **//
export const CONTENT_TYPE_JSON = "json";
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
//** API URLS **//
export const API_STATUS = "/api/v1/status";
export const API_INIT = "/api/v1/init";
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_SECRET = "/api/v1/get-secret";
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 {
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// public
// methods
//--------------------------
//** MEMBER AUTH METHODS **//
login(data) {
return new Promise((resolve, reject) => {
this._request(
API_LOGIN,
DataEvent.AUTH_STATUS,
AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
@ -42,7 +54,7 @@ export default class FipamoAPI {
return new Promise((resolve, reject) => {
this._request(
API_INIT,
DataEvent.API_INIT,
TASK_SITE_INIT,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
@ -61,7 +73,7 @@ export default class FipamoAPI {
var url, event, method, type, data;
url = API_RESTORE;
event = DataEvent.API_BACKUP_RESTORE;
event = TASK_BACKUP_RESTORE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_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) {
return new Promise((resolve, reject) => {
this._request(
API_GET_SECRET,
DataEvent.API_GET_SECRET,
TASK_GET_SECRET,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
@ -114,7 +109,7 @@ export default class FipamoAPI {
return new Promise((resolve, reject) => {
this._request(
API_RESET_PASS,
DataEvent.API_RESET_PASS,
TASK_RESET_PASS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
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) => {
this._request(
API_GET_PAGE + "/" + id,
DataEvent.API_GET_PAGES,
API_GET_PAGES + "/" + pageNum + "?key=" + key,
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
)
.then((result) => {