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
|
// 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 CopiesSettings object.
* @constructor
*/
function CopiesSettings() {
this.copiesOption_ = $('copies-option');
this.textfield_ = $('copies');
this.incrementButton_ = $('increment');
this.decrementButton_ = $('decrement');
// Minimum allowed value for number of copies.
this.minValue_ = 1;
// Maximum allowed value for number of copies.
this.maxValue_ = 999;
this.collateOption_ = $('collate-option');
this.collateCheckbox_ = $('collate');
this.hint_ = $('copies-hint');
this.twoSidedCheckbox_ = $('two-sided');
this.twoSidedOption_ = $('two-sided-option');
// Constant values matches printing::DuplexMode enum. Not using const
// keyword because it is not allowed by JS strict mode.
this.SIMPLEX = 0;
this.LONG_EDGE = 1;
this.UNKNOWN_DUPLEX_MODE = -1;
this.addEventListeners_();
}
cr.addSingletonGetter(CopiesSettings);
CopiesSettings.prototype = {
/**
* The number of copies represented by the contents of |this.textfield_|.
* If the text is not valid returns |this.minValue_|.
* @type {number}
*/
get numberOfCopies() {
var value = parseInt(this.textfield_.value, 10);
if (!value || value <= this.minValue_)
value = this.minValue_;
return value;
},
/**
* Getter method for |collateOption_|.
* @type {HTMLElement}
*/
get collateOption() {
return this.collateOption_;
},
/**
* Getter method for |twoSidedCheckbox_|.
* @type {HTMLInputElement}
*/
get twoSidedCheckbox() {
return this.twoSidedCheckbox_;
},
/**
* Gets the duplex mode information for printing.
* @return {number} duplex mode.
*/
get duplexMode() {
if (this.twoSidedOption_.hidden)
return this.UNKNOWN_DUPLEX_MODE;
else if (this.twoSidedCheckbox_.checked)
return this.LONG_EDGE;
else
return this.SIMPLEX;
},
/**
* @return {boolean} true if |this.textfield_| is empty, or represents a
* positive integer value.
*/
isValid: function() {
return !this.textfield_.value || isPositiveInteger(this.textfield_.value);
},
/**
* Checks whether the preview collate setting value is set or not.
* @return {boolean} true if collate option is enabled and checked.
*/
isCollated: function() {
return !this.collateOption_.hidden && this.collateCheckbox_.checked;
},
/**
* Resets |this.textfield_| to |this.minValue_|.
* @private
*/
reset_: function() {
this.textfield_.value = this.minValue_;
},
/**
* Listener function to execute whenever the increment/decrement buttons are
* clicked.
* @private
* @param {int} sign Must be 1 for an increment button click and -1 for a
* decrement button click.
*/
onButtonClicked_: function(sign) {
if (!this.isValid()) {
this.reset_();
} else {
var newValue = this.numberOfCopies + sign;
this.textfield_.value = newValue;
}
this.updateButtonsState_();
this.showHideCollateOption_();
if (!hasPendingPreviewRequest) {
cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY);
cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON);
}
},
/**
* Listener function to execute whenever a change occurs in |textfield_|
* textfield.
* @private
*/
onTextfieldChanged_: function() {
this.updateButtonsState_();
this.showHideCollateOption_();
if (!hasPendingPreviewRequest) {
cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY);
cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON);
}
},
/**
* Adding listeners to all copies related controls. The listeners take care
* of altering their behavior depending on |hasPendingPreviewRequest|.
* @private
*/
addEventListeners_: function() {
this.textfield_.oninput = this.onTextfieldChanged_.bind(this);
this.incrementButton_.onclick = this.onIncrementButtonClicked_.bind(this);
this.decrementButton_.onclick = this.onDecrementButtonClicked_.bind(this);
this.twoSidedCheckbox_.onclick = function() {
if (!hasPendingPreviewRequest)
cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY);
}
document.addEventListener(customEvents.PDF_LOADED,
this.updateButtonsState_.bind(this));
document.addEventListener(customEvents.PRINTER_CAPABILITIES_UPDATED,
this.onPrinterCapabilitiesUpdated_.bind(this));
},
/**
* Executes when a |customEvents.PRINTER_CAPABILITIES_UPDATED| event occurs.
* @private
*/
onPrinterCapabilitiesUpdated_: function(e) {
this.updateTwoSidedOption_(
e.printerCapabilities.printerDefaultDuplexValue);
e.printerCapabilities.disableCopiesOption ?
fadeOutOption(this.copiesOption_) :
fadeInOption(this.copiesOption_);
},
/**
* Listener triggered when |incrementButton_| is clicked.
* @private
*/
onIncrementButtonClicked_: function() {
this.onButtonClicked_(1);
},
/**
* Listener triggered when |decrementButton_| is clicked.
* @private
*/
onDecrementButtonClicked_: function() {
this.onButtonClicked_(-1);
},
/**
* Takes care of showing/hiding the collate option.
* @private
*/
showHideCollateOption_: function() {
this.collateOption_.hidden = this.numberOfCopies <= 1;
},
/*
* Takes care of showing/hiding the two sided option and also updates the
* default state of the checkbox.
* @param {number} defaultDuplexValue Specifies the default duplex value.
* @private
*/
updateTwoSidedOption_: function(defaultDuplexValue) {
// On Windows, some printers don't specify their duplex values in the
// printer schema. If the printer duplex value is UNKNOWN_DUPLEX_MODE,
// hide the two sided option in preview tab UI.
// Ref bug: http://crbug.com/89204
this.twoSidedOption_.hidden =
(defaultDuplexValue == this.UNKNOWN_DUPLEX_MODE);
if (!this.twoSidedOption_.hidden)
this.twoSidedCheckbox_.checked = !!defaultDuplexValue;
},
/**
* Updates the state of the increment/decrement buttons based on the current
* |textfield_| value.
* @private
*/
updateButtonsState_: function() {
if (!this.isValid()) {
this.textfield_.classList.add('invalid');
this.incrementButton_.disabled = false;
this.decrementButton_.disabled = false;
fadeInElement(this.hint_);
} else {
this.textfield_.classList.remove('invalid');
this.incrementButton_.disabled = this.numberOfCopies == this.maxValue_;
this.decrementButton_.disabled = this.numberOfCopies == this.minValue_;
fadeOutElement(this.hint_);
}
this.hint_.setAttribute('aria-hidden', this.isValid());
}
};
return {
CopiesSettings: CopiesSettings,
};
});
|