| 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 = Palette.createChild("div", parent, "textarea"); |
| 34 | this.title = Palette.createChild("div", this.elem, "title"); |
| 35 | this.textarea = Palette.createChild("textarea", this.elem, "editor"); |
| 36 | this.buttons = Palette.createChild("div", this.elem, "buttons"); |
| 37 | this.ok = Palette.createChild("button", this.buttons); |
| 38 | this.ok.textContent = "OK"; |
| 39 | this.cancel = Palette.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 | console.log("reposition", left, top); |
| 55 | textarea.elem.style.left = Math.max(left, 0) + "px"; |
| 56 | textarea.elem.style.top = Math.max(top, 0) + "px"; |
| 57 | } |
| 58 | |
| 59 | this.background = Palette.createChild("div", body, "background"); |
| 60 | this.background.id = "modalBackground"; |
| 61 | this.hide(); |
| 62 | } |
| 63 | |
| 64 | TextArea.prototype.cancelCallback = function() { |
| 65 | }; |
| 66 | |
| 67 | TextArea.prototype.okCallback = function(content) { |
| 68 | }; |
| 69 | |
| 70 | TextArea.prototype.show = function(title, content) { |
| 71 | this.title.textContent = title; |
| 72 | this.textarea.value = content; |
| 73 | |
| 74 | var height = 315; |
| 75 | var width = 445; |
| 76 | |
| 77 | |
| 78 | var sums = function(adds, subtracts, field) { |
| 79 | var total = 0; |
| 80 | for (var idx in adds) { |
| 81 | total += parseInt(adds[idx][field]); |
| 82 | } |
| 83 | for (var idx2 in subtracts) { |
| 84 | total -= parseInt(subtracts[idx2][field]); |
| 85 | } |
| 86 | return total; |
| 87 | } |
| 88 | this.elem.style.display = "block"; |
| 89 | this.background.style.display = "block"; |
| 90 | |
| 91 | this.elem.style.height = height + "px"; |
| 92 | this.elem.style.width = width + "px"; |
| 93 | |
| 94 | this.textarea.style.height = (-18 + sums([this.elem], [this.title, this.buttons], "offsetHeight")) + "px"; |
| 95 | this.textarea.style.width = (-16 + sums([this.elem], [ ], "offsetWidth")) + "px"; |
| 96 | |
| 97 | this.reposition(); |
| 98 | window.addEventListener('resize', this.reposition, false); |
| 99 | document.documentElement.addEventListener('onscroll', this.reposition); |
| 100 | } |
| 101 | |
| 102 | TextArea.prototype.hide = function() { |
| 103 | this.elem.style.display = "none"; |
| 104 | this.background.style.display = "none"; |
| 105 | window.removeEventListener("resize", this.reposition); |
| 106 | document.documentElement.removeEventListener("onscroll", this.reposition); |
| 107 | } |