Requires

Provides

OverText.js

Shows text over an input that disappears when the user clicks into it. The text remains hidden if the user adds a value.

License:
MIT-style license
Authors:
Aaron Newton
  1. 30
  2. 31
  3. 32
  4. 33
  5. 34
  6. 35
  7. 36
  8. 37
  9. 38
  10. 39
  11. 40
  12. 41
  13. 42
  14. 43
  15. 44
  16. 45
  17. 46
  18. 47
  19. 48
  20. 49
  21. 50
  22. 51
  23. 52
  24. 53
  25. 54
  26. 55
  27. 56
  28. 57
  29. 58
  30. 59
  31. 60
  32. 61
  33. 62
  34. 63
  35. 64
  36. 65
  37. 66
  38. 67
  39. 68
  40. 69
  41. 70
  42. 71
  43. 72
  44. 73
  45. 74
  46. 75
  47. 76
  48. 77
  49. 78
  50. 79
  51. 80
  52. 81
  53. 82
  54. 83
  55. 84
  56. 85
  57. 86
  58. 87
  59. 88
  60. 89
  61. 90
  62. 91
  63. 92
  64. 93
  65. 94
  66. 95
  67. 96
  68. 97
  69. 98
  70. 99
  71. 100
  72. 101
  73. 102
  74. 103
  75. 104
  76. 105
  77. 106
  78. 107
  79. 108
  80. 109
  81. 110
  82. 111
  83. 112
  84. 113
  85. 114
  86. 115
  87. 116
  88. 117
  89. 118
  90. 119
  91. 120
  92. 121
  93. 122
  94. 123
  95. 124
  96. 125
  97. 126
  98. 127
  99. 128
  100. 129
  101. 130
  102. 131
  103. 132
  104. 133
  105. 134
  106. 135
  107. 136
  108. 137
  109. 138
  110. 139
  111. 140
  112. 141
  113. 142
  114. 143
  115. 144
  116. 145
  117. 146
  118. 147
var OverText = new Class({ Implements: [Options, Events, Class.Occlude], Binds: ['reposition', 'assert', 'focus', 'hide'], options: {/* textOverride: null, onFocus: function(){}, onTextHide: function(textEl, inputEl){}, onTextShow: function(textEl, inputEl){}, */ element: 'label', positionOptions: { position: 'upperLeft', edge: 'upperLeft', offset: { x: 4, y: 2 } }, poll: false, pollInterval: 250, wrap: false }, property: 'OverText', initialize: function(element, options){ this.element = document.id(element); if (this.occlude()) return this.occluded; this.setOptions(options); this.attach(this.element); OverText.instances.push(this); if (this.options.poll) this.poll(); return this; }, toElement: function(){ return this.element; }, attach: function(){ var val = this.options.textOverride || this.element.get('alt') || this.element.get('title'); if (!val) return; this.text = new Element(this.options.element, { 'class': 'overTxtLabel', styles: { lineHeight: 'normal', position: 'absolute', cursor: 'text' }, html: val, events: { click: this.hide.pass(this.options.element == 'label', this) } }).inject(this.element, 'after'); if (this.options.element == 'label'){ if (!this.element.get('id')) this.element.set('id', 'input_' + new Date().getTime()); this.text.set('for', this.element.get('id')); } if (this.options.wrap){ this.textHolder = new Element('div', { styles: { lineHeight: 'normal', position: 'relative' }, 'class':'overTxtWrapper' }).adopt(this.text).inject(this.element, 'before'); } return this.enable(); }, destroy: function(){ this.element.eliminate('OverTextDiv').eliminate('OverText'); this.disable(); if (this.text) this.text.destroy(); if (this.textHolder) this.textHolder.destroy(); return this; }, disable: function(){ this.element.removeEvents({ focus: this.focus, blur: this.assert, change: this.assert }); window.removeEvent('resize', this.reposition); this.hide(true, true); return this; }, enable: function(){ this.element.addEvents({ focus: this.focus, blur: this.assert, change: this.assert }); window.addEvent('resize', this.reposition); this.assert(true); this.reposition(); return this; }, wrap: function(){ if (this.options.element == 'label'){ if (!this.element.get('id')) this.element.set('id', 'input_' + new Date().getTime()); this.text.set('for', this.element.get('id')); } }, startPolling: function(){ this.pollingPaused = false; return this.poll(); }, poll: function(stop){

start immediately pause on focus resumeon blur

  1. 151
  2. 152
  3. 153
  4. 154
  5. 155
  6. 156
  7. 157
  8. 158
  9. 159
  10. 160
  11. 161
  12. 162
  13. 163
  14. 164
  15. 165
  16. 166
  17. 167
  18. 168
  19. 169
  20. 170
  21. 171
  22. 172
  23. 173
  24. 174
  25. 175
  26. 176
  27. 177
  28. 178
  29. 179
  30. 180
  31. 181
  32. 182
  33. 183
  34. 184
  35. 185
  36. 186
  37. 187
  38. 188
  39. 189
  40. 190
  41. 191
  42. 192
  43. 193
  44. 194
  45. 195
  46. 196
  47. 197
  48. 198
  49. 199
  50. 200
  51. 201
  52. 202
  53. 203
  54. 204
  55. 205
  56. 206
  57. 207
  58. 208
  59. 209
  60. 210
  61. 211
  62. 212
  63. 213
  64. 214
  65. 215
  66. 216
  67. 217
  68. 218
  69. 219
  70. 220
  71. 221
  72. 222
  73. 223
  74. 224
  75. 225
  76. 226
  77. 227
  78. 228
  79. 229
  80. 230
  81. 231
  82. 232
  83. 233
  84. 234
  85. 235
  86. 236
  87. 237
  88. 238
  89. 239
  90. 240
  91. 241
  92. 242
  93. 243
  94. 244
  95. 245
  96. 246
  97. 247
  98. 248
  99. 249
  100. 250
  101. 251
  102. 252
if (this.poller && !stop) return this; var test = function(){ if (!this.pollingPaused) this.assert(true); }.bind(this); if (stop) clearInterval(this.poller); else this.poller = test.periodical(this.options.pollInterval, this); return this; }, stopPolling: function(){ this.pollingPaused = true; return this.poll(true); }, focus: function(){ if (this.text && (!this.text.isDisplayed() || this.element.get('disabled'))) return; this.hide(); }, hide: function(suppressFocus, force){ if (this.text && (this.text.isDisplayed() && (!this.element.get('disabled') || force))){ this.text.hide(); this.fireEvent('textHide', [this.text, this.element]); this.pollingPaused = true; if (!suppressFocus){ try { this.element.fireEvent('focus'); this.element.focus(); } catch(e){} //IE barfs if you call focus on hidden elements } } return this; }, show: function(){ if (this.text && !this.text.isDisplayed()){ this.text.show(); this.reposition(); this.fireEvent('textShow', [this.text, this.element]); this.pollingPaused = false; } return this; }, assert: function(suppressFocus){ this[this.test() ? 'show' : 'hide'](suppressFocus); }, test: function(){ var v = this.element.get('value'); return !v; }, reposition: function(){ this.assert(true); if (!this.element.isVisible()) return this.stopPolling().hide(); if (this.text && this.test()) this.text.position(Object.merge(this.options.positionOptions, {relativeTo: this.element})); return this; } }); OverText.instances = []; Object.append(OverText, { each: function(fn){ return OverText.instances.map(function(ot, i){ if (ot.element && ot.text) return fn.apply(OverText, [ot, i]); return null; //the input or the text was destroyed }); }, update: function(){ return OverText.each(function(ot){ return ot.reposition(); }); }, hideAll: function(){ return OverText.each(function(ot){ return ot.hide(true, true); }); }, showAll: function(){ return OverText.each(function(ot){ return ot.show(); }); } }); if (window.Fx && Fx.Reveal){ Fx.Reveal.implement({ hideInputs: Browser.ie ? 'select, input, textarea, object, embed, .overTxtLabel' : false }); }