-
Notifications
You must be signed in to change notification settings - Fork 0
/
textDrawer.js
93 lines (81 loc) · 2.89 KB
/
textDrawer.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
// create a sliding drawer effect for text-area elements
(function($){
// show all elements in a textarea box
$.fn.showTextDrawer = function(options){
var _defaults = {
expandText : "expand",
expandAnimEasing: "linear",
contractText : "contract",
contractAnimEasing: "linear",
animationSpeed : 700,
minSize : 200,
maxSize : 400,
zIndex : 200,
afterChange : function(){ return true; }
};
// extend the default settings
var _settings = $.extend({}, _defaults, options);
this.each(function(index, elm){
var $textarea = $(elm),
$parent = $textarea.parent(),
bindingBox = $('<div>').addClass("show-text-drawer"),
showAllButton = $('<a>').attr('href', '#').text("expand"),
textareaContainer = $('<div>').css({'z-index' : _settings.zIndex--,
'position' : 'absolute',
'width' : $textarea.width()
});
// remove textelement from DOM so we can work on it
$textarea.detach();
// append all to parent
$parent.append(
// binding box will be a placeholder for the textarea
bindingBox.append(
// textareaContainer will hold the textarea and the button to show/hide
textareaContainer.append($textarea, showAllButton)
)
);
// get the height difference
bindingBox.css('margin-bottom', textareaContainer.height() - $textarea.height());
// get new height / width from whole element
bindingBox.css({ 'min-width' : textareaContainer.outerWidth(), 'height' : $textarea.outerHeight()});
// set event handler on a elm
showAllButton.on("click.show-text-drawer", function(e){
// stop default behavior
e.preventDefault();
// show or hide box?
if(bindingBox.data('show-text-drawer')){
// animate the textbox to it's original form
$textarea.animate(
{'height': bindingBox.data('show-text-drawer')},
700,
_settings.contractAnimEasing,
function(){
bindingBox.removeData('show-text-drawer');
});
//set button text
$(this).text(_settings.expandText);
} else {
// get line number of textarea
var lineNum = $textarea.val().split("\n").length;
var textareaHeight = $textarea.height();
// test if show-all is greater than the maxSize limit
lineNum = lineNum * 20 > _settings.maxSize ? _settings.maxSize : lineNum * 20;
// make sure lineNum is larger than the our minimum display size
lineNum = lineNum < _settings.minSize ? _settings.minSize : lineNum;
// set data to textarea's default height
bindingBox.data('show-text-drawer', textareaHeight);
textareaContainer.css( 'min-width', $textarea.width() );
// textareaContainer.css({ position : 'absolute'});
// animate to our new height
$textarea.animate(
{'height': lineNum},
700,
_settings.expandAnimEasing
);
//set button text
$(this).text(_settings.contractText);
}
});
});
};
})( jQuery );