| 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 | this.height = 315; |
| 42 | this.width = 445; |
| 43 | |
| 44 | var textarea = this; |
| 45 | this.ok.onclick = function() { |
| 46 | textarea.hide(); |
| 47 | textarea.okCallback(textarea.textarea.value); |
| 48 | }; |
| 49 | this.cancel.onclick = function() { |
| 50 | textarea.hide(); |
| 51 | textarea.cancelCallback(); |
| 52 | }; |
| 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"; |
| 58 | } |
| 59 | |
| 60 | this.background = TextArea.createChild("div", body, "background"); |
| 61 | this.background.id = "modalBackground"; |
| 62 | this.hide(); |
| 63 | } |
| 64 | |
| 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); |
| 69 | if (className) { |
| 70 | elem.className = className; |
| 71 | } |
| 72 | return elem; |
| 73 | }; |
| 74 | |
| 75 | TextArea.prototype.cancelCallback = function() { |
| 76 | }; |
| 77 | |
| 78 | TextArea.prototype.okCallback = function(content) { |
| 79 | }; |
| 80 | |
| 81 | TextArea.prototype.show = function(title, content) { |
| 82 | this.title.textContent = title; |
| 83 | this.textarea.value = content; |
| 84 | |
| 85 | var sums = function(adds, subtracts, field) { |
| 86 | var total = 0; |
| 87 | for (var idx in adds) { |
| 88 | total += parseInt(adds[idx][field]); |
| 89 | } |
| 90 | for (var idx2 in subtracts) { |
| 91 | total -= parseInt(subtracts[idx2][field]); |
| 92 | } |
| 93 | return total; |
| 94 | } |
| 95 | this.elem.style.display = "block"; |
| 96 | this.background.style.display = "block"; |
| 97 | |
| 98 | this.elem.style.height = this.height + "px"; |
| 99 | this.elem.style.width = this.width + "px"; |
| 100 | |
| 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"; |
| 103 | |
| 104 | var textarea = this; |
| 105 | |
| 106 | this.keyDownListener_ = function(event) { |
| 107 | if(event.keyCode == 13) { // enter / return |
| 108 | textarea.hide(); |
| 109 | } |
| 110 | if(event.keyCode == 27) { // esc |
| 111 | textarea.hide(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Dygraph.addEvent(document, "keydown", this.keyDownListener_); |
| 116 | this.reposition(); |
| 117 | window.addEventListener('resize', this.reposition, false); |
| 118 | document.documentElement.addEventListener('onscroll', this.reposition); |
| 119 | } |
| 120 | |
| 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); |
| 128 | } |