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