blob: 27da9be3e12a6d1a40c73ee5db34a42c2efb579b [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 Huangffc725f2012-10-01 14:48:02 -0700102function normalizeAllMessageWidths() {
103 normalizeElementWidths(document.querySelectorAll(".expanded > .mail-message-content"));
104}
105
106/*
107 * Normalizes the width of all elements supplied to the document body's overall width.
108 * Narrower elements are zoomed in, and wider elements are zoomed out.
109 * This method is idempotent.
110 */
111function normalizeElementWidths(elements) {
Andy Huangf70fc402012-02-17 15:37:42 -0800112 var i;
Andy Huangffc725f2012-10-01 14:48:02 -0700113 var el;
Andy Huang256b35c2012-08-22 15:19:13 -0700114 var documentWidth = document.body.offsetWidth;
Andy Huangffc725f2012-10-01 14:48:02 -0700115 var newZoom, oldZoom;
Andy Huang256b35c2012-08-22 15:19:13 -0700116
Andy Huangf70fc402012-02-17 15:37:42 -0800117 for (i = 0; i < elements.length; i++) {
Andy Huangffc725f2012-10-01 14:48:02 -0700118 el = elements[i];
119 oldZoom = el.style.zoom;
120 // reset any existing normalization
121 if (oldZoom) {
122 el.style.zoom = 1;
123 }
124 newZoom = documentWidth / el.scrollWidth;
125 el.style.zoom = newZoom;
Andy Huangf70fc402012-02-17 15:37:42 -0800126 }
127}
128
Andy Huang3233bff2012-03-20 19:38:45 -0700129function hideUnsafeImages() {
130 var i, bodyCount;
131 var j, imgCount;
132 var body, image;
133 var images;
134 var showImages;
135 var bodies = document.getElementsByClassName("mail-message-content");
136 for (i = 0, bodyCount = bodies.length; i < bodyCount; i++) {
137 body = bodies[i];
138 showImages = body.classList.contains("mail-show-images");
139
140 images = body.getElementsByTagName("img");
141 for (j = 0, imgCount = images.length; j < imgCount; j++) {
142 image = images[j];
Paul Westbrook41dca182012-08-07 10:43:42 -0700143 rewriteRelativeImageSrc(image);
Andy Huang3233bff2012-03-20 19:38:45 -0700144 attachImageLoadListener(image);
145 // TODO: handle inline image attachments for all supported protocols
146 if (!showImages) {
147 blockImage(image);
148 }
149 }
150 }
151}
152
Paul Westbrook41dca182012-08-07 10:43:42 -0700153/**
154 * Changes relative paths to absolute path by pre-pending the account uri
155 * @param {Element} imgElement Image for which the src path will be updated.
156 */
157function rewriteRelativeImageSrc(imgElement) {
158 var src = imgElement.src;
Paul Westbrookcebcc642012-08-08 10:06:04 -0700159
160 // DOC_BASE_URI will always be a unique x-thread:// uri for this particular conversation
161 if (src.indexOf(DOC_BASE_URI) == 0 && (DOC_BASE_URI != CONVERSATION_BASE_URI)) {
162 // The conversation specifies a different base uri than the document
163 src = CONVERSATION_BASE_URI + src.substring(DOC_BASE_URI.length);
164 imgElement.src = src;
Paul Westbrook41dca182012-08-07 10:43:42 -0700165 }
166};
167
168
Andy Huang3233bff2012-03-20 19:38:45 -0700169function attachImageLoadListener(imageElement) {
170 // Reset the src attribute to the empty string because onload will only fire if the src
171 // attribute is set after the onload listener.
172 var originalSrc = imageElement.src;
173 imageElement.src = '';
Andy Huangffc725f2012-10-01 14:48:02 -0700174 imageElement.onload = imageOnLoad;
Andy Huang3233bff2012-03-20 19:38:45 -0700175 imageElement.src = originalSrc;
176}
177
Andy Huang63b3c672012-10-05 19:27:28 -0700178/**
179 * Handle an onload event for an <img> tag.
180 * The image could be within an elided-text block, or at the top level of a message.
181 * When a new image loads, its new bounds may affect message or elided-text geometry,
182 * so we need to inspect and adjust the enclosing element's zoom level where necessary.
183 *
184 * Because this method can be called really often, and zoom-level adjustment is slow,
185 * we collect the elements to be processed and do them all later in a single deferred pass.
186 */
Andy Huangffc725f2012-10-01 14:48:02 -0700187function imageOnLoad(e) {
188 // normalize the quoted text parent if we're in a quoted text block, or else
189 // normalize the parent message content element
190 var parent = up(e.target, "elided-text") || up(e.target, "mail-message-content");
Andy Huang63b3c672012-10-05 19:27:28 -0700191 if (!parent) {
192 // sanity check. shouldn't really happen.
193 return;
Andy Huangffc725f2012-10-01 14:48:02 -0700194 }
Andy Huang63b3c672012-10-05 19:27:28 -0700195
196 // if there was no previous work, schedule a new deferred job
197 if (gImageLoadElements.length == 0) {
198 window.setTimeout(handleAllImageOnLoads, 0);
199 }
200
201 // enqueue the work if it wasn't already enqueued
202 if (gImageLoadElements.indexOf(parent) == -1) {
203 gImageLoadElements.push(parent);
204 }
205}
206
207// handle all deferred work from image onload events
208function handleAllImageOnLoads() {
209 normalizeElementWidths(gImageLoadElements);
Andy Huangffc725f2012-10-01 14:48:02 -0700210 measurePositions();
Andy Huang63b3c672012-10-05 19:27:28 -0700211 // clear the queue so the next onload event starts a new job
212 gImageLoadElements = [];
Andy Huangffc725f2012-10-01 14:48:02 -0700213}
214
Andy Huang3233bff2012-03-20 19:38:45 -0700215function blockImage(imageElement) {
216 var src = imageElement.src;
Paul Westbrook41dca182012-08-07 10:43:42 -0700217 if (src.indexOf("http://") == 0 || src.indexOf("https://") == 0 ||
218 src.indexOf("content://") == 0) {
Andy Huang3233bff2012-03-20 19:38:45 -0700219 imageElement.setAttribute(BLOCKED_SRC_ATTR, src);
220 imageElement.src = "data:";
221 }
222}
223
Andy Huang23014702012-07-09 12:50:36 -0700224function setWideViewport() {
225 var metaViewport = document.getElementById('meta-viewport');
226 metaViewport.setAttribute('content', 'width=' + WIDE_VIEWPORT_WIDTH);
227}
228
Andy Huange964eee2012-10-02 19:24:58 -0700229function restoreScrollPosition() {
230 var scrollYPercent = window.mail.getScrollYPercent();
231 if (scrollYPercent && document.body.offsetHeight > window.innerHeight) {
232 document.body.scrollTop = Math.floor(scrollYPercent * document.body.offsetHeight);
233 }
234}
235
Andy Huangbffc3122012-10-05 21:20:56 -0700236function onContentReady(event) {
237 window.mail.onContentReady();
238}
239
240function setupContentReady() {
241 var signalDiv;
242
243 // PAGE READINESS SIGNAL FOR JELLYBEAN AND NEWER
244 // Notify the app on 'webkitAnimationStart' of a simple dummy element with a simple no-op
245 // animation that immediately runs on page load. The app uses this as a signal that the
246 // content is loaded and ready to draw, since WebView delays firing this event until the
247 // layers are composited and everything is ready to draw.
248 //
249 // This code is conditionally enabled on JB+ by setting the ENABLE_CONTENT_READY flag.
250 if (ENABLE_CONTENT_READY) {
251 signalDiv = document.getElementById("initial-load-signal");
252 signalDiv.addEventListener("webkitAnimationStart", onContentReady, false);
253 signalDiv.classList.add("initial-load");
254 }
255}
256
Andy Huang23014702012-07-09 12:50:36 -0700257// BEGIN Java->JavaScript handlers
Andy Huangf70fc402012-02-17 15:37:42 -0800258function measurePositions() {
Andy Huang7bdc3752012-03-25 17:18:19 -0700259 var overlayBottoms;
Andy Huangb5078b22012-03-05 19:52:29 -0800260 var h;
Andy Huangf70fc402012-02-17 15:37:42 -0800261 var i;
262 var len;
263
Andy Huang7bdc3752012-03-25 17:18:19 -0700264 var expandedSpacerDivs = document.querySelectorAll(".expanded > .spacer");
Andy Huangf70fc402012-02-17 15:37:42 -0800265
Andy Huang7bdc3752012-03-25 17:18:19 -0700266 overlayBottoms = new Array(expandedSpacerDivs.length + 1);
267 for (i = 0, len = expandedSpacerDivs.length; i < len; i++) {
268 h = expandedSpacerDivs[i].offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800269 // addJavascriptInterface handler only supports string arrays
Andy Huang7bdc3752012-03-25 17:18:19 -0700270 overlayBottoms[i] = "" + (getTotalOffset(expandedSpacerDivs[i]).top + h);
Andy Huangf70fc402012-02-17 15:37:42 -0800271 }
Andy Huang7bdc3752012-03-25 17:18:19 -0700272 // add an extra one to mark the bottom of the last message
273 overlayBottoms[i] = "" + document.body.offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800274
Andy Huang7bdc3752012-03-25 17:18:19 -0700275 window.mail.onWebContentGeometryChange(overlayBottoms);
Andy Huangf70fc402012-02-17 15:37:42 -0800276}
277
Andy Huang3233bff2012-03-20 19:38:45 -0700278function unblockImages(messageDomId) {
279 var i, images, imgCount, image, blockedSrc;
280 var msg = document.getElementById(messageDomId);
281 if (!msg) {
282 console.log("can't unblock, no matching message for id: " + messageDomId);
283 return;
284 }
285 images = msg.getElementsByTagName("img");
286 for (i = 0, imgCount = images.length; i < imgCount; i++) {
287 image = images[i];
288 blockedSrc = image.getAttribute(BLOCKED_SRC_ATTR);
289 if (blockedSrc) {
290 image.src = blockedSrc;
291 image.removeAttribute(BLOCKED_SRC_ATTR);
292 }
293 }
294}
Andy Huangc7543572012-04-03 15:34:29 -0700295
Mark Weiab2d9982012-09-25 13:06:17 -0700296function setConversationHeaderSpacerHeight(spacerHeight) {
297 var spacer = document.getElementById("conversation-header");
298 if (!spacer) {
299 console.log("can't set spacer for conversation header");
300 return;
301 }
302 spacer.style.height = spacerHeight + "px";
303 measurePositions();
304}
305
Andy Huangc7543572012-04-03 15:34:29 -0700306function setMessageHeaderSpacerHeight(messageDomId, spacerHeight) {
307 var spacer = document.querySelector("#" + messageDomId + " > .mail-message-header");
308 if (!spacer) {
309 console.log("can't set spacer for message with id: " + messageDomId);
310 return;
311 }
312 spacer.style.height = spacerHeight + "px";
313 measurePositions();
314}
315
316function setMessageBodyVisible(messageDomId, isVisible, spacerHeight) {
317 var i, len;
318 var visibility = isVisible ? "block" : "none";
319 var messageDiv = document.querySelector("#" + messageDomId);
320 var collapsibleDivs = document.querySelectorAll("#" + messageDomId + " > .collapsible");
321 if (!messageDiv || collapsibleDivs.length == 0) {
322 console.log("can't set body visibility for message with id: " + messageDomId);
323 return;
324 }
325 messageDiv.classList.toggle("expanded");
326 for (i = 0, len = collapsibleDivs.length; i < len; i++) {
327 collapsibleDivs[i].style.display = visibility;
328 }
Andy Huangffc725f2012-10-01 14:48:02 -0700329
330 // revealing new content should trigger width normalization, since the initial render
331 // skips collapsed and super-collapsed messages
332 if (isVisible) {
333 normalizeElementWidths(messageDiv.getElementsByClassName("mail-message-content"));
334 }
335
Andy Huangc7543572012-04-03 15:34:29 -0700336 setMessageHeaderSpacerHeight(messageDomId, spacerHeight);
337}
Andy Huang46dfba62012-04-19 01:47:32 -0700338
339function replaceSuperCollapsedBlock(startIndex) {
340 var parent, block, header;
341
342 block = document.querySelector(".mail-super-collapsed-block[index='" + startIndex + "']");
343 if (!block) {
344 console.log("can't expand super collapsed block at index: " + startIndex);
345 return;
346 }
347 parent = block.parentNode;
348 block.innerHTML = window.mail.getTempMessageBodies();
349
350 header = block.firstChild;
351 while (header) {
352 parent.insertBefore(header, block);
353 header = block.firstChild;
354 }
355 parent.removeChild(block);
356 measurePositions();
357}
mindyp3bcf1802012-09-09 11:17:00 -0700358
Andy Huang014ea4c2012-09-25 14:50:54 -0700359function replaceMessageBodies(messageIds) {
360 var i;
361 var id;
362 var msgContentDiv;
363
364 for (i = 0, len = messageIds.length; i < len; i++) {
365 id = messageIds[i];
366 msgContentDiv = document.querySelector("#" + id + " > .mail-message-content");
367 msgContentDiv.innerHTML = window.mail.getMessageBody(id);
Andy Huangdf0d91c2012-10-08 18:44:42 -0700368 processQuotedText(msgContentDiv, true /* showElided */);
Andy Huang014ea4c2012-09-25 14:50:54 -0700369 }
370}
371
Andy Huang3233bff2012-03-20 19:38:45 -0700372// END Java->JavaScript handlers
373
Andy Huang014ea4c2012-09-25 14:50:54 -0700374collapseAllQuotedText();
Andy Huang3233bff2012-03-20 19:38:45 -0700375hideUnsafeImages();
Andy Huangffc725f2012-10-01 14:48:02 -0700376normalizeAllMessageWidths();
Andy Huang23014702012-07-09 12:50:36 -0700377//setWideViewport();
Andy Huange964eee2012-10-02 19:24:58 -0700378restoreScrollPosition();
Andy Huangf70fc402012-02-17 15:37:42 -0800379measurePositions();
Andy Huangbffc3122012-10-05 21:20:56 -0700380setupContentReady();
Andy Huangf70fc402012-02-17 15:37:42 -0800381