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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
// Copyright (c) 2011 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.
* @constructor
*/
function PageSettings() {
this.allPagesRadioButton_ = $('all-pages');
this.selectedPagesRadioButton_ = $('print-pages');
this.selectedPagesTextfield_ = $('individual-pages');
this.selectedPagesHint_ = $('individual-pages-hint');
// Timer id of |this.selectedPagesTextfield|. It is used to reset the timer
// whenever needed.
this.timerId_;
// Contains the previously selected pages (pages requested by last
// preview request). It is used in
// |this.onSelectedPagesMayHaveChanged_()| to make sure that a new preview
// is not requested more often than necessary.
this.previouslySelectedPages_ = [];
// The total page count of the previewed document regardless of which pages
// the user has selected.
this.totalPageCount_ = undefined;
this.addEventListeners_();
}
cr.addSingletonGetter(PageSettings);
PageSettings.prototype = {
/**
* The text that is currently in |this.selectedPagesTextfield|.
* @type {string}
*/
get selectedPagesText() {
return this.selectedPagesTextfield_.value;
},
/**
* The radio button corresponding to "all pages selected".
* @type {HTMLInputElement}
*/
get allPagesRadioButton() {
return this.allPagesRadioButton_;
},
/**
* The radio button corresponding to "specific pages selected".
* @type {HTMLInputElement}
*/
get selectedPagesRadioButton() {
return this.selectedPagesRadioButton_;
},
/**
* The textfield containing page ranges as specified by the user.
* @type {HTMLInputElement}
*/
get selectedPagesTextfield() {
return this.selectedPagesTextfield_;
},
/**
* The span element containing the hint shown to the user when page
* selection is not valid.
* @type {HTMLElement}
*/
get selectedPagesHint() {
return this.selectedPagesHint_;
},
/**
* The total page count of the previewed document regardless of which pages
* the user has selected. If the total count is not known this value must
* be undefined.
* @type {number}
*/
get totalPageCount() {
return this.totalPageCount_;
},
/**
* @param {number} count The number to assing to |this.totalPageCount_|.
*/
set totalPageCount(count) {
this.totalPageCount_ = count;
},
/**
* Returns the selected pages in ascending order without any duplicates.
*
* @return {Array}
*/
get selectedPagesSet() {
var selectedPagesText = this.selectedPagesText;
if (this.allPagesRadioButton.checked || selectedPagesText.length == 0)
selectedPagesText = '1-' + this.totalPageCount_;
var pageList = pageRangeTextToPageList(selectedPagesText,
this.totalPageCount_);
return pageListToPageSet(pageList);
},
/**
* Returns the previously selected pages in ascending order without any
* duplicates.
*
* @return {Array}
*/
get previouslySelectedPages() {
return this.previouslySelectedPages_;
},
/**
* Returns an array of objects describing the selected page ranges. See
* documentation of pageSetToPageRanges() for more details.
* @return {Array}
*/
get selectedPageRanges() {
return pageSetToPageRanges(this.selectedPagesSet);
},
/**
* Invalidates |this.totalPageCount_| to indicate that the total number of
* pages is not known.
* @private
*/
invalidateTotalPageCount_: function() {
this.totalPageCount_ = undefined;
},
/**
* Invlidates |this.previouslySelectedPages_| to indicate that this value
* does no longer apply.
* @private
*/
invalidatePreviouslySelectedPages_: function() {
this.previouslySelectedPages_.length = 0;
},
/**
* Resets all the state variables of this object and hides
* |this.selectedPagesHint|.
*/
resetState: function() {
this.selectedPagesTextfield.classList.remove('invalid');
fadeOutElement(this.selectedPagesHint_);
this.invalidateTotalPageCount_();
this.invalidatePreviouslySelectedPages_();
},
/**
* Updates |this.totalPageCount_| and |this.previouslySelectedPages_|,
* only if they have been previously invalidated.
* @param {number} newTotalPageCount The new total page count.
*/
updateState: function(newTotalPageCount) {
if (!this.totalPageCount_)
this.totalPageCount_ = newTotalPageCount;
if (this.previouslySelectedPages_.length == 0) {
for (var i = 0; i < this.totalPageCount_; i++)
this.previouslySelectedPages_.push(i + 1);
}
if (!this.isPageSelectionValid())
this.onSelectedPagesTextfieldChanged();
},
/**
* Updates |this.previouslySelectedPages_| with the currently selected
* pages.
*/
updatePageSelection: function() {
this.previouslySelectedPages_ = this.selectedPagesSet;
},
/**
* @private
* @return {boolean} true if currently selected pages differ from
* |this.previouslySelectesPages_|.
*/
hasPageSelectionChanged_: function() {
return !areArraysEqual(this.previouslySelectedPages_,
this.selectedPagesSet);
},
/**
* Checks if the page selection has changed and is valid.
* @return {boolean} true if the page selection is changed and is valid.
*/
hasPageSelectionChangedAndIsValid: function() {
return this.isPageSelectionValid() && this.hasPageSelectionChanged_();
},
/**
* Validates the contents of |this.selectedPagesTextfield|.
*
* @return {boolean} true if the text is valid.
*/
isPageSelectionValid: function() {
if (this.allPagesRadioButton_.checked ||
this.selectedPagesText.length == 0) {
return true;
}
return isPageRangeTextValid(this.selectedPagesText, this.totalPageCount_);
},
/**
* Checks all page selection related settings and requests a new print
* previw if needed.
* @return {boolean} true if a new preview was requested.
*/
requestPrintPreviewIfNeeded: function() {
if (this.hasPageSelectionChangedAndIsValid()) {
this.updatePageSelection();
requestPrintPreview();
return true;
}
if (!this.isPageSelectionValid())
this.onSelectedPagesTextfieldChanged();
return false;
},
/**
* Validates the selected pages and updates the hint accordingly.
* @private
*/
validateSelectedPages_: function() {
if (this.isPageSelectionValid()) {
this.selectedPagesTextfield.classList.remove('invalid');
fadeOutElement(this.selectedPagesHint_);
this.selectedPagesHint.setAttribute('aria-hidden', 'true');
} else {
this.selectedPagesTextfield.classList.add('invalid');
this.selectedPagesHint.classList.remove('suggestion');
this.selectedPagesHint.setAttribute('aria-hidden', 'false');
this.selectedPagesHint.innerHTML =
localStrings.getStringF('pageRangeInstruction',
localStrings.getString(
'examplePageRangeText'));
fadeInElement(this.selectedPagesHint);
}
},
/**
* Executes whenever a blur event occurs on |this.selectedPagesTextfield|
* or when the timer expires. It takes care of
* 1) showing/hiding warnings/suggestions
* 2) updating print button/summary
*/
onSelectedPagesTextfieldChanged: function() {
this.validateSelectedPages_();
cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY);
cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON);
},
/**
* When the user stops typing in |this.selectedPagesTextfield| or clicks on
* |allPagesRadioButton|, a new print preview is requested, only if
* 1) The input is compeletely valid (it can be parsed in its entirety).
* 2) The newly selected pages differ from |this.previouslySelectedPages_|.
* @private
*/
onSelectedPagesMayHaveChanged_: function() {
if (this.selectedPagesRadioButton_.checked)
this.onSelectedPagesTextfieldChanged();
// Toggling between "all pages"/"some pages" radio buttons while having an
// invalid entry in the page selection textfield still requires updating
// the print summary and print button.
if (!this.isPageSelectionValid() || !this.hasPageSelectionChanged_()) {
cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY);
cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON);
return;
}
requestPrintPreview();
},
/**
* Whenever |this.selectedPagesTextfield| gains focus we add a timer to
* detect when the user stops typing in order to update the print preview.
* @private
*/
addTimerToSelectedPagesTextfield_: function() {
this.timerId_ = window.setTimeout(
this.onSelectedPagesMayHaveChanged_.bind(this), 1000);
},
/**
* As the user types in |this.selectedPagesTextfield|, we need to reset
* this timer, since the page ranges are still being edited.
* @private
*/
resetSelectedPagesTextfieldTimer_: function() {
clearTimeout(this.timerId_);
this.addTimerToSelectedPagesTextfield_();
},
/**
* Handles the blur event of |this.selectedPagesTextfield|. Un-checks
* |this.selectedPagesRadioButton| if the input field is empty.
* @private
*/
onSelectedPagesTextfieldBlur_: function() {
clearTimeout(this.timerId_);
if (!this.selectedPagesText.length) {
this.allPagesRadioButton_.checked = true;
this.validateSelectedPages_();
}
this.onSelectedPagesMayHaveChanged_();
},
/**
* Gives focus to |this.selectedPagesTextfield| when
* |this.selectedPagesRadioButton| is clicked.
* @private
*/
onSelectedPagesRadioButtonChecked_: function() {
this.selectedPagesTextfield_.focus();
},
/**
* Listener executing when an input event occurs in
* |this.selectedPagesTextfield|. Ensures that
* |this.selectedPagesTextfield| is non-empty before checking
* |this.selectedPagesRadioButton|.
* @private
*/
onSelectedPagesTextfieldInput_: function() {
if (this.selectedPagesText.length)
this.selectedPagesRadioButton.checked = true;
this.resetSelectedPagesTextfieldTimer_();
},
/**
* Adding listeners to all pages related controls. The listeners take care
* of altering their behavior depending on |hasPendingPreviewRequest|.
* @private
*/
addEventListeners_: function() {
this.allPagesRadioButton.onclick =
this.onSelectedPagesMayHaveChanged_.bind(this);
this.selectedPagesRadioButton.onclick =
this.onSelectedPagesMayHaveChanged_.bind(this);
this.selectedPagesTextfield.oninput =
this.onSelectedPagesTextfieldInput_.bind(this);
this.selectedPagesTextfield.onfocus =
this.addTimerToSelectedPagesTextfield_.bind(this);
this.selectedPagesTextfield.onblur =
this.onSelectedPagesTextfieldBlur_.bind(this);
}
};
return {
PageSettings: PageSettings,
};
});
|