Skip to content

Commit 37a4fb1

Browse files
Working autosize
1 parent 53ffc5f commit 37a4fb1

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

jquery.tagsinput.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,60 @@
1818

1919
var delimiter = new Array();
2020
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, '&amp;').replace(/\s/g,' ').replace(/</g, '&lt;').replace(/>/g, '&gt;');
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+
};
2447
$.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+
};
2775
$.fn.addTag = function(value,options) {
2876
var options = jQuery.extend({focus:false,callback:true},options);
2977
this.each(function() {
@@ -135,7 +183,9 @@
135183
'unique':true,
136184
removeWithBackspace:true,
137185
placeholderColor:'#666666',
138-
autosize: true
186+
autosize: true,
187+
comfortZone: 20,
188+
inputPadding: 6*2,
139189
},options);
140190

141191
this.each(function() {
@@ -181,7 +231,8 @@
181231
}
182232
if (settings.interactive) {
183233
$(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);
185236

186237
$(data.holder).bind('click',data,function(event) {
187238
$(event.data.fake_input).focus();

0 commit comments

Comments
 (0)