-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
CanvasInput.js
1475 lines (1240 loc) · 41.6 KB
/
CanvasInput.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* CanvasInput v1.2.7
* http://goldfirestudios.com/blog/108/CanvasInput-HTML5-Canvas-Text-Input
*
* (c) 2013-2017, James Simpson of GoldFire Studios
* goldfirestudios.com
*
* MIT License
*/
(function() {
// create a buffer that stores all inputs so that tabbing
// between them is made possible.
var inputs = [];
// initialize the Canvas Input
var CanvasInput = window.CanvasInput = function(o) {
var self = this;
o = o ? o : {};
// setup the defaults
self._canvas = o.canvas || null;
self._ctx = self._canvas ? self._canvas.getContext('2d') : null;
self._x = o.x || 0;
self._y = o.y || 0;
self._extraX = o.extraX || 0;
self._extraY = o.extraY || 0;
self._fontSize = o.fontSize || 14;
self._fontFamily = o.fontFamily || 'Arial';
self._fontColor = o.fontColor || '#000';
self._placeHolderColor = o.placeHolderColor || '#bfbebd';
self._fontWeight = o.fontWeight || 'normal';
self._fontStyle = o.fontStyle || 'normal';
self._fontShadowColor = o.fontShadowColor || '';
self._fontShadowBlur = o.fontShadowBlur || 0;
self._fontShadowOffsetX = o.fontShadowOffsetX || 0;
self._fontShadowOffsetY = o.fontShadowOffsetY || 0;
self._readonly = o.readonly || false;
self._maxlength = o.maxlength || null;
self._width = o.width || 150;
self._height = o.height || self._fontSize;
self._padding = o.padding >= 0 ? o.padding : 5;
self._borderWidth = o.borderWidth >= 0 ? o.borderWidth : 1;
self._borderColor = o.borderColor || '#959595';
self._borderRadius = o.borderRadius >= 0 ? o.borderRadius : 3;
self._backgroundImage = o.backgroundImage || '';
self._boxShadow = o.boxShadow || '1px 1px 0px rgba(255, 255, 255, 1)';
self._innerShadow = o.innerShadow || '0px 0px 4px rgba(0, 0, 0, 0.4)';
self._selectionColor = o.selectionColor || 'rgba(179, 212, 253, 0.8)';
self._placeHolder = o.placeHolder || '';
self._value = (o.value || self._placeHolder) + '';
self._onsubmit = o.onsubmit || function() {};
self._onkeydown = o.onkeydown || function() {};
self._onkeyup = o.onkeyup || function() {};
self._onfocus = o.onfocus || function() {};
self._onblur = o.onblur || function() {};
self._cursor = false;
self._cursorPos = 0;
self._hasFocus = false;
self._selection = [0, 0];
self._wasOver = false;
// parse box shadow
self.boxShadow(self._boxShadow, true);
// calculate the full width and height with padding, borders and shadows
self._calcWH();
// setup the off-DOM canvas
self._renderCanvas = document.createElement('canvas');
self._renderCanvas.setAttribute('width', self.outerW);
self._renderCanvas.setAttribute('height', self.outerH);
self._renderCtx = self._renderCanvas.getContext('2d');
// setup another off-DOM canvas for inner-shadows
self._shadowCanvas = document.createElement('canvas');
self._shadowCanvas.setAttribute('width', self._width + self._padding * 2);
self._shadowCanvas.setAttribute('height', self._height + self._padding * 2);
self._shadowCtx = self._shadowCanvas.getContext('2d');
// setup the background color
if (typeof o.backgroundGradient !== 'undefined') {
self._backgroundColor = self._renderCtx.createLinearGradient(
0,
0,
0,
self.outerH
);
self._backgroundColor.addColorStop(0, o.backgroundGradient[0]);
self._backgroundColor.addColorStop(1, o.backgroundGradient[1]);
} else {
self._backgroundColor = o.backgroundColor || '#fff';
}
// setup main canvas events
if (self._canvas) {
self._canvas.addEventListener('mousemove', function(e) {
e = e || window.event;
self.mousemove(e, self);
}, false);
self._canvas.addEventListener('mousedown', function(e) {
e = e || window.event;
self.mousedown(e, self);
}, false);
self._canvas.addEventListener('mouseup', function(e) {
e = e || window.event;
self.mouseup(e, self);
}, false);
}
// setup a global mouseup to blur the input outside of the canvas
var autoBlur = function(e) {
e = e || window.event;
if (self._hasFocus && !self._mouseDown) {
self.blur();
}
};
window.addEventListener('mouseup', autoBlur, true);
window.addEventListener('touchend', autoBlur, true);
// create the hidden input element
self._hiddenInput = document.createElement('input');
self._hiddenInput.type = 'text';
self._hiddenInput.style.position = 'absolute';
self._hiddenInput.style.opacity = 0;
self._hiddenInput.style.pointerEvents = 'none';
self._hiddenInput.style.zIndex = 0;
// hide native blue text cursor on iOS
self._hiddenInput.style.transform = 'scale(0)';
self._updateHiddenInput();
if (self._maxlength) {
self._hiddenInput.maxLength = self._maxlength;
}
document.body.appendChild(self._hiddenInput);
self._hiddenInput.value = self._value;
// setup the keydown listener
self._hiddenInput.addEventListener('keydown', function(e) {
e = e || window.event;
if (self._hasFocus) {
// hack to fix touch event bug in iOS Safari
window.focus();
self._hiddenInput.focus();
// continue with the keydown event
self.keydown(e, self);
}
});
// setup the keyup listener
self._hiddenInput.addEventListener('keyup', function(e) {
e = e || window.event;
// update the canvas input state information from the hidden input
self._value = self._hiddenInput.value;
self._cursorPos = self._hiddenInput.selectionStart;
// update selection to hidden input's selection in case user did keyboard-based selection
self._selection = [self._hiddenInput.selectionStart, self._hiddenInput.selectionEnd];
self.render();
if (self._hasFocus) {
self._onkeyup(e, self);
}
});
// add this to the buffer
inputs.push(self);
self._inputsIndex = inputs.length - 1;
// draw the text box
self.render();
};
// setup the prototype
CanvasInput.prototype = {
/**
* Get/set the main canvas.
* @param {Object} data Canvas reference.
* @return {Mixed} CanvasInput or current canvas.
*/
canvas: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._canvas = data;
self._ctx = self._canvas.getContext('2d');
return self.render();
} else {
return self._canvas;
}
},
/**
* Get/set the x-position.
* @param {Number} data The pixel position along the x-coordinate.
* @return {Mixed} CanvasInput or current x-value.
*/
x: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._x = data;
self._updateHiddenInput();
return self.render();
} else {
return self._x;
}
},
/**
* Get/set the y-position.
* @param {Number} data The pixel position along the y-coordinate.
* @return {Mixed} CanvasInput or current y-value.
*/
y: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._y = data;
self._updateHiddenInput();
return self.render();
} else {
return self._y;
}
},
/**
* Get/set the extra x-position (generally used when no canvas is specified).
* @param {Number} data The pixel position along the x-coordinate.
* @return {Mixed} CanvasInput or current x-value.
*/
extraX: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._extraX = data;
self._updateHiddenInput();
return self.render();
} else {
return self._extraX;
}
},
/**
* Get/set the extra y-position (generally used when no canvas is specified).
* @param {Number} data The pixel position along the y-coordinate.
* @return {Mixed} CanvasInput or current y-value.
*/
extraY: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._extraY = data;
self._updateHiddenInput();
return self.render();
} else {
return self._extraY;
}
},
/**
* Get/set the font size.
* @param {Number} data Font size.
* @return {Mixed} CanvasInput or current font size.
*/
fontSize: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontSize = data;
return self.render();
} else {
return self._fontSize;
}
},
/**
* Get/set the font family.
* @param {String} data Font family.
* @return {Mixed} CanvasInput or current font family.
*/
fontFamily: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontFamily = data;
return self.render();
} else {
return self._fontFamily;
}
},
/**
* Get/set the font color.
* @param {String} data Font color.
* @return {Mixed} CanvasInput or current font color.
*/
fontColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontColor = data;
return self.render();
} else {
return self._fontColor;
}
},
/**
* Get/set the place holder font color.
* @param {String} data Font color.
* @return {Mixed} CanvasInput or current place holder font color.
*/
placeHolderColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._placeHolderColor = data;
return self.render();
} else {
return self._placeHolderColor;
}
},
/**
* Get/set the font weight.
* @param {String} data Font weight.
* @return {Mixed} CanvasInput or current font weight.
*/
fontWeight: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontWeight = data;
return self.render();
} else {
return self._fontWeight;
}
},
/**
* Get/set the font style.
* @param {String} data Font style.
* @return {Mixed} CanvasInput or current font style.
*/
fontStyle: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontStyle = data;
return self.render();
} else {
return self._fontStyle;
}
},
/**
* Get/set the font shadow color.
* @param {String} data Font shadow color.
* @return {Mixed} CanvasInput or current font shadow color.
*/
fontShadowColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontShadowColor = data;
return self.render();
} else {
return self._fontShadowColor;
}
},
/**
* Get/set the font shadow blur.
* @param {String} data Font shadow blur.
* @return {Mixed} CanvasInput or current font shadow blur.
*/
fontShadowBlur: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontShadowBlur = data;
return self.render();
} else {
return self._fontShadowBlur;
}
},
/**
* Get/set the font shadow x-offset.
* @param {String} data Font shadow x-offset.
* @return {Mixed} CanvasInput or current font shadow x-offset.
*/
fontShadowOffsetX: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontShadowOffsetX = data;
return self.render();
} else {
return self._fontShadowOffsetX;
}
},
/**
* Get/set the font shadow y-offset.
* @param {String} data Font shadow y-offset.
* @return {Mixed} CanvasInput or current font shadow y-offset.
*/
fontShadowOffsetY: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._fontShadowOffsetY = data;
return self.render();
} else {
return self._fontShadowOffsetY;
}
},
/**
* Get/set the width of the text box.
* @param {Number} data Width in pixels.
* @return {Mixed} CanvasInput or current width.
*/
width: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._width = data;
self._calcWH();
self._updateCanvasWH();
self._updateHiddenInput();
return self.render();
} else {
return self._width;
}
},
/**
* Get/set the height of the text box.
* @param {Number} data Height in pixels.
* @return {Mixed} CanvasInput or current height.
*/
height: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._height = data;
self._calcWH();
self._updateCanvasWH();
self._updateHiddenInput();
return self.render();
} else {
return self._height;
}
},
/**
* Get/set the padding of the text box.
* @param {Number} data Padding in pixels.
* @return {Mixed} CanvasInput or current padding.
*/
padding: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._padding = data;
self._calcWH();
self._updateCanvasWH();
return self.render();
} else {
return self._padding;
}
},
/**
* Get/set the border width.
* @param {Number} data Border width.
* @return {Mixed} CanvasInput or current border width.
*/
borderWidth: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._borderWidth = data;
self._calcWH();
self._updateCanvasWH();
return self.render();
} else {
return self._borderWidth;
}
},
/**
* Get/set the border color.
* @param {String} data Border color.
* @return {Mixed} CanvasInput or current border color.
*/
borderColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._borderColor = data;
return self.render();
} else {
return self._borderColor;
}
},
/**
* Get/set the border radius.
* @param {Number} data Border radius.
* @return {Mixed} CanvasInput or current border radius.
*/
borderRadius: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._borderRadius = data;
return self.render();
} else {
return self._borderRadius;
}
},
/**
* Get/set the background color.
* @param {Number} data Background color.
* @return {Mixed} CanvasInput or current background color.
*/
backgroundColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._backgroundColor = data;
return self.render();
} else {
return self._backgroundColor;
}
},
/**
* Get/set the background gradient.
* @param {Number} data Background gradient.
* @return {Mixed} CanvasInput or current background gradient.
*/
backgroundGradient: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._backgroundColor = self._renderCtx.createLinearGradient(
0,
0,
0,
self.outerH
);
self._backgroundColor.addColorStop(0, data[0]);
self._backgroundColor.addColorStop(1, data[1]);
return self.render();
} else {
return self._backgroundColor;
}
},
/**
* Get/set the box shadow.
* @param {String} data Box shadow in CSS format (1px 1px 1px rgba(0, 0, 0.5)).
* @param {Boolean} doReturn (optional) True to prevent a premature render.
* @return {Mixed} CanvasInput or current box shadow.
*/
boxShadow: function(data, doReturn) {
var self = this;
if (typeof data !== 'undefined') {
// parse box shadow
var boxShadow = data.split('px ');
self._boxShadow = {
x: self._boxShadow === 'none' ? 0 : parseInt(boxShadow[0], 10),
y: self._boxShadow === 'none' ? 0 : parseInt(boxShadow[1], 10),
blur: self._boxShadow === 'none' ? 0 : parseInt(boxShadow[2], 10),
color: self._boxShadow === 'none' ? '' : boxShadow[3]
};
// take into account the shadow and its direction
if (self._boxShadow.x < 0) {
self.shadowL = Math.abs(self._boxShadow.x) + self._boxShadow.blur;
self.shadowR = self._boxShadow.blur + self._boxShadow.x;
} else {
self.shadowL = Math.abs(self._boxShadow.blur - self._boxShadow.x);
self.shadowR = self._boxShadow.blur + self._boxShadow.x;
}
if (self._boxShadow.y < 0) {
self.shadowT = Math.abs(self._boxShadow.y) + self._boxShadow.blur;
self.shadowB = self._boxShadow.blur + self._boxShadow.y;
} else {
self.shadowT = Math.abs(self._boxShadow.blur - self._boxShadow.y);
self.shadowB = self._boxShadow.blur + self._boxShadow.y;
}
self.shadowW = self.shadowL + self.shadowR;
self.shadowH = self.shadowT + self.shadowB;
self._calcWH();
if (!doReturn) {
self._updateCanvasWH();
return self.render();
}
} else {
return self._boxShadow;
}
},
/**
* Get/set the inner shadow.
* @param {String} data In the format of a CSS box shadow (1px 1px 1px rgba(0, 0, 0.5)).
* @return {Mixed} CanvasInput or current inner shadow.
*/
innerShadow: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._innerShadow = data;
return self.render();
} else {
return self._innerShadow;
}
},
/**
* Get/set the text selection color.
* @param {String} data Color.
* @return {Mixed} CanvasInput or current selection color.
*/
selectionColor: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._selectionColor = data;
return self.render();
} else {
return self._selectionColor;
}
},
/**
* Get/set the place holder text.
* @param {String} data Place holder text.
* @return {Mixed} CanvasInput or current place holder text.
*/
placeHolder: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._placeHolder = data;
return self.render();
} else {
return self._placeHolder;
}
},
/**
* Get/set the current text box value.
* @param {String} data Text value.
* @return {Mixed} CanvasInput or current text value.
*/
value: function(data) {
var self = this;
if (typeof data !== 'undefined') {
self._value = data + '';
self._hiddenInput.value = data + '';
// update the cursor position
self._cursorPos = self._clipText().length;
self.render();
return self;
} else {
return (self._value === self._placeHolder) ? '' : self._value;
}
},
/**
* Set or fire the onsubmit event.
* @param {Function} fn Custom callback.
*/
onsubmit: function(fn) {
var self = this;
if (typeof fn !== 'undefined') {
self._onsubmit = fn;
return self;
} else {
self._onsubmit();
}
},
/**
* Set or fire the onkeydown event.
* @param {Function} fn Custom callback.
*/
onkeydown: function(fn) {
var self = this;
if (typeof fn !== 'undefined') {
self._onkeydown = fn;
return self;
} else {
self._onkeydown();
}
},
/**
* Set or fire the onkeyup event.
* @param {Function} fn Custom callback.
*/
onkeyup: function(fn) {
var self = this;
if (typeof fn !== 'undefined') {
self._onkeyup = fn;
return self;
} else {
self._onkeyup();
}
},
/**
* Place focus on the CanvasInput box, placing the cursor
* either at the end of the text or where the user clicked.
* @param {Number} pos (optional) The position to place the cursor.
* @return {CanvasInput}
*/
focus: function(pos) {
var self = this;
// only fire the focus event when going from unfocussed
if (!self._hasFocus) {
self._onfocus(self);
// remove focus from all other inputs
for (var i=0; i<inputs.length; i++) {
if (inputs[i]._hasFocus) {
inputs[i].blur();
}
}
}
// remove selection
if (!self._selectionUpdated) {
self._selection = [0, 0];
} else {
delete self._selectionUpdated;
}
// if this is readonly, don't allow it to get focus
self._hasFocus = true;
if (self._readonly) {
self._hiddenInput.readOnly = true;
} else {
self._hiddenInput.readOnly = false;
// update the cursor position
self._cursorPos = (typeof pos === 'number') ? pos : self._clipText().length;
// clear the place holder
if (self._placeHolder === self._value) {
self._value = '';
self._hiddenInput.value = '';
}
self._cursor = true;
// setup cursor interval
if (self._cursorInterval) {
clearInterval(self._cursorInterval);
}
self._cursorInterval = setInterval(function() {
self._cursor = !self._cursor;
self.render();
}, 500);
}
// move the real focus to the hidden input
var hasSelection = (self._selection[0] > 0 || self._selection[1] > 0);
self._hiddenInput.focus();
self._hiddenInput.selectionStart = hasSelection ? self._selection[0] : self._cursorPos;
self._hiddenInput.selectionEnd = hasSelection ? self._selection[1] : self._cursorPos;
return self.render();
},
/**
* Removes focus from the CanvasInput box.
* @param {Object} _this Reference to this.
* @return {CanvasInput}
*/
blur: function(_this) {
var self = _this || this;
self._onblur(self);
if (self._cursorInterval) {
clearInterval(self._cursorInterval);
}
self._hasFocus = false;
self._cursor = false;
self._selection = [0, 0];
self._hiddenInput.blur();
// fill the place holder
if (self._value === '') {
self._value = self._placeHolder;
}
return self.render();
},
/**
* Fired with the keydown event to draw the typed characters.
* @param {Event} e The keydown event.
* @param {CanvasInput} self
* @return {CanvasInput}
*/
keydown: function(e, self) {
var keyCode = e.which,
isShift = e.shiftKey,
key = null,
startText, endText;
// make sure the correct text field is being updated
if (self._readonly || !self._hasFocus) {
return;
}
// fire custom user event
self._onkeydown(e, self);
// add support for Ctrl/Cmd+A selection
if (keyCode === 65 && (e.ctrlKey || e.metaKey)) {
self.selectText();
e.preventDefault();
return self.render();
}
// block keys that shouldn't be processed
if (keyCode === 17 || e.metaKey || e.ctrlKey) {
return self;
}
if (keyCode === 13) { // enter key
e.preventDefault();
self._onsubmit(e, self);
} else if (keyCode === 9) { // tab key
e.preventDefault();
if (inputs.length > 1) {
var next = (inputs[self._inputsIndex + 1]) ? self._inputsIndex + 1 : 0;
self.blur();
setTimeout(function() {
inputs[next].focus();
}, 10);
}
}
// update the canvas input state information from the hidden input
self._value = self._hiddenInput.value;
self._cursorPos = self._hiddenInput.selectionStart;
self._selection = [0, 0];
return self.render();
},
/**
* Fired with the click event on the canvas, and puts focus on/off
* based on where the user clicks.
* @param {Event} e The click event.
* @param {CanvasInput} self
* @return {CanvasInput}
*/
click: function(e, self) {
var mouse = self._mousePos(e),
x = mouse.x,
y = mouse.y;
if (self._endSelection) {
delete self._endSelection;
delete self._selectionUpdated;
return;
}
if (self._canvas && self._overInput(x, y) || !self._canvas) {
if (self._mouseDown) {
self._mouseDown = false;
self.click(e, self);
return self.focus(self._clickPos(x, y));
}
} else {
return self.blur();
}
},
/**
* Fired with the mousemove event to update the default cursor.
* @param {Event} e The mousemove event.
* @param {CanvasInput} self
* @return {CanvasInput}
*/
mousemove: function(e, self) {
var mouse = self._mousePos(e),
x = mouse.x,
y = mouse.y,
isOver = self._overInput(x, y);
if (isOver && self._canvas) {
self._canvas.style.cursor = 'text';
self._wasOver = true;
} else if (self._wasOver && self._canvas) {
self._canvas.style.cursor = 'default';
self._wasOver = false;
}
if (self._hasFocus && self._selectionStart >= 0) {
var curPos = self._clickPos(x, y),
start = Math.min(self._selectionStart, curPos),
end = Math.max(self._selectionStart, curPos);
if (!isOver) {
self._selectionUpdated = true;
self._endSelection = true;
delete self._selectionStart;
self.render();
return;
}
if (self._selection[0] !== start || self._selection[1] !== end) {
self._selection = [start, end];
self.render();
}
}
},
/**
* Fired with the mousedown event to start a selection drag.
* @param {Event} e The mousedown event.
* @param {CanvasInput} self
*/
mousedown: function(e, self) {
var mouse = self._mousePos(e),
x = mouse.x,
y = mouse.y,
isOver = self._overInput(x, y);
// setup the 'click' event
self._mouseDown = isOver;
// start the selection drag if inside the input
if (self._hasFocus && isOver) {
self._selectionStart = self._clickPos(x, y);
}
},