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
3 // found in the LICENSE file.
5 // This file contains the navigation controls that are visible on the left side
6 // of the uber page. It exists separately from uber.js so that it may be loaded
7 // in an iframe. Iframes can be layered on top of each other, but not mixed in
8 // with page content, so all overlapping content on uber must be framed.
10 <include src="../../../../ui/webui/resources/js/util.js">
11 <include src="uber_utils.js">
13 cr.define('uber_frame', function() {
16 * Handles page initialization.
19 var navigationItems = document.querySelectorAll('li');
21 for (var i = 0; i < navigationItems.length; ++i) {
22 navigationItems[i].addEventListener('click', onNavItemClicked);
25 window.addEventListener('message', handleWindowMessage);
26 uber.invokeMethodOnParent('navigationControlsLoaded');
28 document.documentElement.addEventListener('mousewheel', onMouseWheel);
29 document.documentElement.addEventListener('mousedown', onMouseDown);
30 cr.ui.FocusManager.disableMouseFocusOnButtons();
34 * Handles clicks on the navigation controls (switches the page and updates
36 * @param {Event} e The click event.
38 function onNavItemClicked(e) {
39 // Though pointer-event: none; is applied to the .selected nav item, users
40 // can still tab to them and press enter/space which simulates a click.
41 if (e.target.classList.contains('selected'))
44 // Extensions can override Uber content (e.g., if the user has a history
45 // extension, it should display when the 'History' navigation is clicked).
46 if (e.currentTarget.getAttribute('override') == 'yes') {
47 window.open('chrome://' + e.currentTarget.getAttribute('controls'),
52 uber.invokeMethodOnParent('showPage',
53 {pageId: e.currentTarget.getAttribute('controls')});
55 setSelection(e.currentTarget);
59 * Handles postMessage from chrome://chrome.
60 * @param {Event} e The post data.
62 function handleWindowMessage(e) {
63 if (e.data.method === 'changeSelection')
64 changeSelection(e.data.params);
65 else if (e.data.method === 'adjustToScroll')
66 adjustToScroll(e.data.params);
67 else if (e.data.method === 'setContentChanging')
68 setContentChanging(e.data.params);
70 console.error('Received unexpected message', e.data);
74 * Changes the selected nav control.
75 * @param {Object} params Must contain pageId.
77 function changeSelection(params) {
79 document.querySelector('li[controls="' + params.pageId + '"]');
80 setSelection(navItem);
85 * @return {Element} The currently selected nav item, if any.
87 function getSelectedNavItem() {
88 return document.querySelector('li.selected');
92 * Sets selection on the given nav item.
93 * @param {Element} newSelection The item to be selected.
95 function setSelection(newSelection) {
96 var lastSelectedNavItem = getSelectedNavItem();
97 if (lastSelectedNavItem !== newSelection) {
98 newSelection.classList.add('selected');
99 if (lastSelectedNavItem)
100 lastSelectedNavItem.classList.remove('selected');
105 * Shows nav items belonging to the same group as the selected item.
107 function showNavItems() {
108 var navItems = document.querySelectorAll('li');
109 var selectedNavItem = getSelectedNavItem();
110 assert(selectedNavItem);
112 var selectedGroup = selectedNavItem.getAttribute('group');
113 for (var i = 0; i < navItems.length; ++i) {
114 navItems[i].hidden = navItems[i].getAttribute('group') != selectedGroup;
119 * Adjusts this frame's content to scrolls from the outer frame. This is done
120 * to obscure text in RTL as a user scrolls over the content of this frame (as
121 * currently RTL scrollbars still draw on the right).
122 * @param {number} scroll document.body.scrollLeft of the content frame.
124 function adjustToScroll(scrollLeft) {
126 document.body.style.webkitTransform = 'translateX(' + -scrollLeft + 'px)';
130 * Enable/disable an animation to ease the nav bar back into view when
131 * changing content while horizontally scrolled.
132 * @param {boolean} enabled Whether easing should be enabled.
134 function setContentChanging(enabled) {
136 document.documentElement.classList[enabled ? 'add' : 'remove'](
141 * Handles mouse wheels on the top level element. Forwards them to uber.js.
142 * @param {Event} e The mouse wheel event.
144 function onMouseWheel(e) {
145 uber.invokeMethodOnParent('mouseWheel',
146 {deltaX: e.wheelDeltaX, deltaY: e.wheelDeltaY});
150 * Handles mouse presses on the top level element. Forwards them to uber.js.
151 * @param {Event} e The mouse down event.
153 function onMouseDown(e) {
154 uber.invokeMethodOnParent('mouseDown');
158 * @return {Element} The currently selected iframe container.
161 function getSelectedIframe() {
162 return document.querySelector('.iframe-container.selected');
166 * Finds the <li> element whose 'controls' attribute is |controls| and sets
167 * its 'override' attribute to |override|.
168 * @param {string} controls The value of the 'controls' attribute of the
170 * @param {string} override The value to set for the 'override' attribute of
171 * that element (either 'yes' or 'no').
173 function setNavigationOverride(controls, override) {
175 document.querySelector('li[controls="' + controls + '"]');
176 navItem.setAttribute('override', override);
181 setNavigationOverride: setNavigationOverride,
186 document.addEventListener('DOMContentLoaded', uber_frame.onLoad);