Merge pull request #761 from justinsg/master
[dygraphs.git] / common / textarea.js
index ce45d8a..ec79e0d 100644 (file)
@@ -38,6 +38,8 @@ function TextArea(parent) {
   this.ok.textContent = "OK";
   this.cancel = TextArea.createChild("button", this.buttons);
   this.cancel.textContent = "Cancel";
+  this.height = 315;
+  this.width = 445;
 
   var textarea = this;
   this.ok.onclick = function() {
@@ -80,10 +82,6 @@ TextArea.prototype.show = function(title, content) {
   this.title.textContent = title;
   this.textarea.value = content;
 
-  var height = 315;
-  var width = 445;
-
-
   var sums = function(adds, subtracts, field) {
     var total = 0;
     for (var idx in adds) {
@@ -97,18 +95,32 @@ TextArea.prototype.show = function(title, content) {
   this.elem.style.display = "block";
   this.background.style.display = "block";
 
-  this.elem.style.height = height + "px";
-  this.elem.style.width = width + "px";
+  this.elem.style.height = this.height + "px";
+  this.elem.style.width = this.width + "px";
 
   this.textarea.style.height = (-18 + sums([this.elem], [this.title, this.buttons], "offsetHeight")) + "px";
   this.textarea.style.width = (-16 + sums([this.elem], [ ], "offsetWidth")) + "px";
 
+  var textarea = this;
+
+  this.keyDownListener_ = function(event) {
+    if(event.keyCode == 13) { // enter / return
+      textarea.hide();
+    }
+    if(event.keyCode == 27) { // esc
+      textarea.hide();
+    }
+  }
+
+  document.addEventListener("keydown", this.keyDownListener_);
   this.reposition();
   window.addEventListener('resize', this.reposition, false);
   document.documentElement.addEventListener('onscroll', this.reposition);
 }
 
 TextArea.prototype.hide = function() {
+  document.removeEventListener('keypress', this.keyDownListener_);
+  this.keyDownListener_ = null;
   this.elem.style.display = "none";
   this.background.style.display = "none";
   window.removeEventListener("resize", this.reposition);