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() {
14 * @extends {HTMLButtonElement}
16 function PageSwitcher() {
19 PageSwitcher
.prototype = {
20 __proto__
: HTMLButtonElement
.prototype,
22 decorate: function(el
) {
23 el
.__proto__
= PageSwitcher
.prototype;
25 el
.addEventListener('click', el
.activate_
);
27 el
.direction_
= el
.id
== 'page-switcher-start' ? -1 : 1;
29 el
.dragWrapper_
= new cr
.ui
.DragWrapper(el
, el
);
33 * Activate the switcher (go to the next card).
36 activate_: function() {
37 ntp
.getCardSlider().selectCard(this.nextCardIndex_(), true);
41 * Calculate the index of the card that this button will switch to.
44 nextCardIndex_: function() {
45 var cardSlider
= ntp
.getCardSlider();
46 var index
= cardSlider
.currentCard
+ this.direction_
;
47 var numCards
= cardSlider
.cardCount
- 1;
48 return Math
.max(0, Math
.min(index
, numCards
));
52 * Update the accessible label attribute of this button, based on the
53 * current position in the card slider and the names of the cards.
54 * @param {NodeList} dots The dot elements which display the names of the
57 updateButtonAccessibleLabel: function(dots
) {
58 var currentIndex
= ntp
.getCardSlider().currentCard
;
59 var nextCardIndex
= this.nextCardIndex_();
60 if (nextCardIndex
== currentIndex
) {
61 this.setAttribute('aria-label', ''); // No next card.
65 var currentDot
= dots
[currentIndex
];
66 var nextDot
= dots
[nextCardIndex
];
67 if (!currentDot
|| !nextDot
) {
68 this.setAttribute('aria-label', ''); // Dots not initialised yet.
72 var currentPageTitle
= currentDot
.displayTitle
;
73 var nextPageTitle
= nextDot
.displayTitle
;
74 var msgName
= (currentPageTitle
== nextPageTitle
) ?
75 'page_switcher_same_title' : 'page_switcher_change_title';
76 var ariaLabel
= loadTimeData
.getStringF(msgName
, nextPageTitle
);
77 this.setAttribute('aria-label', ariaLabel
);
80 shouldAcceptDrag: function(e
) {
81 // Only allow page switching when a drop could happen on the page being
83 var nextPage
= ntp
.getCardSlider().getCardAtIndex(this.nextCardIndex_());
84 return nextPage
.shouldAcceptDrag(e
);
87 doDragEnter: function(e
) {
88 this.scheduleDelayedSwitch_(e
);
92 doDragLeave: function(e
) {
93 this.cancelDelayedSwitch_();
96 doDragOver: function(e
) {
98 var targetPage
= ntp
.getCardSlider().currentCardValue
;
99 if (targetPage
.shouldAcceptDrag(e
))
100 targetPage
.setDropEffect(e
.dataTransfer
);
103 doDrop: function(e
) {
105 this.cancelDelayedSwitch_();
107 var tile
= ntp
.getCurrentlyDraggingTile();
111 var sourcePage
= tile
.tilePage
;
112 var targetPage
= ntp
.getCardSlider().currentCardValue
;
113 if (targetPage
== sourcePage
|| !targetPage
.shouldAcceptDrag(e
))
116 targetPage
.appendDraggingTile();
120 * Starts a timer to activate the switcher. The timer repeats until
121 * cancelled by cancelDelayedSwitch_.
124 scheduleDelayedSwitch_: function(e
) {
125 // Stop switching when the next page can't be dropped onto.
126 var nextPage
= ntp
.getCardSlider().getCardAtIndex(this.nextCardIndex_());
127 if (!nextPage
.shouldAcceptDrag(e
))
131 function navPageClearTimeout() {
133 self
.dragNavTimeout_
= null;
134 self
.scheduleDelayedSwitch_(e
);
136 this.dragNavTimeout_
= window
.setTimeout(navPageClearTimeout
, 500);
140 * Cancels the timer that activates the switcher while dragging.
143 cancelDelayedSwitch_: function() {
144 if (this.dragNavTimeout_
) {
145 window
.clearTimeout(this.dragNavTimeout_
);
146 this.dragNavTimeout_
= null;
153 var initializePageSwitcher
= PageSwitcher
.prototype.decorate
;
156 initializePageSwitcher
: initializePageSwitcher
,
157 PageSwitcher
: PageSwitcher