/* Based on react-ui-tree https://github.com/pqx/react-ui-tree The MIT License (MIT) Copyright (c) 2015 pqx Limited Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import React, { createRef, ChangeEvent, Component, RefObject } from "react"; import Tree, { TreeId, TreeIndex } from "../../lib/Tree"; import { Attributes, PageNode } from "./types"; interface NodeProps { addChild: (index: TreeIndex) => void; dir: string; dragging: number; index: TreeIndex; locale: string; onCollapse: (id: TreeId) => void; onDragStart: (id: number, element: HTMLDivElement, evt: Event) => void; paddingLeft: number; tree: Tree; updatePage: (index: TreeIndex, attributes: Attributes) => void; } interface NodeState { newName: string; } interface ButtonOptions { icon: string; className: string; onClick: (evt: Event) => void; } export default class Node extends Component { innerRef: RefObject; constructor(props: NodeProps) { super(props); this.state = { newName: props.index.node.name }; this.innerRef = createRef(); } permitted(action: string): boolean { return ( this.node().permissions && this.node().permissions.indexOf(action) != -1 ); } actions() { const statusLabel = this.node().status != 2 ? "Publish" : "Hide"; const statusIcon = this.node().status != 2 ? "check" : "ban"; if (this.node().editing) { return null; } if (this.props.index.id === 1) { return ( ); } else { return ( {this.permitted("edit") && this.button(statusLabel, { className: "toggle-status", icon: statusIcon, onClick: () => this.toggleStatus() })} {this.permitted("edit") && this.button("Rename", { className: "edit", icon: "pencil", onClick: () => this.edit() })} {this.permitted("edit") && this.button("Delete", { className: "delete", icon: "trash", onClick: () => this.deletePage() })} {this.permitted("create") && this.button("Add child", { className: "add", icon: "plus", onClick: () => this.props.addChild(this.props.index) })} ); } } addButton() { const node = this.node(); const handleClick = () => { if (this.props.addChild) { this.props.addChild(this.props.index); } }; if ( !node.collapsed && this.permitted("create") && (node.root || this.visibleChildren().length > 0) ) { return this.button("Add page here", { className: "add add-inline", icon: "plus", onClick: handleClick }); } } button(label: string, options: ButtonOptions) { const icon = "fa-solid fa-" + options.icon + " icon"; return ( ); } childNodes() { const { index, tree, dragging, dir, locale } = this.props; if (index.children && index.children.length && !index.node.collapsed) { const childrenStyles = {}; if (index.node.collapsed) { childrenStyles.display = "none"; } childrenStyles["paddingLeft"] = `${this.props.paddingLeft}px`; return (
{index.children.map((child) => { const childIndex = tree.getIndex(child); return ( ); })}
); } return null; } collapseArrow() { const index = this.props.index; // Don't collapse the root node if (!index.parent) { return null; } const handleCollapse = (e: Event) => { e.stopPropagation(); const nodeId = this.props.index.id; if (this.props.onCollapse) { this.props.onCollapse(nodeId); } }; if (this.visibleChildren().length > 0) { const collapsed = index.node.collapsed; let classnames = ""; if (collapsed) { classnames = "collapse fa-solid fa-caret-right"; } else { classnames = "collapse fa-solid fa-caret-down"; } return ( ); } return null; } collapsedLabel() { if ( this.node().collapsed && this.node().children && this.node().children.length > 0 ) { const pluralized = this.node().children.length == 1 ? "item" : "items"; return ( ({this.node().children.length} {pluralized}) ); } else { return null; } } deletePage() { if (confirm("Are you sure you want to delete this page?")) { this.updatePage({ status: 4 }); } } edit() { this.updatePage({ editing: true }); } editUrl(page: PageNode) { return `/admin/${page.locale}/pages/${page.param}/edit`; } node(): PageNode { return this.props.index.node; } pageName() { const name = this.node().name || Untitled; return ( {name} ); } render() { const props = this.props; const index = props.index; const dragging = props.dragging; const editing = this.node().editing; let classnames = "node"; const node = editing ? this.renderEditNode() : this.renderNode(); if (index.id === dragging) { classnames = "node placeholder"; } const handleMouseDown = (e: Event) => { if (this.permitted("edit") && !editing && props.onDragStart) { props.onDragStart(props.index.id, this.innerRef.current, e); } }; if (this.node().status != 4) { return (
{this.collapseArrow()} {node}
{this.childNodes()} {this.addButton()}
); } else { return null; } } renderEditNode() { const { dir, locale } = this.props; const handleNameChange = (event: ChangeEvent) => { this.setState({ newName: event.target.value }); }; const performEdit = (event: Event) => { event.preventDefault(); this.updatePage({ name: this.state.newName, editing: false }); }; const cancelEdit = () => { this.setState({ newName: this.node().name }); this.updatePage({ editing: false }); }; return (
{this.button("Cancel", { className: "cancel", icon: "ban", onClick: cancelEdit })}
); } renderNode() { const index = this.props.index; const node = index.node; let pageName = {this.pageName()}; let className = "page"; let iconClass = "fa-regular fa-file icon"; if (typeof node.status != "undefined") { className = `page status-${this.node().status}`; } if (node.id && node.locale && this.permitted("edit")) { pageName = ( {this.pageName()} ); } if (node.news_page) { iconClass = "fa-regular fa-file-lines icon"; } else if (node.pinned) { iconClass = "fa-regular fa-flag icon"; } return (
{pageName} {this.statusLabel()} {this.collapsedLabel()} {this.actions()}
); } statusLabel() { const labels = ["Draft", "Reviewed", "Published", "Hidden", "Deleted"]; if (typeof this.node().status != "undefined" && this.node().status != 2) { return ( ({labels[this.node().status]}) ); } else { return ""; } } toggleStatus() { if (this.node().status != 2) { this.updatePage({ status: 2 }); } else { this.updatePage({ status: 3 }); } } updatePage(attributes: Attributes) { if (this.props.updatePage) { return this.props.updatePage(this.props.index, attributes); } } visibleChildren(): PageNode[] { if (this.node().children) { return this.node().children.filter((p) => p.status != 4); } else { return []; } } }