UNPKG

1.42 kBJavaScriptView Raw
1/**
2 * @module settings-panel/src/custom
3 *
4 * A custom html component
5 */
6
7const EventEmitter = require('events').EventEmitter
8const inherits = require('inherits')
9const extend = require('just-extend')
10
11module.exports = Custom
12inherits(Custom, EventEmitter)
13
14function Custom (opts) {
15 if (!(this instanceof Custom)) return new Custom(opts);
16
17 //FIXME: these guys force unnecessary events, esp if element returns wrong value
18 // opts.container.addEventListener('input', (e) => {
19 // this.emit('input', e.target.value);
20 // });
21 // opts.container.addEventListener('change', (e) => {
22 // this.emit('change', e.target.value);
23 // });
24
25 this.update(opts);
26}
27
28Custom.prototype.update = function (opts) {
29 extend(this, opts);
30 var el = this.content;
31 if (this.content instanceof Function) {
32 el = this.content(this);
33 if (!el) return;
34
35 if (typeof el === 'string') {
36 this.container.innerHTML = el;
37 }
38 else if (!this.container.contains(el)) {
39 this.container.appendChild(el);
40 }
41 }
42 else if (typeof this.content === 'string') {
43 this.container.innerHTML = el;
44 }
45 else if (this.content instanceof Element && (!this.container.contains(el))) {
46 this.container.appendChild(el);
47 }
48 else {
49 //empty content is allowable, in case if user wants to show only label for example
50 // throw Error('`content` should be a function returning html element or string');
51 }
52};
\No newline at end of file