Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquery.longpress.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Project: Long Press
  3. * Description: Pops a list of alternate characters when a key is long-pressed
  4. * Author: Quentin Thiaucourt, http://toki-woki.net
  5. * Licence: MIT License http://opensource.org/licenses/mit-license.php
  6. */
  7. ;(function ($, window, undefined) {
  8. var pluginName = 'longPress',
  9. document = window.document,
  10. defaults = {/*
  11. propertyName: "value"
  12. */};
  13. var moreChars={
  14. // extended latin (and african latin)
  15. // upper
  16. 'A':'ĀĂÀÁÂÃÄÅĄⱭ∀Æ',
  17. 'B':'Ɓ',
  18. 'C':'ÇĆĈĊČƆ',
  19. 'D':'ÐĎĐḎƊ',
  20. 'E':'ÈÉÊËĒĖĘẸĚƏÆƎƐ€',
  21. 'F':'ƑƩ',
  22. 'G':'ĜĞĠĢƢ',
  23. 'H':'ĤĦ',
  24. 'I':'ÌÍÎÏĪĮỊİIƗIJ',
  25. 'J':'ĴIJ',
  26. 'K':'ĶƘ',
  27. 'L':'ĹĻĽŁΛ',
  28. 'N':'ÑŃŅŇŊƝ₦',
  29. 'O':'ÒÓÔÕÖŌØŐŒƠƟ',
  30. 'P':'Ƥ¶',
  31. 'R':'ŔŘɌⱤ',
  32. 'S':'ßſŚŜŞṢŠÞ§',
  33. 'T':'ŢŤṮƬƮ',
  34. 'U':'ÙÚÛÜŪŬŮŰŲɄƯƱ',
  35. 'V':'Ʋ',
  36. 'W':'ŴẄΩ',
  37. 'Y':'ÝŶŸƔƳ',
  38. 'Z':'ŹŻŽƵƷẔ',
  39. // lower
  40. 'a':'āăàáâãäåąɑæαª',
  41. 'b':'ßβɓ',
  42. 'c':'çςćĉċč¢ɔ',
  43. 'd':'ðďđɖḏɖɗ',
  44. 'e':'èéêëēėęẹěəæεɛ€',
  45. 'f':'ƒʃƭ',
  46. 'g':'ĝğġģɠƣ',
  47. 'h':'ĥħɦẖ',
  48. 'i':'ìíîïīįịiiɨijι',
  49. 'j':'ĵɟij',
  50. 'k':'ķƙ',
  51. 'l':'ĺļľłλ',
  52. 'n':'ñńņňŋɲ',
  53. 'o':'òóôõöōøőœơɵ°',
  54. 'p':'ƥ¶',
  55. 'r':'ŕřɍɽ',
  56. 's':'ßſśŝşṣšþ§',
  57. 't':'ţťṯƭʈ',
  58. 'u':'ùúûüūŭůűųưμυʉʊ',
  59. 'v':'ʋ',
  60. 'w':'ŵẅω',
  61. 'y':'ýŷÿɣyƴ',
  62. 'z':'źżžƶẕʒƹ',
  63. // Misc
  64. '$':'£¥€₩₨₳Ƀ¤',
  65. '!':'¡‼‽',
  66. '?':'¿‽',
  67. '%':'‰',
  68. '.':'…••',
  69. '-':'±‐–—',
  70. '+':'±†‡',
  71. '\'':'′″‴‘’‚‛',
  72. '"':'“”„‟',
  73. '<':'≤‹',
  74. '>':'≥›',
  75. '=':'≈≠≡'
  76. };
  77. var ignoredKeys=[8, 13, 37, 38, 39, 40];
  78. var selectedCharIndex;
  79. var lastWhich;
  80. var timer;
  81. var activeElement;
  82. var popup=$('<ul class=long-press-popup />');
  83. $(window).mousewheel(onWheel);
  84. $(window).keyup(onKeyUp);
  85. function onKeyDown(e) {
  86. // Arrow key with popup visible
  87. if ($('.long-press-popup').length>0 && (e.which==37 || e.which==39)) {
  88. if (e.which==37) activePreviousLetter();
  89. else if (e.which==39) activateNextLetter();
  90. e.preventDefault();
  91. return;
  92. }
  93. if (ignoredKeys.indexOf(e.which)>-1) return;
  94. activeElement=e.target;
  95. if (e.which==lastWhich) {
  96. e.preventDefault();
  97. if (!timer) timer=setTimeout(onTimer, 10);
  98. return;
  99. }
  100. lastWhich=e.which;
  101. }
  102. function onKeyUp(e) {
  103. if (ignoredKeys.indexOf(e.which)>-1) return;
  104. if (activeElement==null) return;
  105. lastWhich=null;
  106. clearTimeout(timer);
  107. timer=null;
  108. hidePopup();
  109. }
  110. function onTimer() {
  111. var typedChar=$(activeElement).val().split('')[getCaretPosition(activeElement)-1];
  112. if (moreChars[typedChar]) {
  113. showPopup((moreChars[typedChar]));
  114. } else {
  115. hidePopup();
  116. }
  117. }
  118. function showPopup(chars) {
  119. popup.empty();
  120. var letter;
  121. for (var i=0; i<chars.length; i++) {
  122. letter=$('<li class=long-press-letter />').text(chars[i]);
  123. letter.mouseenter(activateLetter);
  124. popup.append(letter);
  125. }
  126. $('body').append(popup);
  127. selectedCharIndex=-1;
  128. }
  129. function activateLetter(e) {
  130. selectCharIndex($(e.target).index());
  131. }
  132. function activateRelativeLetter(i) {
  133. selectCharIndex(($('.long-press-letter').length+selectedCharIndex+i) % $('.long-press-letter').length);
  134. }
  135. function activateNextLetter() {
  136. activateRelativeLetter(1);
  137. }
  138. function activePreviousLetter() {
  139. activateRelativeLetter(-1);
  140. }
  141. function hidePopup() {
  142. popup.detach();
  143. }
  144. function onWheel(e, delta, deltaX, deltaY) {
  145. if ($('.long-press-popup').length==0) return;
  146. e.preventDefault();
  147. delta<0 ? activateNextLetter() : activePreviousLetter();
  148. }
  149. function selectCharIndex(i) {
  150. $('.long-press-letter.selected').removeClass('selected');
  151. $('.long-press-letter').eq(i).addClass('selected');
  152. selectedCharIndex=i;
  153. updateChar();
  154. }
  155. function updateChar() {
  156. var newChar=$('.long-press-letter.selected').text();
  157. var pos=getCaretPosition(activeElement);
  158. var arVal=$(activeElement).val().split('');
  159. arVal[pos-1]=newChar;
  160. $(activeElement).val(arVal.join(''));
  161. setCaretPosition(activeElement, pos);
  162. }
  163. function LongPress( element, options ) {
  164. this.element = element;
  165. this.options = $.extend( {}, defaults, options) ;
  166. this._defaults = defaults;
  167. this._name = pluginName;
  168. this.init();
  169. }
  170. LongPress.prototype = {
  171. init: function () {
  172. $(this.element).keydown(onKeyDown);
  173. }
  174. };
  175. $.fn[pluginName] = function ( options ) {
  176. return this.each(function () {
  177. if (!$.data(this, 'plugin_' + pluginName)) {
  178. $.data(this, 'plugin_' + pluginName, new LongPress( this, options ));
  179. }
  180. });
  181. };
  182. }(jQuery, window));