-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.textarea.js
487 lines (409 loc) · 14.4 KB
/
jquery.textarea.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Copyright (c) 2009 Benoit Chesneau <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Some code borrowed to livepipe :
* Copyright 2008 PersonalGrid Corporation <http://personalgrid.com/>
* License MIT
* Url: http://livepipe.net/control/textarea
*
*/
(function($) {
// From Prototype's array.js
var $A = function(iterable) {
if (!iterable) return [];
if (iterable.toArray) {
return iterable.toArray();
} else {
var results = [];
for (var i = 0; i < iterable.length; i++)
results.push(iterable[i]);
return results;
}
}
Function.prototype.ebind = function() {
var __method = this, args = $A(arguments), object = args.shift();
return function() {
return __method.apply(object, args.concat($A(arguments)));
}
}
Function.prototype.bindAsEventListener = function() {
var __method = this,
args = $A(arguments),
object = args.shift();
return function(event) {
return __method.apply(object, [event || window.event].concat(args));
}
}
K = function(x) {
return x
}
Array.prototype.collect = function(iterator, context) {
iterator = iterator || K;
var results = [];
$(this).each(function(index, value) {
results.push(iterator.call(context, value, index));
});
return results;
},
$.fn.TextArea = function(options) {
options = options || {};
var defaults = {
tab_spacing: true,
tab_char: 4,
lineHeight: 16,
change: null,
resizeable: true,
resizeClassName: "resizeHandle"
};
var options = $.extend(defaults, options);
return this.each(function() {
new TextArea(this, options);
});
}
function TextArea(el, options) {
return this instanceof TextArea
? this.init(el, options)
: TextArea(el, options);
}
$.extend(TextArea.prototype, {
onChangeTimeoutLength: 500,
init: function(el, options) {
var self = this;
this.el = $(el);
this.element = this.el[0];
this.options = options;
this.toolbar = options.toolbar;
this._change_callback = options.change;
$.extend(el, {
textarea: this
});
// detection of browser for webkit
var ua = navigator.userAgent.toLowerCase();
this.isWebkit = (ua.indexOf('webkit') >= 0);
this.isChrome = (ua.indexOf('chrome') >= 0);
this.isOpera = (ua.indexOf('opera') >= 0);
if (this.options.resizeable && !this.isWebkit) {
resizeHandle = $('<div class="' + this.options.resizeClassName + '"></div>')
.insertAfter(this.el)
.bind("mousedown", function(e) {
var h = self.el.height(), y = e.clientY, mouseMove, mouseUp;
mouseMove = function(e) {
self.el.css("height", Math.max(20, e.clientY+h-y)+"px");
return false;
};
mouseUp = function(e) {
$("html").unbind("mousemove", mouseMove).unbind("mouseup", mouseUp);
return false;
};
$("html").bind("mousemove", mouseMove).bind("mouseup", mouseUp);
});
}
this.lastSelection = {};
// we need to add one character for webkit
if (this.isWebkit)
this.options.tab_char += 1;
if (this.options.tab_spacing) {
this.tabulation = "";
for (var i=0; i<this.options.tab_char; i++)
this.tabulation += " ";
} else {
this.tabulation = "\t";
}
this.tab_detected = false;
// init selection for ie
if (!!document.selection)
this.element.selectionStart = this.element.selectionEnd = 0;
this.el.keydown(this.handleKey.ebind(this));
var self = this;
this.doOnChange = function(e) {
if (self._change_callback) {
self._change_callback(self.element.value);
}
return;
}
this.el.keyup(this.doOnChange);
this.el.bind('paste', this.doOnChange);
this.el.bind('input', this.doOnChange);
if(!!document.selection){
this.el.bind('mouseup',this.saveRange.bindAsEventListener(this));
this.el.bind('keyup',this.saveRange.bindAsEventListener(this));
}
if (this.isOpera) {
this.el.bind('blur', function(e) {
if (self.lastKey && self.lastKey == 9)
self.el.focus();
});
}
},
saveRange: function(){
this.range = document.selection.createRange();
},
getValue: function(){
return this.element.value;
},
getSelection: function(){
if(!!document.selection)
return document.selection.createRange().text;
else if(!!this.element.setSelectionRange)
return this.element.value.substring(this.element.selectionStart,this.element.selectionEnd);
else
return false;
},
replaceSelection: function(text){
var scroll_top = this.element.scrollTop;
if(!!document.selection){
this.element.focus();
var range = (this.range) ? this.range : document.selection.createRange();
range.text = text;
range.select();
}else if(!!this.element.setSelectionRange){
var selection_start = this.element.selectionStart;
this.element.value = this.element.value.substring(0,selection_start) + text + this.element.value.substring(this.element.selectionEnd);
this.element.setSelectionRange(selection_start + text.length,selection_start + text.length);
}
this.doOnChange();
this.element.focus();
this.element.scrollTop = scroll_top;
},
wrapSelection: function(before,after){
this.replaceSelection(before + this.getSelection() + after);
},
insertBeforeSelection: function(text){
this.replaceSelection(text + this.getSelection());
},
insertAfterSelection: function(text){
this.replaceSelection(this.getSelection() + text);
},
collectFromEachSelectedLine: function(callback,before,after){
this.replaceSelection((before || '') + $A(this.getSelection().split("\n")).collect(callback).join("\n") + (after || ''));
},
insertBeforeEachSelectedLine: function(text,before,after){
this.collectFromEachSelectedLine(function(line){
},before,after);
},
handleKey: function(e) {
c = e.charCode || e.keyCode;
this.lastKey = c;
if (c == 9) {
this.tab_selection();
if( window.event ){
e.returnValue = false;
}
e.preventDefault();
e.stopPropagation();
return false;
} else if ((c == 13 || c == 10) && (!this.isOpera)) {
//FIXME: opera disabled for now
if (this.do_enter()) {
if( window.event ){
e.returnValue = false;
}
e.preventDefault();
e.stopPropagation();
return false;
}
}
return true;
},
tab_selection: function() {
if (this._is_tabbing)
return;
this._is_tabbing = true;
if (!!document.selection && !this.isOpera)
this._getIESelection();
if (!this._tab_detected)
this._detect_tab();
var start = this.element.selectionStart;
var end = this.element.selectionEnd;
var insText = this.element.value.substring(start, end);
var scrollTop = this.el.scrollTop();
var scrollLeft = this.el.scrollLeft();
var pos_start = start;
var pos_end = end;
if (insText.length == 0) {
// if only one line selected
this.element.value = this.element.value.substr(0, start) +
this.tabulation + this.element.value.substr(end);
pos_start = start + this.tabulation.length;
pos_end = pos_start;
} else {
start = Math.max(0, this.element.value.substr(0, start).lastIndexOf("\n") + 1);
endText = this.element.value.substr(end);
startText = this.element;
value.substr(0, start);
tmp = insText.split("\n");
insText = this.tabulation + tmp.join("\n" + this.tabulation);
this.el.val(startText + insText + endText);
pos_start = start;
pos_end = this.element.value.indexOf("\n", startText.length + insText.length);
if (pos_end == -1)
pos_end = this.element.value.length;
}
this.element.selectionStart = pos_start;
this.element.selectionEnd = pos_end;
if (!!document.selection && !this.isOpera) {
this._setIESelection();
setTimeout(function() {
self._is_tabbing = false;
}, 100);
this._is_tabbing = false;
} else {
this._is_tabbing = false;
}
this.el.scrollTop(scrollTop);
this.el.scrollLeft(scrollLeft);
},
do_enter: function() {
if (!!document.selection && !this.isOpera)
this._getIESelection();
var scrollTop = this.el.scrollTop();
var scrollLeft = this.el.scrollLeft();
var start = this.element.selectionStart;
var end = this.element.selectionEnd;
var start_last_line = Math.max(0, this.element.value.substring(0, start).lastIndexOf("\n") + 1);
var latest_line = this.element.value.substring(start_last_line, start)
if (latest_line.match(/^[ \t]+$/mg, ""))
return false;
var begin_line = latest_line.replace(/^([ \t]*).*/gm, "$1");
if (begin_line == "\n" || begin_line == "\r\n")
return false;
begin_line = begin_line.replace(/\r?\n/g, '')
if ( !!document.selection) {
begin_line = "\r\n" + begin_line;
} else {
begin_line = "\n" + begin_line;
}
this.element.value = this.element.value.substring(0, start) +
begin_line + this.element.value.substring(end);
this.area_select(start + begin_line.length, 0);
this.el.scrollTop(scrollTop);
this.el.scrollLeft(scrollLeft);
return true;
},
area_select: function(start, length) {
value = this.el.val();
start = Math.max(0, Math.min(value.length, start));
end = Math.max(start, Math.min(value.length, start + length));
if (!!document.selection && !this.isOpera) {
this.element.selectionStart = start;
this.element.selectionEnd = end;
this._setIESelection();
} else {
if (this.isOpera) {
this.element.setSelectionRange(0, 0);
this.element.setSelectionRange(end, end);
} else {
this.element.setSelectionRange(start, end);
}
}
},
_detect_tab: function() {
if (this.element.value.indexOf("\t") > 0) {
this.tabulation = "\t";
} else {
this.tabulation = "";
for (var i = 0; i < this.options.tab_char; i++)
this.tabulation += " ";
}
this._tab_detected = true;
},
_getIESelection: function() {
this.el.focus();
var start_range = this.elelement.createTextRange();
var end_range = start_range.duplicate();
start_range.moveToBookmark(document.selection.createRange().getBookmark());
start_range.moveEnd('character', this.element.value.length);
this.el.selectionStart = this.element.value.length - start_range.text.length;
end_range.moveToBookmark(document.selection.createRange().getBookmark());
end_range.moveStart('character', -this.element.value.length);
this.el.selectionEnd = end_range.text.length;
if (this.element.selectionEnd < this.element.selectionStart)
this.element.selectionEnd = this.element.selectionStart;
},
_setIESelection: function() {
var nbLineStart = this.element.value.substr(0,
this.element.selectionStart).split("\n").length - 1;
var nbLineEnd = this.element.value.substr(0,
this.element.selectionEnd).split("\n").length - 1;
var range = document.selection.createRange();
range.moveToElementText(this.element);
range.setEndPoint('EndToStart', range);
range.collapse(true);
range.moveStart('character', this.element.selectionStart - nbLineStart);
range.moveEnd('character', this.element.selectionEnd - nbLineEnd -
(this.element.selectionStart - nbLineStart));
range.select();
}
});
$.Toolbar = function(textarea, options) {
options = options || {};
var defaults = {
className: null
};
var options = $.extend(defaults, options);
return new Toolbar(textarea, options);
}
function Toolbar(textarea, options) {
return this instanceof Toolbar
? this.init(textarea, options)
: Toolbar(textarea, options);
}
$.extend(Toolbar.prototype, {
init: function(textarea, options) {
this.textarea = textarea;
this.options = options;
this.containers =[];
var className = this.options.className || "toolbar";
var self = this;
this.textarea.each(function() {
var container = $(document.createElement('ul'));
container.addClass(className)
container.insertBefore(this);
var obj = this;
self.containers.push({
container: container,
textarea: obj
});
})
},
attachButton: function(node, textarea, callback){
node.onclick = function(){return false;}
var obj = textarea.textarea
$(node).bind('click', callback.bindAsEventListener(obj));
},
addButton: function(link_text,callback,attrs){
for (var i=0; i< this.containers.length; i++) {
var container = this.containers[i];
var li = document.createElement('li');
var a = document.createElement('a');
a.href = '#';
this.attachButton(a, container.textarea, callback);
li.appendChild(a);
if ('className' in attrs) {
var cls = attrs['className'];
attrs['className'] = null;
delete attrs['className'];
$(a).addClass(cls);
}
$.extend(a,attrs || {});
if(link_text){
var span = document.createElement('span');
span.innerHTML = link_text;
a.appendChild(span);
}
container.container[0].appendChild(li);
}
},
});
})(jQuery);