jquery-mobile/js/jQuery.mobile.forms.textinput.js
2010-10-05 23:54:51 -04:00

69 lines
1.8 KiB
JavaScript

/*
* jQuery Mobile Framework : "customTextInput" plugin for text inputs, textareas (based on code from Filament Group,Inc)
* Copyright (c) jQuery Project
* Dual licensed under the MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
* Note: Code is in draft form and is subject to change
*/
(function($){
jQuery.fn.customTextInput = function(options){
return $(this).each(function(){
var input = $(this);
var o = $.extend({
search: input.is('[data-role="search"]')
//defaultTheme: "a"
}, options);
$('label[for='+input.attr('id')+']').addClass('ui-input-text');
input.addClass('ui-input-text');
var focusedEl = input;
//"search" input widget
if(o.search){
focusedEl = input.wrap('<div class="ui-input-search ui-btn-corner-all ui-body-c ui-btn-shadow ui-icon-search"></div>').parent();
var clearbtn = $('<a href="#" class="ui-input-clear" title="clear text">clear text</a>')
.buttonMarkup({icon: 'delete', iconpos: 'notext', corners:true, shadow:true})
.click(function(){
input.val('');
toggleClear();
return false;
})
.appendTo(focusedEl);
function toggleClear(){
if(input.val() == ''){
clearbtn.addClass('ui-input-clear-hidden');
}
else{
clearbtn.removeClass('ui-input-clear-hidden');
}
}
toggleClear();
input.keyup(toggleClear);
}
else{
input.addClass('ui-corner-all ui-body-c');
}
input
.focus(function(){
focusedEl.addClass('ui-focus');
})
.blur(function(){
focusedEl.removeClass('ui-focus');
});
//autogrow
if(input.is('textarea')){
input.keydown(function(){
if( input[0].offsetHeight < input[0].scrollHeight ){
input.css({height: input[0].scrollHeight + 10 });
}
})
}
});
};
})(jQuery);