Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. var TextareaTabs = new Class({
  2.  
  3. Implements: [Options, Events],
  4.  
  5. options: {
  6. character: '\t'
  7. },
  8.  
  9. initialize: function(element, options){
  10. this.element = document.id(element);
  11.  
  12. if (!this.element) return;
  13.  
  14. this.setOptions(options);
  15.  
  16. var keys = ['keydown', 'keypress'];
  17. var fn = function(key){
  18. this[key] = this[key].bind(this);
  19. };
  20.  
  21. keys.each(fn, this);
  22.  
  23. this.element.addEvents({
  24. 'keydown': this.keydown,
  25. 'keyup': this.keypress
  26. });
  27. },
  28.  
  29. keydown: function(e){
  30. if (e.key !== 'tab') return;
  31. e.preventDefault();
  32. },
  33.  
  34. keypress: function(e){
  35. if (e.key !== 'tab') return;
  36. e.preventDefault();
  37.  
  38. //IE support
  39. if (document.selection){
  40. this.element.focus();
  41. sel = document.selection.createRange();
  42. sel.text = this.options.character;
  43. }
  44. //MOZILLA/NETSCAPE support
  45. else if (this.element.selectionStart || this.element.selectionStart == '0') {
  46. var startPos = this.element.selectionStart;
  47. var endPos = this.element.selectionEnd;
  48. restoreTop = this.element.scrollTop;
  49.  
  50. this.element.value = this.element.value.substring(0, startPos) + this.options.character + this.element.value.substring(endPos, this.element.value.length);
  51.  
  52. this.element.selectionStart = startPos + this.options.character.length;
  53. this.element.selectionEnd = startPos + this.options.character.length;
  54.  
  55. if (restoreTop > 0)
  56. this.element.scrollTop = restoreTop;
  57. }
  58. else {
  59. this.element.value += this.options.character;
  60. }
  61. }
  62. });
Add Comment
Please, Sign In to add comment