blob: 1e9a2a87440d652fd824256295ed02af9d6f5238 [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
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 */
26function 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
42function 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
50function 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
64function 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
90function measurePositions() {
Andy Huangb5078b22012-03-05 19:52:29 -080091 var headerHeights;
Andy Huang2ffaeab2012-02-27 19:43:46 -080092 var headerBottoms;
Andy Huangb5078b22012-03-05 19:52:29 -080093 var h;
Andy Huangf70fc402012-02-17 15:37:42 -080094 var i;
95 var len;
96
Andy Huang2ffaeab2012-02-27 19:43:46 -080097 var headers = document.querySelectorAll(".spacer");
Andy Huangf70fc402012-02-17 15:37:42 -080098
Andy Huangb5078b22012-03-05 19:52:29 -080099 headerHeights = new Array(headers.length);
Andy Huang2ffaeab2012-02-27 19:43:46 -0800100 headerBottoms = new Array(headers.length);
Andy Huangf70fc402012-02-17 15:37:42 -0800101 for (i = 0, len = headers.length; i < len; i++) {
Andy Huangb5078b22012-03-05 19:52:29 -0800102 h = headers[i].offsetHeight;
Andy Huangf70fc402012-02-17 15:37:42 -0800103 // addJavascriptInterface handler only supports string arrays
Andy Huangb5078b22012-03-05 19:52:29 -0800104 headerHeights[i] = "" + h;
105 headerBottoms[i] = "" + (getTotalOffset(headers[i]).top + h);
Andy Huangf70fc402012-02-17 15:37:42 -0800106 }
107
Andy Huangb5078b22012-03-05 19:52:29 -0800108 window.mail.onWebContentGeometryChange(headerBottoms, headerHeights);
Andy Huangf70fc402012-02-17 15:37:42 -0800109}
110
111collapseQuotedText();
112shrinkWideMessages();
113measurePositions();
114