This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
/
jquery.longpress.js
209 lines (182 loc) · 4.93 KB
/
jquery.longpress.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
/*
* Project: Long Press
* Description: Pops a list of alternate characters when a key is long-pressed
* Author: Quentin Thiaucourt, http://toki-woki.net
* Licence: MIT License http://opensource.org/licenses/mit-license.php
*/
;(function ($, window, undefined) {
var pluginName = 'longPress',
document = window.document,
defaults = {/*
propertyName: "value"
*/};
var moreChars={
// extended latin (and african latin)
// upper
'A':'ĀĂÀÁÂÃÄÅĄⱭ∀Æ',
'B':'Ɓ',
'C':'ÇĆĈĊČƆ',
'D':'ÐĎĐḎƊ',
'E':'ÈÉÊËĒĖĘẸĚƏÆƎƐ€',
'F':'ƑƩ',
'G':'ĜĞĠĢƢ',
'H':'ĤĦ',
'I':'ÌÍÎÏĪĮỊİIƗIJ',
'J':'ĴIJ',
'K':'ĶƘ',
'L':'ĹĻĽŁΛ',
'N':'ÑŃŅŇŊƝ₦',
'O':'ÒÓÔÕÖŌØŐŒƠƟ',
'P':'Ƥ¶',
'R':'ŔŘɌⱤ',
'S':'ßſŚŜŞṢŠÞ§',
'T':'ŢŤṮƬƮ',
'U':'ÙÚÛÜŪŬŮŰŲɄƯƱ',
'V':'Ʋ',
'W':'ŴẄΩ',
'Y':'ÝŶŸƔƳ',
'Z':'ŹŻŽƵƷẔ',
// lower
'a':'āăàáâãäåąɑæαª',
'b':'ßβɓ',
'c':'çςćĉċč¢ɔ',
'd':'ðďđɖḏɖɗ',
'e':'èéêëēėęẹěəæεɛ€',
'f':'ƒʃƭ',
'g':'ĝğġģɠƣ',
'h':'ĥħɦẖ',
'i':'ìíîïīįịiiɨijι',
'j':'ĵɟij',
'k':'ķƙ',
'l':'ĺļľłλ',
'n':'ñńņňŋɲ',
'o':'òóôõöōøőœơɵ°',
'p':'ƥ¶',
'r':'ŕřɍɽ',
's':'ßſśŝşṣšþ§',
't':'ţťṯƭʈ',
'u':'ùúûüūŭůűųưμυʉʊ',
'v':'ʋ',
'w':'ŵẅω',
'y':'ýŷÿɣyƴ',
'z':'źżžƶẕʒƹ',
// Misc
'$':'£¥€₩₨₳Ƀ₹¤',
'!':'¡‼‽',
'?':'¿‽',
'%':'‰',
'.':'…••',
'-':'±‐–—',
'+':'±†‡',
'\'':'′″‴‘’‚‛',
'"':'“”„‟',
'<':'≤‹',
'>':'≥›',
'=':'≈≠≡'
};
var ignoredKeys=[8, 13, 37, 38, 39, 40];
var selectedCharIndex;
var lastWhich;
var timer;
var activeElement;
var popup=$('<ul class=long-press-popup />');
$(window).mousewheel(onWheel);
$(window).keyup(onKeyUp);
function onKeyDown(e) {
// Arrow key with popup visible
if ($('.long-press-popup').length>0 && (e.which==37 || e.which==39)) {
if (e.which==37) activePreviousLetter();
else if (e.which==39) activateNextLetter();
e.preventDefault();
return;
}
if (ignoredKeys.indexOf(e.which)>-1) return;
activeElement=e.target;
if (e.which==lastWhich) {
e.preventDefault();
if (!timer) timer=setTimeout(onTimer, 10);
return;
}
lastWhich=e.which;
}
function onKeyUp(e) {
if (ignoredKeys.indexOf(e.which)>-1) return;
if (activeElement==null) return;
lastWhich=null;
clearTimeout(timer);
timer=null;
hidePopup();
}
function onTimer() {
var typedChar=$(activeElement).val().split('')[getCaretPosition(activeElement)-1];
if (moreChars[typedChar]) {
showPopup((moreChars[typedChar]));
} else {
hidePopup();
}
}
function showPopup(chars) {
popup.empty();
var letter;
for (var i=0; i<chars.length; i++) {
letter=$('<li class=long-press-letter />').text(chars[i]);
letter.mouseenter(activateLetter);
popup.append(letter);
}
$('body').append(popup);
selectedCharIndex=-1;
}
function activateLetter(e) {
selectCharIndex($(e.target).index());
}
function activateRelativeLetter(i) {
selectCharIndex(($('.long-press-letter').length+selectedCharIndex+i) % $('.long-press-letter').length);
}
function activateNextLetter() {
activateRelativeLetter(1);
}
function activePreviousLetter() {
activateRelativeLetter(-1);
}
function hidePopup() {
popup.detach();
}
function onWheel(e, delta, deltaX, deltaY) {
if ($('.long-press-popup').length==0) return;
e.preventDefault();
delta<0 ? activateNextLetter() : activePreviousLetter();
}
function selectCharIndex(i) {
$('.long-press-letter.selected').removeClass('selected');
$('.long-press-letter').eq(i).addClass('selected');
selectedCharIndex=i;
updateChar();
}
function updateChar() {
var newChar=$('.long-press-letter.selected').text();
var pos=getCaretPosition(activeElement);
var arVal=$(activeElement).val().split('');
arVal[pos-1]=newChar;
$(activeElement).val(arVal.join(''));
setCaretPosition(activeElement, pos);
}
function LongPress( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
LongPress.prototype = {
init: function () {
$(this.element).keydown(onKeyDown);
}
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new LongPress( this, options ));
}
});
};
}(jQuery, window));