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 cr.define('print_preview', function() {
9 * Component that displays a list of destinations with a heading, action link,
10 * and "Show All..." button. An event is dispatched when the action link is
12 * @param {!cr.EventTarget} eventTarget Event target to pass to destination
13 * items for dispatching SELECT events.
14 * @param {string} title Title of the destination list.
15 * @param {string=} opt_actionLinkLabel Optional label of the action link. If
16 * no label is provided, the action link will not be shown.
18 * @extends {print_preview.Component}
20 function DestinationList(eventTarget, title, opt_actionLinkLabel) {
21 print_preview.Component.call(this);
24 * Event target to pass to destination items for dispatching SELECT events.
25 * @type {!cr.EventTarget}
28 this.eventTarget_ = eventTarget;
31 * Title of the destination list.
38 * Label of the action link.
42 this.actionLinkLabel_ = opt_actionLinkLabel || null;
45 * Backing store for the destination list.
46 * @type {!Array.<print_preview.Destination>}
49 this.destinations_ = [];
52 * Current query used for filtering.
59 * Whether the destination list is fully expanded.
63 this.isShowAll_ = false;
66 * Maximum number of destinations before showing the "Show All..." button.
70 this.shortListSize_ = DestinationList.DEFAULT_SHORT_LIST_SIZE_;
74 * Enumeration of event types dispatched by the destination list.
77 DestinationList.EventType = {
78 // Dispatched when the action linked is activated.
79 ACTION_LINK_ACTIVATED: 'print_preview.DestinationList.ACTION_LINK_ACTIVATED'
83 * Default maximum number of destinations before showing the "Show All..."
89 DestinationList.DEFAULT_SHORT_LIST_SIZE_ = 4;
92 * Height of a destination list item in pixels.
97 DestinationList.HEIGHT_OF_ITEM_ = 30;
99 DestinationList.prototype = {
100 __proto__: print_preview.Component.prototype,
102 /** @param {boolean} isShowAll Whether the show-all button is activated. */
103 setIsShowAll: function(isShowAll) {
104 this.isShowAll_ = isShowAll;
105 this.renderDestinations_();
109 * @return {number} Size of list when destination list is in collapsed
110 * mode (a.k.a non-show-all mode).
112 getShortListSize: function() {
113 return this.shortListSize_;
116 /** @return {number} Count of the destinations in the list. */
117 getDestinationsCount: function() {
118 return this.destinations_.length;
122 * Gets estimated height of the destination list for the given number of
124 * @param {number} Number of items to render in the destination list.
125 * @return {number} Height (in pixels) of the destination list.
127 getEstimatedHeightInPixels: function(numItems) {
128 numItems = Math.min(numItems, this.destinations_.length);
130 this.getChildElement('.destination-list > header').offsetHeight;
132 getIsVisible(this.getChildElement('.throbber-container')) ?
133 DestinationList.HEIGHT_OF_ITEM_ : 0;
134 return headerHeight + numItems * DestinationList.HEIGHT_OF_ITEM_ +
138 /** @param {boolean} isVisible Whether the throbber is visible. */
139 setIsThrobberVisible: function(isVisible) {
140 setIsVisible(this.getChildElement('.throbber-container'), isVisible);
144 * @param {number} size Size of list when destination list is in collapsed
145 * mode (a.k.a non-show-all mode).
147 updateShortListSize: function(size) {
148 size = Math.max(1, Math.min(size, this.destinations_.length));
149 if (size == 1 && this.destinations_.length > 1) {
150 // If this is the case, we will only show the "Show All" button and
151 // nothing else. Increment the short list size by one so that we can see
152 // at least one print destination.
155 this.setShortListSizeInternal(size);
159 createDom: function() {
160 this.setElementInternal(this.cloneTemplateInternal(
161 'destination-list-template'));
162 this.getChildElement('.title').textContent = this.title_;
163 if (this.actionLinkLabel_) {
164 var actionLinkEl = this.getChildElement('.action-link');
165 actionLinkEl.textContent = this.actionLinkLabel_;
166 setIsVisible(actionLinkEl, true);
171 enterDocument: function() {
172 print_preview.Component.prototype.enterDocument.call(this);
174 this.getChildElement('.action-link'),
176 this.onActionLinkClick_.bind(this));
178 this.getChildElement('.show-all-button'),
180 this.setIsShowAll.bind(this, true));
184 * Updates the destinations to render in the destination list.
185 * @param {!Array.<print_preview.Destination>} destinations Destinations to
188 updateDestinations: function(destinations) {
189 this.destinations_ = destinations;
190 this.renderDestinations_();
193 /** @param {?string} query Query to update the filter with. */
194 updateSearchQuery: function(query) {
196 this.renderDestinations_();
200 * @param {string} text Text to set the action link to.
203 setActionLinkTextInternal: function(text) {
204 this.actionLinkLabel_ = text;
205 this.getChildElement('.action-link').textContent = text;
209 * Sets the short list size without constraints.
212 setShortListSizeInternal: function(size) {
213 this.shortListSize_ = size;
214 this.renderDestinations_();
218 * Renders all destinations in the given list.
219 * @param {!Array.<print_preview.Destination>} destinations List of
220 * destinations to render.
223 renderListInternal: function(destinations) {
224 setIsVisible(this.getChildElement('.no-destinations-message'),
225 destinations.length == 0);
226 setIsVisible(this.getChildElement('.destination-list > footer'), false);
227 var numItems = destinations.length;
228 if (destinations.length > this.shortListSize_ && !this.isShowAll_) {
229 numItems = this.shortListSize_ - 1;
230 this.getChildElement('.total').textContent =
231 localStrings.getStringF('destinationCount', destinations.length);
232 setIsVisible(this.getChildElement('.destination-list > footer'), true);
234 for (var i = 0; i < numItems; i++) {
235 var destListItem = new print_preview.DestinationListItem(
236 this.eventTarget_, destinations[i]);
237 this.addChild(destListItem);
238 destListItem.render(this.getChildElement('.destination-list > ul'));
243 * Renders all destinations in the list that match the current query. For
244 * each render, all old destination items are first removed.
247 renderDestinations_: function() {
248 this.removeChildren();
250 var filteredDests = [];
251 this.destinations_.forEach(function(destination) {
252 if (!this.query_ || destination.matches(this.query_)) {
253 filteredDests.push(destination);
257 this.renderListInternal(filteredDests);
261 * Called when the action link is clicked. Dispatches an
262 * ACTION_LINK_ACTIVATED event.
265 onActionLinkClick_: function() {
266 cr.dispatchSimpleEvent(this,
267 DestinationList.EventType.ACTION_LINK_ACTIVATED);
273 DestinationList: DestinationList