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 // TODO(rltoscano): This class needs a throbber while loading the destination |
| 9 // or another solution is persist the settings of the printer so that next |
| 10 // load is fast. |
| 11 |
| 12 /** |
| 13 * Component used to render the print destination. |
| 14 * @param {!print_preview.DestinationStore} destinationStore Used to determine |
| 15 * the selected destination. |
| 16 * @constructor |
| 17 * @extends {print_preview.Component} |
| 18 */ |
| 19 function DestinationSettings(destinationStore) { |
| 20 print_preview.Component.call(this); |
| 21 |
| 22 /** |
| 23 * Used to determine the selected destination. |
| 24 * @type {!print_preview.DestinationStore} |
| 25 * @private |
| 26 */ |
| 27 this.destinationStore_ = destinationStore; |
| 28 }; |
| 29 |
| 30 /** |
| 31 * Event types dispatched by the component. |
| 32 * @enum {string} |
| 33 */ |
| 34 DestinationSettings.EventType = { |
| 35 MANAGE_PRINTERS_SELECT: |
| 36 'print_preview.DestinationSettings.MANAGE_PRINTERS_SELECT' |
| 37 }; |
| 38 |
| 39 /** |
| 40 * CSS classes used by the component. |
| 41 * @enum {string} |
| 42 * @private |
| 43 */ |
| 44 DestinationSettings.Classes_ = { |
| 45 SELECT: 'destination-settings-select' |
| 46 }; |
| 47 |
| 48 /** |
| 49 * Option value of the "Manage Printers..." select option. |
| 50 * @type {string} |
| 51 * @const |
| 52 * @private |
| 53 */ |
| 54 DestinationSettings.MANAGE_ID_ = '__manage'; |
| 55 |
| 56 DestinationSettings.prototype = { |
| 57 __proto__: print_preview.Component.prototype, |
| 58 |
| 59 set isEnabled(isEnabled) { |
| 60 this.select_.disabled = !isEnabled; |
| 61 }, |
| 62 |
| 63 /** @override */ |
| 64 enterDocument: function() { |
| 65 print_preview.Component.prototype.enterDocument.call(this); |
| 66 this.tracker.add( |
| 67 this.select_, 'change', this.onSelectChange_.bind(this)); |
| 68 this.tracker.add( |
| 69 this.destinationStore_, |
| 70 print_preview.DestinationStore.EventType.DESTINATION_SELECT, |
| 71 this.onDestinationSelect_.bind(this)); |
| 72 this.tracker.add( |
| 73 this.destinationStore_, |
| 74 print_preview.DestinationStore.EventType.DESTINATIONS_INSERTED, |
| 75 this.onDestinationsInserted_.bind(this)); |
| 76 }, |
| 77 |
| 78 get select_() { |
| 79 return this.getElement().getElementsByClassName( |
| 80 DestinationSettings.Classes_.SELECT)[0]; |
| 81 }, |
| 82 |
| 83 renderDestinations_: function() { |
| 84 var select = this.select_; |
| 85 select.innerHTML = ''; |
| 86 var destinations = this.destinationStore_.destinations; |
| 87 var selectedDestination = this.destinationStore_.selectedDestination; |
| 88 var saveToPdfDest = null; |
| 89 var printWithCloudPrintDest = null; |
| 90 for (var dest, i = 0; dest = destinations[i]; i++) { |
| 91 if (dest.id == print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) { |
| 92 saveToPdfDest = dest; |
| 93 continue; |
| 94 } |
| 95 if (dest.isPrintWithCloudPrint) { |
| 96 printWithCloudPrintDest = dest; |
| 97 continue; |
| 98 } |
| 99 var optionEl = document.createElement('option'); |
| 100 optionEl.value = dest.id; |
| 101 optionEl.selected = |
| 102 selectedDestination && selectedDestination.id == dest.id; |
| 103 optionEl.textContent = dest.displayName; |
| 104 select.appendChild(optionEl); |
| 105 } |
| 106 |
| 107 // Add special destinations. |
| 108 if (saveToPdfDest) { |
| 109 select.appendChild(this.createSeparatorOption_()); |
| 110 var printToPdfOptionEl = document.createElement('option'); |
| 111 printToPdfOptionEl.value = saveToPdfDest.id; |
| 112 printToPdfOptionEl.selected = |
| 113 selectedDestination && selectedDestination.id == saveToPdfDest.id; |
| 114 printToPdfOptionEl.textContent = saveToPdfDest.displayName; |
| 115 select.appendChild(printToPdfOptionEl); |
| 116 } |
| 117 if (printWithCloudPrintDest) { |
| 118 select.appendChild(this.createSeparatorOption_()); |
| 119 var printWithCloudPrintOptionEl = document.createElement('option'); |
| 120 printWithCloudPrintOptionEl.value = printWithCloudPrintDest.id; |
| 121 printWithCloudPrintOptionEl.selected = |
| 122 selectedDestination && |
| 123 selectedDestination.id == printWithCloudPrintDest.id; |
| 124 printWithCloudPrintOptionEl.textContent = |
| 125 printWithCloudPrintDest.displayName; |
| 126 select.appendChild(printWithCloudPrintOptionEl); |
| 127 } |
| 128 select.appendChild(this.createSeparatorOption_()); |
| 129 var manageOptionEl = document.createElement('option'); |
| 130 manageOptionEl.value = DestinationSettings.MANAGE_ID_; |
| 131 manageOptionEl.textContent = localStrings.getString('managePrinters'); |
| 132 select.appendChild(manageOptionEl); |
| 133 }, |
| 134 |
| 135 createSeparatorOption_: function() { |
| 136 var sep = document.createElement('option'); |
| 137 sep.disabled = true; |
| 138 sep.role = 'separator'; |
| 139 return sep; |
| 140 }, |
| 141 |
| 142 /** |
| 143 * Called when a destination is selected. Selects the corresponding option. |
| 144 * @private |
| 145 */ |
| 146 onDestinationSelect_: function() { |
| 147 var select = this.select_; |
| 148 if (select.options.length > 0) { |
| 149 select.options[select.selectedIndex].selected = false; |
| 150 } |
| 151 var selectedDestination = this.destinationStore_.selectedDestination; |
| 152 for (var option, i = 0; option = select.options[i]; i++) { |
| 153 if (selectedDestination.id == option.value) { |
| 154 option.selected = true; |
| 155 break; |
| 156 } |
| 157 } |
| 158 }, |
| 159 |
| 160 /** |
| 161 * Called when destinations are inserted into the destination store. Updates |
| 162 * the select element. |
| 163 * @private |
| 164 */ |
| 165 onDestinationsInserted_: function() { |
| 166 this.renderDestinations_(); |
| 167 }, |
| 168 |
| 169 /** |
| 170 * Called when the select element changes options. Selects the corresponding |
| 171 * print destination. |
| 172 * @private |
| 173 */ |
| 174 onSelectChange_: function() { |
| 175 var select = this.select_; |
| 176 var selectedDestId = select.options[select.selectedIndex].value; |
| 177 |
| 178 if (selectedDestId == DestinationSettings.MANAGE_ID_) { |
| 179 cr.dispatchSimpleEvent( |
| 180 this, DestinationSettings.EventType.MANAGE_PRINTERS_SELECT); |
| 181 // Select first in the list. |
| 182 this.destinationStore_.selectDestination( |
| 183 this.destinationStore_.destinations[0]); |
| 184 } else { |
| 185 var destinations = this.destinationStore_.destinations; |
| 186 for (var dest, i = 0; dest = destinations[i]; i++) { |
| 187 if (dest.id == selectedDestId) { |
| 188 this.destinationStore_.selectDestination(dest); |
| 189 break; |
| 190 } |
| 191 } |
| 192 } |
| 193 } |
| 194 }; |
| 195 |
| 196 return { |
| 197 DestinationSettings: DestinationSettings |
| 198 }; |
| 199 }); |
OLD | NEW |