Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: chrome/browser/resources/print_preview/previewarea/margin_control_container.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes broken tests Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 cr.define('print_preview', function() {
6 'use strict';
7
8 /**
9 * UI component used for setting custom print margins.
10 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and
11 * write custom margin values.
12 * @constructor
13 * @extends {print_preview.Component}
14 */
15 function MarginControlContainer(printTicketStore) {
16 print_preview.Component.call(this);
17
18 /**
19 * Used to read and write custom margin values.
20 * @type {!print_preview.PrintTicketStore}
21 * @private
22 */
23 this.printTicketStore_ = printTicketStore;
24
25 /**
26 * Convenience array that contains all of the margin controls.
27 * @type {!Object<
28 * print_preview.ticket_items.CustomMargins.Orientation,
29 * !print_preview.MarginControl>}
30 * @private
31 */
32 this.controls_ = {};
33 for (var key in print_preview.ticket_items.CustomMargins.Orientation) {
34 var orientation = print_preview.ticket_items.CustomMargins.Orientation[
35 key];
36 var control = new print_preview.MarginControl(orientation);
37 this.controls_[orientation] = control;
38 this.addChild(control);
39 }
40
41 /**
42 * Margin control currently being dragged. Null if no control is being
43 * dragged.
44 * @type {print_preview.MarginControl}
45 * @private
46 */
47 this.draggedControl_ = null;
48
49 /**
50 * Translation transformation in pixels to translate from the origin of the
51 * custom margins component to the top-left corner of the most visible
52 * preview page.
53 * @type {!print_preview.Coordinate2d}
54 * @private
55 */
56 this.translateTransform_ = new print_preview.Coordinate2d(0, 0);
57
58 /**
59 * Scaling transformation to scale from pixels to the units which the
60 * print preview is in. The scaling factor is the same in both dimensions,
61 * so this field is just a single number.
62 * @type {number}
63 * @private
64 */
65 this.scaleTransform_ = 1;
66
67 /**
68 * Clipping size for clipping the margin controls.
69 * @type {print_preview.Size}
70 * @private
71 */
72 this.clippingSize_ = null;
73 };
74
75 /**
76 * CSS classes used by the custom margins component.
77 * @enum {string}
78 * @private
79 */
80 MarginControlContainer.Classes_ = {
81 DRAGGING_HORIZONTAL: 'margin-control-container-dragging-horizontal',
82 DRAGGING_VERTICAL: 'margin-control-container-dragging-vertical'
83 };
84
85 /**
86 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation
87 * Orientation value to test.
88 * @return {boolean} Whether the given orientation is TOP or BOTTOM.
89 * @private
90 */
91 MarginControlContainer.isTopOrBottom_ = function(orientation) {
92 return orientation ==
93 print_preview.ticket_items.CustomMargins.Orientation.TOP ||
94 orientation ==
95 print_preview.ticket_items.CustomMargins.Orientation.BOTTOM;
96 };
97
98 MarginControlContainer.prototype = {
99 __proto__: print_preview.Component.prototype,
100
101 /**
102 * Updates the translation transformation that translates pixel values in
103 * the space of the HTML DOM.
104 * @param {print_preview.Coordinate2d} translateTransform Updated value of
105 * the translation transformation.
106 */
107 updateTranslationTransform: function(translateTransform) {
108 if (!translateTransform.equals(this.translateTransform_)) {
109 this.translateTransform_ = translateTransform;
110 for (var orientation in this.controls_) {
111 this.controls_[orientation].setTranslateTransform(translateTransform);
112 }
113 }
114 },
115
116 /**
117 * Updates the scaling transform that scales pixels values to point values.
118 * @param {number} scaleTransform Updated value of the scale transform.
119 */
120 updateScaleTransform: function(scaleTransform) {
121 if (scaleTransform != this.scaleTransform_) {
122 this.scaleTransform_ = scaleTransform;
123 for (var orientation in this.controls_) {
124 this.controls_[orientation].setScaleTransform(scaleTransform);
125 }
126 }
127 },
128
129 /**
130 * Clips margin controls to the given clip size in pixels.
131 * @param {print_preview.Size} Size to clip the margin controls to.
132 */
133 updateClippingMask: function(clipSize) {
134 if (!clipSize) {
135 return;
136 }
137 this.clippingSize_ = clipSize;
138 for (var orientation in this.controls_) {
139 var el = this.controls_[orientation].getElement();
140 el.style.clip = 'rect(' +
141 (-el.offsetTop) + 'px, ' +
142 (clipSize.width - el.offsetLeft) + 'px, ' +
143 (clipSize.height - el.offsetTop) + 'px, ' +
144 (-el.offsetLeft) + 'px)';
145 }
146 },
147
148 /** @override */
149 decorateInternal: function() {
150 for (var orientation in this.controls_) {
151 this.controls_[orientation].render(this.getElement());
152 }
153 },
154
155 /** @override */
156 enterDocument: function() {
157 print_preview.Component.prototype.enterDocument.call(this);
158
159 this.tracker.add(
160 this.getElement(), 'mousemove', this.onMouseMove_.bind(this));
161 this.tracker.add(
162 this.getElement(), 'mouseup', this.onMouseUp_.bind(this));
163 this.tracker.add(
164 this.getElement(), 'mouseover', this.onMouseOver_.bind(this));
165 this.tracker.add(
166 this.getElement(), 'mouseout', this.onMouseOut_.bind(this));
167
168 this.tracker.add(
169 this.printTicketStore_,
170 print_preview.PrintTicketStore.EventType.INITIALIZE,
171 this.onTicketChange_.bind(this));
172 this.tracker.add(
173 this.printTicketStore_,
174 print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
175 this.onTicketChange_.bind(this));
176 this.tracker.add(
177 this.printTicketStore_,
178 print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
179 this.onTicketChange_.bind(this));
180 this.tracker.add(
181 this.printTicketStore_,
182 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE,
183 this.onTicketChange_.bind(this));
184
185 for (var orientation in this.controls_) {
186 this.tracker.add(
187 this.controls_[orientation],
188 print_preview.MarginControl.EventType.DRAG_START,
189 this.onControlDragStart_.bind(this, this.controls_[orientation]));
190 this.tracker.add(
191 this.controls_[orientation],
192 print_preview.MarginControl.EventType.TEXT_CHANGE,
193 this.onControlTextChange_.bind(this, this.controls_[orientation]));
194 }
195 },
196
197 /**
198 * @param {boolean} isVisible Whether the margin controls are visible.
199 * @private
200 */
201 setIsMarginControlsVisible_: function(isVisible) {
202 for (var orientation in this.controls_) {
203 this.controls_[orientation].setIsVisible(isVisible);
204 }
205 },
206
207 /**
208 * Moves the position of the given control to the desired position in
209 * pixels.
210 * @param {!print_preview.MarginControl} control Control to move.
211 * @param {!print_preview.Coordinate2d} posInPixels Desired position to move
212 * to in pixels.
213 * @private
214 */
215 moveControl_: function(control, posInPixels) {
216 var newPosInPts;
217 if (MarginControlContainer.isTopOrBottom_(control.getOrientation())) {
218 newPosInPts = control.convertPixelsToPts(posInPixels.y);
219 } else {
220 newPosInPts = control.convertPixelsToPts(posInPixels.x);
221 }
222 control.setPositionInPts(newPosInPts);
223 control.setTextboxValue(
224 this.printTicketStore_.serializeMarginFromPts(newPosInPts));
225 },
226
227 /**
228 * Called when a margin control starts to drag.
229 * @param {print_preview.MarginControl} control The control which started to
230 * drag.
231 * @private
232 */
233 onControlDragStart_: function(control) {
234 this.draggedControl_ = control;
235 this.getElement().classList.add(
236 MarginControlContainer.isTopOrBottom_(control.getOrientation()) ?
237 MarginControlContainer.Classes_.DRAGGING_VERTICAL :
238 MarginControlContainer.Classes_.DRAGGING_HORIZONTAL);
239 },
240
241 /**
242 * Called when the mouse moves in the custom margins component. Moves the
243 * dragged margin control.
244 * @param {MouseEvent} event Contains the position of the mouse.
245 * @private
246 */
247 onMouseMove_: function(event) {
248 if (this.draggedControl_) {
249 this.moveControl_(
250 this.draggedControl_,
251 this.draggedControl_.translateMouseToPositionInPixels(
252 new print_preview.Coordinate2d(event.x, event.y)));
253 this.updateClippingMask(this.clippingSize_);
254 }
255 },
256
257 /**
258 * Called when the mouse is released in the custom margins component.
259 * Releases the dragged margin control.
260 * @param {MouseEvent} event Contains the position of the mouse.
261 * @private
262 */
263 onMouseUp_: function(event) {
264 if (this.draggedControl_) {
265 this.getElement().classList.remove(
266 MarginControlContainer.Classes_.DRAGGING_VERTICAL);
267 this.getElement().classList.remove(
268 MarginControlContainer.Classes_.DRAGGING_HORIZONTAL);
269 if (event) {
270 var posInPixels =
271 this.draggedControl_.translateMouseToPositionInPixels(
272 new print_preview.Coordinate2d(event.x, event.y));
273 this.draggedControl_.setIsAnimated(true);
274 this.moveControl_(this.draggedControl_, posInPixels);
275 }
276 this.updateClippingMask(this.clippingSize_);
277 this.printTicketStore_.updateCustomMargin(
278 this.draggedControl_.getOrientation(),
279 this.draggedControl_.getTextboxValue());
280 this.draggedControl_ = null;
281 }
282 },
283
284 /**
285 * Called when the mouse moves onto the component. Shows the margin
286 * controls.
287 * @private
288 */
289 onMouseOver_: function() {
290 var fromElement = event.fromElement;
291 while (fromElement != null) {
292 if (fromElement == this.getElement()) {
293 return;
294 }
295 fromElement = fromElement.parentElement;
296 }
297 if (this.printTicketStore_.hasMarginsCapability() &&
298 this.printTicketStore_.getMarginsType() ==
299 print_preview.ticket_items.MarginsType.Value.CUSTOM) {
300 this.setIsMarginControlsVisible_(true);
301 }
302 },
303
304 /**
305 * Called when the mouse moves off of the component. Hides the margin
306 * controls.
307 * @private
308 */
309 onMouseOut_: function(event) {
310 var toElement = event.toElement;
311 while (toElement != null) {
312 if (toElement == this.getElement()) {
313 return;
314 }
315 toElement = toElement.parentElement;
316 }
317 this.onMouseUp_(null);
318 for (var orientation in this.controls_) {
319 if (this.controls_[orientation].getIsFocused() ||
320 this.controls_[orientation].getIsInError()) {
321 return;
322 }
323 }
324 this.setIsMarginControlsVisible_(false);
325 },
326
327 /**
328 * Called when the print ticket changes. Updates the position of the margin
329 * controls.
330 * @private
331 */
332 onTicketChange_: function() {
333 var stringMargins = this.printTicketStore_.getCustomMargins();
334 var isCustomMarginsValid = this.printTicketStore_.isCustomMarginsValid();
335 for (var orientation in this.controls_) {
336 var control = this.controls_[orientation];
337 var isInError =
338 !this.printTicketStore_.isCustomMarginParseable(orientation);
339 control.setIsInError(isInError);
340 control.setPageSize(this.printTicketStore_.pageSize);
341 control.setTextboxValue(stringMargins.get(orientation));
342 control.setIsEnabled(isCustomMarginsValid || isInError);
343 }
344 if (isCustomMarginsValid) {
345 var customMarginsInPts = this.printTicketStore_.getCustomMarginsInPts();
346 for (var orientation in this.controls_) {
347 var control = this.controls_[orientation];
348 control.setPositionInPts(customMarginsInPts.get(orientation));
349 }
350 }
351 this.updateClippingMask(this.clippingSize_);
352 this.setIsMarginControlsVisible_(
353 this.printTicketStore_.getMarginsType() ==
354 print_preview.ticket_items.MarginsType.Value.CUSTOM);
355 },
356
357 /**
358 * Called when the text in a textbox of a margin control changes.
359 * Updates the print ticket store.
360 * @param {!print_preview.MarginControl} control Updated control.
361 * @private
362 */
363 onControlTextChange_: function(control) {
364 control.setIsAnimated(true);
365 this.printTicketStore_.updateCustomMargin(
366 control.getOrientation(), control.getTextboxValue());
367 }
368 };
369
370 // Export
371 return {
372 MarginControlContainer: MarginControlContainer
373 };
374 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698