-
Notifications
You must be signed in to change notification settings - Fork 0
/
caret-plugin.js
329 lines (261 loc) · 12.1 KB
/
caret-plugin.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
YUI.add('caret-plugin', function (Y) {
var _SelectionControl,
Caret,
L = Y.Lang;
//the caret control logic is separated from the YUI framework to enable anyone who wants
//to port it to another framework, or just use it as it is.
_SelectionControl = {
isValidNode: function (elem) {
//this is meant as a caret control, not a selection one. Because of that, it will only
//cover input and textarea. Any selection in the document should be done in another way
return elem && (
(elem.nodeName.toLowerCase() === 'textarea')
|| (
(elem.nodeName.toLowerCase() === 'input')
//not allowed input types
&& (['radio', 'checkbox', 'submit', 'file', 'hidden',
'reset', 'button', 'image', 'range'].indexOf(
elem.getAttribute('type') || elem.type
) === -1)
)
);
},
//since the selection has a start and an end
//direction 1 means to get the index at the start of the selection
//direction -1 means to get the index at the end of the selection
//direction 0 means to return a object containing a start and end properties.
getSelection: function (elem, direction) {
var caretPos = -1,
ie = {};
//IE (tests the setSelectionRange first to use it if IE implements it someday,
// and avoid a browser that simulates the IE selection control to use it)
if (!elem.setSelectionRange && document.selection) {
//another browser, not IE and not standards
if (!elem.ownerDocument.selection || !elem.ownerDocument.selection.createRange) {
Y.log('The caret-plugin does not support this browser.',
'error', 'selection-control');
return caretPos;
}
ie.oRange = elem.ownerDocument.selection.createRange();
//checking if the selection is in the given element "elem"
if (elem !== ie.oRange.parentElement()) {
return caretPos;
}
//moveStart and moveEnd use the first argument to determine the unit for walk
//this unit is one of the strings:
// character | word | sentence | textedit
ie.unit = 'character';
ie.isInput = (elem.nodeName.toLowerCase() === 'input');
//finding the start or end position
ie.helperRange = ie.oRange.duplicate();
//true means collapse at start of the selection
//false means collapse at end
ie.helperRange.collapse(direction !== -1);
if (ie.isInput) {
//in case of input, this is more faster
caretPos = -ie.helperRange.moveStart(ie.unit, (-1 * elem.value.length));
} else {
//WARNING:
//this is the only way I found to specific find the index of the selection
//position inside a TEXTAREA. I red all the MSDN documentation about this
//and I failed finding a better way. If anyone knows how, please email me.
//the caretPos must starts with -1 because it will run at least one time
for (caretPos = -1; ie.helperRange.parentElement() === elem; caretPos += 1) {
ie.helperRange.moveStart(ie.unit, -1);
}
}
if ((direction === 1) || (direction === -1)) {
return caretPos;
}
caretPos = {start: caretPos};
//finding the end position
ie.helperRange = ie.oRange.duplicate();
ie.helperRange.collapse(false); //collapsing at the end of the selection this time
if (ie.isInput) {
caretPos.end = -ie.helperRange.moveStart(ie.unit, (-1 * elem.value.length));
} else {
for (caretPos.end = -1; ie.helperRange.parentElement() === elem; caretPos.end += 1) {
ie.helperRange.moveStart(ie.unit, -1);
}
}
} else {
//this is for everyone else
if (direction === -1) {
return (elem.selectionEnd === undefined) ? 0 : elem.selectionEnd;
} else if (direction === 1) {
return (elem.selectionStart === undefined) ? 0 : elem.selectionStart;
} else {
caretPos = {
start: (elem.selectionEnd === undefined) ? 0 : elem.selectionStart,
end: (elem.selectionStart === undefined) ? 0 : elem.selectionEnd
};
}
}
return caretPos;
},
//put the caret in the position or make a selection
setSelection: function (elem, indexStart, indexEnd) {
var oRange;
if (indexStart !== undefined) {
indexEnd = indexEnd ? indexEnd : indexStart;
} else {
indexStart = 0;
indexEnd = elem.value.length;
}
if (elem.setSelectionRange) {
//standards
elem.focus();
elem.setSelectionRange(indexStart, indexEnd);
} else if (elem.createTextRange) {
//IE
elem.focus();
oRange = elem.createTextRange();
oRange.collapse(true); //collapsing at the begining
oRange.moveEnd('character', indexEnd);
oRange.moveStart('character', indexStart);
oRange.select();
} else {
//unknow
elem.select();
}
return elem;
},
getSelectedContent: function (elem) {
var content,
selection = this.getSelection(elem);
if (selection === -1) {
return '';
}
content = elem.value;
//eliminating the carriage return that can appear in textarea
//and is treated as one char by IE
if ((content.indexOf('\r\n') !== -1) && (document.selection !== undefined)) {
content = content.replace(/\r\n/g, '\n');
}
return content.substring(selection.start, selection.end);
}
};
Caret = Y.Base.create('caret', Y.Plugin.Base, [], {
//pointer to the DOM node
_DOMNode: null,
initializer: function () {
this._DOMNode = this.get('host').getDOMNode();
},
isValidNode: function () {
return _SelectionControl.isValidNode(this._DOMNode);
},
//this will insert as if the user itself had typed
insert: function (content) {
this.set('content', content);
this.set('position', this.get('selectionEnd'));
},
clearSelection: function () {
_SelectionControl.setSelection(this._DOMNode, 0, 0);
},
selectAll: function () {
_SelectionControl.setSelection(this._DOMNode);
}
}, {
NS: 'caret',
ATTRS: {
selectionStart: {
lazyAdd: false,
value: null,
getter: function () {
if (!this.isValidNode()) return 0;
return _SelectionControl.getSelection(this._DOMNode, 1);
},
setter: function (index) {
var end;
if ((index === null) || !this.isValidNode()) return;
end = _SelectionControl.getSelection(this._DOMNode, -1);
if (index > end) {
end = index;
}
_SelectionControl.setSelection(this._DOMNode, index, end);
return index;
},
validator: function (index) {
return (L.isNumber(index)) && (index >= 0) && (index < this.get('host').get('value').length);
}
},
selectionEnd: {
lazyAdd: false,
value: null,
getter: function () {
if (!this.isValidNode()) return 0;
return _SelectionControl.getSelection(this._DOMNode, -1);
},
setter: function (index) {
var start;
if ((index === null) || !this.isValidNode()) return;
start = _SelectionControl.getSelection(this._DOMNode, 1);
if (index < start) {
start = index;
}
_SelectionControl.setSelection(this._DOMNode, start, index);
return index;
},
validator: function (index) {
return (L.isNumber(index)) && (index >= 0) && (index < this.get('host').get('value').length);
}
},
//shortcut to start and end attributes, but optimized
range: {
lazyAdd: false,
value: null,
getter: function () {
if (!this.isValidNode()) return null;
return _SelectionControl.getSelection(this._DOMNode, 0);
},
setter: function (index) {
if ((index === null) || !this.isValidNode()) return;
_SelectionControl.setSelection(this._DOMNode, index.start, index.end);
}
},
//places the caret at the especified position index
position: {
lazyAdd: false,
value: null,
getter: function () {
if (!this.isValidNode()) return null;
return _SelectionControl.getSelection(this._DOMNode, 1);
},
setter: function (index) {
if ((index === null) || !this.isValidNode()) return;
_SelectionControl.setSelection(this._DOMNode, index, index);
}
},
//manipulates the content of the selection
//if content is setted, it will insert the new content replacing the selected content
//keeping the selection
content: {
lazyAdd: false,
value: null,
getter: function () {
if (!this.isValidNode()) return null;
return _SelectionControl.getSelectedContent(this._DOMNode);
},
setter: function (newContent) {
var index,
actualContent,
contentParts;
if ((newContent === null) || !this.isValidNode()) return;
newContent = L.isUndefined(newContent) ? '' : newContent.toString();
index = _SelectionControl.getSelection(this._DOMNode, 0);
actualContent = this.get('host').get('value');
contentParts = actualContent.substring(0, index.start) + //before selection
newContent + //selection
actualContent.substring(index.end); //after selection
this.get('host').set('value', contentParts);
index.end = index.start + newContent.length;
_SelectionControl.setSelection(this._DOMNode, index.start, index.end);
}
}
}
});
//publishing plugin
Y.Plugin.Caret = Caret;
}, '1.0', {
requires: ['node', 'base', 'plugin']
});