summaryrefslogtreecommitdiffstats
path: root/core/js/jquery.placeholder.js
blob: 689462582b35e60ab43eea58f3ccc16798315b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
  jQuery placeholder plugin
  by Andrey Kuzmin, @unsoundscapes

  Based on existing plugin http://mths.be/placeholder by @mathias
  and this demo http://robertnyman.com/2011/05/02/ by @robertnyman

  Adopted to toggle placeholder on user input instead of focus

  Released under the MIT license
*/

(function (factory) {
  'use strict';

  if (typeof define === 'function' && define.amd) {
    // AMD. Register as anonymous module.
    define(['jquery'], factory)
  } else {
    // Browser globals.
    factory(jQuery)
  }
}(function ($) {
  'use strict';

  var isInputSupported = 'placeholder' in document.createElement('input')
    , isTextareaSupported = 'placeholder' in document.createElement('textarea')
    , $placeholders = $()

  function getAttributes (element) {
    // Return an object of element attributes
    var newAttrs = {}
      , rinlinejQuery = /^jQuery\d+$/

    $.each(element.attributes, function () {
      if (this.specified && !rinlinejQuery.test(this.name)) {
        newAttrs[this.name] = this.value
      }
    })
    return newAttrs
  }

  function setCaretTo (element, index) {
    // Set caret to specified @index
    if (element.createTextRange) {
      var range = element.createTextRange()
      range.move('character', index)
      range.select()
    } else if (element.selectionStart !== null) {
      element.focus()
      element.setSelectionRange(index, index)
    }
  }


  function Placeholder (element, options) {
    this.options = options || {}
    this.$replacement = this.$element = $(element)
    this.initialize.apply(this, arguments)
    // Cache all elements with placeholders
    $placeholders = $placeholders.add(element)
  }

  Placeholder.prototype = {

    initialize: function () {
      this.isHidden = true
      this.placeholderAttr = this.$element.attr('placeholder')
      // do not mess with default behavior
      this.$element.removeAttr('placeholder')
      this.isPassword = this.$element.is('[type=password]')
      if (this.isPassword) this.makeReplacement()
      this.$replacement.on({
        'keydown.placeholder': $.proxy(this.hide, this)
      , 'focus.placeholder drop.placeholder click.placeholder': $.proxy(this.setCaret, this)
      })
      this.$element.on({
        'blur.placeholder keyup.placeholder': $.proxy(this.show, this)
      })
      this.show()
    }

    // Set or get input value
    // Setting value toggles placeholder
  , val: function (value) {
      if (value === undefined) {
        return this.isHidden ? this.$element[0].value : '';
      }
      if (value === '') {
        if (this.isHidden) {
          this.$element[0].value = value
          this.show()
        }
      } else {
        if (!this.isHidden) this.hide()
        this.$element[0].value = value
      }
      return this
    }

    // Hide placeholder at user input
  , hide: function (e) {
      var isActiveElement = this.$replacement.is(':focus')
      if (this.isHidden) return;
      if (!e || !(e.shiftKey && e.keyCode === 16) && e.keyCode !== 9) {
        this.isHidden = true
        if (this.isPassword) {
          this.$replacement.before(this.$element.show()).hide()
          if (isActiveElement) this.$element.focus()
        } else {
          this.$element[0].value = ''
          this.$element.removeClass(this.options.className)
        }
      }
    }

    // Show placeholder on blur and keyup
  , show: function (e) {
      var isActiveElement = this.$element.is(':focus')
      if (!this.isHidden) return;
      if (this.$element[0].value === '') {
        this.isHidden = false
        if (this.isPassword) {
          this.$element.before(this.$replacement.show()).hide()
          if (isActiveElement) this.$replacement.focus()
        } else {
          this.$element[0].value = this.placeholderAttr
          this.$element.addClass(this.options.className)
          if (isActiveElement) this.setCaret(e)
        }
      }
    }

    // Set caret at the beginning of the input
  , setCaret: function (e) {
      if (e && !this.isHidden) {
        setCaretTo(this.$replacement[0], 0)
        e.preventDefault()
      }
    }

    // Make and return replacement element
  , makeReplacement: function () {
      // we can't use $.fn.clone because ie <= 8 doesn't allow type change
      var replacementAttributes =
        $.extend(
          getAttributes(this.$element[0])
        , { 'type': 'text'
          , 'value': this.placeholderAttr
          }
        )

      // replacement should not have input name
      delete replacementAttributes.name

      this.$replacement = $('<input>', replacementAttributes)
        .data('placeholder', this)
        .addClass(this.options.className)

      return this.$replacement;
    }

  }


  // Override jQuery val and prop hooks
  $.valHooks.input = $.valHooks.textarea = $.propHooks.value = {
    get: function (element) {
      var placeholder = $(element).data('placeholder')
      return placeholder ? placeholder.val() : element.value;
    }
  , set: function (element, value) {
      var placeholder = $(element).data('placeholder')
      return placeholder ? placeholder.val(value) : element.value = value;
    }
  }


  // Plugin definition
  $.fn.placeholder = function (option) {
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('placeholder')
        , options = $.extend({}, $.fn.placeholder.defaults, typeof option === 'object' && option)

      if (!data && $this.is('[placeholder]') && (options.force ||
          !isInputSupported && $this.is('input') ||
          !isTextareaSupported && $this.is('textarea'))) {
        $this.data('placeholder', data = new Placeholder(this, options))
      }

      if (data && typeof option === 'string') data[option]()
    })
  }
  $.fn.placeholder.defaults = {
    force: false
  , className: 'placeholder'
  }
  $.fn.placeholder.Constructor = Placeholder


  // Events
  $(document).on('submit.placeholder', 'form', function () {
    // Clear the placeholder values so they don't get submitted
    $placeholders.placeholder('hide')
    // And then restore them back
    setTimeout(function () { $placeholders.placeholder('show') }, 10)
  })
  $(window).on('beforeunload.placeholder', function () {
    // Clear placeholders upon page reload
    $placeholders.placeholder('hide')
  })

  return Placeholder

}));