Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(567)

Side by Side Diff: chrome/browser/resources/uber/uber_utils.js

Issue 298553002: Options: maintain history entries on the parent frame. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More comments (try jobs on previous) Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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() {
11 11
12 /** 12 /**
13 * Fixed position header elements on the page to be shifted by handleScroll. 13 * Fixed position header elements on the page to be shifted by handleScroll.
14 * @type {NodeList} 14 * @type {NodeList}
15 */ 15 */
16 var headerElements; 16 var headerElements;
17 17
18 /** 18 /**
19 * This should be called by uber content pages when DOM content has loaded. 19 * This should be called by uber content pages when DOM content has loaded.
20 */ 20 */
21 function onContentFrameLoaded() { 21 function onContentFrameLoaded() {
22 headerElements = document.getElementsByTagName('header'); 22 headerElements = document.getElementsByTagName('header');
23 document.addEventListener('scroll', handleScroll); 23 document.addEventListener('scroll', handleScroll);
24 24
25 invokeMethodOnParent('ready');
26
25 // Prevent the navigation from being stuck in a disabled state when a 27 // Prevent the navigation from being stuck in a disabled state when a
26 // content page is reloaded while an overlay is visible (crbug.com/246939). 28 // content page is reloaded while an overlay is visible (crbug.com/246939).
27 invokeMethodOnParent('stopInterceptingEvents'); 29 invokeMethodOnParent('stopInterceptingEvents');
28 30
29 // Trigger the scroll handler to tell the navigation if our page started 31 // Trigger the scroll handler to tell the navigation if our page started
30 // with some scroll (happens when you use tab restore). 32 // with some scroll (happens when you use tab restore).
31 handleScroll(); 33 handleScroll();
32 34
33 window.addEventListener('message', handleWindowMessage); 35 window.addEventListener('message', handleWindowMessage);
34 } 36 }
(...skipping 18 matching lines...) Expand all
53 55
54 /** 56 /**
55 * Handles 'message' events on window. 57 * Handles 'message' events on window.
56 * @param {Event} e The message event. 58 * @param {Event} e The message event.
57 */ 59 */
58 function handleWindowMessage(e) { 60 function handleWindowMessage(e) {
59 if (e.data.method === 'frameSelected') 61 if (e.data.method === 'frameSelected')
60 handleFrameSelected(); 62 handleFrameSelected();
61 else if (e.data.method === 'mouseWheel') 63 else if (e.data.method === 'mouseWheel')
62 handleMouseWheel(e.data.params); 64 handleMouseWheel(e.data.params);
65 else if (e.data.method === 'popState')
66 handlePopState(e.data.params.state, e.data.params.path);
63 } 67 }
64 68
65 /** 69 /**
66 * This is called when a user selects this frame via the navigation bar 70 * This is called when a user selects this frame via the navigation bar
67 * frame (and is triggered via postMessage() from the uber page). 71 * frame (and is triggered via postMessage() from the uber page).
68 * @private 72 * @private
69 */ 73 */
70 function handleFrameSelected() { 74 function handleFrameSelected() {
71 setScrollTopForDocument(document, 0); 75 setScrollTopForDocument(document, 0);
72 } 76 }
73 77
74 /** 78 /**
75 * Called when a user mouse wheels (or trackpad scrolls) over the nav frame. 79 * Called when a user mouse wheels (or trackpad scrolls) over the nav frame.
76 * The wheel event is forwarded here and we scroll the body. 80 * The wheel event is forwarded here and we scroll the body.
77 * There's no way to figure out the actual scroll amount for a given delta. 81 * There's no way to figure out the actual scroll amount for a given delta.
78 * It differs for every platform and even initWebKitWheelEvent takes a 82 * It differs for every platform and even initWebKitWheelEvent takes a
79 * pixel amount instead of a wheel delta. So we just choose something 83 * pixel amount instead of a wheel delta. So we just choose something
80 * reasonable and hope no one notices the difference. 84 * reasonable and hope no one notices the difference.
81 * @param {Object} params A structure that holds wheel deltas in X and Y. 85 * @param {Object} params A structure that holds wheel deltas in X and Y.
82 */ 86 */
83 function handleMouseWheel(params) { 87 function handleMouseWheel(params) {
84 window.scrollBy(-params.deltaX * 49 / 120, -params.deltaY * 49 / 120); 88 window.scrollBy(-params.deltaX * 49 / 120, -params.deltaY * 49 / 120);
85 } 89 }
86 90
87 /** 91 /**
92 * Called when the parent window restores some state saved by uber.pushState
93 * or uber.replaceState. Simulates a popstate event.
94 */
95 function handlePopState(state, path) {
96 history.replaceState(state, '', path);
97 window.dispatchEvent(new PopStateEvent('popstate', {state: state}));
Dan Beam 2014/05/19 23:06:49 why do we simulate a popstate event here?
davidben 2014/05/19 23:32:28 history.pushState and history.replaceState don't t
98 }
99
100 /**
101 * Returns true if this frame has a parent.
Dan Beam 2014/05/19 23:06:49 @return {boolean} Whether this frame has parent.
davidben 2014/05/19 23:32:28 Done.
102 */
103 function hasParent() {
104 return window != window.parent;
105 }
106
107 /**
88 * Invokes a method on the parent window (UberPage). This is a convenience 108 * Invokes a method on the parent window (UberPage). This is a convenience
89 * method for API calls into the uber page. 109 * method for API calls into the uber page.
90 * @param {string} method The name of the method to invoke. 110 * @param {string} method The name of the method to invoke.
91 * @param {Object=} opt_params Optional property bag of parameters to pass to 111 * @param {Object=} opt_params Optional property bag of parameters to pass to
92 * the invoked method. 112 * the invoked method.
93 * @private 113 * @private
94 */ 114 */
95 function invokeMethodOnParent(method, opt_params) { 115 function invokeMethodOnParent(method, opt_params) {
96 if (window.location == window.parent.location) 116 if (!hasParent())
97 return; 117 return;
98 118
99 invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome'); 119 invokeMethodOnWindow(window.parent, method, opt_params, 'chrome://chrome');
100 } 120 }
101 121
102 /** 122 /**
103 * Invokes a method on the target window. 123 * Invokes a method on the target window.
104 * @param {string} method The name of the method to invoke. 124 * @param {string} method The name of the method to invoke.
105 * @param {Object=} opt_params Optional property bag of parameters to pass to 125 * @param {Object=} opt_params Optional property bag of parameters to pass to
106 * the invoked method. 126 * the invoked method.
107 * @param {string=} opt_url The origin of the target window. 127 * @param {string=} opt_url The origin of the target window.
108 * @private 128 * @private
109 */ 129 */
110 function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) { 130 function invokeMethodOnWindow(targetWindow, method, opt_params, opt_url) {
111 var data = {method: method, params: opt_params}; 131 var data = {method: method, params: opt_params};
112 targetWindow.postMessage(data, opt_url ? opt_url : '*'); 132 targetWindow.postMessage(data, opt_url ? opt_url : '*');
113 } 133 }
114 134
135 /**
136 * 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
138 * is a replacement of history.replaceState and history.pushState.
139 * @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.
142 * @param {boolean} replace If true, navigate with replacement.
143 * @private
144 */
145 function updateHistory(state, path, replace) {
146 var historyFunction = replace ?
147 window.history.replaceState :
148 window.history.pushState;
Dan Beam 2014/05/19 23:06:49 indent 4\s instead of 2\s
davidben 2014/05/19 23:32:28 Done.
149
150 if (hasParent()) {
151 // If there's a parent, always replaceState. The parent will do the actual
152 // pushState.
153 historyFunction = window.history.replaceState;
154 invokeMethodOnParent('updateHistory', {
155 state: state, path: path, replace: replace});
156 }
157 historyFunction.call(window.history, state, '', '/' + path);
158 }
159
160 /**
161 * Sets the current title for the page. If the page is embedded in a child,
162 * forward the information to the parent. This is a replacement for setting
163 * document.title.
164 * @param {string} title The new title for the page.
165 */
166 function setTitle(title) {
167 document.title = title;
168 invokeMethodOnParent('setTitle', {title: title});
169 }
170
171 /**
172 * Pushes new history state for the page. If the page is embedded in a child,
173 * forward the information to the parent; when embedded, all history entries
174 * are attached to the parent. This is a replacement of history.pushState.
175 * @param {Object} state A state object for replaceState and pushState.
176 * @param {string} path The path the page navigated to.
177 */
178 function pushState(state, path) {
179 updateHistory(state, path, false);
180 }
181
182 /**
183 * Replaces the page's history state. If the page is embedded in a child,
184 * forward the information to the parent; when embedded, all history entries
185 * are attached to the parent. This is a replacement of history.replaceState.
186 * @param {Object} state A state object for replaceState and pushState.
187 * @param {string} path The path the page navigated to.
188 */
189 function replaceState(state, path) {
190 updateHistory(state, path, true);
191 }
192
115 return { 193 return {
116 invokeMethodOnParent: invokeMethodOnParent, 194 invokeMethodOnParent: invokeMethodOnParent,
117 invokeMethodOnWindow: invokeMethodOnWindow, 195 invokeMethodOnWindow: invokeMethodOnWindow,
118 onContentFrameLoaded: onContentFrameLoaded, 196 onContentFrameLoaded: onContentFrameLoaded,
197 pushState: pushState,
198 replaceState: replaceState,
199 setTitle: setTitle,
119 }; 200 };
120 }); 201 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698