This repository was archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-autoexpand.js
117 lines (104 loc) · 3.39 KB
/
jquery-autoexpand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright © 2013 DB Medialab AS http://medialaben.no
(function($) {
'use strict';
//!Public methods
var pub = {
//Init the editor. Takes options.
init: function(options) {
return this.each(function(){
var $this = $(this);
//If the plugin hasn't been initialized yet save all our settings
if(!$this.data('autoexpand')){
$this.data('autoexpand', $.extend({}, $.fn.autoexpand.defaults, options) );
//Read options from the data-attr
if( $this.attr('data-autoexpand-options') !== undefined ){
options = $this.attr('data-autoexpand-options').split(';');
$.each(options, function( index, value ){
var p = value.split(':');
if (/true/i.test(p[1])) p[1] = true;
if (/false/i.test(p[1])) p[1] = false;
if(! isNaN (p[1]-0) && p[1] !== null && p[1] !== "" && p[1] !== false && p[1] !== true){
p[1] = parseInt(p[1], 10);
}
if (p.length === 2 && p[0].length > 0){
p[0] = $.trim(p[0]);
p[1] = $.trim(p[1]);
$this.data('autoexpand')[p[0]] = p[1];
}
});
$this.data('autoexpand');
}
}
//Store the original height so this can be applied again if we destroy the plugin
$this.data('autoexpand').originalHeight = $this.outerHeight();
if($this.data('autoexpand').resizeOnInit) pub.resize.apply($this);
$this.on('keyup focus', function(){
pub.resize.apply($this);
});
if( $this.data('autoexpand').actAsInput ){
$this.on('keypress', function(e){
if((e.keyCode || e.which) == 13) {
return false;
}
});
$this.on('keyup', function(e){
if((e.keyCode || e.which) == 13) {
$this.parents('form').submit();
}
});
$this.on('paste', function(e){
setTimeout(function(){
$this.val( $this.val().replace(/(\r\n|\n|\r)/gm," ") );
pub.resize.apply($this);
}, 20);
});
}
});
},
resize: function(){
return this.each(function(){
var $this = $(this);
var data = $this.data('autoexpand');
//The height is set to zero so that we can also handle shrinking of the field.
$this.css({ height: 0 });
//Set the height to the scrollHeight + the width of the top and bottom border
var height = $this[0].scrollHeight + (parseFloat($this.css('border-top-width')) + parseFloat($this.css('border-bottom-width')));
//Check if we are within our boundaries
if( data.min !== 0 && height < data.min ){
height = data.min;
}
if( data.max !== 0 && height > data.max ){
height = data.max;
$this.css({ overflow: 'auto' });
}
$this.css({ height: height });
});
},
destroy: function(){
return this.each(function(){
var $this = $(this);
$this.unbind('keyup focus');
$this.css({ height: $this.data('autoexpand').originalHeight });
$this.removeData('autoexpand');
});
}
};
//!Method calling logic
$.fn.autoexpand = function(method){
if ( pub[method] ) {
return pub[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return pub.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.autoexpand' );
}
};
// !Default options
$.fn.autoexpand.defaults = {
resizeOnInit: false,
actAsInput: false,
min: 0,
max: 0
};
$('textarea[data-autoexpand]').autoexpand();
}(jQuery));