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