Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 Card slider implementation. Allows you to create interactions | 6 * @fileoverview Card slider implementation. Allows you to create interactions |
| 7 * that have items that can slide left to right to reveal additional items. | 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 | 8 * Works by adding the necessary event handlers to a specific DOM structure |
| 9 * including a frame, container and cards. | 9 * including a frame, container and cards. |
| 10 * - The frame defines the boundary of one item. Each card will be expanded to | 10 * - The frame defines the boundary of one item. Each card will be expanded to |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 }, | 214 }, |
| 215 | 215 |
| 216 /** | 216 /** |
| 217 * Handle horizontal scrolls to flip between pages. | 217 * Handle horizontal scrolls to flip between pages. |
| 218 * @private | 218 * @private |
| 219 */ | 219 */ |
| 220 onMouseWheel_: function(e) { | 220 onMouseWheel_: function(e) { |
| 221 if (e.wheelDeltaX == 0) | 221 if (e.wheelDeltaX == 0) |
| 222 return; | 222 return; |
| 223 | 223 |
| 224 var scrollAmountPerPage = ntp4.isRTL() ? 120 : -120; | 224 var preventDefault = true; |
| 225 // If the horizontal scroll didn't change cards (on the far right of | |
| 226 // the NTP), let the browser take a shot at the event as well. | |
| 227 if (this.currentCard === 0 && this.mouseWheelScrollAmount_ > 0) | |
| 228 preventDefault = false; | |
| 229 if (this.currentCard === this.cards_.length - 1 && | |
| 230 this.mouseWheelScrollAmount_ < 0) | |
|
Evan Stade
2011/08/19 22:35:40
curlies
Nico
2011/08/19 22:54:47
Done.
| |
| 231 preventDefault = false; | |
| 232 | |
| 233 var scrollAmountPerPage = cr.isMac ? 400 : 120; | |
|
Evan Stade
2011/08/19 22:35:40
it strikes me you should test this with a mouse wi
Nico
2011/08/19 22:54:47
I tested with a multitouch touch pad (feels good),
Evan Stade
2011/08/19 23:10:56
you could do something hacky like "if the scroll v
| |
| 234 if (!ntp4.isRTL()) | |
| 235 scrollAmountPerPage *= -1; | |
| 225 this.mouseWheelScrollAmount_ += e.wheelDeltaX; | 236 this.mouseWheelScrollAmount_ += e.wheelDeltaX; |
| 226 if (Math.abs(this.mouseWheelScrollAmount_) >= | 237 if (Math.abs(this.mouseWheelScrollAmount_) >= |
| 227 Math.abs(scrollAmountPerPage)) { | 238 Math.abs(scrollAmountPerPage)) { |
| 228 var pagesToScroll = this.mouseWheelScrollAmount_ / scrollAmountPerPage; | 239 var pagesToScroll = this.mouseWheelScrollAmount_ / scrollAmountPerPage; |
| 229 pagesToScroll = | 240 pagesToScroll = |
| 230 (pagesToScroll > 0 ? Math.floor : Math.ceil)(pagesToScroll); | 241 (pagesToScroll > 0 ? Math.floor : Math.ceil)(pagesToScroll); |
| 231 var newCardIndex = this.currentCard + pagesToScroll; | 242 var newCardIndex = this.currentCard + pagesToScroll; |
| 232 newCardIndex = Math.min(this.cards_.length, | 243 newCardIndex = Math.min(this.cards_.length - 1, |
| 233 Math.max(0, newCardIndex)); | 244 Math.max(0, newCardIndex)); |
| 234 this.selectCard(newCardIndex, true); | 245 this.selectCard(newCardIndex, true); |
| 235 this.mouseWheelScrollAmount_ -= pagesToScroll * scrollAmountPerPage; | 246 this.mouseWheelScrollAmount_ -= pagesToScroll * scrollAmountPerPage; |
| 236 } | 247 } |
| 237 | 248 |
| 238 // We got a mouse wheel event, so cancel any pending scroll wheel timeout. | 249 // We got a mouse wheel event, so cancel any pending scroll wheel timeout. |
| 239 if (this.scrollClearTimeout_ != null) | 250 if (this.scrollClearTimeout_ != null) |
| 240 clearTimeout(this.scrollClearTimeout_); | 251 clearTimeout(this.scrollClearTimeout_); |
| 241 // If we didn't use up all the scroll, hold onto it for a little bit, but | 252 // If we didn't use up all the scroll, hold onto it for a little bit, but |
| 242 // drop it after a delay. | 253 // drop it after a delay. |
| 243 if (this.mouseWheelScrollAmount_ != 0) { | 254 if (this.mouseWheelScrollAmount_ != 0) { |
| 244 this.scrollClearTimeout_ = | 255 this.scrollClearTimeout_ = |
| 245 setTimeout(this.clearMouseWheelScroll_.bind(this), 500); | 256 setTimeout(this.clearMouseWheelScroll_.bind(this), 500); |
| 246 } | 257 } |
| 258 | |
| 259 if (preventDefault) | |
| 260 e.preventDefault(); | |
| 247 }, | 261 }, |
| 248 | 262 |
| 249 /** | 263 /** |
| 250 * Resets the amount of horizontal scroll we've seen to 0. See | 264 * Resets the amount of horizontal scroll we've seen to 0. See |
| 251 * onMouseWheel_. | 265 * onMouseWheel_. |
| 252 * @private | 266 * @private |
| 253 */ | 267 */ |
| 254 clearMouseWheelScroll_: function() { | 268 clearMouseWheelScroll_: function() { |
| 255 this.mouseWheelScrollAmount_ = 0; | 269 this.mouseWheelScrollAmount_ = 0; |
| 256 }, | 270 }, |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 // Stop listening to any current touch | 414 // Stop listening to any current touch |
| 401 this.touchHandler_.cancelTouch(); | 415 this.touchHandler_.cancelTouch(); |
| 402 | 416 |
| 403 // Ensure we're at a card bounary | 417 // Ensure we're at a card bounary |
| 404 this.transformToCurrentCard_(true); | 418 this.transformToCurrentCard_(true); |
| 405 }, | 419 }, |
| 406 }; | 420 }; |
| 407 | 421 |
| 408 return CardSlider; | 422 return CardSlider; |
| 409 })(); | 423 })(); |
| OLD | NEW |