Skip to content

Commit

Permalink
Improve naming
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Mar 5, 2024
1 parent 211c9dc commit 10a79d2
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 96 deletions.
6 changes: 6 additions & 0 deletions dist/phlex.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
interface Event {
readonly targetNode: Element | null;
readonly newNode: Element | null;
}
export declare function init(): void;
export declare function defineAction(name: string, action: (event: Event) => void): void;
export {};
76 changes: 34 additions & 42 deletions dist/phlex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 47 additions & 54 deletions src/phlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,13 @@ import { morph } from "morphlex";

type ActionMap = Map<string, (event: Event) => void>;

const Actions: ActionMap = new Map();

Actions.set("morph", ({ target, node }) => {
if (target && node) {
morph(target, node);
}
});

Actions.set("replace", ({ target, node }) => {
if (target && node) {
target.replaceWith(node);
}
});

Actions.set("append", ({ target, node }) => {
if (target && node) {
target.appendChild(node);
}
});

Actions.set("prepend", ({ target, node }) => {
if (target && node) {
target.prepend(node);
}
});

Actions.set("refresh", () => {
fetch(document.location.href).then((response) => {
if (response.ok) {
response.text().then((text) => {
morph(document.documentElement, text);
});
}
});
});

Actions.set("remove", ({ target }) => {
target?.remove();
});

Actions.set("navigate", ({ node }) => {
if (node) morph(document.documentElement, node);
});

interface Event {
readonly node: Element | null;
readonly target: HTMLElement | null;
readonly targetNode: Element | null;
readonly newNode: Element | null;
}

const Actions: ActionMap = new Map();

export function init(): void {
// document.addEventListener("submit", (event) => {
// if (event.target instanceof HTMLFormElement) {
Expand All @@ -70,9 +28,9 @@ export function init(): void {
}

const href = link.href;
const action = link.dataset.action;
const targetId = link.dataset.target;
const fragment = link.dataset.fragment;
const action = link.getAttribute("phlex-action");
const targetId = link.getAttribute("phlex-target");
const fragment = link.getAttribute("phlex-fragment");

if (action) {
event.preventDefault();
Expand All @@ -89,7 +47,7 @@ export function init(): void {
});
}

async function createEvent(href: string, targetId: string | undefined, fragment: string | undefined): Promise<Event> {
async function createEvent(href: string, targetId: string | null, fragment: string | null): Promise<Event> {
const headers = new Headers();
if (fragment) headers.append("X-Fragment", fragment);

Expand All @@ -100,12 +58,47 @@ async function createEvent(href: string, targetId: string | undefined, fragment:
const node = template.content.firstElementChild;
const target = targetId ? document.getElementById(targetId) : null;

return {
target,
node,
};
return { targetNode: target, newNode: node };
});
});

return event;
}

export function defineAction(name: string, action: (event: Event) => void): void {
Actions.set(name, action);
}

defineAction("morph", ({ targetNode: target, newNode: node }) => {
if (target && node) morph(target, node);
});

defineAction("replace", ({ targetNode: target, newNode: node }) => {
if (node) target?.replaceWith(node);
});

defineAction("append", ({ targetNode: target, newNode: node }) => {
if (node) target?.appendChild(node);
});

defineAction("prepend", ({ targetNode: target, newNode: node }) => {
if (node) target?.prepend(node);
});

defineAction("refresh", () => {
fetch(document.location.href).then((response) => {
if (response.ok) {
response.text().then((text) => {
morph(document.documentElement, text);

Check failure on line 92 in src/phlex.ts

View workflow job for this annotation

GitHub Actions / JavaScript Test Action

Argument of type 'string' is not assignable to parameter of type 'ChildNode'.
});
}
});
});

defineAction("remove", ({ targetNode: target }) => {
target?.remove();
});

defineAction("navigate", ({ newNode: node }) => {
if (node) morph(document.documentElement, node);
});

0 comments on commit 10a79d2

Please sign in to comment.