npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

view-tree

v1.2.0

Published

A View engine

Downloads

11

Readme

view-tree

A DOM based view engine

Allows to configure a website view tree, which allows automated switching between different pages (tree nodes)

Basic example:

Let's say we have following HTML files:

head.html

<link rel="icon" href="/favicon.png" />
<link rel="stylesheet" href="/style.css" />

body.html

<header>
  <nav><ul>
    <li><a href="/">Main page</a></li>
  </ul></nav>
</header>
<main>
  <!-- To be filled by extension views -->
</main>
<footer><p>© footer example</p></footer>

homepage.html

<h1>Homepage of SiteTree demo</h1>
<p>Homepage content ...</p>

subpage.html

<h1>Subpage of SiteTree demo</h1>
<p>Subpage content ...</p>

SiteTree configuration for them may look as:

var SiteTree = require('view-tree');

// Initialize SiteTree instance:
var siteTree = new SiteTree(document);

// Configure view nodes:
// Root node
var baseView = {
  title: "SiteTree test page",
	head: function () {
		return htmlToDOM('./head.html');
	},
	body: function () {
		return htmlToDOM('./body.html');
	},
};

// Homepage node
var homepageView = {
	_parent: baseView,
  main: function () {
    return htmlToDOM('./homepage.html');
  }
};

// Subpage node
var subpageView = {
	_parent: baseView,
  main: function () {
    return htmlToDOM('./subpage.html');
  }
};

// Switch between views in document:
// Present homepage
siteTree.load(homepageView);

// Switch to subpage
siteTree.load(subpageView);

// Switch back to homepage
siteTree.load(homepageView)

SiteTree expects view functions to return instances of DocumentFragment, but its agnostic in how fragments are resolved, it's why above example doesn't expose logic for htmlToDOM function.

There are ready solutions that adapt some template formats:

Above configuration example exposes very basic case, and does not unleash all possibilities of SiteTree, follow below documentation, for all the details

Installation

$ npm install view-tree

API

new SiteTree(document)

var SiteTree = require('view-tree');

var siteTree = new SiteTree();

On initialization HTMLDocument istance needs to be provided.

There's no need to provide any view nodes at this step, instead they should be added to tree on demand (via siteTree.load method) at point when we want to show them in document.

SiteTree constructor properties:

SiteTree.ensureView(conf)

Validates provided view configuration. Used also internally on view load. Can be overriden by extensions

SiteTree.ensureTemplate(template)

Validates provided template. Used also internally on view load. Can be overriden by extensions

Properties of siteTree instance:

siteTree.current

Currently loaded node.

siteTree.load(view[, context])

Loads provided view configuration into document. context is optional (if not provided, plain object is created internally) it's a context in which view functions will be called. It should be used to transport necessary data to the views.

If given view was already loaded in a past (taking also into account eventual match setting), then previously generated DOM is reused.

siteTree._resolveTemplate(templates, context)

Resolves provided template with provided context. Should not be used externally. Listed in documentation, only to indicate that this method should be overriden by extension when some specific template format is to be used.

siteTree._resetElement(element)

Invokes reset operation on elements which were just exposed in a document due to view change. Should not be used externally. Listed in documentation, only to indicate that this method may be overriden for custom needs.

Configuration of view nodes

View node is a typical key/value hash map, which says where in a tree node is placed and instructs which and how elements should be altered when view is loaded

View map keys

All keys that start with _ are treated as special instructions:

  • _parent - A parent view configuration. Mandatory if given view is not a root one.
  • _match - Matching instruction: Typically view is resolved once, and generated DOM is reused. However there are cases when we want to generate different view per each entity, e.g. imagine page which lists properties of some object, for each object we may want to use same view configuration, but we don't want to reuse DOM generated for object A, when we want to show data for object B.
    With match we indicate which property of a context (passed to siteTree.load) will identify entity for which view is generated. e.g. if match is set to 'name', then two different views will be generated for following contexts { name: 'Mark' } and { name: 'Paul' }. However previously generated view will be reused if again we provide context as { name: 'Mark' }. Matching value in a context doesn't have to be a string, it can be any JavaScript value, it's uniqueness is compared with SameValueZero algorithm

Any other instruction key in core implementation will be ignored (but can be used by extensions).

All other keys are expected to be valid ident values which address specific DOM elements.

Following core elements can be addressed by name:

  • html (configuration may contain only attributes setting)
  • head
  • title
  • body
  • main

Any other HTML elements need to be addressed by id.

Aside of html, head, title and body elements, it is expected that referenced elements exist in a document tree when parent of given node view is loaded, otherwise load of a view will crash with exception.

View map values

Each value for a key that addresses element is an update instruction

Configuration value can be directly a function that returns DocumentFragment instance, in such case it's treated as a replace content instruction, then on a view load, content of given element is replaced with one returned by function.

If it's not a function, then it's assumed it's object configuration which can contain following instructions:

  • class - Map of class changes to element, e.g. { visible: true, hidden: false } instructs that visible class should be added and hidden removed
  • attributes - Map of attribute changes to element, e.g. { 'data-foo': 'lorem', 'data-bar': null } instructs to set value of data-foo to 'lorem', and to remove data-bar attribute
  • content - Function that generates a new content (DocumentFragment) for the element. Should not be used together with prepend or append.
  • prepend - Function that generates an extra content (DocumentFragment) for the element, which will be prepended before element's first child. Should not be used together with content
  • append - Function that generates an extra content (DocumentFragment) for the element, which will be appended after element's last child. Should not be used together with content

Tests Build Status

$ npm test