-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (54 loc) · 1.96 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var emitter = require('emitter');
var editable = require('editable');
module.exports = makeEditable;
function makeEditable(elements, options) {
options = options || {};
editable.click(elements, function (element) {
if (element.getAttribute('data-in-edit-mode') == 'true') return;
element.setAttribute('data-in-edit-mode', 'true');
edit(element, options);
});
}
emitter(makeEditable);
function edit(element, options) {
var dimensions;
var oldStyle;
if (options.maintainSize === true) {
dimensions = editable.dimensions(element);
}
emit('pre-begin-edit', element);
var value = element.textContent.trim();
element.innerHTML = '';
var edit = document.createElement('input');
edit.type = "text";
edit.value = value;
element.appendChild(edit);
if (options.maintainSize === true) {
var editDimensions = editable.transformDimensions(edit, dimensions);
edit.style.width = editDimensions.width + 'px';
edit.style.height = editDimensions.height + 'px';
oldStyle = {width: element.style.width, height: element.style.height};
element.style.width = dimensions.width + 'px';
element.style.height = dimensions.height + 'px';
}
edit.focus();
editable.blur(edit, function () {
if (element.getAttribute('data-in-edit-mode') != 'true') return;
element.setAttribute('data-in-edit-mode', 'false');
emit('pre-end-edit', element);
element.innerHTML = edit.value.trim();
if (options.maintainSize === true) {
element.style.width = oldStyle.width;
element.style.height = oldStyle.height;
}
if (value != edit.value.trim()) {
emit('update', element, edit.value.trim());
}
emit('post-end-edit', element);
});
emit('post-begin-edit', element);
}
function emit() {
module.exports.emit.apply(module.exports, arguments);
editable.emit.apply(editable, arguments);
}