Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 * @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 {number=} opt_maxSize Maximum size of the list. If not specified, | |
| 16 * defaults to no max. | |
| 17 * @param {string=} opt_actionLinkLabel Optional label of the action link. If | |
| 18 * no label is provided, the action link will not be shown. | |
| 19 * @constructor | |
| 20 * @extends {print_preview.Component} | |
| 21 */ | |
| 22 function DestinationList( | |
| 23 eventTarget, title, opt_maxSize, opt_actionLinkLabel) { | |
| 24 print_preview.Component.call(this); | |
| 25 | |
| 26 /** | |
| 27 * Event target to pass to destination items for dispatching SELECT events. | |
| 28 * @type {!cr.EventTarget} | |
| 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 assert(this.maxSize_ <= DestinationList.SHORT_LIST_SIZE_, | |
| 47 'Max size must be less than or equal to ' + | |
| 48 DestinationList.SHORT_LIST_SIZE_); | |
| 49 | |
| 50 /** | |
| 51 * Label of the action link. | |
| 52 * @type {?string} | |
| 53 * @private | |
| 54 */ | |
| 55 this.actionLinkLabel_ = opt_actionLinkLabel || null; | |
| 56 | |
| 57 /** | |
| 58 * Backing store for the destination list. | |
| 59 * @type {!Array.<print_preview.Destination>} | |
| 60 * @private | |
| 61 */ | |
| 62 this.destinations_ = []; | |
| 63 | |
| 64 /** | |
| 65 * Current query used for filtering. | |
| 66 * @type {?string} | |
| 67 * @private | |
| 68 */ | |
| 69 this.query_ = null; | |
| 70 | |
| 71 /** | |
| 72 * Whether the destination list is fully expanded. | |
| 73 * @type {boolean} | |
| 74 * @private | |
| 75 */ | |
| 76 this.isShowAll_ = false; | |
| 77 | |
| 78 /** | |
| 79 * Message to show when no destinations are available. | |
| 80 * @type {HTMLElement} | |
| 81 * @private | |
| 82 */ | |
| 83 this.noDestinationsMessageEl_ = null; | |
|
Dan Beam
2012/05/29 23:36:10
?HTMLElement
Robert Toscano
2012/05/30 21:08:00
'?' is extraneous.
| |
| 84 | |
| 85 /** | |
| 86 * Footer of the list. | |
| 87 * @type {HTMLElement} | |
| 88 * @private | |
| 89 */ | |
| 90 this.footerEl_ = null; | |
| 91 | |
| 92 /** | |
| 93 * Container for the destination list items. | |
| 94 * @type {HTMLElement} | |
| 95 * @private | |
| 96 */ | |
| 97 this.destinationListItemContainer_ = null; | |
| 98 }; | |
| 99 | |
| 100 /** | |
| 101 * Enumeration of event types dispatched by the destination list. | |
| 102 * @enum {string} | |
| 103 */ | |
| 104 DestinationList.EventType = { | |
| 105 // Dispatched when the action linked is activated. | |
| 106 ACTION_LINK_ACTIVATED: 'print_preview.DestinationList.ACTION_LINK_ACTIVATED' | |
| 107 }; | |
| 108 | |
| 109 /** | |
| 110 * Classes used by the destination list. | |
| 111 * @enum {string} | |
| 112 * @private | |
| 113 */ | |
| 114 DestinationList.Classes_ = { | |
| 115 ACTION_LINK: 'destination-list-action-link', | |
| 116 FOOTER: 'destination-list-footer', | |
| 117 NO_PRINTERS_MESSAGE: 'destination-list-no-destinations-message', | |
| 118 PRINTER_ITEM_CONTAINER: 'destination-list-destination-list-item-container', | |
| 119 SHOW_ALL_BUTTON: 'destination-list-show-all-button', | |
| 120 TITLE: 'destination-list-title', | |
| 121 TOTAL: 'destination-list-total' | |
| 122 }; | |
| 123 | |
| 124 /** | |
| 125 * Maximum number of destinations before showing the "Show All..." button. | |
| 126 * @type {number} | |
| 127 * @const | |
| 128 * @private | |
| 129 */ | |
| 130 DestinationList.SHORT_LIST_SIZE_ = 5; | |
| 131 | |
| 132 DestinationList.prototype = { | |
| 133 __proto__: print_preview.Component.prototype, | |
| 134 | |
| 135 /** @param {boolean} isShowAll Whether the show-all button is activated. */ | |
| 136 setIsShowAll: function(isShowAll) { | |
| 137 this.isShowAll_ = isShowAll; | |
| 138 this.renderDestinations_(); | |
| 139 }, | |
| 140 | |
| 141 /** @override */ | |
| 142 createDom: function() { | |
| 143 this.setElementInternal(this.cloneTemplateInternal( | |
| 144 'destination-list-template')); | |
| 145 | |
| 146 var titleEl = this.getElement().getElementsByClassName( | |
| 147 DestinationList.Classes_.TITLE)[0]; | |
| 148 titleEl.textContent = this.title_; | |
| 149 | |
| 150 var actionLinkEl = this.getElement().getElementsByClassName( | |
| 151 DestinationList.Classes_.ACTION_LINK)[0]; | |
| 152 if (this.actionLinkLabel_) { | |
| 153 actionLinkEl.textContent = this.actionLinkLabel_; | |
| 154 } else { | |
| 155 setIsVisible(actionLinkEl, false); | |
| 156 } | |
| 157 | |
| 158 this.noDestinationsMessageEl_ = this.getElement().getElementsByClassName( | |
| 159 DestinationList.Classes_.NO_PRINTERS_MESSAGE)[0]; | |
| 160 this.footerEl_ = this.getElement().getElementsByClassName( | |
| 161 DestinationList.Classes_.FOOTER)[0]; | |
| 162 this.destinationListItemContainer_ = | |
| 163 this.getElement().getElementsByClassName( | |
| 164 DestinationList.Classes_.PRINTER_ITEM_CONTAINER)[0]; | |
| 165 }, | |
| 166 | |
| 167 /** @override */ | |
| 168 enterDocument: function() { | |
| 169 print_preview.Component.prototype.enterDocument.call(this); | |
| 170 var actionLinkEl = this.getElement().getElementsByClassName( | |
| 171 DestinationList.Classes_.ACTION_LINK)[0]; | |
| 172 var showAllButton = this.getElement().getElementsByClassName( | |
| 173 DestinationList.Classes_.SHOW_ALL_BUTTON)[0]; | |
| 174 this.tracker.add( | |
| 175 actionLinkEl, 'click', this.onActionLinkClick_.bind(this)); | |
| 176 this.tracker.add( | |
| 177 showAllButton, 'click', this.setIsShowAll.bind(this, true)); | |
| 178 }, | |
| 179 | |
| 180 /** @override */ | |
| 181 exitDocument: function() { | |
| 182 print_preview.Component.prototype.exitDocument.call(this); | |
| 183 this.noDestinationsMessageEl_ = null; | |
|
Dan Beam
2012/05/29 23:36:10
is there a reason this nulling is required?
Robert Toscano
2012/05/30 21:08:00
So this object can release its reference to the DO
Dan Beam
2012/05/31 00:17:58
If the ref count hits 0 it should do this automagi
| |
| 184 this.footerEl_ = null; | |
| 185 this.destinationListItemContainer_ = null; | |
| 186 }, | |
| 187 | |
| 188 /** | |
| 189 * Updates the destinations to render in the destination list. | |
| 190 * @param {!Array.<print_preview.Destination>} destinations Destinations to | |
| 191 * render. | |
| 192 */ | |
| 193 updateDestinations: function(destinations) { | |
| 194 this.destinations_ = destinations; | |
| 195 this.renderDestinations_(); | |
| 196 }, | |
| 197 | |
| 198 /** | |
| 199 * Filters the destination list with the given query. | |
| 200 * @param {?string} query Query to filter the list with. | |
| 201 */ | |
| 202 filter: function(query) { | |
| 203 this.query_ = query; | |
| 204 this.renderDestinations_(); | |
| 205 }, | |
| 206 | |
| 207 /** | |
| 208 * @param {string} text Text to set the action link to. | |
| 209 * @protected | |
| 210 */ | |
| 211 setActionLinkTextInternal: function(text) { | |
| 212 this.actionLinkLabel_ = text; | |
| 213 var actionLinkEl = this.getElement().getElementsByClassName( | |
| 214 DestinationList.Classes_.ACTION_LINK)[0]; | |
| 215 actionLinkEl.textContent = this.actionLinkLabel_; | |
| 216 }, | |
| 217 | |
| 218 /** | |
| 219 * Renders all destinations in the list that match the current query. For | |
| 220 * each render, all old destination items are first removed. | |
| 221 * @private | |
| 222 */ | |
| 223 renderDestinations_: function() { | |
| 224 this.removeChildren(); | |
| 225 | |
| 226 var filteredDests = []; | |
| 227 this.destinations_.forEach(function(destination) { | |
| 228 if (!this.query_ || destination.matches(this.query_)) { | |
| 229 filteredDests.push(destination); | |
| 230 } | |
| 231 }, this); | |
| 232 | |
| 233 // TODO(rltoscano): Sort filtered list? | |
| 234 | |
| 235 if (filteredDests.length == 0) { | |
| 236 this.renderEmptyList_(); | |
| 237 } else if (this.maxSize_) { | |
| 238 this.renderListWithMaxSize_(filteredDests); | |
| 239 } else { | |
| 240 this.renderListWithNoMaxSize_(filteredDests); | |
| 241 } | |
| 242 }, | |
| 243 | |
| 244 /** | |
| 245 * Renders a "No destinations found" element. | |
| 246 * @private | |
| 247 */ | |
| 248 renderEmptyList_: function() { | |
| 249 setIsVisible(this.noDestinationsMessageEl_, true); | |
| 250 setIsVisible(this.footerEl_, false); | |
| 251 }, | |
| 252 | |
| 253 /** | |
| 254 * Renders the list of destinations up to the maximum size. | |
| 255 * @param {!Array.<print_preview.Destination>} filteredDests Filtered list | |
| 256 * of print destinations to render. | |
| 257 * @private | |
| 258 */ | |
| 259 renderListWithMaxSize_: function(filteredDests) { | |
| 260 setIsVisible(this.noDestinationsMessageEl_, false); | |
| 261 setIsVisible(this.footerEl_, false); | |
| 262 for (var dest, i = 0; | |
| 263 i < this.maxSize_ && (dest = filteredDests[i]); | |
| 264 i++) { | |
| 265 var destListItem = new print_preview.DestinationListItem( | |
| 266 this.eventTarget_, dest); | |
| 267 this.addChild(destListItem); | |
| 268 destListItem.render(this.destinationListItemContainer_); | |
| 269 } | |
| 270 }, | |
| 271 | |
| 272 /** | |
| 273 * Renders all destinations in the given list. | |
| 274 * @param {!Array.<print_preview.Destination>} filteredDests Filtered list | |
| 275 * of print destinations to render. | |
| 276 * @private | |
| 277 */ | |
| 278 renderListWithNoMaxSize_: function(filteredDests) { | |
| 279 setIsVisible(this.noDestinationsMessageEl_, false); | |
| 280 if (filteredDests.length <= DestinationList.SHORT_LIST_SIZE_ || | |
| 281 this.isShowAll_) { | |
| 282 filteredDests.forEach(function(dest) { | |
| 283 var destListItem = new print_preview.DestinationListItem( | |
| 284 this.eventTarget_, dest); | |
| 285 this.addChild(destListItem); | |
| 286 destListItem.render(this.destinationListItemContainer_); | |
| 287 }, this); | |
| 288 setIsVisible(this.footerEl_, false); | |
| 289 } else { | |
| 290 for (var dest, i = 0; i < DestinationList.SHORT_LIST_SIZE_ - 1; i++) { | |
| 291 var destListItem = new print_preview.DestinationListItem( | |
| 292 this.eventTarget_, filteredDests[i]); | |
| 293 this.addChild(destListItem); | |
| 294 destListItem.render(this.destinationListItemContainer_); | |
| 295 } | |
| 296 setIsVisible(this.footerEl_, true); | |
| 297 var totalCountEl = this.getElement().getElementsByClassName( | |
| 298 DestinationList.Classes_.TOTAL)[0]; | |
| 299 totalCountEl.textContent = | |
| 300 localStrings.getStringF('destinationCount', filteredDests.length); | |
| 301 } | |
| 302 }, | |
| 303 | |
| 304 /** | |
| 305 * Called when the action link is clicked. Dispatches an | |
| 306 * ACTION_LINK_ACTIVATED event. | |
| 307 * @private | |
| 308 */ | |
| 309 onActionLinkClick_: function() { | |
| 310 cr.dispatchSimpleEvent( | |
| 311 this, DestinationList.EventType.ACTION_LINK_ACTIVATED); | |
| 312 } | |
| 313 }; | |
| 314 | |
| 315 // Export | |
| 316 return { | |
| 317 DestinationList: DestinationList | |
| 318 }; | |
| 319 }); | |
| OLD | NEW |