Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/browser/resources/print_preview/settings/other_options_settings.js

Issue 2576233003: Print Preview: Simplify other_options_settings javascript code. (Closed)
Patch Set: Small fixes Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 /** 8 /**
9 * Represents a single option in the Other Options settings section.
10 * @param {!print_preview.ticket_items.TicketItem} The ticket item for this
11 * element, used to read and write.
12 * @param {boolean} Whether this option is collapsible or not.
13 * @param {string} The CSS id selector string for the container element for
dpapad 2016/12/15 20:32:59 s/selector string// (See related following comment
rbpotter 2016/12/15 23:28:17 Done.
14 * this option, used to access the container and checkbox HTML elements.
15 * @constructor
16 */
17 function CheckboxTicketItemElement(ticketItem, collapsible, cssId) {
18 /**
19 * Ticket item for this element, used to read/write.
20 * @private {!print_preview.ticket_items.TicketItem}
21 */
22 this.ticketItem_ = ticketItem;
23
24 /**
25 * Whether this element is collapsible.
26 * @private {boolean}
27 */
28 this.collapsible_ = collapsible;
29
30 /**
31 * The CSS id string for the container element.
32 * @private {string}
33 */
34 this.cssId_ = cssId;
35
36 /**
37 * The HTML container element. Populated when decorate() is called.
38 * @private {HTMLElement}
39 */
40 this.container_ = null;
41
42 /**
43 * The HTML checkbox input element. The checkbox child element of
44 * container_. Populated when decorate() is called.
45 * @private {HTMLElement}
46 */
47 this.checkbox_ = null;
48 };
49
50 CheckboxTicketItemElement.prototype = {
51
52 /** @return {boolean} Whether the element is collapsible */
53 get collapsible() {
54 return this.collapsible_;
55 },
56
57 /**
58 * @return {!print_preview.ticket_items.TicketItem} The ticket item for this
59 * element.
60 */
61 get ticketItem() {
62 return this.ticketItem_;
63 },
64
65 /** @return {HTMLElement} The checkbox HTML element. */
66 get checkbox() {
67 return this.checkbox_;
68 },
69
70 /** Initializes container and checkbox */
71 decorate: function(otherOptionsSettings) {
72 this.container_ = otherOptionsSettings.getElement().querySelector(
73 this.cssId_);
dpapad 2016/12/15 20:32:59 Technically the "#" prefix is not part of the ID.
rbpotter 2016/12/15 23:28:17 Done.
74 this.checkbox_ = this.container_.querySelector('.checkbox');
75 },
76
77 /** Resets container and checkbox. */
78 exitDocument: function() {
79 this.container_ = null;
80 this.checkbox_ = null;
81 },
82
83 /** Called when the checkbox is clicked. Updates the ticket item value. */
84 onCheckboxClick: function() {
85 this.ticketItem_.updateValue(this.checkbox_.checked);
86 },
87
88 /**
89 * Called when the ticket item changes. Updates the UI state.
90 * @param {!print_preview.SettingsSection.OtherOptionsSettings} The
91 * settings section that this element is part of.
92 */
93 onTicketItemChange: function(otherOptionsSettings) {
94 this.checkbox_.checked = this.ticketItem_.getValue();
95 otherOptionsSettings.updateUiStateInternal();
96 },
97
98 /**
99 * @param {boolean} Whether the settings section has content collapsed.
100 * @return {boolean} Whether this element should be visible.
101 */
102 isVisible: function(collapseContent) {
103 return this.ticketItem_.isCapabilityAvailable() &&
104 (!this.collapsible_ || !collapseContent);
105 },
106
107 /**
108 * Sets the visibility of the element.
109 * @param {boolean} Whether the settings section has content collapsed.
110 */
111 setVisibility: function(collapseContent) {
112 setIsVisible(this.container_, this.isVisible(collapseContent));
113 },
114
115 };
116
117 /**
9 * UI component that renders checkboxes for various print options. 118 * UI component that renders checkboxes for various print options.
10 * @param {!print_preview.ticket_items.Duplex} duplex Duplex ticket item. 119 * @param {!print_preview.ticket_items.Duplex} duplex Duplex ticket item.
11 * @param {!print_preview.ticket_items.FitToPage} fitToPage Fit-to-page ticket 120 * @param {!print_preview.ticket_items.FitToPage} fitToPage Fit-to-page ticket
12 * item. 121 * item.
13 * @param {!print_preview.ticket_items.CssBackground} cssBackground CSS 122 * @param {!print_preview.ticket_items.CssBackground} cssBackground CSS
14 * background ticket item. 123 * background ticket item.
15 * @param {!print_preview.ticket_items.SelectionOnly} selectionOnly Selection 124 * @param {!print_preview.ticket_items.SelectionOnly} selectionOnly Selection
16 * only ticket item. 125 * only ticket item.
17 * @param {!print_preview.ticket_items.HeaderFooter} headerFooter Header 126 * @param {!print_preview.ticket_items.HeaderFooter} headerFooter Header
18 * footer ticket item. 127 * footer ticket item.
19 * @constructor 128 * @constructor
20 * @extends {print_preview.SettingsSection} 129 * @extends {print_preview.SettingsSection}
21 */ 130 */
22 function OtherOptionsSettings( 131 function OtherOptionsSettings(
23 duplex, fitToPage, cssBackground, selectionOnly, headerFooter) { 132 duplex, fitToPage, cssBackground, selectionOnly, headerFooter) {
24 print_preview.SettingsSection.call(this); 133 print_preview.SettingsSection.call(this);
25 134
26 /** 135 /*
27 * Duplex ticket item, used to read/write the duplex selection. 136 * @private {!Array<!CheckboxTicketItemElement>} checkbox ticket item
28 * @type {!print_preview.ticket_items.Duplex} 137 * elements representing the different options in the section.
29 * @private 138 * Selection only must always be the last element in the array.
30 */ 139 */
31 this.duplexTicketItem_ = duplex; 140 this.elements_ = [
141 new CheckboxTicketItemElement(headerFooter, true,
142 '#header-footer-container'),
143 new CheckboxTicketItemElement(fitToPage, false,
144 '#fit-to-page-container'),
145 new CheckboxTicketItemElement(duplex, false, '#duplex-container'),
146 new CheckboxTicketItemElement(cssBackground, true,
147 '#css-background-container'),
148 new CheckboxTicketItemElement(selectionOnly, true,
149 '#selection-only-container')
150 ];
32 151
33 /**
34 * Fit-to-page ticket item, used to read/write the fit-to-page selection.
35 * @type {!print_preview.ticket_items.FitToPage}
36 * @private
37 */
38 this.fitToPageTicketItem_ = fitToPage;
39
40 /**
41 * Enable CSS backgrounds ticket item, used to read/write.
42 * @type {!print_preview.ticket_items.CssBackground}
43 * @private
44 */
45 this.cssBackgroundTicketItem_ = cssBackground;
46
47 /**
48 * Print selection only ticket item, used to read/write.
49 * @type {!print_preview.ticket_items.SelectionOnly}
50 * @private
51 */
52 this.selectionOnlyTicketItem_ = selectionOnly;
53
54 /**
55 * Header-footer ticket item, used to read/write.
56 * @type {!print_preview.ticket_items.HeaderFooter}
57 * @private
58 */
59 this.headerFooterTicketItem_ = headerFooter;
60
61 /**
62 * Header footer container element.
63 * @type {HTMLElement}
64 * @private
65 */
66 this.headerFooterContainer_ = null;
67
68 /**
69 * Header footer checkbox.
70 * @type {HTMLInputElement}
71 * @private
72 */
73 this.headerFooterCheckbox_ = null;
74
75 /**
76 * Fit-to-page container element.
77 * @type {HTMLElement}
78 * @private
79 */
80 this.fitToPageContainer_ = null;
81
82 /**
83 * Fit-to-page checkbox.
84 * @type {HTMLInputElement}
85 * @private
86 */
87 this.fitToPageCheckbox_ = null;
88
89 /**
90 * Duplex container element.
91 * @type {HTMLElement}
92 * @private
93 */
94 this.duplexContainer_ = null;
95
96 /**
97 * Duplex checkbox.
98 * @type {HTMLInputElement}
99 * @private
100 */
101 this.duplexCheckbox_ = null;
102
103 /**
104 * Print CSS backgrounds container element.
105 * @type {HTMLElement}
106 * @private
107 */
108 this.cssBackgroundContainer_ = null;
109
110 /**
111 * Print CSS backgrounds checkbox.
112 * @type {HTMLInputElement}
113 * @private
114 */
115 this.cssBackgroundCheckbox_ = null;
116
117 /**
118 * Print selection only container element.
119 * @type {HTMLElement}
120 * @private
121 */
122 this.selectionOnlyContainer_ = null;
123
124 /**
125 * Print selection only checkbox.
126 * @type {HTMLInputElement}
127 * @private
128 */
129 this.selectionOnlyCheckbox_ = null;
130 }; 152 };
131 153
132 OtherOptionsSettings.prototype = { 154 OtherOptionsSettings.prototype = {
133 __proto__: print_preview.SettingsSection.prototype, 155 __proto__: print_preview.SettingsSection.prototype,
134 156
135 /** @override */ 157 /** @override */
136 isAvailable: function() { 158 isAvailable: function() {
137 return this.headerFooterTicketItem_.isCapabilityAvailable() || 159 return this.elements_.some(function(element) {
138 this.fitToPageTicketItem_.isCapabilityAvailable() || 160 return element.ticketItem.isCapabilityAvailable();
139 this.duplexTicketItem_.isCapabilityAvailable() || 161 });
140 this.cssBackgroundTicketItem_.isCapabilityAvailable() ||
141 this.selectionOnlyTicketItem_.isCapabilityAvailable();
142 }, 162 },
143 163
144 /** @override */ 164 /** @override */
145 hasCollapsibleContent: function() { 165 hasCollapsibleContent: function() {
146 return this.headerFooterTicketItem_.isCapabilityAvailable() || 166 return this.elements_.some(function(element) {
147 this.cssBackgroundTicketItem_.isCapabilityAvailable() || 167 return element.collapsible;
148 this.selectionOnlyTicketItem_.isCapabilityAvailable(); 168 });
149 }, 169 },
150 170
151 /** @override */ 171 /** @override */
152 set isEnabled(isEnabled) { 172 set isEnabled(isEnabled) {
153 this.headerFooterCheckbox_.disabled = !isEnabled; 173 /* Skip selection only, which must always be the last element, as this
dpapad 2016/12/15 20:32:59 Nit: Skip |ticket_items.SelectionOnly|, which must
rbpotter 2016/12/15 23:28:17 Done.
154 this.fitToPageCheckbox_.disabled = !isEnabled; 174 * checkbox is enabled based on whether the user has selected something
155 this.duplexCheckbox_.disabled = !isEnabled; 175 * in the page, which is different logic from the other elements. */
156 this.cssBackgroundCheckbox_.disabled = !isEnabled; 176 for (var i = 0; i < this.elements_.length - 1; i++)
177 this.elements_[i].checkbox.disabled = !isEnabled;
157 }, 178 },
158 179
159 /** @override */ 180 /** @override */
160 enterDocument: function() { 181 enterDocument: function() {
161 print_preview.SettingsSection.prototype.enterDocument.call(this); 182 print_preview.SettingsSection.prototype.enterDocument.call(this);
162 this.tracker.add( 183 this.elements_.forEach(function(element) {
163 this.headerFooterCheckbox_, 184 this.tracker.add(
164 'click', 185 element.checkbox,
165 this.onHeaderFooterCheckboxClick_.bind(this)); 186 'click',
166 this.tracker.add( 187 element.onCheckboxClick.bind(element));
167 this.fitToPageCheckbox_, 188 this.tracker.add(
168 'click', 189 element.ticketItem,
169 this.onFitToPageCheckboxClick_.bind(this)); 190 print_preview.ticket_items.TicketItem.EventType.CHANGE,
170 this.tracker.add( 191 element.onTicketItemChange.bind(element, this));
171 this.duplexCheckbox_, 192 }, this);
172 'click',
173 this.onDuplexCheckboxClick_.bind(this));
174 this.tracker.add(
175 this.cssBackgroundCheckbox_,
176 'click',
177 this.onCssBackgroundCheckboxClick_.bind(this));
178 this.tracker.add(
179 this.selectionOnlyCheckbox_,
180 'click',
181 this.onSelectionOnlyCheckboxClick_.bind(this));
182 this.tracker.add(
183 this.duplexTicketItem_,
184 print_preview.ticket_items.TicketItem.EventType.CHANGE,
185 this.onDuplexChange_.bind(this));
186 this.tracker.add(
187 this.fitToPageTicketItem_,
188 print_preview.ticket_items.TicketItem.EventType.CHANGE,
189 this.onFitToPageChange_.bind(this));
190 this.tracker.add(
191 this.cssBackgroundTicketItem_,
192 print_preview.ticket_items.TicketItem.EventType.CHANGE,
193 this.onCssBackgroundChange_.bind(this));
194 this.tracker.add(
195 this.selectionOnlyTicketItem_,
196 print_preview.ticket_items.TicketItem.EventType.CHANGE,
197 this.onSelectionOnlyChange_.bind(this));
198 this.tracker.add(
199 this.headerFooterTicketItem_,
200 print_preview.ticket_items.TicketItem.EventType.CHANGE,
201 this.onHeaderFooterChange_.bind(this));
202 }, 193 },
203 194
204 /** @override */ 195 /** @override */
205 exitDocument: function() { 196 exitDocument: function() {
206 print_preview.SettingsSection.prototype.exitDocument.call(this); 197 print_preview.SettingsSection.prototype.exitDocument.call(this);
207 this.headerFooterContainer_ = null; 198 for (var i = 0; i < this.elements_.length; i++)
208 this.headerFooterCheckbox_ = null; 199 this.elements_[i].exitDocument();
209 this.fitToPageContainer_ = null;
210 this.fitToPageCheckbox_ = null;
211 this.duplexContainer_ = null;
212 this.duplexCheckbox_ = null;
213 this.cssBackgroundContainer_ = null;
214 this.cssBackgroundCheckbox_ = null;
215 this.selectionOnlyContainer_ = null;
216 this.selectionOnlyCheckbox_ = null;
217 }, 200 },
218 201
219 /** @override */ 202 /** @override */
220 decorateInternal: function() { 203 decorateInternal: function() {
221 this.headerFooterContainer_ = this.getElement().querySelector( 204 for (var i = 0; i < this.elements_.length; i++)
222 '.header-footer-container'); 205 this.elements_[i].decorate(this);
223 this.headerFooterCheckbox_ = this.headerFooterContainer_.querySelector(
224 '.header-footer-checkbox');
225 this.fitToPageContainer_ = this.getElement().querySelector(
226 '.fit-to-page-container');
227 this.fitToPageCheckbox_ = this.fitToPageContainer_.querySelector(
228 '.fit-to-page-checkbox');
229 this.duplexContainer_ = this.getElement().querySelector(
230 '.duplex-container');
231 this.duplexCheckbox_ = this.duplexContainer_.querySelector(
232 '.duplex-checkbox');
233 this.cssBackgroundContainer_ = this.getElement().querySelector(
234 '.css-background-container');
235 this.cssBackgroundCheckbox_ = this.cssBackgroundContainer_.querySelector(
236 '.css-background-checkbox');
237 this.selectionOnlyContainer_ = this.getElement().querySelector(
238 '.selection-only-container');
239 this.selectionOnlyCheckbox_ = this.selectionOnlyContainer_.querySelector(
240 '.selection-only-checkbox');
241 }, 206 },
242 207
243 /** @override */ 208 /** @override */
244 updateUiStateInternal: function() { 209 updateUiStateInternal: function() {
245 if (this.isAvailable()) { 210 if (this.isAvailable()) {
246 setIsVisible(this.headerFooterContainer_, 211 for (var i = 0; i < this.elements_.length; i++)
247 this.headerFooterTicketItem_.isCapabilityAvailable() && 212 this.elements_[i].setVisibility(this.collapseContent);
248 !this.collapseContent);
249 setIsVisible(this.fitToPageContainer_,
250 this.fitToPageTicketItem_.isCapabilityAvailable());
251 setIsVisible(this.duplexContainer_,
252 this.duplexTicketItem_.isCapabilityAvailable());
253 setIsVisible(this.cssBackgroundContainer_,
254 this.cssBackgroundTicketItem_.isCapabilityAvailable() &&
255 !this.collapseContent);
256 setIsVisible(this.selectionOnlyContainer_,
257 this.selectionOnlyTicketItem_.isCapabilityAvailable() &&
258 !this.collapseContent);
259 } 213 }
260 print_preview.SettingsSection.prototype.updateUiStateInternal.call(this); 214 print_preview.SettingsSection.prototype.updateUiStateInternal.call(this);
261 }, 215 },
262 216
263 /** @override */ 217 /** @override */
264 isSectionVisibleInternal: function() { 218 isSectionVisibleInternal: function() {
265 if (this.collapseContent) { 219 return this.elements_.some(function(element) {
266 return this.fitToPageTicketItem_.isCapabilityAvailable() || 220 return element.isVisible(this.collapseContent);
267 this.duplexTicketItem_.isCapabilityAvailable(); 221 }, this);
268 }
269
270 return this.isAvailable();
271 }, 222 },
272 223
273 /**
274 * Called when the header-footer checkbox is clicked. Updates the print
275 * ticket.
276 * @private
277 */
278 onHeaderFooterCheckboxClick_: function() {
279 this.headerFooterTicketItem_.updateValue(
280 this.headerFooterCheckbox_.checked);
281 },
282
283 /**
284 * Called when the fit-to-page checkbox is clicked. Updates the print
285 * ticket.
286 * @private
287 */
288 onFitToPageCheckboxClick_: function() {
289 this.fitToPageTicketItem_.updateValue(this.fitToPageCheckbox_.checked);
290 },
291
292 /**
293 * Called when the duplex checkbox is clicked. Updates the print ticket.
294 * @private
295 */
296 onDuplexCheckboxClick_: function() {
297 this.duplexTicketItem_.updateValue(this.duplexCheckbox_.checked);
298 },
299
300 /**
301 * Called when the print CSS backgrounds checkbox is clicked. Updates the
302 * print ticket store.
303 * @private
304 */
305 onCssBackgroundCheckboxClick_: function() {
306 this.cssBackgroundTicketItem_.updateValue(
307 this.cssBackgroundCheckbox_.checked);
308 },
309
310 /**
311 * Called when the print selection only is clicked. Updates the
312 * print ticket store.
313 * @private
314 */
315 onSelectionOnlyCheckboxClick_: function() {
316 this.selectionOnlyTicketItem_.updateValue(
317 this.selectionOnlyCheckbox_.checked);
318 },
319
320 /**
321 * Called when the duplex ticket item has changed. Updates the duplex
322 * checkbox.
323 * @private
324 */
325 onDuplexChange_: function() {
326 this.duplexCheckbox_.checked = this.duplexTicketItem_.getValue();
327 this.updateUiStateInternal();
328 },
329
330 /**
331 * Called when the fit-to-page ticket item has changed. Updates the
332 * fit-to-page checkbox.
333 * @private
334 */
335 onFitToPageChange_: function() {
336 this.fitToPageCheckbox_.checked = this.fitToPageTicketItem_.getValue();
337 this.updateUiStateInternal();
338 },
339
340 /**
341 * Called when the CSS background ticket item has changed. Updates the
342 * CSS background checkbox.
343 * @private
344 */
345 onCssBackgroundChange_: function() {
346 this.cssBackgroundCheckbox_.checked =
347 this.cssBackgroundTicketItem_.getValue();
348 this.updateUiStateInternal();
349 },
350
351 /**
352 * Called when the print selection only ticket item has changed. Updates the
353 * CSS background checkbox.
354 * @private
355 */
356 onSelectionOnlyChange_: function() {
357 this.selectionOnlyCheckbox_.checked =
358 this.selectionOnlyTicketItem_.getValue();
359 this.updateUiStateInternal();
360 },
361
362 /**
363 * Called when the header-footer ticket item has changed. Updates the
364 * header-footer checkbox.
365 * @private
366 */
367 onHeaderFooterChange_: function() {
368 this.headerFooterCheckbox_.checked =
369 this.headerFooterTicketItem_.getValue();
370 this.updateUiStateInternal();
371 },
372 }; 224 };
373 225
374 // Export 226 // Export
375 return { 227 return {
376 OtherOptionsSettings: OtherOptionsSettings 228 OtherOptionsSettings: OtherOptionsSettings
377 }; 229 };
378 }); 230 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview/settings/other_options_settings.html ('k') | chrome/test/data/webui/print_preview.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698