-
Notifications
You must be signed in to change notification settings - Fork 36
/
handsontable-chosen-editor.js
295 lines (226 loc) · 10.1 KB
/
handsontable-chosen-editor.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/// chosen plugin
(function (Handsontable) {
"use strict";
var ChosenEditor = Handsontable.editors.TextEditor.prototype.extend();
ChosenEditor.prototype.prepare = function (row, col, prop, td, originalValue, cellProperties) {
Handsontable.editors.TextEditor.prototype.prepare.apply(this, arguments);
this.options = {};
if (this.cellProperties.chosenOptions) {
this.options = $.extend(this.options, cellProperties.chosenOptions);
}
cellProperties.chosenOptions = $.extend({}, cellProperties.chosenOptions);
};
ChosenEditor.prototype.createElements = function () {
this.$body = $(document.body);
this.TEXTAREA = document.createElement('select');
// Handsontable copy paste plugin calls this.TEXTAREA.select()
this.TEXTAREA.select = function () {};
//this.TEXTAREA.setAttribute('type', 'text');
this.$textarea = $(this.TEXTAREA);
Handsontable.dom.addClass(this.TEXTAREA, 'handsontableInput');
this.textareaStyle = this.TEXTAREA.style;
this.textareaStyle.width = 0;
this.textareaStyle.height = 0;
this.TEXTAREA_PARENT = document.createElement('DIV');
Handsontable.dom.addClass(this.TEXTAREA_PARENT, 'handsontableInputHolder');
this.textareaParentStyle = this.TEXTAREA_PARENT.style;
this.textareaParentStyle.top = 0;
this.textareaParentStyle.left = 0;
this.textareaParentStyle.display = 'none';
this.textareaParentStyle.width = "200px";
this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
this.instance.rootElement.appendChild(this.TEXTAREA_PARENT);
var that = this;
this.instance._registerTimeout(setTimeout(function () {
that.refreshDimensions();
}, 0));
};
var onChosenChanged = function () {
var options = this.cellProperties.chosenOptions;
if (!options.multiple) {
this.close();
this.finishEditing();
}
};
var onChosenClosed = function () {
var options = this.cellProperties.chosenOptions;
if (!options.multiple) {
this.close();
this.finishEditing();
} else {
}
};
var onBeforeKeyDown = function (event) {
var instance = this;
var that = instance.getActiveEditor();
var keyCodes = Handsontable.helper.KEY_CODES;
var ctrlDown = (event.ctrlKey || event.metaKey) && !event.altKey; //catch CTRL but not right ALT (which in some systems triggers ALT+CTRL)
//Process only events that have been fired in the editor
if (event.target.tagName !== "INPUT") {
return;
}
if (event.keyCode === 17 || event.keyCode === 224 || event.keyCode === 91 || event.keyCode === 93) {
//when CTRL or its equivalent is pressed and cell is edited, don't prepare selectable text in textarea
event.stopImmediatePropagation();
return;
}
var target = event.target;
switch (event.keyCode) {
case keyCodes.ARROW_RIGHT:
if (Handsontable.dom.getCaretPosition(target) !== target.value.length) {
event.stopImmediatePropagation();
} else {
that.$textarea.trigger("chosen:close");
}
break;
case keyCodes.ARROW_LEFT:
if (Handsontable.dom.getCaretPosition(target) !== 0) {
event.stopImmediatePropagation();
} else {
that.$textarea.trigger("chosen:close");
}
break;
case keyCodes.ENTER:
if (that.cellProperties.chosenOptions.multiple) {
event.stopImmediatePropagation();
event.preventDefault();
event.stopPropagation();
}
break;
case keyCodes.A:
case keyCodes.X:
case keyCodes.C:
case keyCodes.V:
if (ctrlDown) {
event.stopImmediatePropagation(); //CTRL+A, CTRL+C, CTRL+V, CTRL+X should only work locally when cell is edited (not in table context)
}
break;
case keyCodes.BACKSPACE:
var txt = $(that.TEXTAREA_PARENT).find("input").val();
$(that.TEXTAREA_PARENT).find("input").val(txt.substr(0,txt.length-1)).trigger("keyup.chosen");
event.stopImmediatePropagation();
break;
case keyCodes.DELETE:
case keyCodes.HOME:
case keyCodes.END:
event.stopImmediatePropagation(); //backspace, delete, home, end should only work locally when cell is edited (not in table context)
break;
}
};
ChosenEditor.prototype.open = function (keyboardEvent) {
this.refreshDimensions();
this.textareaParentStyle.display = 'block';
this.instance.addHook('beforeKeyDown', onBeforeKeyDown);
this.$textarea.css({
height: $(this.TD).height() + 4,
'min-width': $(this.TD).outerWidth() - 4
});
//display the list
this.$textarea.hide();
//make sure that list positions matches cell position
//this.$textarea.offset($(this.TD).offset());
var options = $.extend({}, this.options, {
width: "100%",
search_contains: true
});
if (options.multiple) {
this.$textarea.attr("multiple", true);
} else {
this.$textarea.attr("multiple", false);
}
this.$textarea.empty();
this.$textarea.append("<option value=''></option>");
var el = null;
var originalValue = (this.originalValue + "").split(",");
if (options.data && options.data.length) {
for (var i = 0; i < options.data.length; i++) {
el = $("<option />");
el.attr("value", options.data[i].id);
el.html(options.data[i].label);
if (originalValue.indexOf(options.data[i].id + "") > -1) {
el.attr("selected", true);
}
this.$textarea.append(el);
}
}
if ($(this.TEXTAREA_PARENT).find(".chosen-container").length) {
this.$textarea.chosen("destroy");
}
this.$textarea.chosen(options);
var self = this;
setTimeout(function () {
self.$textarea.on('change', onChosenChanged.bind(self));
self.$textarea.on('chosen:hiding_dropdown', onChosenClosed.bind(self));
self.$textarea.trigger("chosen:open");
$(self.TEXTAREA_PARENT).find("input").on("keydown", function(e) {
if(e.keyCode === Handsontable.helper.KEY_CODES.ENTER /*|| e.keyCode === Handsontable.helper.KEY_CODES.BACKSPACE*/) {
if($(this).val()) {
e.preventDefault();
e.stopPropagation();
} else {
e.preventDefault();
e.stopPropagation();
self.close();
self.finishEditing();
}
}
if( e.keyCode === Handsontable.helper.KEY_CODES.BACKSPACE) {
var txt = $(self.TEXTAREA_PARENT).find("input").val();
$(self.TEXTAREA_PARENT).find("input").val(txt.substr(0,txt.length-1)).trigger("keyup.chosen");
e.preventDefault();
e.stopPropagation();
}
if(e.keyCode === Handsontable.helper.KEY_CODES.ARROW_DOWN || e.keyCode === Handsontable.helper.KEY_CODES.ARROW_UP) {
e.preventDefault();
e.stopPropagation();
}
});
setTimeout(function () {
self.$textarea.trigger("chosen:activate").focus();
if (keyboardEvent && keyboardEvent.keyCode && keyboardEvent.keyCode != 113) {
var key = keyboardEvent.keyCode;
var keyText = (String.fromCharCode((96 <= key && key <= 105) ? key - 48 : key)).toLowerCase();
$(self.TEXTAREA_PARENT).find("input").val(keyText).trigger("keyup.chosen");
self.$textarea.trigger("chosen:activate");
}
}, 1);
}, 1);
};
ChosenEditor.prototype.init = function () {
Handsontable.editors.TextEditor.prototype.init.apply(this, arguments);
};
ChosenEditor.prototype.close = function () {
this.instance.listen();
this.instance.removeHook('beforeKeyDown', onBeforeKeyDown);
this.$textarea.off();
this.$textarea.hide();
Handsontable.editors.TextEditor.prototype.close.apply(this, arguments);
};
ChosenEditor.prototype.getValue = function() {
if(!this.$textarea.val()) {
return "";
}
if(typeof this.$textarea.val() === "object") {
return this.$textarea.val().join(",");
}
return this.$textarea.val();
};
ChosenEditor.prototype.focus = function () {
this.instance.listen();
// DO NOT CALL THE BASE TEXTEDITOR FOCUS METHOD HERE, IT CAN MAKE THIS EDITOR BEHAVE POORLY AND HAS NO PURPOSE WITHIN THE CONTEXT OF THIS EDITOR
//Handsontable.editors.TextEditor.prototype.focus.apply(this, arguments);
};
ChosenEditor.prototype.beginEditing = function (initialValue) {
var onBeginEditing = this.instance.getSettings().onBeginEditing;
if (onBeginEditing && onBeginEditing() === false) {
return;
}
Handsontable.editors.TextEditor.prototype.beginEditing.apply(this, arguments);
};
ChosenEditor.prototype.finishEditing = function (isCancelled, ctrlDown) {
this.instance.listen();
return Handsontable.editors.TextEditor.prototype.finishEditing.apply(this, arguments);
};
Handsontable.editors.ChosenEditor = ChosenEditor;
Handsontable.editors.registerEditor('chosen', ChosenEditor);
})(Handsontable);