Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / resources / ntp4 / page_switcher.js
blob5babba48ce6a3470002116ad177707c086b7a826
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 /**
6 * @fileoverview Page switcher
7 * This is the class for the left and right navigation arrows that switch
8 * between pages.
9 */
10 cr.define('ntp', function() {
12 /**
13 * @constructor
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);
32 /**
33 * Activate the switcher (go to the next card).
34 * @private
36 activate_: function() {
37 ntp.getCardSlider().selectCard(this.nextCardIndex_(), true);
40 /**
41 * Calculate the index of the card that this button will switch to.
42 * @private
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));
51 /**
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
55 * cards.
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.
62 return;
65 var currentDot = dots[currentIndex];
66 var nextDot = dots[nextCardIndex];
67 if (!currentDot || !nextDot) {
68 this.setAttribute('aria-label', ''); // Dots not initialised yet.
69 return;
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
82 // switched to.
83 var nextPage = ntp.getCardSlider().getCardAtIndex(this.nextCardIndex_());
84 return nextPage.shouldAcceptDrag(e);
87 doDragEnter: function(e) {
88 this.scheduleDelayedSwitch_(e);
89 this.doDragOver(e);
92 doDragLeave: function(e) {
93 this.cancelDelayedSwitch_();
96 doDragOver: function(e) {
97 e.preventDefault();
98 var targetPage = ntp.getCardSlider().currentCardValue;
99 if (targetPage.shouldAcceptDrag(e))
100 targetPage.setDropEffect(e.dataTransfer);
103 doDrop: function(e) {
104 e.stopPropagation();
105 this.cancelDelayedSwitch_();
107 var tile = ntp.getCurrentlyDraggingTile();
108 if (!tile)
109 return;
111 var sourcePage = tile.tilePage;
112 var targetPage = ntp.getCardSlider().currentCardValue;
113 if (targetPage == sourcePage || !targetPage.shouldAcceptDrag(e))
114 return;
116 targetPage.appendDraggingTile();
120 * Starts a timer to activate the switcher. The timer repeats until
121 * cancelled by cancelDelayedSwitch_.
122 * @private
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))
128 return;
130 var self = this;
131 function navPageClearTimeout() {
132 self.activate_();
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.
141 * @private
143 cancelDelayedSwitch_: function() {
144 if (this.dragNavTimeout_) {
145 window.clearTimeout(this.dragNavTimeout_);
146 this.dragNavTimeout_ = null;
152 /** @const */
153 var initializePageSwitcher = PageSwitcher.prototype.decorate;
155 return {
156 initializePageSwitcher: initializePageSwitcher,
157 PageSwitcher: PageSwitcher