diaspora/vendor/assets/javascripts/jquery.placeholder.js
Steven Hancock 1aa0b15c8c Move Javascript to the asset pipeline
* Move all Diaspora-specific javascripts to app/assets/javascripts
* Move all vendored javascripts to vendor/assets/javascripts
* Add the appropriate Sprockets require directives to make sure
  everything gets included in the right order
* Remove Jammit dependencies
* Fix all templates that were using Jammit's include_javascripts helper
* Add handlebars_assets gem for compiling Handlebars templates
* Move all Handlebars templates to app/assets/templates and rename
  from .handlebars to .jst.hbs (this is to keep them in the same
  global JST namespace that they were in under Jammit)
* Add public/assets to .gitignore since these files can and should
  be re-generated by Heroku or Capistrano during each deploy
* Fix a few Handlebars templates that were looking for images in the
  wrong location (I'm sure there are others, but it's late)
* Configure application.rb to precompile all javascript and css assets
  that were compiled by Jammit in the Rails 3.0 code
2012-03-27 18:07:44 -07:00

104 lines
No EOL
3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*! http://mths.be/placeholder v1.8.7 by @mathias */
;(function(window, document, $) {
var isInputSupported = 'placeholder' in document.createElement('input'),
isTextareaSupported = 'placeholder' in document.createElement('textarea'),
prototype = $.fn,
placeholder;
if (isInputSupported && isTextareaSupported) {
placeholder = prototype.placeholder = function() {
return this;
};
placeholder.input = placeholder.textarea = true;
} else {
placeholder = prototype.placeholder = function() {
return this
.filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
.not('.placeholder')
.bind('focus.placeholder', clearPlaceholder)
.bind('blur.placeholder', setPlaceholder)
.trigger('blur.placeholder').end();
};
placeholder.input = isInputSupported;
placeholder.textarea = isTextareaSupported;
$(function() {
// Look for forms
$(document).delegate('form', 'submit.placeholder', function() {
// Clear the placeholder values so they dont get submitted
var $inputs = $('.placeholder', this).each(clearPlaceholder);
setTimeout(function() {
$inputs.each(setPlaceholder);
}, 10);
});
});
// Clear placeholder values upon page reload
$(window).bind('unload.placeholder', function() {
$('.placeholder').val('');
});
}
function args(elem) {
// Return an object of element attributes
var newAttrs = {},
rinlinejQuery = /^jQuery\d+$/;
$.each(elem.attributes, function(i, attr) {
if (attr.specified && !rinlinejQuery.test(attr.name)) {
newAttrs[attr.name] = attr.value;
}
});
return newAttrs;
}
function clearPlaceholder() {
var $input = $(this);
if ($input.val() === $input.attr('placeholder') && $input.hasClass('placeholder')) {
if ($input.data('placeholder-password')) {
$input.hide().next().show().focus().attr('id', $input.removeAttr('id').data('placeholder-id'));
} else {
$input.val('').removeClass('placeholder');
}
}
}
function setPlaceholder() {
var $replacement,
$input = $(this),
$origInput = $input,
id = this.id;
if ($input.val() === '') {
if ($input.is(':password')) {
if (!$input.data('placeholder-textinput')) {
try {
$replacement = $input.clone().attr({ 'type': 'text' });
} catch(e) {
$replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
}
$replacement
.removeAttr('name')
// We could just use the `.data(obj)` syntax here, but that wouldnt work in pre-1.4.3 jQueries
.data('placeholder-password', true)
.data('placeholder-id', id)
.bind('focus.placeholder', clearPlaceholder);
$input
.data('placeholder-textinput', $replacement)
.data('placeholder-id', id)
.before($replacement);
}
$input = $input.removeAttr('id').hide().prev().attr('id', id).show();
}
$input.addClass('placeholder').val($input.attr('placeholder'));
} else {
$input.removeClass('placeholder');
}
}
}(this, document, jQuery));