|
18 | 18 |
|
19 | 19 | var delimiter = new Array();
|
20 | 20 | var tags_callbacks = new Array();
|
21 |
| - $.fn.doAutosize = function(options){ |
22 |
| - alert(JSON.stringify(options)); |
23 |
| - } |
| 21 | + $.fn.doAutosize = function(o){ |
| 22 | + var minWidth = $(this).data('minwidth'), |
| 23 | + maxWidth = $(this).data('maxwidth'), |
| 24 | + val = '', |
| 25 | + input = $(this), |
| 26 | + testSubject = $('#'+$(this).data('tester_id')); |
| 27 | + |
| 28 | + if (val === (val = input.val())) {return;} |
| 29 | + |
| 30 | + // Enter new content into testSubject |
| 31 | + var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(/</g, '<').replace(/>/g, '>'); |
| 32 | + testSubject.html(escaped); |
| 33 | + // Calculate new width + whether to change |
| 34 | + var testerWidth = testSubject.width(), |
| 35 | + newWidth = (testerWidth + o.comfortZone) >= minWidth ? testerWidth + o.comfortZone : minWidth, |
| 36 | + currentWidth = input.width(), |
| 37 | + isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) |
| 38 | + || (newWidth > minWidth && newWidth < maxWidth); |
| 39 | + |
| 40 | + // Animate width |
| 41 | + if (isValidWidthChange) { |
| 42 | + input.width(newWidth); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + }; |
24 | 47 | $.fn.resetAutosize = function(options){
|
25 |
| - alert(JSON.stringify(options)); |
26 |
| - } |
| 48 | + // alert(JSON.stringify(options)); |
| 49 | + var minWidth = $(this).data('minwidth') || options.minInputWidth || $(this).width(), |
| 50 | + maxWidth = $(this).data('maxwidth') || options.maxInputWidth || ($(this).closest('.tagsinput').width() - options.inputPadding), |
| 51 | + val = '', |
| 52 | + input = $(this), |
| 53 | + testSubject = $('<tester/>').css({ |
| 54 | + position: 'absolute', |
| 55 | + top: -9999, |
| 56 | + left: -9999, |
| 57 | + width: 'auto', |
| 58 | + fontSize: input.css('fontSize'), |
| 59 | + fontFamily: input.css('fontFamily'), |
| 60 | + fontWeight: input.css('fontWeight'), |
| 61 | + letterSpacing: input.css('letterSpacing'), |
| 62 | + whiteSpace: 'nowrap' |
| 63 | + }), |
| 64 | + testerId = $(this).attr('id')+'_autosize_tester'; |
| 65 | + if(! $('#'+testerId).length > 0){ |
| 66 | + testSubject.attr('id', testerId); |
| 67 | + testSubject.appendTo('body'); |
| 68 | + } |
| 69 | + |
| 70 | + input.data('minwidth', minWidth); |
| 71 | + input.data('maxwidth', maxWidth); |
| 72 | + input.data('tester_id', testerId); |
| 73 | + input.css('width', minWidth); |
| 74 | + }; |
27 | 75 | $.fn.addTag = function(value,options) {
|
28 | 76 | var options = jQuery.extend({focus:false,callback:true},options);
|
29 | 77 | this.each(function() {
|
|
135 | 183 | 'unique':true,
|
136 | 184 | removeWithBackspace:true,
|
137 | 185 | placeholderColor:'#666666',
|
138 |
| - autosize: true |
| 186 | + autosize: true, |
| 187 | + comfortZone: 20, |
| 188 | + inputPadding: 6*2, |
139 | 189 | },options);
|
140 | 190 |
|
141 | 191 | this.each(function() {
|
|
181 | 231 | }
|
182 | 232 | if (settings.interactive) {
|
183 | 233 | $(data.fake_input).val($(data.fake_input).attr('data-default'));
|
184 |
| - $(data.fake_input).css('color',settings.placeholderColor); |
| 234 | + $(data.fake_input).css('color',settings.placeholderColor); |
| 235 | + $(data.fake_input).resetAutosize(settings); |
185 | 236 |
|
186 | 237 | $(data.holder).bind('click',data,function(event) {
|
187 | 238 | $(event.data.fake_input).focus();
|
|
0 commit comments