Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(565)

Side by Side Diff: chrome/browser/resources/print_preview/search/destination_list.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 cr.define('print_preview', function() {
6 'use strict';
7
8 /**
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
11 * activated.
12 *
13 * @param {cr.EventTarget} eventTarget Event target to pass to destination
14 * items for dispatching SELECT events.
15 * @param {string} title Title of the destination list.
16 * @param {number?} opt_maxSize Maximum size of the list. If not specified,
17 * defaults to no max.
18 * @param {string?} opt_actionLinkLabel Optional label of the action link. If
19 * no label is provided, the action link will not be shown.
20 * @constructor
21 * @extends {print_preview.Component}
22 */
23 function DestinationList(
24 eventTarget, title, opt_maxSize, opt_actionLinkLabel) {
25 print_preview.Component.call(this);
26
27 /**
28 * Event target to pass to destination items for dispatching SELECT events.
29 * @private
30 */
31 this.eventTarget_ = eventTarget;
32
33 /**
34 * Title of the destination list.
35 * @type {string}
36 * @private
37 */
38 this.title_ = title;
39
40 /**
41 * Maximum size of the destination list.
42 * @type {number}
43 * @private
44 */
45 this.maxSize_ = opt_maxSize || 0;
46 if (this.maxSize_ > DestinationList.SHORT_LIST_SIZE_) {
47 throw Error(
48 'Max size must be less than ' + DestinationList.SHORT_LIST_SIZE_);
49 }
50
51 /**
52 * Label of the action link.
53 * @type {string?}
54 * @private
55 */
56 this.actionLinkLabel_ = opt_actionLinkLabel || null;
57
58 /**
59 * Backing store for the destination list.
60 * @type {Array.<print_preview.Destination>}
61 * @private
62 */
63 this.destinations_ = [];
64
65 /**
66 * Current query used for filtering.
67 * @type {string?}
68 * @private
69 */
70 this.query_ = null;
71
72 /**
73 * Whether the destination list is fully expanded.
74 * @type {boolean}
75 * @private
76 */
77 this.isShowAll_ = false;
78 };
79
80 /**
81 * Enumeration of events dispatched by the destination list.
82 * @enum {string}
83 */
84 DestinationList.Event = {
85 ACTION_LINK_ACTIVATED: 'print_preview.DestinationList.ACTION_LINK_ACTIVATED'
86 };
87
88 /**
89 * Classes used by the destination list.
90 * @enum {string}
91 * @private
92 */
93 DestinationList.Classes_ = {
94 ACTION_LINK: 'destination-list-action-link',
95 FOOTER: 'destination-list-footer',
96 NO_PRINTERS_MESSAGE: 'destination-list-no-destinations-message',
97 PRINTER_ITEM_CONTAINER: 'destination-list-destination-list-item-container',
98 SHOW_ALL_BUTTON: 'destination-list-show-all-button',
99 TITLE: 'destination-list-title',
100 TOTAL_COUNT: 'destination-list-total-count'
101 };
102
103 /**
104 * Maximum number of destinations before showing the 'Show All...' button.
105 * @type {number}
106 * @const
107 * @private
108 */
109 DestinationList.SHORT_LIST_SIZE_ = 5;
110
111 DestinationList.prototype = {
112 __proto__: print_preview.Component.prototype,
113
114 set isShowAll(isShowAll) {
115 this.isShowAll_ = isShowAll;
116 this.renderDestinations_();
117 },
118
119 /** @override */
120 createDom: function() {
121 this.setElementInternal(this.cloneTemplateInternal(
122 'destination-list-template'));
123
124 var titleEl = this.getElement().getElementsByClassName(
125 DestinationList.Classes_.TITLE)[0];
126 titleEl.textContent = this.title_;
127
128 var actionLinkEl = this.getElement().getElementsByClassName(
129 DestinationList.Classes_.ACTION_LINK)[0];
130 actionLinkEl.textContent = this.actionLinkLabel_;
131 },
132
133 /** @override */
134 enterDocument: function() {
135 print_preview.Component.prototype.enterDocument.call(this);
136 var actionLinkEl = this.getElement().getElementsByClassName(
137 DestinationList.Classes_.ACTION_LINK)[0];
138 var showAllButton = this.getElement().getElementsByClassName(
139 DestinationList.Classes_.SHOW_ALL_BUTTON)[0];
140 this.tracker.add(
141 actionLinkEl, 'click', this.onActionLinkClick_.bind(this));
142 this.tracker.add(
143 showAllButton, 'click', this.onShowAllButtonClick_.bind(this));
144 },
145
146 /**
147 * Updates the destinations to render in the destination list.
148 * @param {Array.<print_preview.Destination>} destinations Destinations to
149 * render.
150 */
151 updateDestinations: function(destinations) {
152 this.destinations_ = destinations;
153 this.renderDestinations_();
154 },
155
156 /**
157 * Filters the destination list with the given query.
158 * @param {string?} query Query to filter the list with.
159 */
160 filter: function(query) {
161 this.query_ = query;
162 this.renderDestinations_();
163 },
164
165 get noDestinationsMessageEl_() {
166 return this.getElement().getElementsByClassName(
167 DestinationList.Classes_.NO_PRINTERS_MESSAGE)[0];
168 },
169
170 get footerEl_() {
171 return this.getElement().getElementsByClassName(
172 DestinationList.Classes_.FOOTER)[0];
173 },
174
175 get destinationListItemContainer_() {
176 return this.getElement().getElementsByClassName(
177 DestinationList.Classes_.PRINTER_ITEM_CONTAINER)[0];
178 },
179
180 /**
181 * Renders all destinations in the list that match the current query. For
182 * each render, all old destination items are first removed.
183 * @private
184 */
185 renderDestinations_: function() {
186 this.removeChildren();
187
188 var filteredDests = [];
189 for (var destination, i = 0; destination = this.destinations_[i]; i++) {
190 if (!this.query_ || destination.matches(this.query_)) {
191 filteredDests.push(destination);
192 }
193 }
194
195 // TODO Sort filtered list?
196
197 if (filteredDests.length == 0) {
198 this.renderEmptyList_();
199 } else if (this.maxSize_) {
200 this.renderListWithMaxSize_(filteredDests);
201 } else {
202 this.renderListWithNoMaxSize_(filteredDests);
203 }
204 },
205
206 renderEmptyList_: function() {
207 this.noDestinationsMessageEl_.style.display = '';
208 this.footerEl_.style.display = 'none';
209 },
210
211 renderListWithMaxSize_: function(filteredDests) {
212 this.noDestinationsMessageEl_.style.display = 'none';
213 this.footerEl_.style.display = 'none';
214 var destListItemContainerEl = this.destinationListItemContainer_;
215 for (var dest, i = 0;
216 i < this.maxSize_ && (dest = filteredDests[i]);
217 i++) {
218 var destListItem = new print_preview.DestinationListItem(
219 this.eventTarget_, dest);
220 this.addChild(destListItem);
221 destListItem.render(destListItemContainerEl);
222 }
223 },
224
225 renderListWithNoMaxSize_: function(filteredDests) {
226 this.noDestinationsMessageEl_.style.display = 'none';
227 var destListItemContainerEl = this.destinationListItemContainer_;
228 if (filteredDests.length <= DestinationList.SHORT_LIST_SIZE_ ||
229 this.isShowAll_) {
230 for (var dest, i = 0; dest = filteredDests[i]; i++) {
231 var destListItem = new print_preview.DestinationListItem(
232 this.eventTarget_, dest);
233 this.addChild(destListItem);
234 destListItem.render(destListItemContainerEl);
235 }
236 this.footerEl_.style.display = 'none';
237 } else {
238 for (var dest, i = 0; i < DestinationList.SHORT_LIST_SIZE_ - 1; i++) {
239 var destListItem = new print_preview.DestinationListItem(
240 this.eventTarget_, filteredDests[i]);
241 this.addChild(destListItem);
242 destListItem.render(destListItemContainerEl);
243 }
244 this.footerEl_.style.display = '';
245 var totalCountEl = this.getElement().getElementsByClassName(
246 DestinationList.Classes_.TOTAL_COUNT)[0];
247 totalCountEl.textContent = filteredDests.length + '';
248 }
249 },
250
251 /**
252 * Called when the action link is clicked. Dispatches an
253 * ACTION_LINK_ACTIVATED event.
254 * @private
255 */
256 onActionLinkClick_: function() {
257 cr.dispatchSimpleEvent(this, DestinationList.Event.ACTION_LINK_ACTIVATED);
258 return false;
259 },
260
261 /**
262 * Called when the "Show All..." button is clicked. Renders all
263 * destinations.
264 * @private
265 */
266 onShowAllButtonClick_: function() {
267 this.isShowAll_ = true;
268 this.renderDestinations_();
269 }
270 };
271
272 // Export
273 return {
274 DestinationList: DestinationList
275 };
276 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698