Move textarea to common area.
[dygraphs.git] / common / textarea.js
1 // Copyright (c) 2012 Google, Inc.
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // THE SOFTWARE.
20
21 /**
22 * @fileoverview Dygraphs options palette text area.
23 *
24 * @author konigsberg@google.com (Robert Konigsberg)
25 */
26 "use strict";
27
28 function TextArea(parent) {
29 var body = document.getElementsByTagName("body")[0];
30 if (!parent) {
31 parent = body;
32 }
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";
41
42 var textarea = this;
43 this.ok.onclick = function() {
44 textarea.hide();
45 textarea.okCallback(textarea.textarea.value);
46 };
47 this.cancel.onclick = function() {
48 textarea.hide();
49 textarea.cancelCallback();
50 };
51 this.reposition = function() {
52 var left = (document.documentElement.clientWidth - textarea.elem.offsetWidth) / 2;
53 var top = (document.documentElement.clientHeight - textarea.elem.offsetHeight) / 2;
54 textarea.elem.style.left = Math.max(left, 0) + "px";
55 textarea.elem.style.top = Math.max(top, 0) + "px";
56 }
57
58 this.background = TextArea.createChild("div", body, "background");
59 this.background.id = "modalBackground";
60 this.hide();
61 }
62
63 /* I think this is the third place I've copied this function */
64 TextArea.createChild = function(type, parent, className) {
65 var elem = document.createElement(type);
66 parent.appendChild(elem);
67 if (className) {
68 elem.className = className;
69 }
70 return elem;
71 };
72
73 TextArea.prototype.cancelCallback = function() {
74 };
75
76 TextArea.prototype.okCallback = function(content) {
77 };
78
79 TextArea.prototype.show = function(title, content) {
80 this.title.textContent = title;
81 this.textarea.value = content;
82
83 var height = 315;
84 var width = 445;
85
86
87 var sums = function(adds, subtracts, field) {
88 var total = 0;
89 for (var idx in adds) {
90 total += parseInt(adds[idx][field]);
91 }
92 for (var idx2 in subtracts) {
93 total -= parseInt(subtracts[idx2][field]);
94 }
95 return total;
96 }
97 this.elem.style.display = "block";
98 this.background.style.display = "block";
99
100 this.elem.style.height = height + "px";
101 this.elem.style.width = width + "px";
102
103 this.textarea.style.height = (-18 + sums([this.elem], [this.title, this.buttons], "offsetHeight")) + "px";
104 this.textarea.style.width = (-16 + sums([this.elem], [ ], "offsetWidth")) + "px";
105
106 this.reposition();
107 window.addEventListener('resize', this.reposition, false);
108 document.documentElement.addEventListener('onscroll', this.reposition);
109 }
110
111 TextArea.prototype.hide = function() {
112 this.elem.style.display = "none";
113 this.background.style.display = "none";
114 window.removeEventListener("resize", this.reposition);
115 document.documentElement.removeEventListener("onscroll", this.reposition);
116 }