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

@cartok/hierarchy-select

v1.1.14

Published

Hierarchy Select jQuery Plugin for Twitter Bootstrap

Downloads

7

Readme

Hierarchy Select jQuery Plugin for Twitter Bootstrap

a link to the original project for guidance

Requirements

  • a bundeling system like webpack
  • jQuery
  • bootstrap

Installation

npm install bootstrap@3 jquery@3 @cartok/hierarchy-select

Usage

Restriction

You should only use this module inside of a build environment (ES6 module).

Import and apply the plugin to jQuery

import "jquery"
import "bootstrap"
import hierarchySelect from "@cartok/hierarchy-select"
import "bootstrap/dist/css/bootstrap.css"
import "@cartok/hierarchy-select/build/hierarchy-select.css"
hierarchySelect($)

Example usage

/**
 * create the base html code and append it somewhere.
 * @unwritten-feature: the html could be provided later from inside the plugin
 */
const html = new NodeTemplate(`
    <div class="btn-group hierarchy-select" data-resize="auto">
        <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" data-ref="button">
            <span class="selected-label pull-left" data-ref="text">&nbsp;</span>
            <span class="caret"></span>
        </button>
        <div class="dropdown-menu open">
            <div class="hs-searchbox">
                <input type="text" class="form-control" autocomplete="off">
            </div>
            <ul data-ref="list" class="dropdown-menu inner" role="menu"></ul>
        </div>
        <input class="hidden hidden-field" readonly="readonly" type="text"/>
    </div>
`)
document.body.appendChild(html.fragment)

/**
 * Use HierarchySelect on the html and pass in options and a function
 * that creates the entries of the select view.
 * If you want the select to have a default selection on DOM-load you need
 * a function that creates entries with one of them having the 'data-default-selected' attribute.
 * But you can just take out the whole function code and execute it afterwards.
 */
$(html.root).hierarchySelect({
    hierarchy: false,
    togglePosition: false,
    keepFocused: false,
}, () => {
    // id attributes are optional and will be autofilled in the updateSelect() function.
    const data = [{
        id: 0,
        text: "Label A"
    },{
        id: 1,
        text: "Label B"
    },{
        id: 3,
        text: "Long John Silver!"
    },{
        id: 4,
        text: "This is the longest label in the world!"
    },{
        id: 5,
        text: "Another long sentence containing the word 'world'!"
    },]

    ;(function updateSelect(entries){
        // remove all entries from select.
        html.refs.list.innerHTML = ""

        // check if every entrie-object has an id, else create it.
        entries = entries.every(l => l.id instanceof Number)
        ? entries
        : entries.map((l, idx) => Object.assign(l, { id: idx }))

        // create html node from entries-array.
        const entriesFragment = document.createDocumentFragment()
        entries.forEach((e, idx) => {
            let entry = undefined
            if(idx === 0){
                // the first node gets selected by the plugin via data attribute.
                entry = new NodeTemplate(`
                    <li data-value="${e.id}" data-default-selected>
                        <a href="#">${e.text}</a>
                    </li>
                `)
            } else {
                entry = new NodeTemplate(`
                    <li data-value="${e.id}">
                        <a href="#">${e.text}</a>
                    </li>
                `)
            }
            entriesFragment.appendChild(entry.fragment)
        })
        html.refs.list.appendChild(entriesFragment)
    })(data)
})

Usage additions

How-to react on selection change?

$(html.root).on("change", (e, data) => {
    // The change event is triggered manually by the plugin through jQuery.
    // But it will also be triggered natively.
    // Only if the event was triggered by the plugin, you will get the text of the selection.
    if(data !== undefined){
        console.log("You can use the selected dropdown text and id on selection like this.")
        console.log(data)
    }
})

How-to open the dropdown on shortcut?

$(window).on("keydown", (e) => {
    if(event.getModifierState("Control")){
        event.preventDefault()
        if(e.key.toLowerCase() === "l"){
            e.preventDefault()
            e.stopPropagation()
            html.refs.button.click()
        }
    }
})

How to develop

Start the http-server for testing the example page for CORS. Navigate to the examples folder and open the html file.
Use a browser that supports async ES6 module loading via script tag. compability list
Suggestion: Use Chrome >= 62

node node_modules/.bin/http-server