| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // require: event_tracker.js | 5 // require: event_tracker.js |
| 6 | 6 |
| 7 cr.define('cr.ui', function() { | 7 cr.define('cr.ui', function() { |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * The arrow location specifies how the arrow and bubble are positioned in | 10 * The arrow location specifies how the arrow and bubble are positioned in |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 var BubbleBase = cr.ui.define('div'); | 38 var BubbleBase = cr.ui.define('div'); |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * The horizontal distance between the tip of the arrow and the reference edge | 41 * The horizontal distance between the tip of the arrow and the reference edge |
| 42 * of the bubble (as specified by the arrow location). | 42 * of the bubble (as specified by the arrow location). |
| 43 * @const | 43 * @const |
| 44 */ | 44 */ |
| 45 BubbleBase.ARROW_OFFSET = 30; | 45 BubbleBase.ARROW_OFFSET = 30; |
| 46 | 46 |
| 47 BubbleBase.prototype = { | 47 BubbleBase.prototype = { |
| 48 // Set up the prototype chain | 48 // Set up the prototype chain. |
| 49 __proto__: HTMLDivElement.prototype, | 49 __proto__: HTMLDivElement.prototype, |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Initialization function for the cr.ui framework. | 52 * Initialization function for the cr.ui framework. |
| 53 */ | 53 */ |
| 54 decorate: function() { | 54 decorate: function() { |
| 55 this.className = 'bubble'; | 55 this.className = 'bubble'; |
| 56 this.innerHTML = | 56 this.innerHTML = |
| 57 '<div class="bubble-content"></div>' + | 57 '<div class="bubble-content"></div>' + |
| 58 '<div class="bubble-shadow"></div>' + | 58 '<div class="bubble-shadow"></div>' + |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 location == ArrowLocation.TOP_END; | 103 location == ArrowLocation.TOP_END; |
| 104 }, | 104 }, |
| 105 | 105 |
| 106 /** | 106 /** |
| 107 * Show the bubble. | 107 * Show the bubble. |
| 108 */ | 108 */ |
| 109 show: function() { | 109 show: function() { |
| 110 if (!this.hidden) | 110 if (!this.hidden) |
| 111 return; | 111 return; |
| 112 | 112 |
| 113 document.body.appendChild(this); | 113 this.attachToDOM_(); |
| 114 this.hidden = false; | 114 this.hidden = false; |
| 115 this.anchorNode_.isShowingBubble = true; |
| 115 | 116 |
| 116 var doc = this.ownerDocument; | 117 var doc = this.ownerDocument; |
| 117 this.eventTracker_ = new EventTracker; | 118 this.eventTracker_ = new EventTracker; |
| 118 this.eventTracker_.add(doc, 'keydown', this, true); | 119 this.eventTracker_.add(doc, 'keydown', this, true); |
| 119 this.eventTracker_.add(doc, 'mousedown', this, true); | 120 this.eventTracker_.add(doc, 'mousedown', this, true); |
| 120 }, | 121 }, |
| 121 | 122 |
| 122 /** | 123 /** |
| 123 * Hide the bubble. | 124 * Hide the bubble. |
| 124 */ | 125 */ |
| 125 hide: function() { | 126 hide: function() { |
| 126 if (this.hidden) | 127 if (this.hidden) |
| 127 return; | 128 return; |
| 128 | 129 |
| 130 this.eventTracker_.removeAll(); |
| 131 this.anchorNode_.isShowingBubble = false; |
| 129 this.hidden = true; | 132 this.hidden = true; |
| 130 this.eventTracker_.removeAll(); | |
| 131 this.parentNode.removeChild(this); | 133 this.parentNode.removeChild(this); |
| 132 }, | 134 }, |
| 133 | 135 |
| 134 /** | 136 /** |
| 135 * Handle keyboard events, dismissing the bubble if necessary. | 137 * Handle keyboard events, dismissing the bubble if necessary. |
| 136 * @param {Event} event The event. | 138 * @param {Event} event The event. |
| 137 */ | 139 */ |
| 138 handleEvent: function(event) { | 140 handleEvent: function(event) { |
| 139 // Close the bubble when the user presses <Esc>. | 141 // Close the bubble when the user presses <Esc>. |
| 140 if (event.type == 'keydown' && event.keyCode == 27) { | 142 if (event.type == 'keydown' && event.keyCode == 27) { |
| 141 this.hide(); | 143 this.hide(); |
| 142 event.preventDefault(); | 144 event.preventDefault(); |
| 143 event.stopPropagation(); | 145 event.stopPropagation(); |
| 144 } | 146 } |
| 145 }, | 147 }, |
| 146 | 148 |
| 147 /** | 149 /** |
| 150 * Attach the bubble to the document's DOM. |
| 151 * @private |
| 152 */ |
| 153 attachToDOM_: function() { |
| 154 document.body.appendChild(this); |
| 155 }, |
| 156 |
| 157 /** |
| 148 * Update the arrow so that it appears at the correct position. | 158 * Update the arrow so that it appears at the correct position. |
| 149 * @param {Boolean} visible Whether the arrow should be visible. | 159 * @param {Boolean} visible Whether the arrow should be visible. |
| 150 * @param {number} The horizontal distance between the tip of the arrow and | 160 * @param {Boolean} atTop Whether the arrow should be at the top of the |
| 151 * the reference edge of the bubble (as specified by the arrow location). | 161 * bubble. |
| 162 * @param {number} tipOffset The horizontal distance between the tip of the |
| 163 * arrow and the reference edge of the bubble (as specified by the arrow |
| 164 * location). |
| 152 * @private | 165 * @private |
| 153 */ | 166 */ |
| 154 updateArrowPosition_: function(visible, tipOffset) { | 167 updateArrowPosition_: function(visible, atTop, tipOffset) { |
| 155 var bubbleArrow = this.querySelector('.bubble-arrow'); | 168 var bubbleArrow = this.querySelector('.bubble-arrow'); |
| 156 | 169 bubbleArrow.hidden = !visible; |
| 157 if (visible) { | 170 if (!visible) |
| 158 bubbleArrow.style.display = 'block'; | |
| 159 } else { | |
| 160 bubbleArrow.style.display = 'none'; | |
| 161 return; | 171 return; |
| 162 } | |
| 163 | 172 |
| 164 var edgeOffset = (-bubbleArrow.clientHeight / 2) + 'px'; | 173 var edgeOffset = (-bubbleArrow.clientHeight / 2) + 'px'; |
| 165 bubbleArrow.style.top = this.arrowAtTop_ ? edgeOffset : 'auto'; | 174 bubbleArrow.style.top = atTop ? edgeOffset : 'auto'; |
| 166 bubbleArrow.style.bottom = this.arrowAtTop_ ? 'auto' : edgeOffset; | 175 bubbleArrow.style.bottom = atTop ? 'auto' : edgeOffset; |
| 167 | 176 |
| 168 edgeOffset = (tipOffset - bubbleArrow.clientHeight / 2) + 'px'; | 177 edgeOffset = (tipOffset - bubbleArrow.offsetWidth / 2) + 'px'; |
| 169 bubbleArrow.style.left = this.arrowAtRight_ ? 'auto' : edgeOffset; | 178 bubbleArrow.style.left = this.arrowAtRight_ ? 'auto' : edgeOffset; |
| 170 bubbleArrow.style.right = this.arrowAtRight_ ? edgeOffset : 'auto'; | 179 bubbleArrow.style.right = this.arrowAtRight_ ? edgeOffset : 'auto'; |
| 171 }, | 180 }, |
| 172 }; | 181 }; |
| 173 | 182 |
| 174 /** | 183 /** |
| 175 * The bubble alignment specifies the horizontal position of the bubble in | 184 * The bubble alignment specifies the horizontal position of the bubble in |
| 176 * relation to the anchor node. | 185 * relation to the anchor node. |
| 177 * @enum | 186 * @enum |
| 178 */ | 187 */ |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 left = this.arrowAtRight_ ? | 284 left = this.arrowAtRight_ ? |
| 276 anchorMid - this.clientWidth + BubbleBase.ARROW_OFFSET : | 285 anchorMid - this.clientWidth + BubbleBase.ARROW_OFFSET : |
| 277 anchorMid - BubbleBase.ARROW_OFFSET; | 286 anchorMid - BubbleBase.ARROW_OFFSET; |
| 278 } | 287 } |
| 279 var top = this.arrowAtTop_ ? clientRect.bottom + arrowOffsetY : | 288 var top = this.arrowAtTop_ ? clientRect.bottom + arrowOffsetY : |
| 280 clientRect.top - this.clientHeight - arrowOffsetY; | 289 clientRect.top - this.clientHeight - arrowOffsetY; |
| 281 | 290 |
| 282 this.style.left = left + 'px'; | 291 this.style.left = left + 'px'; |
| 283 this.style.top = top + 'px'; | 292 this.style.top = top + 'px'; |
| 284 | 293 |
| 285 this.updateArrowPosition_(true, BubbleBase.ARROW_OFFSET); | 294 this.updateArrowPosition_(true, this.arrowAtTop_, |
| 295 BubbleBase.ARROW_OFFSET); |
| 286 }, | 296 }, |
| 287 | 297 |
| 288 /** | 298 /** |
| 289 * Show the bubble. | 299 * Show the bubble. |
| 290 */ | 300 */ |
| 291 show: function() { | 301 show: function() { |
| 292 if (!this.hidden) | 302 if (!this.hidden) |
| 293 return; | 303 return; |
| 294 | 304 |
| 295 BubbleBase.prototype.show.call(this); | 305 BubbleBase.prototype.show.call(this); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 320 }, | 330 }, |
| 321 }; | 331 }; |
| 322 | 332 |
| 323 return { | 333 return { |
| 324 ArrowLocation: ArrowLocation, | 334 ArrowLocation: ArrowLocation, |
| 325 BubbleBase: BubbleBase, | 335 BubbleBase: BubbleBase, |
| 326 BubbleAlignment: BubbleAlignment, | 336 BubbleAlignment: BubbleAlignment, |
| 327 Bubble: Bubble | 337 Bubble: Bubble |
| 328 }; | 338 }; |
| 329 }); | 339 }); |
| OLD | NEW |