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