Last active
September 5, 2017 16:12
-
-
Save MartijnR/6943281 to your computer and use it in GitHub Desktop.
Enketo-compliant Widget Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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