Requires

Provides

Element.Forms.js

Extends the Element native object to include methods useful in managing inputs.

License:
MIT-style license
Authors:
Aaron Newton
  1. 26
  2. 27
  3. 28
  4. 29
  5. 30
  6. 31
  7. 32
  8. 33
  9. 34
  10. 35
  11. 36
  12. 37
  13. 38
  14. 39
  15. 40
  16. 41
  17. 42
  18. 43
  19. 44
  20. 45
  21. 46
  22. 47
  23. 48
  24. 49
  25. 50
  26. 51
  27. 52
  28. 53
  29. 54
  30. 55
  31. 56
  32. 57
  33. 58
  34. 59
  35. 60
  36. 61
  37. 62
  38. 63
  39. 64
  40. 65
  41. 66
  42. 67
  43. 68
  44. 69
  45. 70
  46. 71
  47. 72
  48. 73
  49. 74
  50. 75
  51. 76
  52. 77
  53. 78
  54. 79
  55. 80
  56. 81
  57. 82
  58. 83
  59. 84
  60. 85
  61. 86
  62. 87
  63. 88
  64. 89
  65. 90
  66. 91
  67. 92
  68. 93
  69. 94
  70. 95
  71. 96
  72. 97
  73. 98
  74. 99
  75. 100
  76. 101
  77. 102
  78. 103
  79. 104
  80. 105
  81. 106
  82. 107
  83. 108
  84. 109
  85. 110
  86. 111
  87. 112
  88. 113
  89. 114
  90. 115
  91. 116
  92. 117
  93. 118
  94. 119
  95. 120
  96. 121
  97. 122
  98. 123
  99. 124
  100. 125
  101. 126
  102. 127
  103. 128
  104. 129
  105. 130
  106. 131
  107. 132
  108. 133
  109. 134
  110. 135
  111. 136
  112. 137
  113. 138
  114. 139
  115. 140
  116. 141
Element.implement({ tidy: function(){ this.set('value', this.get('value').tidy()); }, getTextInRange: function(start, end){ return this.get('value').substring(start, end); }, getSelectedText: function(){ if (this.setSelectionRange) return this.getTextInRange(this.getSelectionStart(), this.getSelectionEnd()); return document.selection.createRange().text; }, getSelectedRange: function(){ if (this.selectionStart != null){ return { start: this.selectionStart, end: this.selectionEnd }; } var pos = { start: 0, end: 0 }; var range = this.getDocument().selection.createRange(); if (!range || range.parentElement() != this) return pos; var duplicate = range.duplicate(); if (this.type == 'text'){ pos.start = 0 - duplicate.moveStart('character', -100000); pos.end = pos.start + range.text.length; } else { var value = this.get('value'); var offset = value.length; duplicate.moveToElementText(this); duplicate.setEndPoint('StartToEnd', range); if (duplicate.text.length) offset -= value.match(/[\n\r]*$/)[0].length; pos.end = offset - duplicate.text.length; duplicate.setEndPoint('StartToStart', range); pos.start = offset - duplicate.text.length; } return pos; }, getSelectionStart: function(){ return this.getSelectedRange().start; }, getSelectionEnd: function(){ return this.getSelectedRange().end; }, setCaretPosition: function(pos){ if (pos == 'end') pos = this.get('value').length; this.selectRange(pos, pos); return this; }, getCaretPosition: function(){ return this.getSelectedRange().start; }, selectRange: function(start, end){ if (this.setSelectionRange){ this.focus(); this.setSelectionRange(start, end); } else { var value = this.get('value'); var diff = value.substr(start, end - start).replace(/\r/g, '').length; start = value.substr(0, start).replace(/\r/g, '').length; var range = this.createTextRange(); range.collapse(true); range.moveEnd('character', start + diff); range.moveStart('character', start); range.select(); } return this; }, insertAtCursor: function(value, select){ var pos = this.getSelectedRange(); var text = this.get('value'); this.set('value', text.substring(0, pos.start) + value + text.substring(pos.end, text.length)); if (select !== false) this.selectRange(pos.start, pos.start + value.length); else this.setCaretPosition(pos.start + value.length); return this; }, insertAroundCursor: function(options, select){ options = Object.append({ before: '', defaultMiddle: '', after: '' }, options); var value = this.getSelectedText() || options.defaultMiddle; var pos = this.getSelectedRange(); var text = this.get('value'); if (pos.start == pos.end){ this.set('value', text.substring(0, pos.start) + options.before + value + options.after + text.substring(pos.end, text.length)); this.selectRange(pos.start + options.before.length, pos.end + options.before.length + value.length); } else { var current = text.substring(pos.start, pos.end); this.set('value', text.substring(0, pos.start) + options.before + current + options.after + text.substring(pos.end, text.length)); var selStart = pos.start + options.before.length; if (select !== false) this.selectRange(selStart, selStart + current.length); else this.setCaretPosition(selStart + text.length); } return this; } });