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

widget-factory

v0.2.3

Published

Widget Factory is a widget builder object based on Widget Router routing.

Downloads

2

Readme

Description

Widget Factory is a TypeScript widget builder object based on Widget Router routing.

How to Install and Configure

npm install widget-factory

The following code use the DefaultRenderer object (JSRender). You can use whatever render you want (fulfilling the DefaultTemplateRenderer object type), and just send it through the Widget Factory Constructor.

  var WidgetFactory = require('widget-factory');
  var containerId = '#widgetFactoryContainer'; // (Mandatory) Id to the Div Container of all widget pages
  var renderAfterRoute = true;
  var routerConfiguration = { // Widget Router Configuration
    afterRouteInitController: true,
    pageIdPrefix: 'widget_router_',
	usingTemplates: true, // This is set to true because i'm using a TemplateRenderer (JSRender). In this way, the uncompiled and unrendered template will not be showed to the client, and you will need always to render the template to show the content inside the page.
    routes: [
      {
        name: "main",
        template: '<h1>{{:title}}</h1><input type="button" value="go to secondary" onclick="WidgetFactory.router.go(&quot;secondary&quot;);"/><p>{{:description}}</p>',
        controller: function(routeResult) {
           // this controller will return a promise, just for fun...
          var dfd = new Promise ((resolve, reject) => {
            routeResult.routeScope.title = 'Main Page';
            routeResult.routeScope.description = 'Main page description';
            // If you don't set 'renderAfterRoute' parameter to true, this template will not be rendered.
            // If you set it to false, then you will need to render the page like this:
            //routeResult.controllerHelper.render(routeResult.routeName, routeResult.routeScope);
            resolve();
          });
          return dfd;
        }
      },
      {
        name: "secondary",
        template: '<h1>Secondary Page</h1><input type="button" value="go to main" onclick="WidgetFactory.router.go(&quot;main&quot;);"/><p>Secondary page description</p>',
        controller: function (routeResult) {
		  // If you don't set 'renderAfterRoute' parameter to true, this template will show a blank page, because "usingTemplates" is set to true in routerConfiguration
          // If you set it to false, then you will need to render the page like this:
          //routeResult.controllerHelper.render(routeResult.routeName, routeResult.routeScope);
        }
      }
    ]
  }
  window.WidgetFactory = new WidgetFactory.WidgetFactory(containerId, routerConfiguration, null, null, renderAfterRoute);
  WidgetFactory.start();

##Events "beforeroute" and "afterroute"

If you want to inject some functions executions before or after the router is executed. Then you can do it this way:

  
  WidgetFactory.on('beforeroute', (event, sender) => {
    return new Promise(function(resolve, reject) {
      setTimeout(() => {
        console.log('event ' + event + ' will be executed after this async method');
        resolve();
      }, 100);  
    });
  });

  WidgetFactory.on('beforeroute', (event, sender) => {
    console.log('do something before route event gets executed');
  });

  WidgetFactory.on('afterroute', (event, sender) => {
    console.log('do something after route event have finished');
  });

##Events "beforestart" and "afterstart"

If you want to inject some functions executions before or after the WidgetFactory start method is executed. Then you can do it this way:

  
  WidgetFactory.on('beforestart', (event, sender) => {
    return new Promise(function (resolve, reject) {
      setTimeout(() => {
        console.log('event ' + event + ' will be executed after this async method');
        resolve();
      }, 100);
    });
  });

  WidgetFactory.on('afterstart', (event, sender) => {
    console.log('event ' + event + ' was executed');
  });

Notes

There is an IPageController and a default PageController object implemententation of that interface exported in this library, you can extend from it and use its default render function.

If you are going to use this library with webpack, please add this aliases inside your webpack.config.js:

alias: {
	"widget-router": "widget-router/dist/widget-router",
    "widget-factory": "widget-factory/dist/widget-factory",
	"event-handler-manager": "event-handler-manager/dist/event-handler-manager"
}