2
0

foundation.placeholder.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*! http://mths.be/placeholder v2.0.7 by @mathias
  2. Modified to work with Zepto.js by ZURB
  3. */
  4. ;(function(window, document, $) {
  5. var isInputSupported = 'placeholder' in document.createElement('input'),
  6. isTextareaSupported = 'placeholder' in document.createElement('textarea'),
  7. prototype = $.fn,
  8. valHooks = $.valHooks,
  9. hooks,
  10. placeholder;
  11. if (isInputSupported && isTextareaSupported) {
  12. placeholder = prototype.placeholder = function() {
  13. return this;
  14. };
  15. placeholder.input = placeholder.textarea = true;
  16. } else {
  17. placeholder = prototype.placeholder = function() {
  18. var $this = this;
  19. $this
  20. .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
  21. .not('.placeholder')
  22. .bind({
  23. 'focus.placeholder': clearPlaceholder,
  24. 'blur.placeholder': setPlaceholder
  25. })
  26. .data('placeholder-enabled', true)
  27. .trigger('blur.placeholder');
  28. return $this;
  29. };
  30. placeholder.input = isInputSupported;
  31. placeholder.textarea = isTextareaSupported;
  32. hooks = {
  33. 'get': function(element) {
  34. var $element = $(element);
  35. return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;
  36. },
  37. 'set': function(element, value) {
  38. var $element = $(element);
  39. if (!$element.data('placeholder-enabled')) {
  40. return element.value = value;
  41. }
  42. if (value == '') {
  43. element.value = value;
  44. // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
  45. if (element != document.activeElement) {
  46. // We can't use `triggerHandler` here because of dummy text/password inputs :(
  47. setPlaceholder.call(element);
  48. }
  49. } else if ($element.hasClass('placeholder')) {
  50. clearPlaceholder.call(element, true, value) || (element.value = value);
  51. } else {
  52. element.value = value;
  53. }
  54. // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
  55. return $element;
  56. }
  57. };
  58. isInputSupported || (valHooks.input = hooks);
  59. isTextareaSupported || (valHooks.textarea = hooks);
  60. $(function() {
  61. // Look for forms
  62. $(document).delegate('form', 'submit.placeholder', function() {
  63. // Clear the placeholder values so they don't get submitted
  64. var $inputs = $('.placeholder', this).each(clearPlaceholder);
  65. setTimeout(function() {
  66. $inputs.each(setPlaceholder);
  67. }, 10);
  68. });
  69. });
  70. // Clear placeholder values upon page reload
  71. $(window).bind('beforeunload.placeholder', function() {
  72. $('.placeholder').each(function() {
  73. this.value = '';
  74. });
  75. });
  76. }
  77. function args(elem) {
  78. // Return an object of element attributes
  79. var newAttrs = {},
  80. rinlinejQuery = /^jQuery\d+$/;
  81. $.each(elem.attributes, function(i, attr) {
  82. if (attr.specified && !rinlinejQuery.test(attr.name)) {
  83. newAttrs[attr.name] = attr.value;
  84. }
  85. });
  86. return newAttrs;
  87. }
  88. function clearPlaceholder(event, value) {
  89. var input = this,
  90. $input = $(input);
  91. if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) {
  92. if ($input.data('placeholder-password')) {
  93. $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
  94. // If `clearPlaceholder` was called from `$.valHooks.input.set`
  95. if (event === true) {
  96. return $input[0].value = value;
  97. }
  98. $input.focus();
  99. } else {
  100. input.value = '';
  101. $input.removeClass('placeholder');
  102. input == document.activeElement && input.select();
  103. }
  104. }
  105. }
  106. function setPlaceholder() {
  107. var $replacement,
  108. input = this,
  109. $input = $(input),
  110. $origInput = $input,
  111. id = this.id;
  112. if (input.value == '') {
  113. if (input.type == 'password') {
  114. if (!$input.data('placeholder-textinput')) {
  115. try {
  116. $replacement = $input.clone().attr({ 'type': 'text' });
  117. } catch(e) {
  118. $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
  119. }
  120. $replacement
  121. .removeAttr('name')
  122. .data({
  123. 'placeholder-password': true,
  124. 'placeholder-id': id
  125. })
  126. .bind('focus.placeholder', clearPlaceholder);
  127. $input
  128. .data({
  129. 'placeholder-textinput': $replacement,
  130. 'placeholder-id': id
  131. })
  132. .before($replacement);
  133. }
  134. $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
  135. // Note: `$input[0] != input` now!
  136. }
  137. $input.addClass('placeholder');
  138. $input[0].value = $input.attr('placeholder');
  139. } else {
  140. $input.removeClass('placeholder');
  141. }
  142. }
  143. }(this, document, Foundation.zj));