widget.js 628 B

123456789101112131415161718192021222324252627
  1. module.exports = function rikoWidget (init) {
  2. function Widget (options) {
  3. if (!(this instanceof Widget)) return new Widget(options);
  4. this.options = options;
  5. }
  6. Widget.prototype.type = "Widget";
  7. Widget.prototype.init = function () {
  8. this.state = init(this.options);
  9. return this.state.element;
  10. }
  11. Widget.prototype.update = function (previous) {
  12. this.state = previous.state;
  13. return this.state.update ? this.state.update.apply(this, arguments) : null
  14. }
  15. Widget.prototype.destroy = function () {
  16. if (this.state.destroy) this.state.destroy.apply(this, arguments)
  17. }
  18. return Widget;
  19. }