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

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

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address reviewer comments Created 8 years, 7 months 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 | Annotate | Revision Log
OLDNEW
(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 /**
9 * Creates a PageSettings object. This object encapsulates all settings and
10 * logic related to page selection.
11 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and
12 * write page range settings.
13 * @constructor
14 * @extends {print_preview.Component}
15 */
16 function PageSettings(printTicketStore) {
17 print_preview.Component.call(this);
18
19 /**
20 * Used to read and write page range settings.
21 * @type {!print_preview.PrintTicketStore}
22 * @private
23 */
24 this.printTicketStore_ = printTicketStore;
25
26 /**
27 * Timeout used to delay processing of the custom page range input.
28 * @type {Object}
29 * @private
30 */
31 this.customInputTimeout_ = null;
32 };
33
34 /**
35 * CSS classes used by the page settings.
36 * @enum {string}
37 * @private
38 */
39 PageSettings.Classes_ = {
40 ALL_RADIO: 'page-settings-all-radio',
41 CUSTOM_HINT: 'page-settings-custom-hint',
42 CUSTOM_INPUT: 'page-settings-custom-input',
43 CUSTOM_RADIO: 'page-settings-custom-radio'
44 };
45
46 /**
47 * Delay in milliseconds before processing custom page range input.
48 * @type {number}
49 * @private
50 */
51 PageSettings.CUSTOM_INPUT_DELAY_ = 500;
52
53 PageSettings.prototype = {
54 __proto__: print_preview.Component.prototype,
55
56 set isEnabled(isEnabled) {
57 this.customInput_.disabled = !isEnabled;
58 this.allRadio_.disabled = !isEnabled;
59 this.customRadio_.disabled = !isEnabled;
60 },
61
62 /** @override */
63 enterDocument: function() {
64 print_preview.Component.prototype.enterDocument.call(this);
65 this.customHintEl_.textContent = localStrings.getStringF(
66 'pageRangeInstruction',
67 localStrings.getString('examplePageRangeText'));
68 this.tracker.add(
69 this.allRadio_, 'click', this.onAllRadioClick_.bind(this));
70 this.tracker.add(
71 this.customRadio_, 'click', this.onCustomRadioClick_.bind(this));
72 this.tracker.add(
73 this.customInput_, 'blur', this.onCustomInputBlur_.bind(this));
74 this.tracker.add(
75 this.customInput_, 'keyup', this.onCustomInputKeyUp_.bind(this));
76 this.tracker.add(
77 this.printTicketStore_,
78 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
79 this.onPrintTicketStoreChange_.bind(this));
80 this.tracker.add(
81 this.printTicketStore_,
82 print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
83 this.onPrintTicketStoreChange_.bind(this));
84 this.tracker.add(
85 this.printTicketStore_,
86 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE,
87 this.onPrintTicketStoreChange_.bind(this));
88 },
89
90 get customInput_() {
91 return this.getElement().getElementsByClassName(
92 PageSettings.Classes_.CUSTOM_INPUT)[0];
93 },
94
95 get allRadio_() {
96 return this.getElement().getElementsByClassName(
97 PageSettings.Classes_.ALL_RADIO)[0];
98 },
99
100 get customRadio_() {
101 return this.getElement().getElementsByClassName(
102 PageSettings.Classes_.CUSTOM_RADIO)[0];
103 },
104
105 get customHintEl_() {
106 return this.getElement().getElementsByClassName(
107 PageSettings.Classes_.CUSTOM_HINT)[0];
108 },
109
110 setInvalidStateVisible_: function(isVisible) {
111 if (isVisible) {
112 this.customInput_.classList.add('invalid');
113 this.customHintEl_.setAttribute('aria-hidden', 'false');
114 fadeInElement(this.customHintEl_);
115 } else {
116 this.customInput_.classList.remove('invalid');
117 fadeOutElement(this.customHintEl_);
118 this.customHintEl_.setAttribute('aria-hidden', 'true');
119 }
120 },
121
122 onAllRadioClick_: function() {
123 this.printTicketStore_.updatePageRange('');
124 },
125
126 onCustomRadioClick_: function() {
127 this.customInput_.focus();
128 this.printTicketStore_.updatePageRange(this.customInput_.value);
129 },
130
131 onCustomInputBlur_: function() {
132 if (this.customInput_.value == '') {
133 this.allRadio_.checked = true;
134 this.customRadio_.checked = false;
135 }
136 },
137
138 onCustomInputKeyUp_: function(event) {
139 if (this.customInputTimeout_) {
140 clearTimeout(this.customInputTimeout_);
141 }
142 if (event.keyIdentifier == 'Enter') {
143 this.printTicketStore_.updatePageRange(this.customInput_.value);
144 } else {
145 this.allRadio_.checked = false;
146 this.customRadio_.checked = true;
147 this.customInputTimeout_ = setTimeout(
148 this.onCustomInputTimeout_.bind(this),
149 PageSettings.CUSTOM_INPUT_DELAY_);
150 }
151 },
152
153 onCustomInputTimeout_: function() {
154 this.customInputTimeout_ = null;
155 if (this.customRadio_.checked) {
156 this.printTicketStore_.updatePageRange(this.customInput_.value);
157 }
158 },
159
160 onPrintTicketStoreChange_: function() {
161 if (this.printTicketStore_.hasPageRangeCapability()) {
162 var pageRangeStr = this.printTicketStore_.getPageRangeStr();
163 if (pageRangeStr) {
164 // TODO There might be a bad case here where we overwrite what the
165 // user is typing. Also could be the case in copies.
166 this.customInput_.value = pageRangeStr;
167 this.customRadio_.checked = true;
168 this.allRadio_.checked = false;
169 this.setInvalidStateVisible_(
170 !this.printTicketStore_.isPageRangeValid());
171 } else {
172 this.allRadio_.checked = true;
173 this.customRadio_.checked = false;
174 this.customInput_.value = '';
175 this.setInvalidStateVisible_(false);
176 }
177 fadeInOption(this.getElement());
178 } else {
179 fadeOutOption(this.getElement());
180 }
181 }
182 };
183
184 // Export
185 return {
186 PageSettings: PageSettings
187 };
188 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698