| OLD | NEW |
| (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 function MarginsUI() { | |
| 9 var marginsUI = document.createElement('div'); | |
| 10 marginsUI.__proto__ = MarginsUI.prototype; | |
| 11 marginsUI.id = 'customized-margins'; | |
| 12 | |
| 13 // @type {print_preview.MarginsUIPair} The object corresponding to the top | |
| 14 // margin. | |
| 15 marginsUI.topPair_ = new print_preview.MarginsUIPair( | |
| 16 print_preview.MarginSettings.TOP_GROUP); | |
| 17 // @type {print_preview.MarginsUIPair} The object corresponding to the left | |
| 18 // margin. | |
| 19 marginsUI.leftPair_ = new print_preview.MarginsUIPair( | |
| 20 print_preview.MarginSettings.LEFT_GROUP); | |
| 21 // @type {print_preview.MarginsUIPair} The object corresponding to the right | |
| 22 // margin. | |
| 23 marginsUI.rightPair_ = new print_preview.MarginsUIPair( | |
| 24 print_preview.MarginSettings.RIGHT_GROUP); | |
| 25 // @type {print_preview.MarginsUIPair} The object corresponding to the | |
| 26 // bottom margin. | |
| 27 marginsUI.bottomPair_ = new print_preview.MarginsUIPair( | |
| 28 print_preview.MarginSettings.BOTTOM_GROUP); | |
| 29 | |
| 30 var uiPairs = marginsUI.pairsAsList; | |
| 31 for (var i = 0; i < uiPairs.length; i++) | |
| 32 marginsUI.appendChild(uiPairs[i]); | |
| 33 | |
| 34 // @type {print_preview.MarginsUIPair} The object that is being dragged. | |
| 35 // null if no drag session is in progress. | |
| 36 marginsUI.lastClickedMarginsUIPair = null; | |
| 37 | |
| 38 // @type {EventTracker} Used to keep track of certain event listeners. | |
| 39 marginsUI.eventTracker_ = new EventTracker(); | |
| 40 | |
| 41 marginsUI.addEventListeners_(); | |
| 42 return marginsUI; | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * @param {{x: number, y: number}} point The point with respect to the top | |
| 47 * left corner of the webpage. | |
| 48 * @return {{x: number, y: number}} The point with respect to the top left | |
| 49 * corner of the plugin area. | |
| 50 */ | |
| 51 MarginsUI.convert = function(point) { | |
| 52 var mainview = $('mainview'); | |
| 53 return { x: point.x - mainview.offsetLeft, | |
| 54 y: point.y - mainview.offsetTop }; | |
| 55 } | |
| 56 | |
| 57 MarginsUI.prototype = { | |
| 58 __proto__: HTMLDivElement.prototype, | |
| 59 | |
| 60 /** | |
| 61 * Adds an observer for |customEvents.MARGINS_MAY_HAVE_CHANGED| event. | |
| 62 * @param {function} func A callback function to be called when | |
| 63 * |customEvents.MARGINS_MAY_HAVE_CHANGED| event occurs. | |
| 64 */ | |
| 65 addObserver: function(func) { | |
| 66 var uiPairs = this.pairsAsList; | |
| 67 for (var i = 0; i < uiPairs.length; i++) { | |
| 68 uiPairs[i].box_.addEventListener( | |
| 69 customEvents.MARGINS_MAY_HAVE_CHANGED, func); | |
| 70 } | |
| 71 }, | |
| 72 | |
| 73 /** | |
| 74 * @return {array} An array including all |MarginUIPair| objects. | |
| 75 */ | |
| 76 get pairsAsList() { | |
| 77 return [this.topPair_, this.leftPair_, this.rightPair_, this.bottomPair_]; | |
| 78 }, | |
| 79 | |
| 80 /** | |
| 81 * Updates the state of the margins UI. | |
| 82 * @param {print_preview.Rect} marginsRectangle A rectangle describing the | |
| 83 * four margins. | |
| 84 * @param {Margins} marginValues The margin values in points. | |
| 85 * @param {Array.<number>} valueLimits The maximum allowed margins for each | |
| 86 * side in points. | |
| 87 * @param {boolean} keepDisplayedValue True if the currently displayed | |
| 88 * margin values should not be updated. | |
| 89 * @param {Array.<number>} valueLimitsInPercent The maximum allowed margins | |
| 90 * for each side in percentages. | |
| 91 */ | |
| 92 update: function(marginsRectangle, marginValues, valueLimits, | |
| 93 keepDisplayedValue, valueLimitsInPercent) { | |
| 94 var uiPairs = this.pairsAsList; | |
| 95 var order = ['top', 'left', 'right', 'bottom']; | |
| 96 for (var i = 0; i < uiPairs.length; i++) { | |
| 97 uiPairs[i].update(marginsRectangle, | |
| 98 marginValues[order[i]], | |
| 99 valueLimits[i], | |
| 100 keepDisplayedValue, | |
| 101 valueLimitsInPercent[i]); | |
| 102 } | |
| 103 }, | |
| 104 | |
| 105 /** | |
| 106 * Draws |this| based on the latest state. | |
| 107 */ | |
| 108 draw: function() { | |
| 109 this.applyClippingMask_(); | |
| 110 this.pairsAsList.forEach(function(pair) { pair.draw(); }); | |
| 111 }, | |
| 112 | |
| 113 /** | |
| 114 * Shows the margins UI. | |
| 115 */ | |
| 116 show: function() { | |
| 117 this.hidden = false; | |
| 118 this.classList.remove('invisible'); | |
| 119 }, | |
| 120 | |
| 121 /** | |
| 122 * Hides the margins UI and removes from the rendering flow if requested. | |
| 123 * @param {boolean} removeFromFlow True if |this| should also be removed | |
| 124 * from the rendering flow (in order to not interfere with the tab | |
| 125 * order). | |
| 126 */ | |
| 127 hide: function(removeFromFlow) { | |
| 128 removeFromFlow ? this.hidden = true : this.classList.add('invisible'); | |
| 129 }, | |
| 130 | |
| 131 /** | |
| 132 * Applies a clipping mask on |this| so that it does not paint on top of the | |
| 133 * scrollbars (if any). | |
| 134 */ | |
| 135 applyClippingMask_: function() { | |
| 136 var bottom = previewArea.height; | |
| 137 var right = previewArea.width; | |
| 138 this.style.clip = 'rect(0, ' + right + 'px, ' + bottom + 'px, 0)'; | |
| 139 }, | |
| 140 | |
| 141 /** | |
| 142 * Adds event listeners for various events. | |
| 143 * @private | |
| 144 */ | |
| 145 addEventListeners_: function() { | |
| 146 var uiPairs = this.pairsAsList; | |
| 147 for (var i = 0; i < uiPairs.length; i++) { | |
| 148 uiPairs[i].addEventListener(customEvents.MARGIN_LINE_MOUSE_DOWN, | |
| 149 this.onMarginLineMouseDown.bind(this)); | |
| 150 } | |
| 151 // After snapping to min/max the MarginUIPair might not receive the | |
| 152 // mouseup event since it is not under the mouse pointer, so it is handled | |
| 153 // here. | |
| 154 window.document.addEventListener('mouseup', | |
| 155 this.onMarginLineMouseUp.bind(this)); | |
| 156 }, | |
| 157 | |
| 158 /** | |
| 159 * Executes when a "MarginLineMouseDown" event occurs. | |
| 160 * @param {cr.Event} e The event that triggered this listener. | |
| 161 */ | |
| 162 onMarginLineMouseDown: function(e) { | |
| 163 this.lastClickedMarginsUIPair = e.target; | |
| 164 this.bringToFront(this.lastClickedMarginsUIPair); | |
| 165 // Note: Capturing mouse events at a higher level in the DOM than |this|, | |
| 166 // so that the plugin can still receive mouse events. | |
| 167 this.eventTracker_.add( | |
| 168 window.document, 'mousemove', this.onMouseMove_.bind(this), false); | |
| 169 }, | |
| 170 | |
| 171 /** | |
| 172 * Executes when a "MarginLineMouseUp" event occurs. | |
| 173 * @param {cr.Event} e The event that triggered this listener. | |
| 174 */ | |
| 175 onMarginLineMouseUp: function(e) { | |
| 176 if (this.lastClickedMarginsUIPair == null) | |
| 177 return; | |
| 178 this.lastClickedMarginsUIPair.onMouseUp(); | |
| 179 this.lastClickedMarginsUIPair = null; | |
| 180 this.eventTracker_.remove(window.document, 'mousemove'); | |
| 181 }, | |
| 182 | |
| 183 /** | |
| 184 * Brings |uiPair| in front of the other pairs. Used to make sure that the | |
| 185 * dragged pair is visible when overlapping with a not dragged pair. | |
| 186 * @param {print_preview.MarginsUIPair} uiPair The pair to bring in front. | |
| 187 */ | |
| 188 bringToFront: function(uiPair) { | |
| 189 this.pairsAsList.forEach(function(pair) { | |
| 190 pair.classList.remove('dragging'); | |
| 191 }); | |
| 192 uiPair.classList.add('dragging'); | |
| 193 }, | |
| 194 | |
| 195 /** | |
| 196 * Executes when a mousemove event occurs. | |
| 197 * @param {MouseEvent} e The event that triggered this listener. | |
| 198 */ | |
| 199 onMouseMove_: function(e) { | |
| 200 var point = MarginsUI.convert({ x: e.x, y: e.y }); | |
| 201 | |
| 202 var dragEvent = new cr.Event(customEvents.MARGIN_LINE_DRAG); | |
| 203 dragEvent.dragDelta = | |
| 204 this.lastClickedMarginsUIPair.getDragDisplacementFrom(point); | |
| 205 dragEvent.destinationPoint = point; | |
| 206 this.dispatchEvent(dragEvent); | |
| 207 } | |
| 208 }; | |
| 209 | |
| 210 return { | |
| 211 MarginsUI: MarginsUI | |
| 212 }; | |
| 213 }); | |
| OLD | NEW |