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

@weareenvoy/plain-js

v1.2.9

Published

./README.md

Downloads

19

Readme

Plain-JS JavaScript

The Object-Oriented way to write functional code with proper scroping and execution.

Execution

The plain-js system is executed via data attributes. It's designed to work within a component like system where each component is instance-based and controls itself.

Example Execution

We have a component called Hero that has some animation and event listeners.

<section class="hero" data-plain-js-module="Hero">
    // Some child elements
</section>

The point of focus in the example above is data-plain-js-module="Hero"(.) This data attribute references a ES6 class with this functionality.

export default class Hero extends BaseClass {
    constructor(rootElement, args) {
        super(rootElement, args);
        this.imageChild = this.rootElement.querySelector('img');
        this.init();
    }

    init() {
        this.handleScroll();
    }

    handleScroll() {
        window.addEventListener('scroll', () => {
            this.animation()
        })
    }

    animation() {
        // Some animation
    }
}

Example Arguments

The point of ES6 classes is to add flexibility, reusability and handle multiple instances. This architecture allows for arguments/parameters to be passed to the ES6 classes referenced above.

<section class="hero" data-plain-js-module="Hero" data-plain-js-args='{"animationDelay": 200, "animationFiresOnce": true}'>
    // Some child elements
</section>

The point of focus in the example above is data-plain-js-args='{"animationDelay": 200, "animationFiresOnce": true}'(.) NOTE: This is JSON data that will be passed the corresponding class and has to be JSON data.

// Usage

export default class Hero extends BaseClass {
    constructor(rootElement, args) {
        super(rootElement, args);
        this.imageChild = this.rootElement.querySelector('img');
        this.init();

        // Args
        console.log(this.animationDelay) // 200
        console.log(this.animationFiresOnce) // true

    }

    init() {
        this.handleScroll();
    }

    handleScroll() {
        window.addEventListener('scroll', () => {
            this.animation()
        })
    }

    animation() {
        // Some animation
    }
}

Example Refs (Component to Component communcation)

One of the main purposes of Refs is to communicate between components and manage events.

    <video class="video" data-plain-js-module="Video" data-plain-js-refs='{"VideoButton":".video-button"}' data-plain-js-args='{"src":"http://clips.vorwaerts-gmbh.de/VfE_html5.mp4"}'>
        // sources
    </video>

    <button class="video-button" data-plain-js-module="Button" data-plain-js-args='{"name":"Button", "title": "Play Video"}'>
        // Text
    </button>

In this example the video player is concerned with clicks that occur on the button with the class .video-button.

export default class Video extends BaseClass {
    constructor(rootElement, args) {
        super(rootElement, args);
        this.setSrc();
        this.refClick();
    }

    setSrc() {
        this.rootElement.src = this.src;
    }

    refClick() {
        // click event listener on the external ref
        this.refs.VideoButton.addEventListener('click', () => {
            this.rootElement.play();
        });
    }
}

Intialization and the System Function

The initialization takes places in a root import file(the entry point for the bundler webpack, gulp etc). In this file, the System function will recieve one parameter(typeof object) that consists of references to each imports. There are comments in the function if you wish know more.

import SomeClass from 'some-directory';
import SomeClass1 from 'some-directory1';
import SomeClass2 from 'some-directory2';
// etc

new System({
    SomeClass,
    SomeClass1,
    SomeClass2,
}).init();

BaseClass

The main purpose of the BaseClass is automate mudane tasks and to provide consistency. Each class should extend the BaseClass.

/**
 * @class BaseClass
 * @param {node} rootElement Root entry point for the component, the highest html element wrapper => The html element that has the "data-plain-module" attribute
 * @param {object} args Arguments/options to passed to component constructor => "data-plain-args" attribute
 */
export default class BaseClass {
    constructor(rootElement, args) {
        // Automate setting of properties through component "super" call
        this.setRootElement(rootElement)
        this.setProps(args)
    }

    /**
     * @method emit Dispatch custom events from the rootElement
     * @param {node} element HTML node to fire the event on
     * @param {string} eventName Name of the custom event
     * @param {object} args Data object to passed through the custom event => accessed on the listener at "event.detail"
     * @returns void
     */
    emit(element, eventName, args) {
        element.dispatchEvent(
            new CustomEvent(eventName, {
                detail: args,
            })
        )
    }

    /**
     * @method setProps Set the properties of the args object
     * @param {object} args Args/options for the "data-plain-args" attribute
     * @returns void
     */
    setProps(args) {
        if (!args) return false
        Object.keys(args).forEach(key => {
            this[key] = args[key] || null
        })

        this.setRefs()
    }

    /**
     * @function setRefs Set references to other components - generated from an html attribute "data-plain-refs" accepts valid JSON
     * @returns void
     */
    setRefs() {
        if (!this.refs) return false
        Object.keys(this.refs).forEach(key => {
            this.refs[key] = document.querySelector(this.refs[key])
        })
    }

    /**
     * @function hasRefs Check if module has external references
     * @returns {bool}
     */
    hasRefs() {
        return !!this.refs
    }

    /**
     * @method setRootElement The the rootElement property
     * @param {node} rootElement Root entry point element from "data-plain-module" attribute
     * @returns void
     */
    setRootElement(rootElement) {
        if (!rootElement) {
            throw new Error('Each constructor needs a root entry DOM node')
        }

        this.rootElement = rootElement
    }
}