Renamed core API methods.
jump to
@@ -8,7 +8,7 @@ element: document.getElementById("app"),
modules, onInit: () => { h3.dispatch("app/load"); - h3.dispatch("settings/set", h3.state("settings")); + h3.dispatch("settings/set", h3.state.settings); }, routes: { "/settings": SettingsView,
@@ -5,9 +5,9 @@ import NavigationBar from "./NavigationBar.js";
import TodoList from "./TodoList.js"; export default function () { - const { todos, filter } = h3.state(); + const { todos, filter } = h3.state; h3.dispatch("todos/filter", filter); - h3.dispatch("app/save", { todos: todos, settings: h3.state("settings") }); + h3.dispatch("app/save", { todos: todos, settings: h3.state.settings }); return h3("div.container", [ h3("h1", "To Do List"), h3("main", [AddTodoForm, EmptyTodoError, NavigationBar, TodoList]),
@@ -10,7 +10,7 @@ const attrs = {
type: "checkbox", onclick: toggleLogging, }; - if (h3.state("settings").logging) { + if (h3.state.settings.logging) { attrs.checked = true; } return h3("div.settings.container", [@@ -28,7 +28,7 @@ ]),
h3( "a.nav-link", { - onclick: () => h3.go("/"), + onclick: () => h3.navigateTo("/"), }, "← Go Back" )
@@ -5,7 +5,7 @@ const addTodo = () => {
const newTodo = document.getElementById("new-todo"); if (!newTodo.value) { h3.dispatch("error/set"); - h3.update() + h3.redraw() document.getElementById("new-todo").focus(); return; }@@ -15,7 +15,7 @@ key: `todo_${Date.now()}__${newTodo.value}`, // Make todos "unique-enough" to ensure they are processed correctly
text: newTodo.value, }); newTodo.value = ""; - h3.update() + h3.redraw() document.getElementById("new-todo").focus(); }; const addTodoOnEnter = (event) => {
@@ -1,7 +1,7 @@
import h3 from "../h3.js"; export default function EmptyTodoError(data, actions) { - const emptyTodoErrorClass = h3.state('displayEmptyTodoError') ? "" : ".hidden"; + const emptyTodoErrorClass = h3.state.displayEmptyTodoError ? "" : ".hidden"; const clearError = () => { h3.dispatch('error/clear'); h3.dispatch('$update');
@@ -2,10 +2,10 @@ import h3 from "../h3.js";
export default function Paginator() { const hash = window.location.hash; - let { page, pagesize, filteredTodos } = h3.state(); + let { page, pagesize, filteredTodos } = h3.state; let total = filteredTodos.length; - if (h3.route().params.page) { - page = parseInt(h3.route().params.page); + if (h3.route.params.page) { + page = parseInt(h3.route.params.page); } // Recalculate page in case data is filtered. page = Math.min(Math.ceil(filteredTodos.length / pagesize), page) || 1;@@ -14,10 +14,10 @@ const pages = Math.ceil(total / pagesize) || 1;
const previousClass = page > 1 ? ".link" : ".disabled"; const nextClass = page < pages ? ".link" : ".disabled"; const setPage = (value) => { - const page = h3.state("page"); + const page = h3.state.page; const newPage = page + value; h3.dispatch("pages/set", newPage); - h3.go("/", { page: newPage }); + h3.navigateTo("/", { page: newPage }); } return h3("div.paginator", [ h3(
@@ -4,11 +4,11 @@ export default function Todo(data) {
const todoStateClass = data.done ? ".done" : ".todo"; const toggleTodo = (todo) => { h3.dispatch("todos/toggle", data); - h3.update() + h3.redraw() }; const removeTodo = (todo) => { h3.dispatch("todos/remove", data); - h3.update() + h3.redraw() }; return h3(`div#${data.key}.todo-item`, [ h3(`div.todo-content${todoStateClass}`, [
@@ -2,8 +2,8 @@ import h3 from "../h3.js";
import Todo from "./Todo.js"; export default function TodoList() { - const { page, pagesize } = h3.state(); - const filteredTodos = h3.state('filteredTodos'); + const { page, pagesize } = h3.state; + const filteredTodos = h3.state.filteredTodos; const start = (page - 1) * pagesize; const end = Math.min(start + pagesize, filteredTodos.length); return h3(
@@ -154,7 +154,7 @@ return node;
} // Updates the current Virtual Node with a new Virtual Node (and syncs the existing DOM Node) - update(data) { + redraw(data) { let { node, vnode } = data || {}; if (!node && this.id) { node = document.getElementById(this.id);@@ -239,7 +239,7 @@ if (oldvnode.children[i].type === "text") {
oldvnode.children[i] = newvnode.children[i]; node.childNodes[i].nodeValue = newvnode.children[i].value; } else { - oldvnode.children[i].update({ + oldvnode.children[i].redraw({ node: node.childNodes[i], vnode: newvnode.children[i], });@@ -334,7 +334,7 @@
class Router { constructor({ element, routes, store }) { this.element = element; - this.update = null; + this.redraw = null; this.store = store; if (!this.element) { throw new Error(`[Router] No view element specified.`);@@ -347,11 +347,11 @@ this.fallback = defs[defs.length - 1];
this.routes = routes; } - setUpdate() { + setRedraw() { let vnode = this.routes[this.route.def](); - this.update = () => { + this.redraw = () => { const fn = this.routes[this.route.def]; - vnode.update({ node: this.element.childNodes[0], vnode: fn() }); + vnode.redraw({ node: this.element.childNodes[0], vnode: fn() }); }; }@@ -399,14 +399,14 @@ while (this.element.firstChild) {
this.element.removeChild(this.element.firstChild); } this.element.appendChild(this.routes[this.route.def]().render()); - this.setUpdate(); + this.setRedraw(); this.store.dispatch("$navigation", this.route); }; processPath(); window.addEventListener("hashchange", processPath); } - go(path, params) { + navigateTo(path, params) { let query = Object.keys(params || {}) .map((p) => `${encodeURIComponent(p)}=${encodeURIComponent(params[p])}`) .join("&");@@ -439,33 +439,37 @@ // Initialize router
router = new Router({ element, routes, store }); onInit && onInit(); router.start(); - router.setUpdate(); + router.setRedraw(); }; -h3.go = (path, params) => { +h3.navigateTo = (path, params) => { if (!router) { throw new Error("No application initialized, unable to navigate."); } - return router.go(path, params); + return router.navigateTo(path, params); }; -h3.route = () => { - if (!router) { - throw new Error( - "No application initialized, unable to retrieve current route." - ); - } - return router.route; -}; +Object.defineProperty(h3, "route", { + get: () => { + if (!router) { + throw new Error( + "No application initialized, unable to retrieve current route." + ); + } + return router.route; + }, +}); -h3.state = (key) => { - if (!store) { - throw new Error( - "No application initialized, unable to retrieve current state." - ); - } - return store.get(key); -}; +Object.defineProperty(h3, "state", { + get: () => { + if (!store) { + throw new Error( + "No application initialized, unable to retrieve current state." + ); + } + return store.get(); + }, +}); h3.on = (event, cb) => { if (!store) {@@ -481,11 +485,11 @@ }
return store.dispatch(event, data); }; -h3.update = () => { - if (!router || !router.update) { +h3.redraw = () => { + if (!router || !router.redraw) { throw new Error("No application initialized, unable to update."); } - router.update(); + router.redraw(); }; export default h3;