Skip to content

Instantly share code, notes, and snippets.

@MartijnR
Last active September 5, 2017 16:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartijnR/6943281 to your computer and use it in GitHub Desktop.
Save MartijnR/6943281 to your computer and use it in GitHub Desktop.
Enketo-compliant Widget Template
'use strict';
/**
* My Fancy Widget
*
* A jQuery plugin form widget that is compatible with Enketo Smart Paper
* and extends a base Enketo Widget class (see https://github.com/enketo/enketo-core)
*/
var Widget = require('../../js/Widget');
var $ = require('jquery');
// It is very helpful to make this the same as widget class, except for converting the first character to lowercase.
var pluginName = 'myFancyWidget';
/**
* [My Fancy Widget description]
*
* @constructor
* @param {Element} element Element to apply widget to.
* @param {{}|{helpers: *}} options options
* @param {*=} event event
*/
function MyFancyWidget(element, options, event) {
// set the namespace (important!)
this.namespace = pluginName;
// call the Super constructor
Widget.call(this, element, options);
this._init();
}
// copy the prototype functions from the Widget super class
MyFancyWidget.prototype = Object.create(Widget.prototype);
// ensure the constructor is the new one
MyFancyWidget.prototype.constructor = MyFancyWidget;
// add your widget functions
MyFancyWidget.prototype._init = function() {
//initialize the widget
};
/**
* override the super's disable method if necessary
*/
// MyFancyWidget.prototype.disable = function() { };
/**
* override the super's enable method if necessary
*/
// MyFancyWidget.prototype.enable = function() { };
/**
* override the super's update method if necessary
*/
// MyFancyWidget.prototype.update = function() { };
$.fn[pluginName] = function(options, event) {
options = options || {};
return this.each(function() {
var $this = $(this);
var data = $this.data(pluginName);
// only instantiate if options is an object (i.e. not a string) and if it doesn't exist already
if (!data && typeof options === 'object') {
$this.data(pluginName, new MyFancyWidget(this, options, event));
}
// only call method if widget was instantiated before
else if (data && typeof options == 'string') {
// pass the element as a parameter
data[options](this);
}
});
};
// returns its own properties so we can use this to instantiate the widget
module.exports = {
'name': pluginName,
// add selector to be used for the widget
'selector': 'input[type="date"]'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment