1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
cr.define('print_preview', function() {
'use strict';
/**
* Creates a PageSettings object. This object encapsulates all settings and
* logic related to page selection.
* @param {!print_preview.ticket_items.PageRange} pageRangeTicketItem Used to
* read and write page range settings.
* @constructor
* @extends {print_preview.Component}
*/
function PageSettings(pageRangeTicketItem) {
print_preview.Component.call(this);
/**
* Used to read and write page range settings.
* @type {!print_preview.ticket_items.PageRange}
* @private
*/
this.pageRangeTicketItem_ = pageRangeTicketItem;
/**
* Timeout used to delay processing of the custom page range input.
* @type {?number}
* @private
*/
this.customInputTimeout_ = null;
/**
* Custom page range input.
* @type {HTMLInputElement}
* @private
*/
this.customInput_ = null;
/**
* Custom page range radio button.
* @type {HTMLInputElement}
* @private
*/
this.customRadio_ = null;
/**
* All page rage radio button.
* @type {HTMLInputElement}
* @private
*/
this.allRadio_ = null;
/**
* Container of a hint to show when the custom page range is invalid.
* @type {HTMLElement}
* @private
*/
this.customHintEl_ = null;
};
/**
* CSS classes used by the page settings.
* @enum {string}
* @private
*/
PageSettings.Classes_ = {
ALL_RADIO: 'page-settings-all-radio',
CUSTOM_HINT: 'page-settings-custom-hint',
CUSTOM_INPUT: 'page-settings-custom-input',
CUSTOM_RADIO: 'page-settings-custom-radio'
};
/**
* Delay in milliseconds before processing custom page range input.
* @type {number}
* @private
*/
PageSettings.CUSTOM_INPUT_DELAY_ = 500;
PageSettings.prototype = {
__proto__: print_preview.Component.prototype,
set isEnabled(isEnabled) {
this.customInput_.disabled = !isEnabled;
this.allRadio_.disabled = !isEnabled;
this.customRadio_.disabled = !isEnabled;
},
/** @override */
enterDocument: function() {
print_preview.Component.prototype.enterDocument.call(this);
this.tracker.add(
this.allRadio_, 'click', this.onAllRadioClick_.bind(this));
this.tracker.add(
this.customRadio_, 'click', this.onCustomRadioClick_.bind(this));
this.tracker.add(
this.customInput_, 'blur', this.onCustomInputBlur_.bind(this));
this.tracker.add(
this.customInput_, 'focus', this.onCustomInputFocus_.bind(this));
this.tracker.add(
this.customInput_, 'keyup', this.onCustomInputKeyUp_.bind(this));
this.tracker.add(
this.pageRangeTicketItem_,
print_preview.ticket_items.TicketItem.EventType.CHANGE,
this.onPageRangeTicketItemChange_.bind(this));
},
/** @override */
exitDocument: function() {
print_preview.Component.prototype.exitDocument.call(this);
this.customInput_ = null;
this.customRadio_ = null;
this.allRadio_ = null;
this.customHintEl_ = null;
},
/** @override */
decorateInternal: function() {
this.customInput_ = this.getElement().getElementsByClassName(
PageSettings.Classes_.CUSTOM_INPUT)[0];
this.allRadio_ = this.getElement().getElementsByClassName(
PageSettings.Classes_.ALL_RADIO)[0];
this.customRadio_ = this.getElement().getElementsByClassName(
PageSettings.Classes_.CUSTOM_RADIO)[0];
this.customHintEl_ = this.getElement().getElementsByClassName(
PageSettings.Classes_.CUSTOM_HINT)[0];
this.customHintEl_.textContent = localStrings.getStringF(
'pageRangeInstruction',
localStrings.getString('examplePageRangeText'));
},
/**
* @param {boolean} Whether the custom hint is visible.
* @private
*/
setInvalidStateVisible_: function(isVisible) {
if (isVisible) {
this.customInput_.classList.add('invalid');
this.customHintEl_.setAttribute('aria-hidden', 'false');
fadeInElement(this.customHintEl_);
} else {
this.customInput_.classList.remove('invalid');
fadeOutElement(this.customHintEl_);
this.customHintEl_.setAttribute('aria-hidden', 'true');
}
},
/**
* Called when the all radio button is clicked. Updates the print ticket.
* @private
*/
onAllRadioClick_: function() {
this.pageRangeTicketItem_.updateValue(null);
},
/**
* Called when the custom radio button is clicked. Updates the print ticket.
* @private
*/
onCustomRadioClick_: function() {
this.customInput_.focus();
},
/**
* Called when the custom input is blurred. Enables the all radio button if
* the custom input is empty.
* @private
*/
onCustomInputBlur_: function() {
if (this.customInput_.value == '') {
this.allRadio_.checked = true;
}
},
/**
* Called when the custom input is focused.
* @private
*/
onCustomInputFocus_: function() {
this.customRadio_.checked = true;
this.pageRangeTicketItem_.updateValue(this.customInput_.value);
},
/**
* Called when a key is pressed on the custom input.
* @param {Event} event Contains the key that was pressed.
* @private
*/
onCustomInputKeyUp_: function(event) {
if (this.customInputTimeout_) {
clearTimeout(this.customInputTimeout_);
}
if (event.keyIdentifier == 'Enter') {
this.pageRangeTicketItem_.updateValue(this.customInput_.value);
} else {
this.customRadio_.checked = true;
this.customInputTimeout_ = setTimeout(
this.onCustomInputTimeout_.bind(this),
PageSettings.CUSTOM_INPUT_DELAY_);
}
},
/**
* Called after a delay following a key press in the custom input.
* @private
*/
onCustomInputTimeout_: function() {
this.customInputTimeout_ = null;
if (this.customRadio_.checked) {
this.pageRangeTicketItem_.updateValue(this.customInput_.value);
}
},
/**
* Called when the print ticket changes. Updates the state of the component.
* @private
*/
onPageRangeTicketItemChange_: function() {
if (this.pageRangeTicketItem_.isCapabilityAvailable()) {
var pageRangeStr = this.pageRangeTicketItem_.getValue();
if (pageRangeStr || this.customRadio_.checked) {
if (!document.hasFocus() ||
document.activeElement != this.customInput_) {
this.customInput_.value = pageRangeStr;
}
this.customRadio_.checked = true;
this.setInvalidStateVisible_(!this.pageRangeTicketItem_.isValid());
} else {
this.allRadio_.checked = true;
this.setInvalidStateVisible_(false);
}
fadeInOption(this.getElement());
} else {
fadeOutOption(this.getElement());
}
}
};
// Export
return {
PageSettings: PageSettings
};
});
|