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

actuator-elements

v0.7.5

Published

Template Engine for Web Components

Downloads

43

Readme

ActuatorElements

NOTE: It works on latest versions of Chrome, Firefox and Safari. IE never supported. CAUTION: It doesn't work well on Edge currently, but it will be supported soon.

ActuatorElements consist of some custom elements, and bring an template system to html. They depend on Web Components(webcomponents-lite.js polyfill), but not on any other frameworks like Polymer or X-Tag.

ActuatorElements can bind data to an element. However, they can not detect data changes: You need to notify updates to the element manually. If you want to do it automatically, you can use MechanicalElements instead. They are the reactive(without dirty checking!) version of ActuatorElements.

Examples

DEMO

<template is="act-put" id="message">
  <p>${ message }</p>
</template>

<script>
  document.querySelector('#message').bindData({
    message: 'Hello, World!',
  });
</script>

DEMO

<ul>
  <template is="act-each" id="fruits">
    <li>${ capitalize(name) } is ${ color }.</li>
  </template>
</ul>

<script>
  document.querySelector('#fruits').bindData([
    { name: 'apple', color: 'red' },
    { name: 'banana', color: 'yellow' },
    { name: 'orange', color: 'orange' }
  ], {
    capitalize(str) {
      return str.charAt(0).toUpperCase() + str.slice(1);
    }
  });
</script>

Install

$ npm install --save actuator-elements
$ bower install --save actuator-elements

API

All ActuatorElements support interpolation in JavaScript way: ${ ... }. In addition, they have these interfaces.

JavaScript Interface

  • bindData(data, utils)
  • unbindData()
  • notifyUpdate(name)
  • notifyUpdateAll()
  • boundData: read only

Attribute Interface

  • data-bind
  • data-json
  • data-template

ActPutElement

<template is="act-put" id="counter">count = ${ count }</template>

<script>
  const counter = document.querySelector('#counter');
  const data = { count: 0 }

  counter.bindData(data);

  counter.boundData.count++;  // counter.boundData === data;
  // bound data updated
  // but view is not updated yet...

  counter.notifyUpdate('count');
  // view updated!
</script>

ActEachElement

JavaScript API

  • notifySplice(index, removedCount, addedCount)
  • notifySpliceAll()
  • childCreatedCallback(child::document fragment, data, { index })
  • childDetachedCallback(child::document fragment, data, { index })
<template is="act-each" id="hydrocarbons">C2H${ index * 2 }: ${ name }</template>

<script>
  const hydrocarbons = document.querySelector('#hydrocarbons');

  hydrocarbons.bindData([
    { 'name': 'acetylene' },
    { 'name': 'ethylene' },
  ]);

  hydrocarbons.boundData.push({ 'name': 'ethane' });
  hydrocarbons.notifySplice(hydrocarbons.length - 1, 0, 1);
</script>

ActRegisterElement

Attribute Interface

  • data-tag-name: required
  • data-extends
<style>
  [data-if=false] {
    display: none;
  } 
</style>

<template is="act-register" data-tag-name="dynamic-langs" data-extends="ul">
  <template is="act-each" data-bind>
    <!-- dynamic languages only displayed -->
    <li data-if="${ dynamic }">${ name }</li>
  </template>
</template>

<ul is="dynamic-langs" id="langs"></ul>

<script>
  document.querySelector('#langs').bindData([
    { name: 'JavaScript', dynamic: true },
    { name: 'Python', dynamic: true },
    { name: 'Java', dynamic: false },
  ]);
</script>

ActFormElement

ActFormElement and ActInputElement can automatically detect form changes and reflect them in bound data.

Attributes

  • data-updateOn
<style>
  [data-if=false] {
    display: none;
  } 
</style>

<form is="act-form" name="account">
  <label>ID: <input name="id" type="text"></label>
  <label>PASSWORD: <input name="password" type="password"></label>

  <p data-if="${ password.length < 6 }">Your password is too weak...</p>

  <button type="submit">
</form>

<script>
  document.querySelector('[name=account]').bindData({
    completed: false,
    title: 'homework',
  });
</script>

ActInputElement

NOTE: type="radio" not supported

<input is="act-input" type="text" name="sport" placeholder="what's your favorite sport?">

<script>
  document.querySelector('[name=sport]').bindData({
    sport: 'tennis'
  });
</script>

Tips

Extension

One of the advantages to use ActuatorElements is high extendability.

DEMO

class SortableEachElement extends ActEachElement {
  createdCallback() {
    if (this.content.children.length !== 1) {
      throw new Error('child length must be 1');
    }

    // CAUTION: Don't forget to call super.lifecycleCallback
    super.createdCallback();
  }

  childCreatedCallback(child, data, context) {
    const { boundData } = this;

    // NOTE: child is document fragment
    child.firstElementChild.draggable = true;

    child.firstElementChild.addEventListener('dragstart',  e => {
      e.dataTransfer.setData('text/plain', String(context.index));
    });

    child.firstElementChild.addEventListener('dragover', e => {
      e.preventDefault();
    });

    child.firstElementChild.addEventListener('drop', e => {
      e.preventDefault();

      const i = Number(e.dataTransfer.getData('text/plain'));
      const j = context.index;
      const d = boundData[i];

      boundData.splice(i, 1);
      boundData.splice(j, 0, d);

      for (let k = Math.min(i, j); k < Math.max(i, j) + 1; k++) {
        this.notifyUpdate(k);
      }
    });
  }
}

document.registerElement('sortable-each', SortableEachElement);

De-ActuatorElements

You can change the register name not to pollute your namespace.

const act = require('actuator-elements');

// register act-each as 'my-each'
document.registerElement('my-each', act.ActEachElement);
// Or you can register all elements with 'my-' prefix 
act.registerAll(prefix='my');