Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Google Inc. |
| 3 | * Licensed to The Android Open Source Project. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * Returns the page offset of an element. |
| 21 | * |
| 22 | * @param {Element} element The element to return the page offset for. |
| 23 | * @return {left: number, top: number} A tuple including a left and top value representing |
| 24 | * the page offset of the element. |
| 25 | */ |
| 26 | function getTotalOffset(el) { |
| 27 | var result = { |
| 28 | left: 0, |
| 29 | top: 0 |
| 30 | }; |
| 31 | var parent = el; |
| 32 | |
| 33 | while (parent) { |
| 34 | result.left += parent.offsetLeft; |
| 35 | result.top += parent.offsetTop; |
| 36 | parent = parent.offsetParent; |
| 37 | } |
| 38 | |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | function toggleQuotedText(e) { |
| 43 | var toggleElement = e.target; |
| 44 | var elidedTextElement = toggleElement.nextSibling; |
| 45 | var isHidden = getComputedStyle(elidedTextElement).display == 'none'; |
| 46 | toggleElement.innerHTML = isHidden ? MSG_HIDE_ELIDED : MSG_SHOW_ELIDED; |
| 47 | elidedTextElement.style.display = isHidden ? 'block' : 'none'; |
| 48 | } |
| 49 | |
| 50 | function collapseQuotedText() { |
| 51 | var i; |
| 52 | var elements = document.getElementsByClassName("elided-text"); |
| 53 | var elidedElement, toggleElement; |
| 54 | for (i = 0; i < elements.length; i++) { |
| 55 | elidedElement = elements[i]; |
| 56 | toggleElement = document.createElement("div"); |
| 57 | toggleElement.display = "mail-elided-text"; |
| 58 | toggleElement.innerHTML = MSG_SHOW_ELIDED; |
| 59 | toggleElement.onclick = toggleQuotedText; |
| 60 | elidedElement.parentNode.insertBefore(toggleElement, elidedElement); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function shrinkWideMessages() { |
| 65 | var i; |
| 66 | var elements = document.getElementsByClassName("mail-message-content"); |
| 67 | var messageElement; |
| 68 | var documentWidth = document.documentElement.offsetWidth; |
| 69 | var scale; |
| 70 | for (i = 0; i < elements.length; i++) { |
| 71 | messageElement = elements[i]; |
| 72 | if (messageElement.scrollWidth > documentWidth) { |
| 73 | scale = documentWidth / messageElement.scrollWidth; |
| 74 | |
| 75 | // TODO: 'zoom' is nice because it does a proper layout, but WebView seems to clamp the |
| 76 | // minimum 'zoom' level. |
| 77 | if (false) { |
| 78 | // TODO: this alternative works well in Chrome but doesn't work in WebView. |
| 79 | messageElement.style.webkitTransformOrigin = "left top"; |
| 80 | messageElement.style.webkitTransform = "scale(" + scale + ")"; |
| 81 | messageElement.style.height = (messageElement.offsetHeight * scale) + "px"; |
| 82 | messageElement.style.overflowX = "visible"; |
| 83 | } else { |
| 84 | messageElement.style.zoom = documentWidth / messageElement.scrollWidth; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | function measurePositions() { |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 91 | var headerHeights; |
Andy Huang | 2ffaeab | 2012-02-27 19:43:46 -0800 | [diff] [blame] | 92 | var headerBottoms; |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 93 | var h; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 94 | var i; |
| 95 | var len; |
| 96 | |
Andy Huang | 2ffaeab | 2012-02-27 19:43:46 -0800 | [diff] [blame] | 97 | var headers = document.querySelectorAll(".spacer"); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 98 | |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 99 | headerHeights = new Array(headers.length); |
Andy Huang | 2ffaeab | 2012-02-27 19:43:46 -0800 | [diff] [blame] | 100 | headerBottoms = new Array(headers.length); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 101 | for (i = 0, len = headers.length; i < len; i++) { |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 102 | h = headers[i].offsetHeight; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 103 | // addJavascriptInterface handler only supports string arrays |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 104 | headerHeights[i] = "" + h; |
| 105 | headerBottoms[i] = "" + (getTotalOffset(headers[i]).top + h); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame^] | 108 | window.mail.onWebContentGeometryChange(headerBottoms, headerHeights); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | collapseQuotedText(); |
| 112 | shrinkWideMessages(); |
| 113 | measurePositions(); |
| 114 | |