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.
6 * @fileoverview Page switcher
7 * This is the class for the left and right navigation arrows that switch
10 cr
.define('ntp', function() {
12 function PageSwitcher() {
15 PageSwitcher
.template
= {
16 __proto__
: HTMLButtonElement
.prototype,
18 decorate: function(el
) {
19 el
.__proto__
= PageSwitcher
.template
;
21 el
.addEventListener('click', el
.activate_
);
23 el
.direction_
= el
.id
== 'page-switcher-start' ? -1 : 1;
25 el
.dragWrapper_
= new cr
.ui
.DragWrapper(el
, el
);
29 * Activate the switcher (go to the next card).
32 activate_: function() {
33 ntp
.getCardSlider().selectCard(this.nextCardIndex_(), true);
37 * Calculate the index of the card that this button will switch to.
40 nextCardIndex_: function() {
41 var cardSlider
= ntp
.getCardSlider();
42 var index
= cardSlider
.currentCard
+ this.direction_
;
43 var numCards
= cardSlider
.cardCount
- 1;
44 return Math
.max(0, Math
.min(index
, numCards
));
48 * Update the accessible label attribute of this button, based on the
49 * current position in the card slider and the names of the cards.
50 * @param {NodeList} dots The dot elements which display the names of the
53 updateButtonAccessibleLabel: function(dots
) {
54 var currentIndex
= ntp
.getCardSlider().currentCard
;
55 var nextCardIndex
= this.nextCardIndex_();
56 if (nextCardIndex
== currentIndex
) {
57 this.setAttribute('aria-label', ''); // No next card.
61 var currentDot
= dots
[currentIndex
];
62 var nextDot
= dots
[nextCardIndex
];
63 if (!currentDot
|| !nextDot
) {
64 this.setAttribute('aria-label', ''); // Dots not initialised yet.
68 var currentPageTitle
= currentDot
.displayTitle
;
69 var nextPageTitle
= nextDot
.displayTitle
;
70 var msgName
= (currentPageTitle
== nextPageTitle
) ?
71 'page_switcher_same_title' : 'page_switcher_change_title';
72 var ariaLabel
= loadTimeData
.getStringF(msgName
, nextPageTitle
);
73 this.setAttribute('aria-label', ariaLabel
);
76 shouldAcceptDrag: function(e
) {
77 // Only allow page switching when a drop could happen on the page being
79 var nextPage
= ntp
.getCardSlider().getCardAtIndex(this.nextCardIndex_());
80 return nextPage
.shouldAcceptDrag(e
);
83 doDragEnter: function(e
) {
84 this.scheduleDelayedSwitch_(e
);
88 doDragLeave: function(e
) {
89 this.cancelDelayedSwitch_();
92 doDragOver: function(e
) {
94 var targetPage
= ntp
.getCardSlider().currentCardValue
;
95 if (targetPage
.shouldAcceptDrag(e
))
96 targetPage
.setDropEffect(e
.dataTransfer
);
101 this.cancelDelayedSwitch_();
103 var tile
= ntp
.getCurrentlyDraggingTile();
107 var sourcePage
= tile
.tilePage
;
108 var targetPage
= ntp
.getCardSlider().currentCardValue
;
109 if (targetPage
== sourcePage
|| !targetPage
.shouldAcceptDrag(e
))
112 targetPage
.appendDraggingTile();
116 * Starts a timer to activate the switcher. The timer repeats until
117 * cancelled by cancelDelayedSwitch_.
120 scheduleDelayedSwitch_: function(e
) {
121 // Stop switching when the next page can't be dropped onto.
122 var nextPage
= ntp
.getCardSlider().getCardAtIndex(this.nextCardIndex_());
123 if (!nextPage
.shouldAcceptDrag(e
))
127 function navPageClearTimeout() {
129 self
.dragNavTimeout_
= null;
130 self
.scheduleDelayedSwitch_(e
);
132 this.dragNavTimeout_
= window
.setTimeout(navPageClearTimeout
, 500);
136 * Cancels the timer that activates the switcher while dragging.
139 cancelDelayedSwitch_: function() {
140 if (this.dragNavTimeout_
) {
141 window
.clearTimeout(this.dragNavTimeout_
);
142 this.dragNavTimeout_
= null;
149 initializePageSwitcher
: PageSwitcher
.template
.decorate