/*!
* jquery placeholder plugin v2.3.1
* https://github.com/mathiasbynens/jquery-placeholder
*
* copyright 2011, 2015 mathias bynens
* released under the mit license
*/
//
(function(factory) {
if (typeof define === 'function' && define.amd) {
// amd
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
factory(require('jquery'));
} else {
// browser globals
factory(jquery);
}
}(function($) {
/****
* allows plugin behavior simulation in modern browsers for easier debugging.
* when setting to true, use attribute "placeholder-x" rather than the usual "placeholder" in your inputs/textareas
* i.e.
*/
var debugmode = false;
// opera mini v7 doesn't support placeholder although its dom seems to indicate so
var isoperamini = object.prototype.tostring.call(window.operamini) === '[object operamini]';
var isinputsupported = 'placeholder' in document.createelement('input') && !isoperamini && !debugmode;
var istextareasupported = 'placeholder' in document.createelement('textarea') && !isoperamini && !debugmode;
var valhooks = $.valhooks;
var prophooks = $.prophooks;
var hooks;
var placeholder;
var settings = {};
if (isinputsupported && istextareasupported) {
placeholder = $.fn.placeholder = function() {
return this;
};
placeholder.input = true;
placeholder.textarea = true;
} else {
placeholder = $.fn.placeholder = function(options) {
var defaults = {customclass: 'placeholder'};
settings = $.extend({}, defaults, options);
return this.filter((isinputsupported ? 'textarea' : ':input') + '[' + (debugmode ? 'placeholder-x' : 'placeholder') + ']')
.not('.'+settings.customclass)
.not(':radio, :checkbox, [type=hidden]')
.bind({
'focus.placeholder': clearplaceholder,
'blur.placeholder': setplaceholder
})
.data('placeholder-enabled', true)
.trigger('blur.placeholder');
};
placeholder.input = isinputsupported;
placeholder.textarea = istextareasupported;
hooks = {
'get': function(element) {
var $element = $(element);
var $passwordinput = $element.data('placeholder-password');
if ($passwordinput) {
return $passwordinput[0].value;
}
return $element.data('placeholder-enabled') && $element.hasclass(settings.customclass) ? '' : element.value;
},
'set': function(element, value) {
var $element = $(element);
var $replacement;
var $passwordinput;
if (value !== '') {
$replacement = $element.data('placeholder-textinput');
$passwordinput = $element.data('placeholder-password');
if ($replacement) {
clearplaceholder.call($replacement[0], true, value) || (element.value = value);
$replacement[0].value = value;
} else if ($passwordinput) {
clearplaceholder.call(element, true, value) || ($passwordinput[0].value = value);
element.value = value;
}
}
if (!$element.data('placeholder-enabled')) {
element.value = value;
return $element;
}
if (value === '') {
element.value = value;
// setting the placeholder causes problems if the element continues to have focus.
if (element != safeactiveelement()) {
// we can't use `triggerhandler` here because of dummy text/password inputs :(
setplaceholder.call(element);
}
} else {
if ($element.hasclass(settings.customclass)) {
clearplaceholder.call(element);
}
element.value = value;
}
// `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#l2363
return $element;
}
};
if (!isinputsupported) {
valhooks.input = hooks;
prophooks.value = hooks;
}
if (!istextareasupported) {
valhooks.textarea = hooks;
prophooks.value = hooks;
}
$(function() {
// look for forms
$(document).delegate('form', 'submit.placeholder', function() {
// clear the placeholder values so they don't get submitted
var $inputs = $('.'+settings.customclass, this).each(function() {
clearplaceholder.call(this, true, '');
});
settimeout(function() {
$inputs.each(setplaceholder);
}, 10);
});
});
// clear placeholder values upon page reload
$(window).bind('beforeunload.placeholder', function() {
var clearplaceholders = true;
try {
// prevent ie javascript:void(0) anchors from causing cleared values
if (document.activeelement.tostring() === 'javascript:void(0)') {
clearplaceholders = false;
}
} catch (exception) { }
if (clearplaceholders) {
$('.'+settings.customclass).each(function() {
this.value = '';
});
}
});
}
function args(elem) {
// return an object of element attributes
var newattrs = {};
var 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(event, value) {
var input = this;
var $input = $(this);
if (input.value === $input.attr((debugmode ? 'placeholder-x' : 'placeholder')) && $input.hasclass(settings.customclass)) {
input.value = '';
$input.removeclass(settings.customclass);
if ($input.data('placeholder-password')) {
$input = $input.hide().nextall('input[type="password"]:first').show().attr('id', $input.removeattr('id').data('placeholder-id'));
// if `clearplaceholder` was called from `$.valhooks.input.set`
if (event === true) {
$input[0].value = value;
return value;
}
$input.focus();
} else {
input == safeactiveelement() && input.select();
}
}
}
function setplaceholder(event) {
var $replacement;
var input = this;
var $input = $(this);
var id = input.id;
// if the placeholder is activated, triggering blur event (`$input.trigger('blur')`) should do nothing.
if (event && event.type === 'blur' && $input.hasclass(settings.customclass)) {
return;
}
if (input.value === '') {
if (input.type === 'password') {
if (!$input.data('placeholder-textinput')) {
try {
$replacement = $input.clone().prop({ 'type': 'text' });
} catch(e) {
$replacement = $('').attr($.extend(args(this), { 'type': 'text' }));
}
$replacement
.removeattr('name')
.data({
'placeholder-enabled': true,
'placeholder-password': $input,
'placeholder-id': id
})
.bind('focus.placeholder', clearplaceholder);
$input
.data({
'placeholder-textinput': $replacement,
'placeholder-id': id
})
.before($replacement);
}
input.value = '';
$input = $input.removeattr('id').hide().prevall('input[type="text"]:first').attr('id', $input.data('placeholder-id')).show();
} else {
var $passwordinput = $input.data('placeholder-password');
if ($passwordinput) {
$passwordinput[0].value = '';
$input.attr('id', $input.data('placeholder-id')).show().nextall('input[type="password"]:last').hide().removeattr('id');
}
}
$input.addclass(settings.customclass);
$input[0].value = $input.attr((debugmode ? 'placeholder-x' : 'placeholder'));
} else {
$input.removeclass(settings.customclass);
}
}
function safeactiveelement() {
// avoid ie9 `document.activeelement` of death
try {
return document.activeelement;
} catch (exception) {}
}
}));