1 // Copyright (c) 2012 Google, Inc.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * @fileoverview Dygraphs options palette text area.
24 * @author konigsberg@google.com (Robert Konigsberg)
28 function TextArea(parent
) {
29 var body
= document
.getElementsByTagName("body")[0];
33 this.elem
= TextArea
.createChild("div", parent
, "textarea");
34 this.title
= TextArea
.createChild("div", this.elem
, "title");
35 this.textarea
= TextArea
.createChild("textarea", this.elem
, "editor");
36 this.buttons
= TextArea
.createChild("div", this.elem
, "buttons");
37 this.ok
= TextArea
.createChild("button", this.buttons
);
38 this.ok
.textContent
= "OK";
39 this.cancel
= TextArea
.createChild("button", this.buttons
);
40 this.cancel
.textContent
= "Cancel";
45 this.ok
.onclick
= function() {
47 textarea
.okCallback(textarea
.textarea
.value
);
49 this.cancel
.onclick
= function() {
51 textarea
.cancelCallback();
53 this.reposition
= function() {
54 var left
= (document
.documentElement
.clientWidth
- textarea
.elem
.offsetWidth
) / 2;
55 var top
= (document
.documentElement
.clientHeight
- textarea
.elem
.offsetHeight
) / 2;
56 textarea
.elem
.style
.left
= Math
.max(left
, 0) + "px";
57 textarea
.elem
.style
.top
= Math
.max(top
, 0) + "px";
60 this.background
= TextArea
.createChild("div", body
, "background");
61 this.background
.id
= "modalBackground";
65 /* I think this is the third place I've copied this function */
66 TextArea
.createChild
= function(type
, parent
, className
) {
67 var elem
= document
.createElement(type
);
68 parent
.appendChild(elem
);
70 elem
.className
= className
;
75 TextArea
.prototype.cancelCallback
= function() {
78 TextArea
.prototype.okCallback
= function(content
) {
81 TextArea
.prototype.show
= function(title
, content
) {
82 this.title
.textContent
= title
;
83 this.textarea
.value
= content
;
85 var sums
= function(adds
, subtracts
, field
) {
87 for (var idx
in adds
) {
88 total
+= parseInt(adds
[idx
][field
]);
90 for (var idx2
in subtracts
) {
91 total
-= parseInt(subtracts
[idx2
][field
]);
95 this.elem
.style
.display
= "block";
96 this.background
.style
.display
= "block";
98 this.elem
.style
.height
= this.height
+ "px";
99 this.elem
.style
.width
= this.width
+ "px";
101 this.textarea
.style
.height
= (-18 + sums([this.elem
], [this.title
, this.buttons
], "offsetHeight")) + "px";
102 this.textarea
.style
.width
= (-16 + sums([this.elem
], [ ], "offsetWidth")) + "px";
106 this.keyDownListener_
= function(event
) {
107 if(event
.keyCode
== 13) { // enter / return
110 if(event
.keyCode
== 27) { // esc
115 Dygraph
.addEvent(document
, "keydown", this.keyDownListener_
);
117 window
.addEventListener('resize', this.reposition
, false);
118 document
.documentElement
.addEventListener('onscroll', this.reposition
);
121 TextArea
.prototype.hide
= function() {
122 Dygraph
.removeEvent(document
, "keypress", this.keyDownListener_
);
123 this.keyDownListener_
= null;
124 this.elem
.style
.display
= "none";
125 this.background
.style
.display
= "none";
126 window
.removeEventListener("resize", this.reposition
);
127 document
.documentElement
.removeEventListener("onscroll", this.reposition
);