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

Side by Side Diff: chrome/browser/resources/touch_ntp/slider.js

Issue 6661024: Use a specialized new tab page in TOUCH_UI builds (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Tweaks based on CR feedback from arv Created 9 years, 9 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
(Empty)
1 // Copyright (c) 2011 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.
4
5 /**
6 * @fileoverview Card slider implementation. Allows you to create interactions
7 * that have items that can slide left to right to reveal additional items.
8 * Works by adding the necessary event handlers to a specific DOM structure
9 * including a frame, container and cards.
10 * - The frame defines the boundary of one item. Each card will be expanded to
11 * fill the width of the frame. This element is also overflow hidden so that
12 * the additional items left / right do not trigger horizontal scrolling.
13 * - The container is what all the touch events are attached to. This element
14 * will be expanded to be the width of all cards.
15 * - The cards are the individual viewable items. There should be one card for
16 * each item in the list. Only one card will be visible at a time. Two cards
17 * will be visible while you are transitioning between cards.
18 *
19 * This class is designed to work well on any hardware-accelerated touch device.
20 * It should still work on pre-hardware accelerated devices it just won't feel
21 * very good. It should also work well with a mouse.
22 */
23
24 /**
25 * @constructor
26 * @param {!Element} frame The bounding rectangle that cards are visible in.
27 * @param {!Element} container The surrounding element that will have event
28 * listeners attached to it.
29 * @param {!Array.<!Element>} cards The individual viewable cards.
30 * @param {number} currentCard The index of the card that is currently visible.
31 * @param {number} cardWidth The width of each card should have.
32 */
33 function Slider(frame, container, cards, currentCard, cardWidth) {
34 /**
35 * @type {!Element}
36 * @private
37 */
38 this.frame_ = frame;
39
40 /**
41 * @type {!Element}
42 * @private
43 */
44 this.container_ = container;
45
46 /**
47 * @type {!Array.<!Element>}
48 * @private
49 */
50 this.cards_ = cards;
51
52 /**
53 * @type {number}
54 * @private
55 */
56 this.currentCard_ = currentCard;
57
58 /**
59 * @type {number}
60 * @private
61 */
62 this.cardWidth_ = cardWidth;
63
64 /**
65 * @type {!TouchHandler}
66 * @private
67 */
68 this.touchHandler_ = new TouchHandler(this.container_);
69 }
70
71
72 /**
73 * Events fired by the slider.
74 * Events are fired at the container.
75 */
76 Slider.EventType = {
77 // Fired when the user slides to another card.
78 CARD_CHANGED: 'slider:card_changed'
79 };
80
81
82 /**
83 * The current left offset of the container relative to the frame.
84 * @type {number}
85 * @private
86 */
87 Slider.prototype.currentLeft_ = 0;
88
89
90 /**
91 * The time to transition between cards when animating. Measured in ms.
92 * @type {number}
93 * @private
94 */
95 Slider.TRANSITION_TIME_ = 200;
96
97
98 /**
99 * The minimum velocity required to transition cards if they did not drag past
100 * the halfway point between cards. Measured in pixels / ms.
101 * @type {number}
102 * @private
103 */
104 Slider.TRANSITION_VELOCITY_THRESHOLD_ = 0.2;
105
106 /**
107 * Initialize all elements and event handlers. Must call after construction and
108 * before usage.
109 */
110 Slider.prototype.initialize = function() {
111 var view = this.container_.ownerDocument.defaultView;
112 assert(view.getComputedStyle(this.container_).display == '-webkit-box',
113 'Container should be display -webkit-box.');
114 assert(view.getComputedStyle(this.frame_).overflow == 'hidden',
115 'Frame should be overflow hidden.');
116 assert(view.getComputedStyle(this.container_).position == 'static',
117 'Container should be position static.');
118 for (var i = 0, card; card = this.cards_[i]; i++)
119 assert(view.getComputedStyle(card).position == 'static',
120 'Cards should be position static.');
121
122 this.updateCardWidths_();
123 this.transformToCurrentCard_();
124
125 this.container_.addEventListener(TouchHandler.EventType.TOUCH_START,
126 this.onTouchStart_.bind(this), false);
127 this.container_.addEventListener(TouchHandler.EventType.DRAG_START,
128 this.onDragStart_.bind(this), false);
129 this.container_.addEventListener(TouchHandler.EventType.DRAG_MOVE,
130 this.onDragMove_.bind(this), false);
131 this.container_.addEventListener(TouchHandler.EventType.DRAG_END,
132 this.onDragEnd_.bind(this), false);
133
134 this.touchHandler_.enable(/* opt_capture */ false);
135 };
136
137
138 /**
139 * Use in cases where the width of the frame has changed in order to update the
140 * width of cards. For example should be used when orientation changes in
141 * full width sliders.
142 * @param {number} newCardWidth Width all cards should have, in pixels.
143 */
144 Slider.prototype.resize = function(newCardWidth) {
145 if (newCardWidth != this.cardWidth_) {
146 this.cardWidth_ = newCardWidth;
147
148 this.updateCardWidths_();
149
150 // Must upate the transform on the container to show the correct card.
151 this.transformToCurrentCard_();
152 }
153 };
154
155
156 /**
157 * Sets the cards used. Can be called more than once to switch card sets.
158 * @param {!Array.<!Element>} cards The individual viewable cards.
159 * @param {number} index Index of the card to in the new set of cards to
160 * navigate to.
161 */
162 Slider.prototype.setCards = function(cards, index) {
163 assert(index >= 0 && index < cards.length,
164 'Invalid index in Slider#setCards');
165 this.cards_ = cards;
166
167 this.updateCardWidths_();
168
169 // Jump to the given card index.
170 this.setCurrentCard(index);
171 };
172
173
174 /**
175 * Updates the width of each card.
176 * @private
177 */
178 Slider.prototype.updateCardWidths_ = function() {
179 for (var i = 0, card; card = this.cards_[i]; i++)
180 card.style.width = this.cardWidth_ + 'px';
181 };
182
183
184 /**
185 * Returns the index of the current card.
186 * @return {number} index of the current card.
187 */
188 Slider.prototype.getCurrentCard = function() {
189 return this.currentCard_;
190 };
191
192 /**
193 * Clear any transition that is in progress and enable dragging for the touch.
194 * @param {!CustomEvent} e The TouchHandler event.
195 * @private
196 */
197 Slider.prototype.onTouchStart_ = function(e) {
198 this.container_.style.WebkitTransition = '';
199 e.detail.enableDrag = true;
200 };
201
202
203 /**
204 * Tell the TouchHandler that dragging is acceptable when the user begins by
205 * scrolling horizontally.
206 * @param {!CustomEvent} e The TouchHandler event.
207 * @private
208 */
209 Slider.prototype.onDragStart_ = function(e) {
210 e.detail.enableDrag = Math.abs(e.detail.dragDeltaX) >
211 Math.abs(e.detail.dragDeltaY);
212 };
213
214
215 /**
216 * On each drag move event reposition the container appropriately so the cards
217 * look like they are sliding.
218 * @param {!CustomEvent} e The TouchHandler event.
219 * @private
220 */
221 Slider.prototype.onDragMove_ = function(e) {
222 var deltaX = e.detail.dragDeltaX;
223 // If dragging beyond the first or last card then apply a backoff so the
224 // dragging feels stickier than usual.
225 if (!this.currentCard_ && deltaX > 0 ||
226 this.currentCard_ == (this.cards_.length - 1) && deltaX < 0) {
227 deltaX /= 2;
228 }
229 this.translateTo_(this.currentLeft_ + deltaX);
230 };
231
232 /**
233 * Moves the view to the specified position.
234 * @param {number} x Horizontal position to move to.
235 * @private
236 */
237 Slider.prototype.translateTo_ = function(x) {
238 // We use a webkitTransform to slide because this is GPU accelerated on Chrome
239 // and iOS. Once Chrome does GPU acceleration on the position fixed-layout
240 // elements we could simply set the element's position to fixed and modify
241 // 'left' instead.
242 this.container_.style.WebkitTransform = 'translate3d(' + x + 'px, 0, 0)';
243 };
244
245 /**
246 * On drag end events we may want to transition to another card, depending on
247 * the ending position of the drag and the velocity of the drag.
248 * @param {!CustomEvent} e The TouchHandler event.
249 * @private
250 */
251 Slider.prototype.onDragEnd_ = function(e) {
252 var deltaX = e.detail.dragDeltaX;
253 var velocity = this.touchHandler_.getEndVelocity().x;
254 var newX = this.currentLeft_ + deltaX;
255 var newCardIndex = Math.round(-newX / this.cardWidth_);
256
257 if (newCardIndex == this.currentCard_ && Math.abs(velocity) >
258 Slider.TRANSITION_VELOCITY_THRESHOLD_) {
259 // If the drag wasn't far enough to change cards but the velocity was high
260 // enough to transition anyways.
261 // If the velocity is to the left (negative) then the user wishes to go
262 // right (card +1).
263 newCardIndex += velocity > 0 ? -1 : 1;
264 }
265
266 this.setCurrentCard(newCardIndex, /* animate */ true);
267 };
268
269 /**
270 * Cancel any current touch/slide as if we saw a touch end
271 */
272 Slider.prototype.cancelTouch = function() {
273 // Stop listening to any current touch
274 this.touchHandler_.cancelTouch();
275
276 // Ensure we're at a card bounary
277 this.transformToCurrentCard_(true);
278 };
279
280 /**
281 * Selects a new card, ensuring that it is a valid index, transforming the
282 * view and possibly calling the change card callback.
283 * @param {number} newCardIndex Index of card to show.
284 * @param {boolean=} opt_animate If true will animate transition from current
285 * position to new position.
286 */
287 Slider.prototype.setCurrentCard =
288 function(newCardIndex, opt_animate) {
289 var isChangingCard = newCardIndex >= 0 && newCardIndex < this.cards_.length &&
290 newCardIndex != this.currentCard_;
291 if (isChangingCard)
292 // If we have a new card index and it is valid then update the left position
293 // and current card index.
294 this.currentCard_ = newCardIndex;
295
296 this.transformToCurrentCard_(opt_animate);
297
298 if (isChangingCard) {
299 var event = document.createEvent('HTMLEvents');
300 event.initEvent(Slider.EventType.CARD_CHANGED, true, true);
301 event.sender = this;
302 this.container_.dispatchEvent(event);
303 }
304 };
305
306 /**
307 * Centers the view on the card denoted by this.currentCard_. Can either animate
308 * to that card or snap to it.
309 * @param {boolean=} opt_animate If true will animate transition from current
310 * position to new position.
311 * @private
312 */
313 Slider.prototype.transformToCurrentCard_ = function(opt_animate) {
314 this.currentLeft_ = -this.currentCard_ * this.cardWidth_;
315
316 // Animate to the current card, which will either transition if the current
317 // card is new, or reset the existing card if we didn't drag enough to change
318 // cards.
319 var transition = '';
320 if (opt_animate) {
321 transition = '-webkit-transform ' + Slider.TRANSITION_TIME_ +
322 'ms ease-in-out';
323 }
324 this.container_.style.WebkitTransition = transition;
325 this.translateTo_(this.currentLeft_);
326 };
327
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698