| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview A collection of utility methods for UberPage and its contained | 6 * @fileoverview A collection of utility methods for UberPage and its contained |
| 7 * pages. | 7 * pages. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('uber', function() { | 10 cr.define('uber', function() { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 var scrollLeft = scrollLeftForDocument(document); | 44 var scrollLeft = scrollLeftForDocument(document); |
| 45 var offset = scrollLeft * -1; | 45 var offset = scrollLeft * -1; |
| 46 for (var i = 0; i < headerElements.length; i++) { | 46 for (var i = 0; i < headerElements.length; i++) { |
| 47 // As a workaround for http://crbug.com/231830, set the transform to | 47 // As a workaround for http://crbug.com/231830, set the transform to |
| 48 // 'none' rather than 0px. | 48 // 'none' rather than 0px. |
| 49 headerElements[i].style.webkitTransform = offset ? | 49 headerElements[i].style.webkitTransform = offset ? |
| 50 'translateX(' + offset + 'px)' : 'none'; | 50 'translateX(' + offset + 'px)' : 'none'; |
| 51 } | 51 } |
| 52 | 52 |
| 53 invokeMethodOnParent('adjustToScroll', scrollLeft); | 53 invokeMethodOnParent('adjustToScroll', scrollLeft); |
| 54 }; | 54 } |
| 55 | 55 |
| 56 /** | 56 /** |
| 57 * Handles 'message' events on window. | 57 * Handles 'message' events on window. |
| 58 * @param {Event} e The message event. | 58 * @param {Event} e The message event. |
| 59 */ | 59 */ |
| 60 function handleWindowMessage(e) { | 60 function handleWindowMessage(e) { |
| 61 e = /** @type{!MessageEvent.<!{method: string, params: *}>} */ (e); |
| 61 if (e.data.method === 'frameSelected') | 62 if (e.data.method === 'frameSelected') |
| 62 handleFrameSelected(); | 63 handleFrameSelected(); |
| 63 else if (e.data.method === 'mouseWheel') | 64 else if (e.data.method === 'mouseWheel') |
| 64 handleMouseWheel(e.data.params); | 65 handleMouseWheel( |
| 66 /** @type {{deltaX: number, deltaY: number}} */(e.data.params)); |
| 65 else if (e.data.method === 'popState') | 67 else if (e.data.method === 'popState') |
| 66 handlePopState(e.data.params.state, e.data.params.path); | 68 handlePopState(e.data.params.state, e.data.params.path); |
| 67 } | 69 } |
| 68 | 70 |
| 69 /** | 71 /** |
| 70 * This is called when a user selects this frame via the navigation bar | 72 * This is called when a user selects this frame via the navigation bar |
| 71 * frame (and is triggered via postMessage() from the uber page). | 73 * frame (and is triggered via postMessage() from the uber page). |
| 72 * @private | 74 * @private |
| 73 */ | 75 */ |
| 74 function handleFrameSelected() { | 76 function handleFrameSelected() { |
| 75 setScrollTopForDocument(document, 0); | 77 setScrollTopForDocument(document, 0); |
| 76 } | 78 } |
| 77 | 79 |
| 78 /** | 80 /** |
| 79 * Called when a user mouse wheels (or trackpad scrolls) over the nav frame. | 81 * Called when a user mouse wheels (or trackpad scrolls) over the nav frame. |
| 80 * The wheel event is forwarded here and we scroll the body. | 82 * The wheel event is forwarded here and we scroll the body. |
| 81 * There's no way to figure out the actual scroll amount for a given delta. | 83 * There's no way to figure out the actual scroll amount for a given delta. |
| 82 * It differs for every platform and even initWebKitWheelEvent takes a | 84 * It differs for every platform and even initWebKitWheelEvent takes a |
| 83 * pixel amount instead of a wheel delta. So we just choose something | 85 * pixel amount instead of a wheel delta. So we just choose something |
| 84 * reasonable and hope no one notices the difference. | 86 * reasonable and hope no one notices the difference. |
| 85 * @param {Object} params A structure that holds wheel deltas in X and Y. | 87 * @param {{deltaX: number, deltaY: number}} params A structure that holds |
| 88 * wheel deltas in X and Y. |
| 86 */ | 89 */ |
| 87 function handleMouseWheel(params) { | 90 function handleMouseWheel(params) { |
| 88 window.scrollBy(-params.deltaX * 49 / 120, -params.deltaY * 49 / 120); | 91 window.scrollBy(-params.deltaX * 49 / 120, -params.deltaY * 49 / 120); |
| 89 } | 92 } |
| 90 | 93 |
| 91 /** | 94 /** |
| 92 * Called when the parent window restores some state saved by uber.pushState | 95 * Called when the parent window restores some state saved by uber.pushState |
| 93 * or uber.replaceState. Simulates a popstate event. | 96 * or uber.replaceState. Simulates a popstate event. |
| 97 * @param {PopStateEvent} state A state object for replaceState and pushState. |
| 98 * @param {string} path The path the page navigated to. |
| 99 * @suppress {checkTypes} |
| 94 */ | 100 */ |
| 95 function handlePopState(state, path) { | 101 function handlePopState(state, path) { |
| 96 history.replaceState(state, '', path); | 102 window.history.replaceState(state, '', path); |
| 97 window.dispatchEvent(new PopStateEvent('popstate', {state: state})); | 103 window.dispatchEvent(new PopStateEvent('popstate', {state: state})); |
| 98 } | 104 } |
| 99 | 105 |
| 100 /** | 106 /** |
| 101 * @return {boolean} Whether this frame has a parent. | 107 * @return {boolean} Whether this frame has a parent. |
| 102 */ | 108 */ |
| 103 function hasParent() { | 109 function hasParent() { |
| 104 return window != window.parent; | 110 return window != window.parent; |
| 105 } | 111 } |
| 106 | 112 |
| 107 /** | 113 /** |
| 108 * Invokes a method on the parent window (UberPage). This is a convenience | 114 * Invokes a method on the parent window (UberPage). This is a convenience |
| 109 * method for API calls into the uber page. | 115 * method for API calls into the uber page. |
| 110 * @param {string} method The name of the method to invoke. | 116 * @param {string} method The name of the method to invoke. |
| 111 * @param {Object=} opt_params Optional property bag of parameters to pass to | 117 * @param {(Object|number)=} opt_params Optional property bag of parameters |
| 112 * the invoked method. | 118 * to pass to the invoked method. |
| 113 * @private | 119 * @private |
| 114 */ | 120 */ |
| 115 function invokeMethodOnParent(method, opt_params) { | 121 function invokeMethodOnParent(method, opt_params) { |
| 116 if (!hasParent()) | 122 if (!hasParent()) |
| 117 return; | 123 return; |
| 118 | 124 |
| 119 invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome'); | 125 invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome'); |
| 120 } | 126 } |
| 121 | 127 |
| 122 /** | 128 /** |
| 123 * Invokes a method on the target window. | 129 * Invokes a method on the target window. |
| 124 * @param {string} method The name of the method to invoke. | 130 * @param {string} method The name of the method to invoke. |
| 125 * @param {Object=} opt_params Optional property bag of parameters to pass to | 131 * @param {(Object|number)=} opt_params Optional property bag of parameters |
| 126 * the invoked method. | 132 * to pass to the invoked method. |
| 127 * @param {string=} opt_url The origin of the target window. | 133 * @param {string=} opt_url The origin of the target window. |
| 128 * @private | 134 * @private |
| 129 */ | 135 */ |
| 130 function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) { | 136 function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) { |
| 131 var data = {method: method, params: opt_params}; | 137 var data = {method: method, params: opt_params}; |
| 132 targetWindow.postMessage(data, opt_url ? opt_url : '*'); | 138 targetWindow.postMessage(data, opt_url ? opt_url : '*'); |
| 133 } | 139 } |
| 134 | 140 |
| 135 /** | 141 /** |
| 136 * Updates the page's history state. If the page is embedded in a child, | 142 * Updates the page's history state. If the page is embedded in a child, |
| 137 * forward the information to the parent for it to manage history for us. This | 143 * forward the information to the parent for it to manage history for us. This |
| 138 * is a replacement of history.replaceState and history.pushState. | 144 * is a replacement of history.replaceState and history.pushState. |
| 139 * @param {Object} state A state object for replaceState and pushState. | 145 * @param {Object} state A state object for replaceState and pushState. |
| 140 * @param {string} title The title of the page to replace. | |
| 141 * @param {string} path The path the page navigated to. | 146 * @param {string} path The path the page navigated to. |
| 142 * @param {boolean} replace If true, navigate with replacement. | 147 * @param {boolean} replace If true, navigate with replacement. |
| 143 * @private | 148 * @private |
| 144 */ | 149 */ |
| 145 function updateHistory(state, path, replace) { | 150 function updateHistory(state, path, replace) { |
| 146 var historyFunction = replace ? | 151 var historyFunction = replace ? |
| 147 window.history.replaceState : | 152 window.history.replaceState : |
| 148 window.history.pushState; | 153 window.history.pushState; |
| 149 | 154 |
| 150 if (hasParent()) { | 155 if (hasParent()) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 197 |
| 193 return { | 198 return { |
| 194 invokeMethodOnParent: invokeMethodOnParent, | 199 invokeMethodOnParent: invokeMethodOnParent, |
| 195 invokeMethodOnWindow: invokeMethodOnWindow, | 200 invokeMethodOnWindow: invokeMethodOnWindow, |
| 196 onContentFrameLoaded: onContentFrameLoaded, | 201 onContentFrameLoaded: onContentFrameLoaded, |
| 197 pushState: pushState, | 202 pushState: pushState, |
| 198 replaceState: replaceState, | 203 replaceState: replaceState, |
| 199 setTitle: setTitle, | 204 setTitle: setTitle, |
| 200 }; | 205 }; |
| 201 }); | 206 }); |
| OLD | NEW |