blob: f458204ea715fa6377e5c079b8753ea5e6b67684 [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 Huangf70fc402012-02-17 15:37:42 -080062function 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 Huangffc725f2012-10-01 14:48:02 -070068
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 Huangc7543572012-04-03 15:34:29 -070075 measurePositions();
Andy Huangf70fc402012-02-17 15:37:42 -080076}
77
Andy Huang014ea4c2012-09-25 14:50:54 -070078function collapseAllQuotedText() {
79 collapseQuotedText(document.documentElement);
80}
81
82function collapseQuotedText(elt) {
Andy Huangf70fc402012-02-17 15:37:42 -080083 var i;
Andy Huang014ea4c2012-09-25 14:50:54 -070084 var elements = elt.getElementsByClassName("elided-text");
Andy Huangf70fc402012-02-17 15:37:42 -080085 var elidedElement, toggleElement;
86 for (i = 0; i < elements.length; i++) {
87 elidedElement = elements[i];
88 toggleElement = document.createElement("div");
Andy Huang82119af2012-06-26 17:15:40 -070089 toggleElement.className = "mail-elided-text";
Andy Huangf70fc402012-02-17 15:37:42 -080090 toggleElement.innerHTML = MSG_SHOW_ELIDED;
91 toggleElement.onclick = toggleQuotedText;
92 elidedElement.parentNode.insertBefore(toggleElement, elidedElement);
93 }
94}
95
Andy Huangffc725f2012-10-01 14:48:02 -070096function 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 */
105function normalizeElementWidths(elements) {
Andy Huangf70fc402012-02-17 15:37:42 -0800106 var i;
Andy Huangffc725f2012-10-01 14:48:02 -0700107 var el;
Andy Huang256b35c2012-08-22 15:19:13 -0700108 var documentWidth = document.body.offsetWidth;
Andy Huangffc725f2012-10-01 14:48:02 -0700109 var newZoom, oldZoom;
Andy Huang256b35c2012-08-22 15:19:13 -0700110
Andy Huangf70fc402012-02-17 15:37:42 -0800111 for (i = 0; i < elements.length; i++) {
Andy Huangffc725f2012-10-01 14:48:02 -0700112 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 Huangf70fc402012-02-17 15:37:42 -0800120 }
121}
122
Andy Huang3233bff2012-03-20 19:38:45 -0700123function 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 Westbrook41dca182012-08-07 10:43:42 -0700137 rewriteRelativeImageSrc(image);
Andy Huang3233bff2012-03-20 19:38:45 -0700138 attachImageLoadListener(image);
139 // TODO: handle inline image attachments for all supported protocols
140 if (!showImages) {
141 blockImage(image);
142 }
143 }
144 }
145}
146
Paul Westbrook41dca182012-08-07 10:43:42 -0700147/**
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 */
151function rewriteRelativeImageSrc(imgElement) {
152 var src = imgElement.src;
Paul Westbrookcebcc642012-08-08 10:06:04 -0700153
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 Westbrook41dca182012-08-07 10:43:42 -0700159 }
160};
161
162
Andy Huang3233bff2012-03-20 19:38:45 -0700163function 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 Huangffc725f2012-10-01 14:48:02 -0700168 imageElement.onload = imageOnLoad;
Andy Huang3233bff2012-03-20 19:38:45 -0700169 imageElement.src = originalSrc;
170}
171
Andy Huang63b3c672012-10-05 19:27:28 -0700172/**
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 Huangffc725f2012-10-01 14:48:02 -0700181function 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 Huang63b3c672012-10-05 19:27:28 -0700185 if (!parent) {
186 // sanity check. shouldn't really happen.
187 return;
Andy Huangffc725f2012-10-01 14:48:02 -0700188 }
Andy Huang63b3c672012-10-05 19:27:28 -0700189
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
202function handleAllImageOnLoads() {
203 normalizeElementWidths(gImageLoadElements);
Andy Huangffc725f2012-10-01 14:48:02 -0700204 measurePositions();
Andy Huang63b3c672012-10-05 19:27:28 -0700205 // clear the queue so the next onload event starts a new job
206 gImageLoadElements = [];
Andy Huangffc725f2012-10-01 14:48:02 -0700207}
208
Andy Huang3233bff2012-03-20 19:38:45 -0700209function blockImage(imageElement) {
210 var src = imageElement.src;
Paul Westbrook41dca182012-08-07 10:43:42 -0700211 if (src.indexOf("http://") == 0 || src.indexOf("https://") == 0 ||
212 src.indexOf("content://") == 0) {
Andy Huang3233bff2012-03-20 19:38:45 -0700213 imageElement.setAttribute(BLOCKED_SRC_ATTR, src);
214 imageElement.src = "data:";
215 }
216}
217
Andy Huang23014702012-07-09 12:50:36 -0700218function setWideViewport() {
219 var metaViewport = document.getElementById('meta-viewport');
220 metaViewport.setAttribute('content', 'width=' + WIDE_VIEWPORT_WIDTH);
221}
222
Andy Huange964eee2012-10-02 19:24:58 -0700223function 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 Huang23014702012-07-09 12:50:36 -0700230// BEGIN Java->JavaScript handlers
Andy Huangf70fc402012-02-17 15:37:42 -0800231function measurePositions() {
Andy Huang7bdc3752012-03-25 17:18:19 -0700232 var overlayBottoms;
Andy Huangb5078b22012-03-05 19:52:29 -0800233 var h;
Andy Huangf70fc402012-02-17 15:37:42 -0800234 var i;
235 var len;
236
Andy Huang7bdc3752012-03-25 17:18:19 -0700237 var expandedSpacerDivs = document.querySelectorAll(".expanded > .spacer");
Andy Huangf70fc402012-02-17 15:37:42 -0800238
Andy Huang7bdc3752012-03-25 17:18:19 -0700239 overlayBottoms = new Array(expandedSpacerDivs.length + 1);
240 for (i = 0, len = expandedSpacerDivs.length; i < len; i++) {
241 h = expandedSpacerDivs[i].offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800242 // addJavascriptInterface handler only supports string arrays
Andy Huang7bdc3752012-03-25 17:18:19 -0700243 overlayBottoms[i] = "" + (getTotalOffset(expandedSpacerDivs[i]).top + h);
Andy Huangf70fc402012-02-17 15:37:42 -0800244 }
Andy Huang7bdc3752012-03-25 17:18:19 -0700245 // add an extra one to mark the bottom of the last message
246 overlayBottoms[i] = "" + document.body.offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800247
Andy Huang7bdc3752012-03-25 17:18:19 -0700248 window.mail.onWebContentGeometryChange(overlayBottoms);
Andy Huangf70fc402012-02-17 15:37:42 -0800249}
250
Andy Huang3233bff2012-03-20 19:38:45 -0700251function 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 Huangc7543572012-04-03 15:34:29 -0700268
Mark Weiab2d9982012-09-25 13:06:17 -0700269function 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 Huangc7543572012-04-03 15:34:29 -0700279function 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
289function 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 Huangffc725f2012-10-01 14:48:02 -0700302
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 Huangc7543572012-04-03 15:34:29 -0700309 setMessageHeaderSpacerHeight(messageDomId, spacerHeight);
310}
Andy Huang46dfba62012-04-19 01:47:32 -0700311
312function 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}
mindyp3bcf1802012-09-09 11:17:00 -0700331
332function onContentReady(event) {
333 window.mail.onContentReady();
334}
335
Andy Huang014ea4c2012-09-25 14:50:54 -0700336function 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 Huang3233bff2012-03-20 19:38:45 -0700349// END Java->JavaScript handlers
350
mindyp3bcf1802012-09-09 11:17:00 -0700351window.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);
mindyp32d911f2012-09-24 15:14:22 -0700362 document.getElementById("initial-load-signal").style.webkitAnimationName = 'initial-load';
mindyp3bcf1802012-09-09 11:17:00 -0700363};
364
Andy Huang014ea4c2012-09-25 14:50:54 -0700365collapseAllQuotedText();
Andy Huang3233bff2012-03-20 19:38:45 -0700366hideUnsafeImages();
Andy Huangffc725f2012-10-01 14:48:02 -0700367normalizeAllMessageWidths();
Andy Huang23014702012-07-09 12:50:36 -0700368//setWideViewport();
Andy Huange964eee2012-10-02 19:24:58 -0700369restoreScrollPosition();
Andy Huangf70fc402012-02-17 15:37:42 -0800370measurePositions();
371