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

apostrophe-schema-widgets

v0.5.23

Published

An easy form widget builder for the Apostrophe content management system

Downloads

63

Readme

Not for new projects

This module is intended for A2 0.5.x only. In Apostrophe 2.x, all widgets have schemas, so creating your own widgets is easy and requires no extra npm modules.

apostrophe-schema-widgets

apostrophe-schema-widgets is a widget builder for the Apostrophe content management system. You can easily configure any number of custom widgets using Apostrophe schemas.

Installation

npm install apostrophe-schema-widgets

Make sure you add it to package.json so it also installs on deployment.

Usage

In app.js, just configure the module:

'apostrophe-schema-widgets': {
  widgets: [
    {
      name: 'prettyLink',
      label: 'Pretty Link',
      instructions: 'Enter a label and paste a URL to create a link.',
      schema: [
        {
          name: 'label',
          type: 'string',
          label: 'Label',
          required: true
        },
        {
          name: 'url',
          type: 'url',
          label: 'URL',
          required: true
        }
      ]
    }
  ]
}

Here I've configured one simple widget, which has two fields in its editor, "label" and "URL."

Now, create a template for displaying this widget as the file lib/modules/apostrophe-schema-widgets/views/prettyLink.html:

<div class="so-pretty">
  <a href="{{ item.url | e }}">{{ item.label | e }}</a>
</div>

Note that your template can also access options passed to the widget via the options object.

Right away, this is useful. But it's a lot more useful if you use the "array" feature of schemas. This way we can edit an entire list of "pretty links" in a single place, which gives us more control over presentation.

In app.js:

'apostrophe-schema-widgets': {
  widgets: [
    {
      name: 'prettyLinks',
      label: 'Pretty Links',
      instructions: 'Click "add" to add your first link. Enter a label and paste a URL for each link.',
      type: 'array',
      schema: [
        {
          name: 'links',
          type: 'array',
          schema: [
            {
              name: 'label',
              type: 'string',
              label: 'Label',
              required: true
            },
            {
              name: 'url',
              type: 'url',
              label: 'URL',
              required: true
            }
          ]
        }
      ]
    }
  ]
}

And in lib/modules/apostrophe-schema-widgets/views/prettyLinks.html:

<ul class="so-pretty">
  {% for link in item.links %}
    <li><a href="{{ link.url | e }}">{{ link.label | e }}</a></li>
  {% endfor %}
</ul>

Joins in Schema Widgets

They work exactly like joins in snippet schemas.

If the current page contains a schema widget which results in a join with another page or snippet that also contains a schema widget, the second schema widget does not complete its joins. This is necessary to prevent infinite loops.

If you need nested joins, consider adding your joins to the schema of a fancy page rather than using schema widgets. If you take this approach you can use the withJoins option.

Extending Schema Widgets

If you need a custom loader to fetch more data, just subclass the module in lib/modules/apostrophe-schema-widgets/index.js. Here's an example in which we want to do extra work for the schema widget named menuBuilder:


module.exports = schemaWidgets;

function schemaWidgets(options, callback) {
  return new schemaWidgets.SchemaWidgets(options, callback);
}

schemaWidgets.SchemaWidgets = function(options, callback) {
  var self = this;

  module.exports.Super.call(this, options, null);

  var superLoad = self.widgets.menuBuilder.load;
  self.widgets.menuBuilder.load = function(req, item, callback) {
    return superLoad(req, item, function(err) {
      if (err) {
        return callback(err);
      }

      // Do your custom work here, add properties
      // to the item, then...

      return callback(null);
    });
  };

  if (callback) {
    process.nextTick(function() { return callback(null); });
  }
};

If you need to fetch more data that is dependent on all of the joins being hydrated, you can execute custom code after the widget is finished loading in your index.js:

self.widgets.menuBuilder.afterLoad = function(req, item, callback) {
  // Fetch more data
  return callback(null);
}

You also have afterConvertFields available to you for treating fields after they are sanitized. You can use it in index.js like this:

self.widgets.menuBuilder.afterConvertFields = function(req, item, callback) {
  // Do stuff to your fields here
  return callback(null);
}