jquery.sendkeys.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // insert characters in a textarea or text input field
  2. // special characters are enclosed in {}; use {{} for the { character itself
  3. // documentation: http://bililite.com/blog/2008/08/20/the-fnsendkeys-plugin/
  4. // Version: 2.2
  5. // Copyright (c) 2013 Daniel Wachsstock
  6. // MIT license:
  7. // Permission is hereby granted, free of charge, to any person
  8. // obtaining a copy of this software and associated documentation
  9. // files (the "Software"), to deal in the Software without
  10. // restriction, including without limitation the rights to use,
  11. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the
  13. // Software is furnished to do so, subject to the following
  14. // conditions:
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE.
  25. (function($){
  26. $.fn.sendkeys = function (x, opts){
  27. return this.each( function(){
  28. var localkeys = $.extend({}, opts, $(this).data('sendkeys')); // allow for element-specific key functions
  29. // most elements to not keep track of their selection when they lose focus, so we have to do it for them
  30. var rng = $.data (this, 'sendkeys.selection');
  31. if (!rng){
  32. rng = bililiteRange(this).bounds('selection');
  33. $.data(this, 'sendkeys.selection', rng);
  34. $(this).bind('mouseup.sendkeys', function(){
  35. // we have to update the saved range. The routines here update the bounds with each press, but actual keypresses and mouseclicks do not
  36. $.data(this, 'sendkeys.selection').bounds('selection');
  37. }).bind('keyup.sendkeys', function(evt){
  38. // restore the selection if we got here with a tab (a click should select what was clicked on)
  39. if (evt.which == 9){
  40. // there's a flash of selection when we restore the focus, but I don't know how to avoid that.
  41. $.data(this, 'sendkeys.selection').select();
  42. }else{
  43. $.data(this, 'sendkeys.selection').bounds('selection');
  44. }
  45. });
  46. }
  47. this.focus();
  48. if (typeof x === 'undefined') return; // no string, so we just set up the event handlers
  49. $.data(this, 'sendkeys.originalText', rng.text());
  50. x.replace(/\n/g, '{enter}'). // turn line feeds into explicit break insertions
  51. replace(/{[^}]*}|[^{]+/g, function(s){
  52. (localkeys[s] || $.fn.sendkeys.defaults[s] || $.fn.sendkeys.defaults.simplechar)(rng, s);
  53. rng.select();
  54. });
  55. $(this).trigger({type: 'sendkeys', which: x});
  56. });
  57. }; // sendkeys
  58. // add the functions publicly so they can be overridden
  59. $.fn.sendkeys.defaults = {
  60. simplechar: function (rng, s){
  61. // deal with unknown {key}s
  62. if (/^{.*}$/.test(s)) s = s.slice(1,-1);
  63. rng.text(s, 'end');
  64. for (var i =0; i < s.length; ++i){
  65. var x = s.charCodeAt(i);
  66. // a bit of cheating: rng._el is the element associated with rng.
  67. $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
  68. }
  69. },
  70. '{enter}': function (rng){
  71. rng.insertEOL();
  72. rng.select();
  73. var x = '\n'.charCodeAt(0);
  74. $(rng._el).trigger({type: 'keypress', keyCode: x, which: x, charCode: x});
  75. },
  76. '{backspace}': function (rng){
  77. var b = rng.bounds();
  78. if (b[0] == b[1]) rng.bounds([b[0]-1, b[0]]); // no characters selected; it's just an insertion point. Remove the previous character
  79. rng.text('', 'end'); // delete the characters and update the selection
  80. },
  81. '{del}': function (rng){
  82. var b = rng.bounds();
  83. if (b[0] == b[1]) rng.bounds([b[0], b[0]+1]); // no characters selected; it's just an insertion point. Remove the next character
  84. rng.text('', 'end'); // delete the characters and update the selection
  85. },
  86. '{rightarrow}': function (rng){
  87. var b = rng.bounds();
  88. if (b[0] == b[1]) ++b[1]; // no characters selected; it's just an insertion point. Move to the right
  89. rng.bounds([b[1], b[1]]);
  90. },
  91. '{leftarrow}': function (rng){
  92. var b = rng.bounds();
  93. if (b[0] == b[1]) --b[0]; // no characters selected; it's just an insertion point. Move to the left
  94. rng.bounds([b[0], b[0]]);
  95. },
  96. '{selectall}' : function (rng){
  97. rng.bounds('all');
  98. },
  99. '{selection}': function (rng){
  100. $.fn.sendkeys.defaults.simplechar(rng, $.data(rng._el, 'sendkeys.originalText'));
  101. },
  102. '{mark}' : function (rng){
  103. var bounds = rng.bounds();
  104. $(rng._el).one('sendkeys', function(){
  105. // set up the event listener to change the selection after the sendkeys is done
  106. rng.bounds(bounds).select();
  107. });
  108. }
  109. };
  110. })(jQuery)