blob: 7ea607df336c7c4dc38a49c7d7e3d834d031625c [file] [log] [blame]
Andy Huangf70fc402012-02-17 15:37:42 -08001/*
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 Huang3233bff2012-03-20 19:38:45 -070018var BLOCKED_SRC_ATTR = "blocked-src";
Andy Huangf70fc402012-02-17 15:37:42 -080019
Andy Huang63b3c672012-10-05 19:27:28 -070020// 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
22var gImageLoadElements = [];
23
Andy Huangf70fc402012-02-17 15:37:42 -080024/**
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 */
31function 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 Huangffc725f2012-10-01 14:48:02 -070047/**
48 * Walks up the DOM starting at a given element, and returns an element that has the
49 * specified class name or null.
50 */
51function 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 Huangdf0d91c2012-10-08 18:44:42 -070062function onToggleClick(e) {
63 toggleQuotedText(e.target);
64 measurePositions();
65}
66
67function toggleQuotedText(toggleElement) {
Andy Huangf70fc402012-02-17 15:37:42 -080068 var elidedTextElement = toggleElement.nextSibling;
69 var isHidden = getComputedStyle(elidedTextElement).display == 'none';
70 toggleElement.innerHTML = isHidden ? MSG_HIDE_ELIDED : MSG_SHOW_ELIDED;
71 elidedTextElement.style.display = isHidden ? 'block' : 'none';
Andy Huangffc725f2012-10-01 14:48:02 -070072
73 // Revealing the elided text should normalize it to fit-width to prevent
74 // this message from blowing out the conversation width.
75 if (isHidden) {
76 normalizeElementWidths([elidedTextElement]);
77 }
Andy Huangf70fc402012-02-17 15:37:42 -080078}
79
Andy Huang014ea4c2012-09-25 14:50:54 -070080function collapseAllQuotedText() {
Andy Huangdf0d91c2012-10-08 18:44:42 -070081 processQuotedText(document.documentElement, false /* showElided */);
Andy Huang014ea4c2012-09-25 14:50:54 -070082}
83
Andy Huangdf0d91c2012-10-08 18:44:42 -070084function processQuotedText(elt, showElided) {
Andy Huangf70fc402012-02-17 15:37:42 -080085 var i;
Andy Huang014ea4c2012-09-25 14:50:54 -070086 var elements = elt.getElementsByClassName("elided-text");
Andy Huangf70fc402012-02-17 15:37:42 -080087 var elidedElement, toggleElement;
88 for (i = 0; i < elements.length; i++) {
89 elidedElement = elements[i];
90 toggleElement = document.createElement("div");
Andy Huang82119af2012-06-26 17:15:40 -070091 toggleElement.className = "mail-elided-text";
Andy Huangf70fc402012-02-17 15:37:42 -080092 toggleElement.innerHTML = MSG_SHOW_ELIDED;
Andy Huangdf0d91c2012-10-08 18:44:42 -070093 toggleElement.onclick = onToggleClick;
94 elidedElement.style.display = 'none';
Andy Huangf70fc402012-02-17 15:37:42 -080095 elidedElement.parentNode.insertBefore(toggleElement, elidedElement);
Andy Huangdf0d91c2012-10-08 18:44:42 -070096 if (showElided) {
97 toggleQuotedText(toggleElement);
98 }
Andy Huangf70fc402012-02-17 15:37:42 -080099 }
100}
101
Andy Huang4cfe22a2012-10-15 18:20:33 -0700102function isConversationEmpty(bodyDivs) {
103 var i, len;
104 var msgBody;
105 var text;
106
107 // Check if given divs are empty (in appearance), and disable zoom if so.
108 for (i = 0, len = bodyDivs.length; i < len; i++) {
109 msgBody = bodyDivs[i];
110 // use 'textContent' to exclude markup when determining whether bodies are empty
111 // (fall back to more expensive 'innerText' if 'textContent' isn't implemented)
112 text = msgBody.textContent || msgBody.innerText;
113 if (text.trim().length > 0) {
114 return false;
115 }
116 }
117 return true;
118}
119
Andy Huangffc725f2012-10-01 14:48:02 -0700120function normalizeAllMessageWidths() {
Andy Huang4cfe22a2012-10-15 18:20:33 -0700121 var expandedBodyDivs;
122 var metaViewport;
123 var contentValues;
124 var isEmpty;
125
126 if (!NORMALIZE_MESSAGE_WIDTHS) {
127 return;
128 }
129
130 expandedBodyDivs = document.querySelectorAll(".expanded > .mail-message-content");
131
132 isEmpty = isConversationEmpty(expandedBodyDivs);
133
134 normalizeElementWidths(expandedBodyDivs);
135
136 // assemble a working <meta> viewport "content" value from the base value in the
137 // document, plus any dynamically determined options
138 metaViewport = document.getElementById("meta-viewport");
139 contentValues = [metaViewport.getAttribute("content")];
140 if (isEmpty) {
141 contentValues.push(metaViewport.getAttribute("data-zoom-off"));
142 } else {
143 contentValues.push(metaViewport.getAttribute("data-zoom-on"));
144 }
145 metaViewport.setAttribute("content", contentValues.join(","));
Andy Huangffc725f2012-10-01 14:48:02 -0700146}
147
148/*
149 * Normalizes the width of all elements supplied to the document body's overall width.
150 * Narrower elements are zoomed in, and wider elements are zoomed out.
151 * This method is idempotent.
152 */
153function normalizeElementWidths(elements) {
Andy Huangf70fc402012-02-17 15:37:42 -0800154 var i;
Andy Huangffc725f2012-10-01 14:48:02 -0700155 var el;
Andy Huangadbf3e82012-10-13 13:30:19 -0700156 var documentWidth;
Andy Huangffc725f2012-10-01 14:48:02 -0700157 var newZoom, oldZoom;
Andy Huang256b35c2012-08-22 15:19:13 -0700158
Andy Huangadbf3e82012-10-13 13:30:19 -0700159 if (!NORMALIZE_MESSAGE_WIDTHS) {
160 return;
161 }
162
163 documentWidth = document.body.offsetWidth;
164
Andy Huangf70fc402012-02-17 15:37:42 -0800165 for (i = 0; i < elements.length; i++) {
Andy Huangffc725f2012-10-01 14:48:02 -0700166 el = elements[i];
167 oldZoom = el.style.zoom;
168 // reset any existing normalization
169 if (oldZoom) {
170 el.style.zoom = 1;
171 }
172 newZoom = documentWidth / el.scrollWidth;
173 el.style.zoom = newZoom;
Andy Huangf70fc402012-02-17 15:37:42 -0800174 }
175}
176
Andy Huangf500db82012-10-23 16:56:27 -0700177function hideAllUnsafeImages() {
178 hideUnsafeImages(document.getElementsByClassName("mail-message-content"));
179}
180
181function hideUnsafeImages(msgContentDivs) {
182 var i, msgContentCount;
Andy Huang3233bff2012-03-20 19:38:45 -0700183 var j, imgCount;
Andy Huangf500db82012-10-23 16:56:27 -0700184 var msgContentDiv, image;
Andy Huang3233bff2012-03-20 19:38:45 -0700185 var images;
186 var showImages;
Andy Huangf500db82012-10-23 16:56:27 -0700187 for (i = 0, msgContentCount = msgContentDivs.length; i < msgContentCount; i++) {
188 msgContentDiv = msgContentDivs[i];
189 showImages = msgContentDiv.classList.contains("mail-show-images");
Andy Huang3233bff2012-03-20 19:38:45 -0700190
Andy Huangf500db82012-10-23 16:56:27 -0700191 images = msgContentDiv.getElementsByTagName("img");
Andy Huang3233bff2012-03-20 19:38:45 -0700192 for (j = 0, imgCount = images.length; j < imgCount; j++) {
193 image = images[j];
Paul Westbrook41dca182012-08-07 10:43:42 -0700194 rewriteRelativeImageSrc(image);
Andy Huang3233bff2012-03-20 19:38:45 -0700195 attachImageLoadListener(image);
196 // TODO: handle inline image attachments for all supported protocols
197 if (!showImages) {
198 blockImage(image);
199 }
200 }
201 }
202}
203
Paul Westbrook41dca182012-08-07 10:43:42 -0700204/**
205 * Changes relative paths to absolute path by pre-pending the account uri
206 * @param {Element} imgElement Image for which the src path will be updated.
207 */
208function rewriteRelativeImageSrc(imgElement) {
209 var src = imgElement.src;
Paul Westbrookcebcc642012-08-08 10:06:04 -0700210
211 // DOC_BASE_URI will always be a unique x-thread:// uri for this particular conversation
212 if (src.indexOf(DOC_BASE_URI) == 0 && (DOC_BASE_URI != CONVERSATION_BASE_URI)) {
213 // The conversation specifies a different base uri than the document
214 src = CONVERSATION_BASE_URI + src.substring(DOC_BASE_URI.length);
215 imgElement.src = src;
Paul Westbrook41dca182012-08-07 10:43:42 -0700216 }
217};
218
219
Andy Huang3233bff2012-03-20 19:38:45 -0700220function attachImageLoadListener(imageElement) {
221 // Reset the src attribute to the empty string because onload will only fire if the src
222 // attribute is set after the onload listener.
223 var originalSrc = imageElement.src;
224 imageElement.src = '';
Andy Huangffc725f2012-10-01 14:48:02 -0700225 imageElement.onload = imageOnLoad;
Andy Huang3233bff2012-03-20 19:38:45 -0700226 imageElement.src = originalSrc;
227}
228
Andy Huang63b3c672012-10-05 19:27:28 -0700229/**
230 * Handle an onload event for an <img> tag.
231 * The image could be within an elided-text block, or at the top level of a message.
232 * When a new image loads, its new bounds may affect message or elided-text geometry,
233 * so we need to inspect and adjust the enclosing element's zoom level where necessary.
234 *
235 * Because this method can be called really often, and zoom-level adjustment is slow,
236 * we collect the elements to be processed and do them all later in a single deferred pass.
237 */
Andy Huangffc725f2012-10-01 14:48:02 -0700238function imageOnLoad(e) {
239 // normalize the quoted text parent if we're in a quoted text block, or else
240 // normalize the parent message content element
241 var parent = up(e.target, "elided-text") || up(e.target, "mail-message-content");
Andy Huang63b3c672012-10-05 19:27:28 -0700242 if (!parent) {
243 // sanity check. shouldn't really happen.
244 return;
Andy Huangffc725f2012-10-01 14:48:02 -0700245 }
Andy Huang63b3c672012-10-05 19:27:28 -0700246
247 // if there was no previous work, schedule a new deferred job
248 if (gImageLoadElements.length == 0) {
249 window.setTimeout(handleAllImageOnLoads, 0);
250 }
251
252 // enqueue the work if it wasn't already enqueued
253 if (gImageLoadElements.indexOf(parent) == -1) {
254 gImageLoadElements.push(parent);
255 }
256}
257
258// handle all deferred work from image onload events
259function handleAllImageOnLoads() {
260 normalizeElementWidths(gImageLoadElements);
Andy Huangffc725f2012-10-01 14:48:02 -0700261 measurePositions();
Andy Huang63b3c672012-10-05 19:27:28 -0700262 // clear the queue so the next onload event starts a new job
263 gImageLoadElements = [];
Andy Huangffc725f2012-10-01 14:48:02 -0700264}
265
Andy Huang3233bff2012-03-20 19:38:45 -0700266function blockImage(imageElement) {
267 var src = imageElement.src;
Paul Westbrook41dca182012-08-07 10:43:42 -0700268 if (src.indexOf("http://") == 0 || src.indexOf("https://") == 0 ||
269 src.indexOf("content://") == 0) {
Andy Huang3233bff2012-03-20 19:38:45 -0700270 imageElement.setAttribute(BLOCKED_SRC_ATTR, src);
271 imageElement.src = "data:";
272 }
273}
274
Andy Huang23014702012-07-09 12:50:36 -0700275function setWideViewport() {
276 var metaViewport = document.getElementById('meta-viewport');
277 metaViewport.setAttribute('content', 'width=' + WIDE_VIEWPORT_WIDTH);
278}
279
Andy Huange964eee2012-10-02 19:24:58 -0700280function restoreScrollPosition() {
281 var scrollYPercent = window.mail.getScrollYPercent();
282 if (scrollYPercent && document.body.offsetHeight > window.innerHeight) {
283 document.body.scrollTop = Math.floor(scrollYPercent * document.body.offsetHeight);
284 }
285}
286
Andy Huangbffc3122012-10-05 21:20:56 -0700287function onContentReady(event) {
288 window.mail.onContentReady();
289}
290
291function setupContentReady() {
292 var signalDiv;
293
294 // PAGE READINESS SIGNAL FOR JELLYBEAN AND NEWER
295 // Notify the app on 'webkitAnimationStart' of a simple dummy element with a simple no-op
296 // animation that immediately runs on page load. The app uses this as a signal that the
297 // content is loaded and ready to draw, since WebView delays firing this event until the
298 // layers are composited and everything is ready to draw.
299 //
300 // This code is conditionally enabled on JB+ by setting the ENABLE_CONTENT_READY flag.
301 if (ENABLE_CONTENT_READY) {
302 signalDiv = document.getElementById("initial-load-signal");
303 signalDiv.addEventListener("webkitAnimationStart", onContentReady, false);
304 signalDiv.classList.add("initial-load");
305 }
306}
307
Andy Huang23014702012-07-09 12:50:36 -0700308// BEGIN Java->JavaScript handlers
Andy Huangf70fc402012-02-17 15:37:42 -0800309function measurePositions() {
Andy Huangadbf3e82012-10-13 13:30:19 -0700310 var overlayTops, overlayBottoms;
Andy Huangf70fc402012-02-17 15:37:42 -0800311 var i;
312 var len;
313
Andy Huangadbf3e82012-10-13 13:30:19 -0700314 var expandedBody, headerSpacer;
315 var prevBodyBottom = 0;
316 var expandedBodyDivs = document.querySelectorAll(".expanded > .mail-message-content");
Andy Huangf70fc402012-02-17 15:37:42 -0800317
Andy Huangadbf3e82012-10-13 13:30:19 -0700318 // N.B. offsetTop and offsetHeight of an element with the "zoom:" style applied cannot be
319 // trusted.
320
321 overlayTops = new Array(expandedBodyDivs.length + 1);
322 overlayBottoms = new Array(expandedBodyDivs.length + 1);
323 for (i = 0, len = expandedBodyDivs.length; i < len; i++) {
324 expandedBody = expandedBodyDivs[i];
325 headerSpacer = expandedBody.previousElementSibling;
Andy Huangf70fc402012-02-17 15:37:42 -0800326 // addJavascriptInterface handler only supports string arrays
Andy Huangadbf3e82012-10-13 13:30:19 -0700327 overlayTops[i] = "" + prevBodyBottom;
328 overlayBottoms[i] = "" + (getTotalOffset(headerSpacer).top + headerSpacer.offsetHeight);
329 prevBodyBottom = getTotalOffset(expandedBody.nextElementSibling).top;
Andy Huangf70fc402012-02-17 15:37:42 -0800330 }
Andy Huangadbf3e82012-10-13 13:30:19 -0700331 // add an extra one to mark the top/bottom of the last message footer spacer
332 overlayTops[i] = "" + prevBodyBottom;
Andy Huang7bdc3752012-03-25 17:18:19 -0700333 overlayBottoms[i] = "" + document.body.offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800334
Andy Huangadbf3e82012-10-13 13:30:19 -0700335 window.mail.onWebContentGeometryChange(overlayTops, overlayBottoms);
Andy Huangf70fc402012-02-17 15:37:42 -0800336}
337
Andy Huang3233bff2012-03-20 19:38:45 -0700338function unblockImages(messageDomId) {
339 var i, images, imgCount, image, blockedSrc;
340 var msg = document.getElementById(messageDomId);
341 if (!msg) {
342 console.log("can't unblock, no matching message for id: " + messageDomId);
343 return;
344 }
345 images = msg.getElementsByTagName("img");
346 for (i = 0, imgCount = images.length; i < imgCount; i++) {
347 image = images[i];
348 blockedSrc = image.getAttribute(BLOCKED_SRC_ATTR);
349 if (blockedSrc) {
350 image.src = blockedSrc;
351 image.removeAttribute(BLOCKED_SRC_ATTR);
352 }
353 }
354}
Andy Huangc7543572012-04-03 15:34:29 -0700355
Mark Weiab2d9982012-09-25 13:06:17 -0700356function setConversationHeaderSpacerHeight(spacerHeight) {
357 var spacer = document.getElementById("conversation-header");
358 if (!spacer) {
359 console.log("can't set spacer for conversation header");
360 return;
361 }
362 spacer.style.height = spacerHeight + "px";
363 measurePositions();
364}
365
Andy Huangc7543572012-04-03 15:34:29 -0700366function setMessageHeaderSpacerHeight(messageDomId, spacerHeight) {
367 var spacer = document.querySelector("#" + messageDomId + " > .mail-message-header");
368 if (!spacer) {
369 console.log("can't set spacer for message with id: " + messageDomId);
370 return;
371 }
372 spacer.style.height = spacerHeight + "px";
373 measurePositions();
374}
375
376function setMessageBodyVisible(messageDomId, isVisible, spacerHeight) {
377 var i, len;
378 var visibility = isVisible ? "block" : "none";
379 var messageDiv = document.querySelector("#" + messageDomId);
380 var collapsibleDivs = document.querySelectorAll("#" + messageDomId + " > .collapsible");
381 if (!messageDiv || collapsibleDivs.length == 0) {
382 console.log("can't set body visibility for message with id: " + messageDomId);
383 return;
384 }
385 messageDiv.classList.toggle("expanded");
386 for (i = 0, len = collapsibleDivs.length; i < len; i++) {
387 collapsibleDivs[i].style.display = visibility;
388 }
Andy Huangffc725f2012-10-01 14:48:02 -0700389
390 // revealing new content should trigger width normalization, since the initial render
391 // skips collapsed and super-collapsed messages
392 if (isVisible) {
393 normalizeElementWidths(messageDiv.getElementsByClassName("mail-message-content"));
394 }
395
Andy Huangc7543572012-04-03 15:34:29 -0700396 setMessageHeaderSpacerHeight(messageDomId, spacerHeight);
397}
Andy Huang46dfba62012-04-19 01:47:32 -0700398
399function replaceSuperCollapsedBlock(startIndex) {
Andy Huangf500db82012-10-23 16:56:27 -0700400 var parent, block, msg;
Andy Huang46dfba62012-04-19 01:47:32 -0700401
402 block = document.querySelector(".mail-super-collapsed-block[index='" + startIndex + "']");
403 if (!block) {
404 console.log("can't expand super collapsed block at index: " + startIndex);
405 return;
406 }
407 parent = block.parentNode;
408 block.innerHTML = window.mail.getTempMessageBodies();
409
Andy Huangf500db82012-10-23 16:56:27 -0700410 // process the new block contents in one go before we pluck them out of the common ancestor
411 processQuotedText(block, false /* showElided */);
412 hideUnsafeImages(block.getElementsByClassName("mail-message-content"));
413
414 msg = block.firstChild;
415 while (msg) {
416 parent.insertBefore(msg, block);
417 msg = block.firstChild;
Andy Huang46dfba62012-04-19 01:47:32 -0700418 }
419 parent.removeChild(block);
420 measurePositions();
421}
mindyp3bcf1802012-09-09 11:17:00 -0700422
Andy Huang014ea4c2012-09-25 14:50:54 -0700423function replaceMessageBodies(messageIds) {
424 var i;
425 var id;
426 var msgContentDiv;
427
428 for (i = 0, len = messageIds.length; i < len; i++) {
429 id = messageIds[i];
430 msgContentDiv = document.querySelector("#" + id + " > .mail-message-content");
431 msgContentDiv.innerHTML = window.mail.getMessageBody(id);
Andy Huangdf0d91c2012-10-08 18:44:42 -0700432 processQuotedText(msgContentDiv, true /* showElided */);
Andy Huangf500db82012-10-23 16:56:27 -0700433 hideUnsafeImages([msgContentDiv]);
Andy Huang014ea4c2012-09-25 14:50:54 -0700434 }
435}
436
Andy Huang06c03622012-10-22 18:59:45 -0700437// handle the special case of adding a single new message at the end of a conversation
438function appendMessageHtml() {
439 var msg = document.createElement("div");
440 msg.innerHTML = window.mail.getTempMessageBodies();
441 msg = msg.children[0]; // toss the outer div, it was just to render innerHTML into
442 document.body.appendChild(msg);
443 processQuotedText(msg, true /* showElided */);
Andy Huangf500db82012-10-23 16:56:27 -0700444 hideUnsafeImages(msg.getElementsByClassName("mail-message-content"));
Andy Huang06c03622012-10-22 18:59:45 -0700445}
446
Andy Huang3233bff2012-03-20 19:38:45 -0700447// END Java->JavaScript handlers
448
Andy Huang4cfe22a2012-10-15 18:20:33 -0700449// Do this first to ensure that the readiness signal comes through,
450// even if a stray exception later occurs.
451setupContentReady();
452
Andy Huang014ea4c2012-09-25 14:50:54 -0700453collapseAllQuotedText();
Andy Huangf500db82012-10-23 16:56:27 -0700454hideAllUnsafeImages();
Andy Huangffc725f2012-10-01 14:48:02 -0700455normalizeAllMessageWidths();
Andy Huang23014702012-07-09 12:50:36 -0700456//setWideViewport();
Andy Huange964eee2012-10-02 19:24:58 -0700457restoreScrollPosition();
Andy Huangf70fc402012-02-17 15:37:42 -0800458measurePositions();
Andy Huangf70fc402012-02-17 15:37:42 -0800459