OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 'strict'; | 6 'strict'; |
7 | 7 |
8 function MarginTextbox(groupName) { | 8 function MarginTextbox(groupName) { |
9 var box = document.createElement('input'); | 9 var box = document.createElement('input'); |
10 box.__proto__ = MarginTextbox.prototype; | 10 box.__proto__ = MarginTextbox.prototype; |
(...skipping 26 matching lines...) Expand all Loading... |
37 __proto__: HTMLInputElement.prototype, | 37 __proto__: HTMLInputElement.prototype, |
38 | 38 |
39 /** | 39 /** |
40 * @type {number} The margin value currently in the textbox. | 40 * @type {number} The margin value currently in the textbox. |
41 */ | 41 */ |
42 get margin() { | 42 get margin() { |
43 return print_preview.extractMarginValue(this.value); | 43 return print_preview.extractMarginValue(this.value); |
44 }, | 44 }, |
45 | 45 |
46 /** | 46 /** |
| 47 * Sets the contents of the textbox. |
| 48 * @param {number} newValueInPoints The value to be displayed in points. |
| 49 * @private |
| 50 */ |
| 51 setValue_: function(newValueInPoints) { |
| 52 this.value = |
| 53 print_preview.convertPointsToLocaleUnitsText(newValueInPoints); |
| 54 }, |
| 55 |
| 56 /** |
47 * Updates the state of |this|. | 57 * Updates the state of |this|. |
48 * @param {number} value The margin value in points. | 58 * @param {number} value The margin value in points. |
49 * @param {number} valueLimit The upper allowed value for the margin. | 59 * @param {number} valueLimit The upper allowed value for the margin. |
50 * @param {boolean} keepDisplayedValue True if the currently displayed value | 60 * @param {boolean} keepDisplayedValue True if the currently displayed value |
51 * should not be updated. | 61 * should not be updated. |
52 */ | 62 */ |
53 update: function(value, valueLimit, keepDisplayedValue) { | 63 update: function(value, valueLimit, keepDisplayedValue) { |
54 this.lastValidValueInPoints = value; | 64 this.lastValidValueInPoints = value; |
55 if (!keepDisplayedValue) { | 65 if (!keepDisplayedValue) |
56 this.value = print_preview.convertPointsToInchesText( | 66 this.setValue_(this.lastValidValueInPoints); |
57 this.lastValidValueInPoints); | |
58 } | |
59 | 67 |
60 this.valueLimit = valueLimit; | 68 this.valueLimit = valueLimit; |
61 this.validate(); | 69 this.validate(); |
62 }, | 70 }, |
63 | 71 |
64 /** | 72 /** |
65 * Updates |this| while dragging is in progress. | 73 * Updates |this| while dragging is in progress. |
66 * @param {number} dragDeltaInPoints The difference in points between the | 74 * @param {number} dragDeltaInPoints The difference in points between the |
67 * margin value before dragging started and now. | 75 * margin value before dragging started and now. |
68 */ | 76 */ |
69 updateWhileDragging: function(dragDeltaInPoints) { | 77 updateWhileDragging: function(dragDeltaInPoints) { |
70 var validity = this.validateDelta(dragDeltaInPoints); | 78 var validity = this.validateDelta(dragDeltaInPoints); |
71 | 79 |
72 if (validity == print_preview.marginValidationStates.WITHIN_RANGE) { | 80 if (validity == print_preview.marginValidationStates.WITHIN_RANGE) |
73 this.value = print_preview.convertPointsToInchesText( | 81 this.setValue_(this.lastValidValueInPoints + dragDeltaInPoints); |
74 this.lastValidValueInPoints + dragDeltaInPoints); | 82 else if (validity == print_preview.marginValidationStates.TOO_SMALL) |
75 } else if (validity == print_preview.marginValidationStates.TOO_SMALL) { | 83 this.setValue_(0); |
76 this.value = print_preview.convertPointsToInchesText(0); | 84 else if (validity == print_preview.marginValidationStates.TOO_BIG) |
77 } else if (validity == print_preview.marginValidationStates.TOO_BIG) { | 85 this.setValue_(this.valueLimit); |
78 this.value = print_preview.convertPointsToInchesText(this.valueLimit); | |
79 } | |
80 | 86 |
81 this.validate(); | 87 this.validate(); |
82 this.updateColor(); | 88 this.updateColor(); |
83 }, | 89 }, |
84 | 90 |
85 /** | 91 /** |
86 * @param {number} dragDeltaInPoints The difference in points between the | 92 * @param {number} dragDeltaInPoints The difference in points between the |
87 * margin value before dragging started and now. | 93 * margin value before dragging started and now. |
88 * @return {number} An appropriate value from enum |marginValidationStates|. | 94 * @return {number} An appropriate value from enum |marginValidationStates|. |
89 */ | 95 */ |
90 validateDelta: function(dragDeltaInPoints) { | 96 validateDelta: function(dragDeltaInPoints) { |
91 var newValue = this.lastValidValueInPoints + dragDeltaInPoints; | 97 var newValue = this.lastValidValueInPoints + dragDeltaInPoints; |
92 return print_preview.validateMarginValue(newValue, this.valueLimit); | 98 return print_preview.validateMarginValue(newValue, this.valueLimit); |
93 }, | 99 }, |
94 | 100 |
95 /** | 101 /** |
96 * Updates |this.isValid|. | 102 * Updates |this.isValid|. |
97 */ | 103 */ |
98 validate: function() { | 104 validate: function() { |
99 this.isValid = | 105 this.isValid = |
100 print_preview.validateMarginText(this.value, this.valueLimit) == | 106 print_preview.validateMarginText(this.value, this.valueLimit) == |
101 print_preview.marginValidationStates.WITHIN_RANGE; | 107 print_preview.marginValidationStates.WITHIN_RANGE; |
102 if (this.isValid) | |
103 this.value = print_preview.convertInchesToInchesText(this.margin); | |
104 }, | 108 }, |
105 | 109 |
106 /** | 110 /** |
107 * Updates the background color depending on |isValid| by adding/removing | 111 * Updates the background color depending on |isValid| by adding/removing |
108 * the appropriate CSS class. | 112 * the appropriate CSS class. |
109 * @param {boolean} isValid True if the margin is valid. | 113 * @param {boolean} isValid True if the margin is valid. |
110 */ | 114 */ |
111 updateColor: function() { | 115 updateColor: function() { |
112 this.isValid ? this.classList.remove('invalid') : | 116 this.isValid ? this.classList.remove('invalid') : |
113 this.classList.add('invalid'); | 117 this.classList.add('invalid'); |
(...skipping 18 matching lines...) Expand all Loading... |
132 }, | 136 }, |
133 | 137 |
134 /** | 138 /** |
135 * Executes whenever a blur event occurs. | 139 * Executes whenever a blur event occurs. |
136 * @private | 140 * @private |
137 */ | 141 */ |
138 onBlur_: function() { | 142 onBlur_: function() { |
139 clearTimeout(this.timerId_); | 143 clearTimeout(this.timerId_); |
140 this.validate(); | 144 this.validate(); |
141 if (!this.isValid) { | 145 if (!this.isValid) { |
142 this.value = print_preview.convertPointsToInchesText( | 146 this.setValue_(this.lastValidValueInPoints); |
143 this.lastValidValueInPoints); | |
144 this.validate(); | 147 this.validate(); |
145 } | 148 } |
146 | 149 |
147 this.updateColor(); | 150 this.updateColor(); |
148 cr.dispatchSimpleEvent(document, 'updateSummary'); | 151 cr.dispatchSimpleEvent(document, 'updateSummary'); |
149 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 152 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
150 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); | 153 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); |
151 }, | 154 }, |
152 | 155 |
153 /** | 156 /** |
154 * Executes whenever a keypressed event occurs. Note: Only the "Enter" key | 157 * Executes whenever a keypressed event occurs. Note: Only the "Enter" key |
155 * event is handled. The "Escape" key does not result in such event, | 158 * event is handled. The "Escape" key does not result in such event, |
156 * therefore it is handled by |this.onKeyUp_|. | 159 * therefore it is handled by |this.onKeyUp_|. |
157 * @param {KeyboardEvent} e The event that triggered this listener. | 160 * @param {KeyboardEvent} e The event that triggered this listener. |
158 * @private | 161 * @private |
159 */ | 162 */ |
160 onKeyPressed_: function(e) { | 163 onKeyPressed_: function(e) { |
161 if (e.keyCode == MarginTextbox.ENTER_KEYCODE) | 164 if (e.keyCode == MarginTextbox.ENTER_KEYCODE) |
162 this.blur(); | 165 this.blur(); |
163 }, | 166 }, |
164 | 167 |
165 /** | 168 /** |
166 * Executes whenever a keyup event occurs. Note: Only the "Escape" | 169 * Executes whenever a keyup event occurs. Note: Only the "Escape" |
167 * key event is handled. | 170 * key event is handled. |
168 * @param {KeyboardEvent} e The event that triggered this listener. | 171 * @param {KeyboardEvent} e The event that triggered this listener. |
169 * @private | 172 * @private |
170 */ | 173 */ |
171 onKeyUp_: function(e) { | 174 onKeyUp_: function(e) { |
172 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { | 175 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { |
173 this.value = print_preview.convertPointsToInchesText( | 176 this.setValue_(this.lastValidValueInPoints); |
174 this.lastValidValueInPoints); | |
175 this.validate(); | 177 this.validate(); |
176 this.updateColor(); | 178 this.updateColor(); |
177 cr.dispatchSimpleEvent(document, 'updateSummary'); | 179 cr.dispatchSimpleEvent(document, 'updateSummary'); |
178 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | 180 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
179 } | 181 } |
180 }, | 182 }, |
181 | 183 |
182 /** | 184 /** |
183 * Resetting the timer used to detect when the user stops typing in order | 185 * Resetting the timer used to detect when the user stops typing in order |
184 * to update the print preview. | 186 * to update the print preview. |
(...skipping 19 matching lines...) Expand all Loading... |
204 return; | 206 return; |
205 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); | 207 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); |
206 } | 208 } |
207 | 209 |
208 }; | 210 }; |
209 | 211 |
210 return { | 212 return { |
211 MarginTextbox: MarginTextbox | 213 MarginTextbox: MarginTextbox |
212 }; | 214 }; |
213 }); | 215 }); |
OLD | NEW |