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

@gebruederheitz/wp-async-posts-loader

v0.2.0-beta.1

Published

A loader to provide frontend functionality to the gebruederheitz/wp-async-posts-provider composer package

Downloads

7

Readme

Wordpress Async Posts Loader

The front-end script to work with the gebruederheitz/wp-async-post-provider PHP composer package.

Installation

> npm i @gebruederheitz/wp-async-posts-loader

Usage

Basic Usage

import {LoadMore} from '@gebruederheitz/wp-async-posts-loader';

new LoadMore();

This will attach the listener to a button matching .ghwp-load-more button and a container .ghwp-latest-posts. Clicking the button will make an asynchronous REST request to /wp-json/ghwapp/v1/posts/load-more?page=${page}, where the PHP Posts Provider resides. The rendered posts thus retrieved will be appended to the container. The number of posts can be specified through the Provider's configuration server-side.

Available options

You may customize the button & container used by providing options to the constructor:

new LoadMore({
    buttonSelector: 'a.load-more-button[href="#"]',
    containerSelector: 'section#posts',
});

API

Some methods you may find useful:

const loader = new LoadMore();
loader.removeButton();  // when you've had enough – this will be called
                        // automatically when there are no more posts left to be
                        // loaded
loader.onClick();       // Allows you to programmatically trigger loading more
                        // elements. Do not use getMorePosts() unless you know
                        // know what you're doing!
loader.on('event:name', callback) // Attach an event listener

Events

| Event | Parameters | Description | | ------------ | --------------------- | ----------------------------------------------------------------------- | | post:append | {post: Element} | A post has been appended to the container. | | posts:end | n/a | All posts have been retrieved, the button will now be removed. | | posts:parsed | {posts: Element[]} | The posts have been retrieved and parsed into an array of DOM elements. | | load:start | n/a | Loading has been requested. | | load:finish | n/a | All processing has finished. |

Additional classnames

During the loading process, the button element will receive a .busy class, which you can use to give visual feedback to the user.

Extending functionality

You can easily extend the module's functionality by building on top of the class. Here are some examples:

Additional parameters

import {LoadMore} from './load-more';

class MyLoader extends LoadMore {

    /*
     *  Use the initFilters() stub to initialize your filter properties, for
     *  example by reading them from the button's data attributes:
     *
     * <div class="ghwp-load-more">
     *     <button type="button" data-ghwp-category="42" >Load more</button>
     * </div>
     *
     */
    initFilters() {
        this.category = 0;
        this.tag = 0;

        if (this.button.dataset) {
            /* category will be 42 */
            this.category = this.button.dataset.ghwpCategory || 0;
            /* tag will not be set */
            this.tag = this.button.dataset.ghwpTag || 0;
        }
    }

    /*
     *  Override / extend the getRequestPath() method to apply the filters to
     *  the URL's query:
     */
    getRequestPath(page = 0) {
        let path = super.getRequestPath(page);

        if (this.category) {
            path += `&category=${this.category}`;
        }
        if (this.tag) {
            path += `&tag=${this.tag}`;
        }

        return path;
    }
}