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 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 18 | var BLOCKED_SRC_ATTR = "blocked-src"; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 19 | |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame^] | 20 | // the set of Elements currently scheduled for processing in handleAllImageLoads |
| 21 | // this is an Array, but we treat it like a Set and only insert unique items |
| 22 | var gImageLoadElements = []; |
| 23 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Returns the page offset of an element. |
| 26 | * |
| 27 | * @param {Element} element The element to return the page offset for. |
| 28 | * @return {left: number, top: number} A tuple including a left and top value representing |
| 29 | * the page offset of the element. |
| 30 | */ |
| 31 | function getTotalOffset(el) { |
| 32 | var result = { |
| 33 | left: 0, |
| 34 | top: 0 |
| 35 | }; |
| 36 | var parent = el; |
| 37 | |
| 38 | while (parent) { |
| 39 | result.left += parent.offsetLeft; |
| 40 | result.top += parent.offsetTop; |
| 41 | parent = parent.offsetParent; |
| 42 | } |
| 43 | |
| 44 | return result; |
| 45 | } |
| 46 | |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Walks up the DOM starting at a given element, and returns an element that has the |
| 49 | * specified class name or null. |
| 50 | */ |
| 51 | function up(el, className) { |
| 52 | var parent = el; |
| 53 | while (parent) { |
| 54 | if (parent.classList && parent.classList.contains(className)) { |
| 55 | break; |
| 56 | } |
| 57 | parent = parent.parentNode; |
| 58 | } |
| 59 | return parent || null; |
| 60 | } |
| 61 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 62 | function toggleQuotedText(e) { |
| 63 | var toggleElement = e.target; |
| 64 | var elidedTextElement = toggleElement.nextSibling; |
| 65 | var isHidden = getComputedStyle(elidedTextElement).display == 'none'; |
| 66 | toggleElement.innerHTML = isHidden ? MSG_HIDE_ELIDED : MSG_SHOW_ELIDED; |
| 67 | elidedTextElement.style.display = isHidden ? 'block' : 'none'; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 68 | |
| 69 | // Revealing the elided text should normalize it to fit-width to prevent |
| 70 | // this message from blowing out the conversation width. |
| 71 | if (isHidden) { |
| 72 | normalizeElementWidths([elidedTextElement]); |
| 73 | } |
| 74 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 75 | measurePositions(); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 78 | function collapseAllQuotedText() { |
| 79 | collapseQuotedText(document.documentElement); |
| 80 | } |
| 81 | |
| 82 | function collapseQuotedText(elt) { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 83 | var i; |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 84 | var elements = elt.getElementsByClassName("elided-text"); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 85 | var elidedElement, toggleElement; |
| 86 | for (i = 0; i < elements.length; i++) { |
| 87 | elidedElement = elements[i]; |
| 88 | toggleElement = document.createElement("div"); |
Andy Huang | 82119af | 2012-06-26 17:15:40 -0700 | [diff] [blame] | 89 | toggleElement.className = "mail-elided-text"; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 90 | toggleElement.innerHTML = MSG_SHOW_ELIDED; |
| 91 | toggleElement.onclick = toggleQuotedText; |
| 92 | elidedElement.parentNode.insertBefore(toggleElement, elidedElement); |
| 93 | } |
| 94 | } |
| 95 | |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 96 | function normalizeAllMessageWidths() { |
| 97 | normalizeElementWidths(document.querySelectorAll(".expanded > .mail-message-content")); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Normalizes the width of all elements supplied to the document body's overall width. |
| 102 | * Narrower elements are zoomed in, and wider elements are zoomed out. |
| 103 | * This method is idempotent. |
| 104 | */ |
| 105 | function normalizeElementWidths(elements) { |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 106 | var i; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 107 | var el; |
Andy Huang | 256b35c | 2012-08-22 15:19:13 -0700 | [diff] [blame] | 108 | var documentWidth = document.body.offsetWidth; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 109 | var newZoom, oldZoom; |
Andy Huang | 256b35c | 2012-08-22 15:19:13 -0700 | [diff] [blame] | 110 | |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 111 | for (i = 0; i < elements.length; i++) { |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 112 | el = elements[i]; |
| 113 | oldZoom = el.style.zoom; |
| 114 | // reset any existing normalization |
| 115 | if (oldZoom) { |
| 116 | el.style.zoom = 1; |
| 117 | } |
| 118 | newZoom = documentWidth / el.scrollWidth; |
| 119 | el.style.zoom = newZoom; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 123 | function hideUnsafeImages() { |
| 124 | var i, bodyCount; |
| 125 | var j, imgCount; |
| 126 | var body, image; |
| 127 | var images; |
| 128 | var showImages; |
| 129 | var bodies = document.getElementsByClassName("mail-message-content"); |
| 130 | for (i = 0, bodyCount = bodies.length; i < bodyCount; i++) { |
| 131 | body = bodies[i]; |
| 132 | showImages = body.classList.contains("mail-show-images"); |
| 133 | |
| 134 | images = body.getElementsByTagName("img"); |
| 135 | for (j = 0, imgCount = images.length; j < imgCount; j++) { |
| 136 | image = images[j]; |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 137 | rewriteRelativeImageSrc(image); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 138 | attachImageLoadListener(image); |
| 139 | // TODO: handle inline image attachments for all supported protocols |
| 140 | if (!showImages) { |
| 141 | blockImage(image); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Changes relative paths to absolute path by pre-pending the account uri |
| 149 | * @param {Element} imgElement Image for which the src path will be updated. |
| 150 | */ |
| 151 | function rewriteRelativeImageSrc(imgElement) { |
| 152 | var src = imgElement.src; |
Paul Westbrook | cebcc64 | 2012-08-08 10:06:04 -0700 | [diff] [blame] | 153 | |
| 154 | // DOC_BASE_URI will always be a unique x-thread:// uri for this particular conversation |
| 155 | if (src.indexOf(DOC_BASE_URI) == 0 && (DOC_BASE_URI != CONVERSATION_BASE_URI)) { |
| 156 | // The conversation specifies a different base uri than the document |
| 157 | src = CONVERSATION_BASE_URI + src.substring(DOC_BASE_URI.length); |
| 158 | imgElement.src = src; |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 159 | } |
| 160 | }; |
| 161 | |
| 162 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 163 | function attachImageLoadListener(imageElement) { |
| 164 | // Reset the src attribute to the empty string because onload will only fire if the src |
| 165 | // attribute is set after the onload listener. |
| 166 | var originalSrc = imageElement.src; |
| 167 | imageElement.src = ''; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 168 | imageElement.onload = imageOnLoad; |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 169 | imageElement.src = originalSrc; |
| 170 | } |
| 171 | |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame^] | 172 | /** |
| 173 | * Handle an onload event for an <img> tag. |
| 174 | * The image could be within an elided-text block, or at the top level of a message. |
| 175 | * When a new image loads, its new bounds may affect message or elided-text geometry, |
| 176 | * so we need to inspect and adjust the enclosing element's zoom level where necessary. |
| 177 | * |
| 178 | * Because this method can be called really often, and zoom-level adjustment is slow, |
| 179 | * we collect the elements to be processed and do them all later in a single deferred pass. |
| 180 | */ |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 181 | function imageOnLoad(e) { |
| 182 | // normalize the quoted text parent if we're in a quoted text block, or else |
| 183 | // normalize the parent message content element |
| 184 | var parent = up(e.target, "elided-text") || up(e.target, "mail-message-content"); |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame^] | 185 | if (!parent) { |
| 186 | // sanity check. shouldn't really happen. |
| 187 | return; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 188 | } |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame^] | 189 | |
| 190 | // if there was no previous work, schedule a new deferred job |
| 191 | if (gImageLoadElements.length == 0) { |
| 192 | window.setTimeout(handleAllImageOnLoads, 0); |
| 193 | } |
| 194 | |
| 195 | // enqueue the work if it wasn't already enqueued |
| 196 | if (gImageLoadElements.indexOf(parent) == -1) { |
| 197 | gImageLoadElements.push(parent); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // handle all deferred work from image onload events |
| 202 | function handleAllImageOnLoads() { |
| 203 | normalizeElementWidths(gImageLoadElements); |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 204 | measurePositions(); |
Andy Huang | 63b3c67 | 2012-10-05 19:27:28 -0700 | [diff] [blame^] | 205 | // clear the queue so the next onload event starts a new job |
| 206 | gImageLoadElements = []; |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 209 | function blockImage(imageElement) { |
| 210 | var src = imageElement.src; |
Paul Westbrook | 41dca18 | 2012-08-07 10:43:42 -0700 | [diff] [blame] | 211 | if (src.indexOf("http://") == 0 || src.indexOf("https://") == 0 || |
| 212 | src.indexOf("content://") == 0) { |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 213 | imageElement.setAttribute(BLOCKED_SRC_ATTR, src); |
| 214 | imageElement.src = "data:"; |
| 215 | } |
| 216 | } |
| 217 | |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 218 | function setWideViewport() { |
| 219 | var metaViewport = document.getElementById('meta-viewport'); |
| 220 | metaViewport.setAttribute('content', 'width=' + WIDE_VIEWPORT_WIDTH); |
| 221 | } |
| 222 | |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 223 | function restoreScrollPosition() { |
| 224 | var scrollYPercent = window.mail.getScrollYPercent(); |
| 225 | if (scrollYPercent && document.body.offsetHeight > window.innerHeight) { |
| 226 | document.body.scrollTop = Math.floor(scrollYPercent * document.body.offsetHeight); |
| 227 | } |
| 228 | } |
| 229 | |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 230 | // BEGIN Java->JavaScript handlers |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 231 | function measurePositions() { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 232 | var overlayBottoms; |
Andy Huang | b5078b2 | 2012-03-05 19:52:29 -0800 | [diff] [blame] | 233 | var h; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 234 | var i; |
| 235 | var len; |
| 236 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 237 | var expandedSpacerDivs = document.querySelectorAll(".expanded > .spacer"); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 238 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 239 | overlayBottoms = new Array(expandedSpacerDivs.length + 1); |
| 240 | for (i = 0, len = expandedSpacerDivs.length; i < len; i++) { |
| 241 | h = expandedSpacerDivs[i].offsetHeight; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 242 | // addJavascriptInterface handler only supports string arrays |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 243 | overlayBottoms[i] = "" + (getTotalOffset(expandedSpacerDivs[i]).top + h); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 244 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 245 | // add an extra one to mark the bottom of the last message |
| 246 | overlayBottoms[i] = "" + document.body.offsetHeight; |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 247 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 248 | window.mail.onWebContentGeometryChange(overlayBottoms); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 251 | function unblockImages(messageDomId) { |
| 252 | var i, images, imgCount, image, blockedSrc; |
| 253 | var msg = document.getElementById(messageDomId); |
| 254 | if (!msg) { |
| 255 | console.log("can't unblock, no matching message for id: " + messageDomId); |
| 256 | return; |
| 257 | } |
| 258 | images = msg.getElementsByTagName("img"); |
| 259 | for (i = 0, imgCount = images.length; i < imgCount; i++) { |
| 260 | image = images[i]; |
| 261 | blockedSrc = image.getAttribute(BLOCKED_SRC_ATTR); |
| 262 | if (blockedSrc) { |
| 263 | image.src = blockedSrc; |
| 264 | image.removeAttribute(BLOCKED_SRC_ATTR); |
| 265 | } |
| 266 | } |
| 267 | } |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 268 | |
Mark Wei | ab2d998 | 2012-09-25 13:06:17 -0700 | [diff] [blame] | 269 | function setConversationHeaderSpacerHeight(spacerHeight) { |
| 270 | var spacer = document.getElementById("conversation-header"); |
| 271 | if (!spacer) { |
| 272 | console.log("can't set spacer for conversation header"); |
| 273 | return; |
| 274 | } |
| 275 | spacer.style.height = spacerHeight + "px"; |
| 276 | measurePositions(); |
| 277 | } |
| 278 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 279 | function setMessageHeaderSpacerHeight(messageDomId, spacerHeight) { |
| 280 | var spacer = document.querySelector("#" + messageDomId + " > .mail-message-header"); |
| 281 | if (!spacer) { |
| 282 | console.log("can't set spacer for message with id: " + messageDomId); |
| 283 | return; |
| 284 | } |
| 285 | spacer.style.height = spacerHeight + "px"; |
| 286 | measurePositions(); |
| 287 | } |
| 288 | |
| 289 | function setMessageBodyVisible(messageDomId, isVisible, spacerHeight) { |
| 290 | var i, len; |
| 291 | var visibility = isVisible ? "block" : "none"; |
| 292 | var messageDiv = document.querySelector("#" + messageDomId); |
| 293 | var collapsibleDivs = document.querySelectorAll("#" + messageDomId + " > .collapsible"); |
| 294 | if (!messageDiv || collapsibleDivs.length == 0) { |
| 295 | console.log("can't set body visibility for message with id: " + messageDomId); |
| 296 | return; |
| 297 | } |
| 298 | messageDiv.classList.toggle("expanded"); |
| 299 | for (i = 0, len = collapsibleDivs.length; i < len; i++) { |
| 300 | collapsibleDivs[i].style.display = visibility; |
| 301 | } |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 302 | |
| 303 | // revealing new content should trigger width normalization, since the initial render |
| 304 | // skips collapsed and super-collapsed messages |
| 305 | if (isVisible) { |
| 306 | normalizeElementWidths(messageDiv.getElementsByClassName("mail-message-content")); |
| 307 | } |
| 308 | |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 309 | setMessageHeaderSpacerHeight(messageDomId, spacerHeight); |
| 310 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 311 | |
| 312 | function replaceSuperCollapsedBlock(startIndex) { |
| 313 | var parent, block, header; |
| 314 | |
| 315 | block = document.querySelector(".mail-super-collapsed-block[index='" + startIndex + "']"); |
| 316 | if (!block) { |
| 317 | console.log("can't expand super collapsed block at index: " + startIndex); |
| 318 | return; |
| 319 | } |
| 320 | parent = block.parentNode; |
| 321 | block.innerHTML = window.mail.getTempMessageBodies(); |
| 322 | |
| 323 | header = block.firstChild; |
| 324 | while (header) { |
| 325 | parent.insertBefore(header, block); |
| 326 | header = block.firstChild; |
| 327 | } |
| 328 | parent.removeChild(block); |
| 329 | measurePositions(); |
| 330 | } |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 331 | |
| 332 | function onContentReady(event) { |
| 333 | window.mail.onContentReady(); |
| 334 | } |
| 335 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 336 | function replaceMessageBodies(messageIds) { |
| 337 | var i; |
| 338 | var id; |
| 339 | var msgContentDiv; |
| 340 | |
| 341 | for (i = 0, len = messageIds.length; i < len; i++) { |
| 342 | id = messageIds[i]; |
| 343 | msgContentDiv = document.querySelector("#" + id + " > .mail-message-content"); |
| 344 | msgContentDiv.innerHTML = window.mail.getMessageBody(id); |
| 345 | collapseQuotedText(msgContentDiv); |
| 346 | } |
| 347 | } |
| 348 | |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 349 | // END Java->JavaScript handlers |
| 350 | |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 351 | window.onload = function() { |
| 352 | // PAGE READINESS SIGNAL FOR JELLYBEAN AND NEWER |
| 353 | // Notify the app on 'webkitAnimationStart' of a simple dummy element with a simple no-op |
| 354 | // animation that immediately runs on page load. The app uses this as a signal that the |
| 355 | // content is loaded and ready to draw, since WebView delays firing this event until the |
| 356 | // layers are composited and everything is ready to draw. |
| 357 | // |
| 358 | // This code is conditionally enabled on JB+ by setting the 'initial-load' CSS class on this |
| 359 | // dummy element. |
| 360 | document.getElementById("initial-load-signal") |
| 361 | .addEventListener("webkitAnimationStart", onContentReady, false); |
mindyp | 32d911f | 2012-09-24 15:14:22 -0700 | [diff] [blame] | 362 | document.getElementById("initial-load-signal").style.webkitAnimationName = 'initial-load'; |
mindyp | 3bcf180 | 2012-09-09 11:17:00 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 365 | collapseAllQuotedText(); |
Andy Huang | 3233bff | 2012-03-20 19:38:45 -0700 | [diff] [blame] | 366 | hideUnsafeImages(); |
Andy Huang | ffc725f | 2012-10-01 14:48:02 -0700 | [diff] [blame] | 367 | normalizeAllMessageWidths(); |
Andy Huang | 2301470 | 2012-07-09 12:50:36 -0700 | [diff] [blame] | 368 | //setWideViewport(); |
Andy Huang | e964eee | 2012-10-02 19:24:58 -0700 | [diff] [blame] | 369 | restoreScrollPosition(); |
Andy Huang | f70fc40 | 2012-02-17 15:37:42 -0800 | [diff] [blame] | 370 | measurePositions(); |
| 371 | |