-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.js
166 lines (135 loc) · 4.85 KB
/
edit.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
EditJS = (function() {
const MODE_TEXT = 0;
const MODE_EDIT = 1;
const CLASSNAME = "ejs";
function EditJS(opt) {
opt = opt || {};
this.activeIds = opt.activeIds || [];
this.onChange = opt.onChange || function(id, value) {};
this.fields = {}; // id -> object
this.register();
for (let i = 0; i < this.activeIds.length; i++) {
const id = this.activeIds[i];
const field = this.fields[this.activeIds[i]];
if (!field) continue;
this.fields[id].mode = MODE_EDIT;
this.createElement(id);
}
}
// register DOM elements as editjs fields
EditJS.prototype.register = function() {
const elements = document.getElementsByClassName(CLASSNAME);
for (let i = 0; i < elements.length; i++) {
const id = elements[i].id;
if (!id) {
console.log("skipping element (no id)");
continue;
}
this.fields[id] = {
wrapper: elements[i],
mode: MODE_TEXT,
value: elements[i].innerText
};
this.createElement(id);
}
};
// register autoresize
function registerTextareaResize(ta) {
function autoResize(ta) {
ta.style.height = ta.scrollHeight + "px";
}
ta.addEventListener("input", function() {
autoResize(ta);
});
autoResize(ta);
}
function getEditWrapper(content) {
let wrapper = document.createElement("div");
wrapper.className = "edit-wrapper";
let button = document.createElement("button");
button.className = "save";
button.innerHTML = "✓";
let ta = document.createElement("textarea");
ta.value = content;
wrapper.append(ta, button);
return wrapper;
}
function getEditable(content) {
let div = document.createElement("div");
div.className = "editable";
div.innerText = content;
return div;
}
// creates the html element of a specific mode, adds it to the dom and adds event listeners
EditJS.prototype.createElement = function(id) {
let element = null;
let field = this.fields[id]; // readonly
if (field.mode === MODE_EDIT) {
element = getEditWrapper(field.value);
// register save shortcut
element.querySelector("textarea").addEventListener("keydown", function(e) {
if (e.ctrlKey && e.key === "s") {
e.preventDefault();
element.querySelector("button.save").click();
}
})
} else { // default: MODE_TEXT
element = getEditable(field.value);
}
if (element !== null) {
while (field.wrapper.hasChildNodes()) {
field.wrapper.removeChild(field.wrapper.lastChild);
}
field.wrapper.appendChild(element);
this.setEventListener(id);
}
};
// set event listener on field
EditJS.prototype.setEventListener = function(id) {
let field = this.fields[id];
let self = this;
if (field.mode === MODE_TEXT) {
field.wrapper.querySelector("div.editable").addEventListener("click", function() {
self.fields[id].mode = MODE_EDIT;
self.createElement(id);
const ta = field.wrapper.querySelector("textarea");
ta.focus();
registerTextareaResize(ta);
});
}
else if (field.mode === MODE_EDIT) {
const ta = field.wrapper.querySelector("textarea");
field.wrapper.querySelector("button.save").addEventListener("click", function() {
self.fields[id].mode = MODE_TEXT;
self.fields[id].value = ta.value;
self.onChange(id, self.fields[id].value);
self.createElement(id);
});
}
};
EditJS.prototype.setValue = function(id, value) {
if (this.fields[id]) {
this.fields[id].value = value;
this.createElement(id);
}
};
EditJS.prototype.setActive = function(id, active) {
if (this.fields[id]) {
this.fields[id].mode = active === true ? MODE_EDIT : MODE_TEXT;
this.createElement(id);
}
};
EditJS.prototype.getValue = function(id) {
const field = this.fields[id];
return field.value || undefined;
};
EditJS.prototype.getValues = function() {
let ret = {};
const ids = Object.keys(this.fields);
for (let i = 0; i < ids.length; i++) {
ret[ids[i]] = this.fields[ids[i]].value;
}
return ret;
};
return EditJS;
})();