changeset 79:50b8738d6428

Use (modified) autosize to automatically resize textarea.
author Edho Arief <edho@myconan.net>
date Mon, 08 Oct 2012 14:00:36 +0700
parents 6ae3333a0251
children b1de54c37b70
files app/assets/javascripts/application.js app/assets/javascripts/pastes.js app/assets/stylesheets/box.css app/assets/stylesheets/pastes.css lib/assets/javascripts/jquery.autosize.js
diffstat 5 files changed, 189 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/app/assets/javascripts/application.js	Sun Oct 07 16:05:30 2012 +0700
+++ b/app/assets/javascripts/application.js	Mon Oct 08 14:00:36 2012 +0700
@@ -13,4 +13,5 @@
 //= require jquery
 //= require jquery_ujs
 //= require prettify
+//= require jquery.autosize
 //= require_tree .
--- a/app/assets/javascripts/pastes.js	Sun Oct 07 16:05:30 2012 +0700
+++ b/app/assets/javascripts/pastes.js	Mon Oct 08 14:00:36 2012 +0700
@@ -7,5 +7,6 @@
   return false;
 });
 $(document).ready(function() {
+  $('#paste_paste').autosize({ className: 'mirrored_text row-fluid', append: "\n"});
   $('#paste_paste').focus();
 })
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/assets/stylesheets/box.css	Mon Oct 08 14:00:36 2012 +0700
@@ -0,0 +1,5 @@
+* { 
+  -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
+  -moz-box-sizing: border-box;    /* Firefox, other Gecko */
+  box-sizing: border-box;         /* Opera/IE 8+ */
+}
--- a/app/assets/stylesheets/pastes.css	Sun Oct 07 16:05:30 2012 +0700
+++ b/app/assets/stylesheets/pastes.css	Mon Oct 08 14:00:36 2012 +0700
@@ -1,4 +1,4 @@
-textarea#paste_paste {
+textarea#paste_paste, .mirrored_text {
   font-family: Monaco, Menlo, Consolas, "Courier New", monospace;
-  resize: vertical;
+  resize: none;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/assets/javascripts/jquery.autosize.js	Mon Oct 08 14:00:36 2012 +0700
@@ -0,0 +1,180 @@
+// Autosize 1.13 - jQuery plugin for textareas
+// (c) 2012 Jack Moore - jacklmoore.com
+// license: www.opensource.org/licenses/mit-license.php
+
+(function ($) {
+	var
+	defaults = {
+		className: 'autosizejs',
+		append: "",
+		callback: false
+	},
+	hidden = 'hidden',
+	borderBox = 'border-box',
+	lineHeight = 'lineHeight',
+	copy = '<textarea tabindex="-1" style="position:absolute; top:-9999px; left:-9999px; right:auto; bottom:auto; word-wrap:break-word; height:0 !important; min-height:0 !important; overflow:hidden;"/>',
+	// line-height is omitted because IE7/IE8 doesn't return the correct value.
+	copyStyle = [
+		'fontFamily',
+		'fontSize',
+		'fontWeight',
+		'fontStyle',
+		'letterSpacing',
+		'textTransform',
+		'wordSpacing',
+		'textIndent'
+	],
+	oninput = 'oninput',
+	onpropertychange = 'onpropertychange',
+	test = $(copy)[0];
+
+	// For testing support in old FireFox
+	test.setAttribute(oninput, "return");
+
+	if ($.isFunction(test[oninput]) || onpropertychange in test) {
+
+		// test that line-height can be accurately copied to avoid
+		// incorrect value reporting in old IE and old Opera
+		$(test).css(lineHeight, '99px');
+		if ($(test).css(lineHeight) === '99px') {
+			copyStyle.push(lineHeight);
+		}
+
+		$.fn.autosize = function (options) {
+			options = $.extend({}, defaults, options || {});
+
+			return this.each(function () {
+				var
+				ta = this,
+				$ta = $(ta),
+				mirror,
+				minHeight = $ta.height(),
+				maxHeight = parseInt($ta.css('maxHeight'), 10),
+				active,
+				i = copyStyle.length,
+				resize,
+				boxOffset = 0,
+				value = ta.value,
+				callback = $.isFunction(options.callback);
+
+				if ($ta.css('box-sizing') === borderBox || $ta.css('-moz-box-sizing') === borderBox || $ta.css('-webkit-box-sizing') === borderBox){
+					boxOffset = $ta.outerHeight() - $ta.height();
+				}
+
+				if ($ta.data('mirror') || $ta.data('ismirror')) {
+					// if autosize has already been applied, exit.
+					// if autosize is being applied to a mirror element, exit.
+					return;
+				} else {
+					mirror = $(copy).data('ismirror', true).addClass(options.className)[0];
+
+					resize = $ta.css('resize') === 'none' ? 'none' : 'horizontal';
+
+					$ta.data('mirror', $(mirror)).css({
+						overflow: hidden,
+						overflowY: hidden,
+						wordWrap: 'break-word',
+						resize: resize
+					});
+				}
+
+				// Opera returns '-1px' when max-height is set to 'none'.
+				maxHeight = maxHeight && maxHeight > 0 ? maxHeight : 9e4;
+
+				// Using mainly bare JS in this function because it is going
+				// to fire very often while typing, and needs to very efficient.
+				function adjust() {
+					var height, overflow, original;
+
+					// the active flag keeps IE from tripping all over itself.  Otherwise
+					// actions in the adjust function will cause IE to call adjust again.
+					if (!active) {
+						active = true;
+						mirror.value = ta.value + options.append;
+						mirror.style.overflowY = ta.style.overflowY;
+						original = parseInt(ta.style.height,10);
+
+						// Update the width in case the original textarea width has changed
+						mirror.style.width = $ta.css('width');
+
+						// Needed for IE to reliably return the correct scrollHeight
+						mirror.scrollTop = 0;
+
+						// Set a very high value for scrollTop to be sure the
+						// mirror is scrolled all the way to the bottom.
+						mirror.scrollTop = 9e4;
+
+						height = mirror.scrollTop;
+						overflow = hidden;
+						if (height > maxHeight) {
+							height = maxHeight;
+							overflow = 'scroll';
+						} else if (height < minHeight) {
+							height = minHeight;
+						}
+						height += boxOffset;
+						ta.style.overflowY = overflow;
+
+						if (original !== height) {
+							ta.style.height = height + 'px';
+							if (callback) {
+								options.callback.call(ta);
+							}
+						}
+						
+						// This small timeout gives IE a chance to draw it's scrollbar
+						// before adjust can be run again (prevents an infinite loop).
+						setTimeout(function () {
+							active = false;
+						}, 1);
+					}
+				}
+
+				// mirror is a duplicate textarea located off-screen that
+				// is automatically updated to contain the same text as the
+				// original textarea.  mirror always has a height of 0.
+				// This gives a cross-browser supported way getting the actual
+				// height of the text, through the scrollTop property.
+				while (i--) {
+					mirror.style[copyStyle[i]] = $ta.css(copyStyle[i]);
+				}
+
+				$('body').append(mirror);
+
+				if (onpropertychange in ta) {
+					if (oninput in ta) {
+						// Detects IE9.  IE9 does not fire onpropertychange or oninput for deletions,
+						// so binding to onkeyup to catch most of those occassions.  There is no way that I
+						// know of to detect something like 'cut' in IE9.
+						ta[oninput] = ta.onkeyup = adjust;
+					} else {
+						// IE7 / IE8
+						ta[onpropertychange] = adjust;
+					}
+				} else {
+					// Modern Browsers
+					ta[oninput] = adjust;
+
+					// The textarea overflow is now hidden.  But Chrome doesn't reflow the text after the scrollbars are removed.
+					// This is a hack to get Chrome to reflow it's text.
+					ta.value = '';
+					ta.value = value;
+				}
+
+				$(window).resize(adjust);
+
+				// Allow for manual triggering if needed.
+				$ta.bind('autosize', adjust);
+
+				// Call adjust in case the textarea already contains text.
+				adjust();
+			});
+		};
+	} else {
+		// Makes no changes for older browsers (FireFox3- and Safari4-)
+		$.fn.autosize = function (callback) {
+			return this;
+		};
+	}
+
+}(jQuery));