-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var emitter = require('emitter');
var editable = require('editable');
var matchesSelector = require('matches-selector');
module.exports = makeEditable;
function makeEditable(elements, options) {
options = options || {};
if (options.live != false && typeof elements === 'string') {
document.addEventListener('change', function (eventArgs) {
if (matchesSelector(eventArgs.target, elements) || matchesSelector(eventArgs.target, elements + ' *')) {
var element = eventArgs.target.tagName.toLowerCase() === 'input' ? eventArgs.target : eventArgs.target.getElementsByTagName('input')[0];
emit('update', element, element.checked);
}
}, false);
return;
} else if (options.live === true) {
throw new Error('Live option only works when elements is a string.');
}
editable.forEach(elements, function (element) {
if (element.tagName.toLowerCase() === 'input' && element.type.toLowerCase() === 'checkbox') {
edit(element, options);
} else {
var els = element.getElementsByTagName('input');
for (var i = 0; i < els.length; i++) {
if (els[i].type.toLowerCase() === 'checkbox') {
edit(els[i], options);
}
}
}
});
}
emitter(makeEditable);
function edit(element, options) {
element.addEventListener('change', function () {
emit('update', element, element.checked);
});
}
function emit() {
module.exports.emit.apply(module.exports, arguments);
editable.emit.apply(editable, arguments);
}