| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 // TODO(rltoscano): This class needs a throbber while loading the destination | 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 | 9 // or another solution is persist the settings of the printer so that next |
| 10 // load is fast. | 10 // load is fast. |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Component used to render the print destination. | 13 * Component used to render the print destination. |
| 14 * @param {!print_preview.DestinationStore} destinationStore Used to determine | 14 * @param {!print_preview.DestinationStore} destinationStore Used to determine |
| 15 * the selected destination. | 15 * the selected destination. |
| 16 * @constructor | 16 * @constructor |
| 17 * @extends {print_preview.Component} | 17 * @extends {print_preview.Component} |
| 18 */ | 18 */ |
| 19 function DestinationSettings(destinationStore) { | 19 function DestinationSettings(destinationStore) { |
| 20 print_preview.Component.call(this); | 20 print_preview.Component.call(this); |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Used to determine the selected destination. | 23 * Used to determine the selected destination. |
| 24 * @type {!print_preview.DestinationStore} | 24 * @type {!print_preview.DestinationStore} |
| 25 * @private | 25 * @private |
| 26 */ | 26 */ |
| 27 this.destinationStore_ = destinationStore; | 27 this.destinationStore_ = destinationStore; |
| 28 |
| 29 /** |
| 30 * Current CSS class of the destination icon. |
| 31 * @type {?DestinationSettings.Classes_} |
| 32 * @private |
| 33 */ |
| 34 this.iconClass_ = null; |
| 28 }; | 35 }; |
| 29 | 36 |
| 30 /** | 37 /** |
| 31 * Event types dispatched by the component. | 38 * Event types dispatched by the component. |
| 32 * @enum {string} | 39 * @enum {string} |
| 33 */ | 40 */ |
| 34 DestinationSettings.EventType = { | 41 DestinationSettings.EventType = { |
| 35 MANAGE_PRINTERS_SELECT: | 42 CHANGE_BUTTON_ACTIVATE: |
| 36 'print_preview.DestinationSettings.MANAGE_PRINTERS_SELECT' | 43 'print_preview.DestinationSettings.CHANGE_BUTTON_ACTIVATE' |
| 37 }; | 44 }; |
| 38 | 45 |
| 39 /** | 46 /** |
| 40 * CSS classes used by the component. | 47 * CSS classes used by the component. |
| 41 * @enum {string} | 48 * @enum {string} |
| 42 * @private | 49 * @private |
| 43 */ | 50 */ |
| 44 DestinationSettings.Classes_ = { | 51 DestinationSettings.Classes_ = { |
| 45 SELECT: 'destination-settings-select' | 52 CHANGE_BUTTON: 'destination-settings-change-button', |
| 53 ICON: 'destination-settings-icon', |
| 54 ICON_CLOUD: 'destination-settings-icon-cloud', |
| 55 ICON_CLOUD_SHARED: 'destination-settings-icon-cloud-shared', |
| 56 ICON_GOOGLE_PROMOTED: 'destination-settings-icon-google-promoted', |
| 57 ICON_LOCAL: 'destination-settings-icon-local', |
| 58 ICON_MOBILE: 'destination-settings-icon-mobile', |
| 59 ICON_MOBILE_SHARED: 'destination-settings-icon-mobile-shared', |
| 60 LOCATION: 'destination-settings-location', |
| 61 NAME: 'destination-settings-name' |
| 46 }; | 62 }; |
| 47 | 63 |
| 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 = { | 64 DestinationSettings.prototype = { |
| 57 __proto__: print_preview.Component.prototype, | 65 __proto__: print_preview.Component.prototype, |
| 58 | 66 |
| 67 /** @param {boolean} Whether the component is enabled. */ |
| 59 set isEnabled(isEnabled) { | 68 set isEnabled(isEnabled) { |
| 60 this.select_.disabled = !isEnabled; | 69 var changeButton = this.getElement().getElementsByClassName( |
| 70 DestinationSettings.Classes_.CHANGE_BUTTON)[0]; |
| 71 changeButton.disabled = !isEnabled; |
| 61 }, | 72 }, |
| 62 | 73 |
| 63 /** @override */ | 74 /** @override */ |
| 64 enterDocument: function() { | 75 enterDocument: function() { |
| 65 print_preview.Component.prototype.enterDocument.call(this); | 76 print_preview.Component.prototype.enterDocument.call(this); |
| 77 var changeButton = this.getElement().getElementsByClassName( |
| 78 DestinationSettings.Classes_.CHANGE_BUTTON)[0]; |
| 66 this.tracker.add( | 79 this.tracker.add( |
| 67 this.select_, 'change', this.onSelectChange_.bind(this)); | 80 changeButton, 'click', this.onChangeButtonClick_.bind(this)); |
| 68 this.tracker.add( | 81 this.tracker.add( |
| 69 this.destinationStore_, | 82 this.destinationStore_, |
| 70 print_preview.DestinationStore.EventType.DESTINATION_SELECT, | 83 print_preview.DestinationStore.EventType.DESTINATION_SELECT, |
| 71 this.onDestinationSelect_.bind(this)); | 84 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 }, | 85 }, |
| 141 | 86 |
| 142 /** | 87 /** |
| 143 * Called when a destination is selected. Selects the corresponding option. | 88 * Called when the "Change" button is clicked. Dispatches the |
| 89 * CHANGE_BUTTON_ACTIVATE event. |
| 90 * @private |
| 91 */ |
| 92 onChangeButtonClick_: function() { |
| 93 cr.dispatchSimpleEvent( |
| 94 this, DestinationSettings.EventType.CHANGE_BUTTON_ACTIVATE); |
| 95 }, |
| 96 |
| 97 /** |
| 98 * Called when the destination selection has changed. Updates UI elements. |
| 144 * @private | 99 * @private |
| 145 */ | 100 */ |
| 146 onDestinationSelect_: function() { | 101 onDestinationSelect_: function() { |
| 147 var select = this.select_; | 102 var destination = this.destinationStore_.selectedDestination; |
| 148 if (select.options.length > 0) { | 103 var nameEl = this.getElement().getElementsByClassName( |
| 149 select.options[select.selectedIndex].selected = false; | 104 DestinationSettings.Classes_.NAME)[0]; |
| 105 nameEl.textContent = destination.displayName; |
| 106 |
| 107 var iconEl = this.getElement().getElementsByClassName( |
| 108 DestinationSettings.Classes_.ICON)[0]; |
| 109 if (this.iconClass_) { |
| 110 iconEl.classList.remove(this.iconClass_); |
| 150 } | 111 } |
| 151 var selectedDestination = this.destinationStore_.selectedDestination; | 112 if (destination.isGooglePromoted) { |
| 152 for (var option, i = 0; option = select.options[i]; i++) { | 113 this.iconClass_ = DestinationSettings.Classes_.ICON_GOOGLE_PROMOTED; |
| 153 if (selectedDestination.id == option.value) { | 114 } else if (destination.isLocal) { |
| 154 option.selected = true; | 115 this.iconClass_ = DestinationSettings.Classes_.ICON_LOCAL; |
| 155 break; | 116 } else if (destination.type == |
| 156 } | 117 print_preview.Destination.Type.MOBILE && destination.isOwned) { |
| 118 this.iconClass_ = DestinationSettings.Classes_.ICON_MOBILE; |
| 119 } else if (destination.type == |
| 120 print_preview.Destination.Type.MOBILE && !destination.isOwned) { |
| 121 this.iconClass_ = DestinationSettings.Classes_.ICON_MOBILE_SHARED; |
| 122 } else if (destination.type == |
| 123 print_preview.Destination.Type.GOOGLE && destination.isOwned) { |
| 124 this.iconClass_ = DestinationSettings.Classes_.ICON_CLOUD; |
| 125 } else { |
| 126 this.iconClass_ = DestinationSettings.Classes_.ICON_CLOUD_SHARED; |
| 157 } | 127 } |
| 158 }, | 128 iconEl.classList.add(this.iconClass_); |
| 159 | 129 |
| 160 /** | 130 var locationEl = this.getElement().getElementsByClassName( |
| 161 * Called when destinations are inserted into the destination store. Updates | 131 DestinationSettings.Classes_.LOCATION)[0]; |
| 162 * the select element. | 132 locationEl.textContent = destination.location; |
| 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 } | 133 } |
| 194 }; | 134 }; |
| 195 | 135 |
| 136 // Export |
| 196 return { | 137 return { |
| 197 DestinationSettings: DestinationSettings | 138 DestinationSettings: DestinationSettings |
| 198 }; | 139 }; |
| 199 }); | 140 }); |
| OLD | NEW |