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

Side by Side Diff: chrome/browser/resources/print_preview/margins_ui.js

Issue 8233030: Print Preview: Making margin lines draggable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing mouse events outside the plugin area, adding documentations Created 9 years, 2 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
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 /** 8 function MarginsUI() {
9 * @constructor
10 * This class represents a margin line and a textbox corresponding to that
11 * margin.
12 */
13 function MarginsUIPair(groupName) {
14 this.line_ = new print_preview.MarginLine(groupName);
15 this.box_ = new print_preview.MarginTextbox(groupName);
16 }
17
18 MarginsUIPair.prototype = {
19 __proto__: MarginsUIPair.prototype,
20
21 /**
22 * Updates the state.
23 */
24 update: function(marginsRectangle, value, valueLimit, keepDisplayedValue) {
25 this.line_.update(marginsRectangle);
26 this.box_.update(marginsRectangle, value, valueLimit, keepDisplayedValue);
27 },
28
29 /**
30 * Draws |this| based on the state.
31 */
32 draw: function() {
33 this.line_.draw();
34 this.box_.draw();
35 }
36 };
37
38 function MarginsUI(parentNode) {
39 var marginsUI = document.createElement('div'); 9 var marginsUI = document.createElement('div');
40 marginsUI.__proto__ = MarginsUI.prototype; 10 marginsUI.__proto__ = MarginsUI.prototype;
41 marginsUI.id = 'customized-margins'; 11 marginsUI.id = 'customized-margins';
42 12
43 marginsUI.topPair_ = new MarginsUIPair( 13 // @type {print_preview.MarginsUIPair} The object corresponding to the top
14 // margin.
15 marginsUI.topPair_ = new print_preview.MarginsUIPair(
44 print_preview.MarginSettings.TOP_GROUP); 16 print_preview.MarginSettings.TOP_GROUP);
45 marginsUI.leftPair_ = new MarginsUIPair( 17 // @type {print_preview.MarginsUIPair} The object corresponding to the left
18 // margin.
19 marginsUI.leftPair_ = new print_preview.MarginsUIPair(
46 print_preview.MarginSettings.LEFT_GROUP); 20 print_preview.MarginSettings.LEFT_GROUP);
47 marginsUI.rightPair_ = new MarginsUIPair( 21 // @type {print_preview.MarginsUIPair} The object corresponding to the right
22 // margin.
23 marginsUI.rightPair_ = new print_preview.MarginsUIPair(
48 print_preview.MarginSettings.RIGHT_GROUP); 24 print_preview.MarginSettings.RIGHT_GROUP);
49 marginsUI.bottomPair_ = new MarginsUIPair( 25 // @type {print_preview.MarginsUIPair} The object corresponding to the
26 // bottom margin.
27 marginsUI.bottomPair_ = new print_preview.MarginsUIPair(
50 print_preview.MarginSettings.BOTTOM_GROUP); 28 print_preview.MarginSettings.BOTTOM_GROUP);
51 parentNode.appendChild(marginsUI);
52 29
53 var uiPairs = marginsUI.pairsAsList; 30 var uiPairs = marginsUI.pairsAsList;
54 for (var i = 0; i < uiPairs.length; i++) { 31 for (var i = 0; i < uiPairs.length; i++)
55 marginsUI.appendChild(uiPairs[i].line_); 32 marginsUI.appendChild(uiPairs[i]);
56 marginsUI.appendChild(uiPairs[i].box_); 33
57 } 34 // @type {print_preview.MarginsUIPair} The object that is being dragged.
35 marginsUI.lastClickedMarginsUIPair = null;
36
37 marginsUI.addEventListeners();
58 return marginsUI; 38 return marginsUI;
59 } 39 }
60 40
41 /**
42 * @param {print_preview.Point} point The point with respect to the top left
43 * corner of the webpage.
44 * @return {print_preview.Point} The point with respect to the top left corner
45 * of the plugin area.
46 */
47 MarginsUI.convert = function(point) {
48 var mainview = $('mainview');
49 return new print_preview.Point(point.x - mainview.offsetLeft,
50 point.y - mainview.offsetTop);
51 }
52
61 MarginsUI.prototype = { 53 MarginsUI.prototype = {
62 __proto__: HTMLDivElement.prototype, 54 __proto__: HTMLDivElement.prototype,
63 55
64 /** 56 /**
65 * Adds an observer for |MarginsMayHaveChanged| event. 57 * Adds an observer for |MarginsMayHaveChanged| event.
66 * @param {function} func A callback function to be called when 58 * @param {function} func A callback function to be called when
67 * |MarginsMayHaveChanged| event occurs. 59 * |MarginsMayHaveChanged| event occurs.
68 */ 60 */
69 addObserver: function(func) { 61 addObserver: function(func) {
70 var uiPairs = this.pairsAsList; 62 var uiPairs = this.pairsAsList;
71 for (var i = 0; i < uiPairs.length; i++) 63 for (var i = 0; i < uiPairs.length; i++)
72 uiPairs[i].box_.addEventListener('MarginsMayHaveChanged', func); 64 uiPairs[i].box_.addEventListener('MarginsMayHaveChanged', func);
73 }, 65 },
74 66
75 /** 67 /**
76 * @return {array} An array including all |MarginUIPair| objects. 68 * @return {array} An array including all |MarginUIPair| objects.
77 */ 69 */
78 get pairsAsList() { 70 get pairsAsList() {
79 return [this.topPair_, this.leftPair_, this.rightPair_, this.bottomPair_]; 71 return [this.topPair_, this.leftPair_, this.rightPair_, this.bottomPair_];
80 }, 72 },
81 73
82 /** 74 /**
83 * Updates the state of the margins UI. 75 * Updates the state of the margins UI.
84 * @param {print_preview.Rect} 76 * @param {print_preview.Rect}
85 * @param {Margins} marginValues 77 * @param {Margins} marginValues
86 * @param {array} valueLimits 78 * @param {array} valueLimits
87 */ 79 */
88 update: function(marginsRectangle, marginValues, valueLimits, 80 update: function(marginsRectangle, marginValues, valueLimits,
89 keepDisplayedValue) { 81 keepDisplayedValue, valueLimitsInPercent) {
90 var uiPairs = this.pairsAsList; 82 var uiPairs = this.pairsAsList;
91 var order = ['top', 'left', 'right', 'bottom']; 83 var order = ['top', 'left', 'right', 'bottom'];
92 for (var i = 0; i < uiPairs.length; i++) { 84 for (var i = 0; i < uiPairs.length; i++) {
93 uiPairs[i].update(marginsRectangle, 85 uiPairs[i].update(marginsRectangle,
94 marginValues[order[i]], 86 marginValues[order[i]],
95 valueLimits[i], 87 valueLimits[i],
96 keepDisplayedValue); 88 keepDisplayedValue,
89 valueLimitsInPercent[i]);
97 } 90 }
98 }, 91 },
99 92
100 /** 93 /**
101 * Draws |this| based on the latest state. 94 * Draws |this| based on the latest state.
102 */ 95 */
103 draw: function() { 96 draw: function() {
104 this.applyClippingMask_(); 97 this.applyClippingMask_();
105 this.pairsAsList.forEach(function(pair) { pair.draw(); }); 98 this.pairsAsList.forEach(function(pair) { pair.draw(); });
106 }, 99 },
(...skipping 13 matching lines...) Expand all
120 }, 113 },
121 114
122 /** 115 /**
123 * Applies a clipping mask on |this| so that it does not paint on top of the 116 * Applies a clipping mask on |this| so that it does not paint on top of the
124 * scrollbars (if any). 117 * scrollbars (if any).
125 */ 118 */
126 applyClippingMask_: function() { 119 applyClippingMask_: function() {
127 var bottom = previewArea.height; 120 var bottom = previewArea.height;
128 var right = previewArea.width; 121 var right = previewArea.width;
129 this.style.clip = "rect(0, " + right + "px, " + bottom + "px, 0)"; 122 this.style.clip = "rect(0, " + right + "px, " + bottom + "px, 0)";
130 } 123 },
124
125 /**
126 * Adds event listeners for various events.
127 */
128 addEventListeners: function() {
129 var uiPairs = this.pairsAsList;
130 for (var i = 0; i < uiPairs.length; i++) {
131 uiPairs[i].addEventListener('MarginsLineMouseDown',
132 this.onMarginLineMouseDown.bind(this));
133 uiPairs[i].addEventListener('MarginsLineMouseUp',
134 this.onMarginLineMouseUp.bind(this));
Evan Stade 2011/10/14 19:04:49 I don't think this is necessary. You are double-ha
dpapad 2011/10/14 20:57:58 Removed it since it is not necessary. It was not b
135 }
136 },
137
138 /**
139 * Executes when a "MarginLineMouseDown" event occurs.
140 * @param {cr.Event} e The event that triggered this listener.
141 */
142 onMarginLineMouseDown: function(e) {
143 this.lastClickedMarginsUIPair = e.target;
144 this.bringToFront(this.lastClickedMarginsUIPair);
145 // Note: Capturing mouse events at a higher level in the DOM than |this|,
146 // so that the plugin can still receive mouse events.
147 window.document.onmousemove = this.onMouseMove_.bind(this);
Evan Stade 2011/10/14 19:04:49 use addEventListener
dpapad 2011/10/14 20:57:58 Done.
148 // After snapping to min/max the MarginUIPair might not receive the
149 // mouseup event since it is not under the mouse pointer, so it is handled
150 // here.
151 window.document.onmouseup = this.onMarginLineMouseUp.bind(this);
152 },
153
154 /**
155 * Executes when a "MarginLineMouseUp" event occurs.
156 * @param {cr.Event} e The event that triggered this listener.
157 */
158 onMarginLineMouseUp: function(e) {
159 this.lastClickedMarginsUIPair.box_.onBlur_();
Evan Stade 2011/10/14 19:04:49 don't do this -- it's a hack. Make a helper functi
dpapad 2011/10/14 20:57:58 Working on removing this hack. I had in mind to do
160 this.lastClickedMarginsUIPair = null;
161 window.document.onmousemove = null;
Evan Stade 2011/10/14 19:04:49 instead of attaching and detaching, always leave t
dpapad 2011/10/14 20:57:58 I changed this for onmouseup only. Having the onmo
Evan Stade 2011/10/14 21:58:36 this will not measurably impact performance
162 window.document.onmouseup = null;
163 },
164
165 /**
166 * Brings |uiPair| in front of the other pairs.
167 * @param {print_preview.MarginsUIPair} uiPair The pair to bring in front.
168 */
169 bringToFront: function(uiPair) {
170 this.pairsAsList.forEach(function(pair) { pair.style.zIndex = 0; });
171 uiPair.style.zIndex = 10;
172 },
173
174 /**
175 * Executes when a mousemove event occurs.
176 * @param {MouseEvent} e The event that triggered this listener.
177 */
178 onMouseMove_: function(e) {
179 var point = MarginsUI.convert(new print_preview.Point(e.x, e.y));
180
181 var dragEvent = new cr.Event('DragEvent');
182 dragEvent.dragDelta =
183 this.lastClickedMarginsUIPair.getDragDistanceFrom(point);
184 dragEvent.destinationPoint = point;
185 this.dispatchEvent(dragEvent);
186 },
131 }; 187 };
132 188
133 return { 189 return {
134 MarginsUI: MarginsUI 190 MarginsUI: MarginsUI
135 }; 191 };
136 }); 192 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698