John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 1 | // Copyright 2020 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 | import './strings.m.js'; |
| 6 | |
| 7 | import {assert} from 'chrome://resources/js/assert.m.js'; |
| 8 | import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js'; |
| 9 | |
| 10 | import {isTabElement, TabElement} from './tab.js'; |
| 11 | import {isTabGroupElement, TabGroupElement} from './tab_group.js'; |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 12 | import {TabData, TabNetworkState, TabsApiProxy} from './tabs_api_proxy.js'; |
| 13 | |
| 14 | /** @const {number} */ |
| 15 | export const PLACEHOLDER_TAB_ID = -1; |
| 16 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 17 | /** @const {string} */ |
| 18 | export const PLACEHOLDER_GROUP_ID = 'placeholder'; |
| 19 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 20 | /** |
| 21 | * The data type key for pinned state of a tab. Since drag events only expose |
| 22 | * whether or not a data type exists (not the actual value), presence of this |
| 23 | * data type means that the tab is pinned. |
| 24 | * @const {string} |
| 25 | */ |
| 26 | const PINNED_DATA_TYPE = 'pinned'; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Gets the data type of tab IDs on DataTransfer objects in drag events. This |
| 30 | * is a function so that loadTimeData can get overridden by tests. |
| 31 | * @return {string} |
| 32 | */ |
| 33 | function getTabIdDataType() { |
| 34 | return loadTimeData.getString('tabIdDataType'); |
| 35 | } |
| 36 | |
| 37 | /** @return {string} */ |
| 38 | function getGroupIdDataType() { |
| 39 | return loadTimeData.getString('tabGroupIdDataType'); |
| 40 | } |
| 41 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 42 | /** @return {!TabData} */ |
| 43 | function getDefaultTabData() { |
| 44 | return { |
| 45 | active: false, |
| 46 | alertStates: [], |
| 47 | blocked: false, |
| 48 | crashed: false, |
| 49 | id: -1, |
| 50 | index: -1, |
| 51 | isDefaultFavicon: false, |
| 52 | networkState: TabNetworkState.NONE, |
| 53 | pinned: false, |
| 54 | shouldHideThrobber: false, |
| 55 | showIcon: true, |
| 56 | title: '', |
| 57 | url: '', |
| 58 | }; |
| 59 | } |
| 60 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 61 | /** |
| 62 | * @interface |
| 63 | */ |
| 64 | export class DragManagerDelegate { |
| 65 | /** |
| 66 | * @param {!TabElement} tabElement |
| 67 | * @return {number} |
| 68 | */ |
| 69 | getIndexOfTab(tabElement) {} |
| 70 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 71 | /** |
| 72 | * @param {!TabElement} element |
| 73 | * @param {number} index |
| 74 | * @param {boolean} pinned |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 75 | * @param {string=} groupId |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 76 | */ |
| 77 | placeTabElement(element, index, pinned, groupId) {} |
| 78 | |
| 79 | /** |
| 80 | * @param {!TabGroupElement} element |
| 81 | * @param {number} index |
| 82 | */ |
| 83 | placeTabGroupElement(element, index) {} |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** @typedef {!DragManagerDelegate|!HTMLElement} */ |
| 87 | let DragManagerDelegateElement; |
| 88 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 89 | class DragSession { |
| 90 | /** |
| 91 | * @param {!DragManagerDelegateElement} delegate |
| 92 | * @param {!TabElement|!TabGroupElement} element |
| 93 | * @param {number} srcIndex |
| 94 | * @param {string=} srcGroup |
| 95 | */ |
| 96 | constructor(delegate, element, srcIndex, srcGroup) { |
| 97 | /** @const @private {!DragManagerDelegateElement} */ |
| 98 | this.delegate_ = delegate; |
| 99 | |
| 100 | /** @const {!TabElement|!TabGroupElement} */ |
| 101 | this.element_ = element; |
| 102 | |
| 103 | /** @const {number} */ |
| 104 | this.srcIndex = srcIndex; |
| 105 | |
| 106 | /** @const {string|undefined} */ |
| 107 | this.srcGroup = srcGroup; |
| 108 | |
| 109 | /** @private @const {!TabsApiProxy} */ |
| 110 | this.tabsProxy_ = TabsApiProxy.getInstance(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param {!DragManagerDelegateElement} delegate |
| 115 | * @param {!TabElement|!TabGroupElement} element |
| 116 | * @return {!DragSession} |
| 117 | */ |
| 118 | static createFromElement(delegate, element) { |
| 119 | if (isTabGroupElement(element)) { |
| 120 | return new DragSession( |
| 121 | delegate, element, |
| 122 | delegate.getIndexOfTab( |
| 123 | /** @type {!TabElement} */ (element.firstElementChild))); |
| 124 | } |
| 125 | |
| 126 | const srcIndex = delegate.getIndexOfTab( |
| 127 | /** @type {!TabElement} */ (element)); |
| 128 | const srcGroup = |
| 129 | (element.parentElement && isTabGroupElement(element.parentElement)) ? |
| 130 | element.parentElement.dataset.groupId : |
| 131 | undefined; |
| 132 | return new DragSession(delegate, element, srcIndex, srcGroup); |
| 133 | } |
| 134 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 135 | /** |
| 136 | * @param {!DragManagerDelegateElement} delegate |
| 137 | * @param {!DragEvent} event |
| 138 | * @return {?DragSession} |
| 139 | */ |
| 140 | static createFromEvent(delegate, event) { |
| 141 | if (event.dataTransfer.types.includes(getTabIdDataType())) { |
| 142 | const isPinned = event.dataTransfer.types.includes('pinned'); |
| 143 | const placeholderTabElement = |
| 144 | /** @type {!TabElement} */ (document.createElement('tabstrip-tab')); |
| 145 | placeholderTabElement.tab = /** @type {!TabData} */ (Object.assign( |
| 146 | getDefaultTabData(), {id: PLACEHOLDER_TAB_ID, pinned: isPinned})); |
| 147 | placeholderTabElement.setDragging(true); |
| 148 | delegate.placeTabElement(placeholderTabElement, -1, isPinned); |
| 149 | return DragSession.createFromElement(delegate, placeholderTabElement); |
| 150 | } |
| 151 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 152 | if (event.dataTransfer.types.includes(getGroupIdDataType())) { |
| 153 | const placeholderGroupElement = /** @type {!TabGroupElement} */ |
| 154 | (document.createElement('tabstrip-tab-group')); |
| 155 | placeholderGroupElement.dataset.groupId = PLACEHOLDER_GROUP_ID; |
| 156 | placeholderGroupElement.setDragging(true); |
| 157 | delegate.placeTabGroupElement(placeholderGroupElement, -1); |
| 158 | return DragSession.createFromElement(delegate, placeholderGroupElement); |
| 159 | } |
| 160 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 161 | return null; |
| 162 | } |
| 163 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 164 | /** @return {string|undefined} */ |
| 165 | get dstGroup() { |
| 166 | if (isTabElement(this.element_) && this.element_.parentElement && |
| 167 | isTabGroupElement(this.element_.parentElement)) { |
| 168 | return this.element_.parentElement.dataset.groupId; |
| 169 | } |
| 170 | |
| 171 | return undefined; |
| 172 | } |
| 173 | |
| 174 | /** @return {number} */ |
| 175 | get dstIndex() { |
| 176 | if (isTabElement(this.element_)) { |
| 177 | return this.delegate_.getIndexOfTab( |
| 178 | /** @type {!TabElement} */ (this.element_)); |
| 179 | } |
| 180 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 181 | if (this.element_.children.length === 0) { |
| 182 | // If this group element has no children, it was a placeholder element |
| 183 | // being dragged. Find out the destination index by finding the index of |
| 184 | // the tab closest to it and incrementing it by 1. |
| 185 | const previousElement = this.element_.previousElementSibling; |
| 186 | if (!previousElement) { |
| 187 | return 0; |
| 188 | } |
| 189 | if (isTabElement(previousElement)) { |
| 190 | return this.delegate_.getIndexOfTab( |
| 191 | /** @private {!TabElement} */ (previousElement)) + |
| 192 | 1; |
| 193 | } |
| 194 | |
| 195 | assert(isTabGroupElement(previousElement)); |
| 196 | return this.delegate_.getIndexOfTab(/** @private {!TabElement} */ ( |
| 197 | previousElement.lastElementChild)) + |
| 198 | 1; |
| 199 | } |
| 200 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 201 | // If a tab group is moving backwards (to the front of the tab strip), the |
| 202 | // new index is the index of the first tab in that group. If a tab group is |
| 203 | // moving forwards (to the end of the tab strip), the new index is the index |
| 204 | // of the last tab in that group. |
| 205 | let dstIndex = this.delegate_.getIndexOfTab( |
| 206 | /** @type {!TabElement} */ (this.element_.firstElementChild)); |
| 207 | if (this.srcIndex <= dstIndex) { |
| 208 | dstIndex += this.element_.childElementCount - 1; |
| 209 | } |
| 210 | return dstIndex; |
| 211 | } |
| 212 | |
| 213 | cancel() { |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 214 | if (this.isDraggingPlaceholder()) { |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 215 | this.element_.remove(); |
| 216 | return; |
| 217 | } |
| 218 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 219 | if (isTabGroupElement(this.element_)) { |
| 220 | this.delegate_.placeTabGroupElement( |
| 221 | /** @type {!TabGroupElement} */ (this.element_), this.srcIndex); |
| 222 | } else if (isTabElement(this.element_)) { |
| 223 | this.delegate_.placeTabElement( |
| 224 | /** @type {!TabElement} */ (this.element_), this.srcIndex, |
| 225 | this.element_.tab.pinned, this.srcGroup); |
| 226 | } |
| 227 | |
| 228 | this.element_.setDragging(false); |
| 229 | } |
| 230 | |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 231 | /** @return {boolean} */ |
| 232 | isDraggingPlaceholder() { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 233 | return this.isDraggingPlaceholderTab_() || |
| 234 | this.isDraggingPlaceholderGroup_(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @return {boolean} |
| 239 | * @private |
| 240 | */ |
| 241 | isDraggingPlaceholderTab_() { |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 242 | return isTabElement(this.element_) && |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 243 | this.element_.tab.id === PLACEHOLDER_TAB_ID; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @return {boolean} |
| 248 | * @private |
| 249 | */ |
| 250 | isDraggingPlaceholderGroup_() { |
| 251 | return isTabGroupElement(this.element_) && |
| 252 | this.element_.dataset.groupId === PLACEHOLDER_GROUP_ID; |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 253 | } |
| 254 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 255 | /** @param {!DragEvent} event */ |
| 256 | finish(event) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 257 | if (this.isDraggingPlaceholderTab_()) { |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 258 | const id = Number(event.dataTransfer.getData(getTabIdDataType())); |
| 259 | this.element_.tab = Object.assign({}, this.element_.tab, {id}); |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 260 | } else if (this.isDraggingPlaceholderGroup_()) { |
| 261 | this.element_.dataset.groupId = |
| 262 | event.dataTransfer.getData(getGroupIdDataType()); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | const dstIndex = this.dstIndex; |
| 266 | if (isTabElement(this.element_)) { |
| 267 | this.tabsProxy_.moveTab(this.element_.tab.id, dstIndex); |
| 268 | } else if (isTabGroupElement(this.element_)) { |
| 269 | this.tabsProxy_.moveGroup(this.element_.dataset.groupId, dstIndex); |
| 270 | } |
| 271 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 272 | const dstGroup = this.dstGroup; |
| 273 | if (dstGroup && dstGroup !== this.srcGroup) { |
| 274 | this.tabsProxy_.groupTab(this.element_.tab.id, dstGroup); |
| 275 | } else if (!dstGroup && this.srcGroup) { |
| 276 | this.tabsProxy_.ungroupTab(this.element_.tab.id); |
| 277 | } |
| 278 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 279 | this.element_.setDragging(false); |
| 280 | } |
| 281 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 282 | /** |
| 283 | * @param {!TabElement|!TabGroupElement} dragOverElement |
| 284 | * @return {boolean} |
| 285 | */ |
| 286 | shouldOffsetIndexForGroup_(dragOverElement) { |
| 287 | // Since TabGroupElements do not have any TabElements, they need to offset |
| 288 | // the index for any elements that come after it as if there is at least |
| 289 | // one element inside of it. |
| 290 | return this.isDraggingPlaceholder() && |
| 291 | !!(dragOverElement.compareDocumentPosition(this.element_) & |
| 292 | Node.DOCUMENT_POSITION_PRECEDING); |
| 293 | } |
| 294 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 295 | /** @param {!DragEvent} event */ |
| 296 | start(event) { |
| 297 | event.dataTransfer.effectAllowed = 'move'; |
| 298 | const draggedItemRect = this.element_.getBoundingClientRect(); |
| 299 | this.element_.setDragging(true); |
John Lee | 9f2ee9685 | 2020-03-12 02:17:28 | [diff] [blame^] | 300 | |
| 301 | const dragImage = this.element_.getDragImage(); |
| 302 | const dragImageRect = dragImage.getBoundingClientRect(); |
| 303 | |
| 304 | let scaleFactor = 1; |
| 305 | let verticalOffset = 0; |
| 306 | |
| 307 | // <if expr="chromeos"> |
| 308 | // Touch on ChromeOS automatically scales drag images by 1.2 and adds a |
| 309 | // vertical offset of 25px. See //ash/drag_drop/drag_drop_controller.cc. |
| 310 | scaleFactor = 1.2; |
| 311 | verticalOffset = 25; |
| 312 | // </if> |
| 313 | |
| 314 | const xDiffFromCenter = |
| 315 | event.clientX - draggedItemRect.left - (draggedItemRect.width / 2); |
| 316 | const yDiffFromCenter = event.clientY - draggedItemRect.top - |
| 317 | verticalOffset - (draggedItemRect.height / 2); |
| 318 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 319 | event.dataTransfer.setDragImage( |
John Lee | 9f2ee9685 | 2020-03-12 02:17:28 | [diff] [blame^] | 320 | dragImage, (dragImageRect.width / 2 + xDiffFromCenter / scaleFactor), |
| 321 | (dragImageRect.height / 2 + yDiffFromCenter / scaleFactor)); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 322 | |
| 323 | if (isTabElement(this.element_)) { |
| 324 | event.dataTransfer.setData( |
| 325 | getTabIdDataType(), this.element_.tab.id.toString()); |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 326 | |
| 327 | if (this.element_.tab.pinned) { |
| 328 | event.dataTransfer.setData( |
| 329 | 'pinned', this.element_.tab.pinned.toString()); |
| 330 | } |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 331 | } else if (isTabGroupElement(this.element_)) { |
| 332 | event.dataTransfer.setData( |
| 333 | getGroupIdDataType(), this.element_.dataset.groupId); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** @param {!DragEvent} event */ |
| 338 | update(event) { |
| 339 | event.dataTransfer.dropEffect = 'move'; |
| 340 | if (isTabGroupElement(this.element_)) { |
| 341 | this.updateForTabGroupElement_(event); |
| 342 | } else if (isTabElement(this.element_)) { |
| 343 | this.updateForTabElement_(event); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @param {!DragEvent} event |
| 349 | * @private |
| 350 | */ |
| 351 | updateForTabGroupElement_(event) { |
| 352 | const tabGroupElement = |
| 353 | /** @type {!TabGroupElement} */ (this.element_); |
| 354 | const composedPath = /** @type {!Array<!Element>} */ (event.composedPath()); |
| 355 | if (composedPath.includes(assert(this.element_))) { |
| 356 | // Dragging over itself or a child of itself. |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | const dragOverTabElement = |
| 361 | /** @type {!TabElement|undefined} */ (composedPath.find(isTabElement)); |
| 362 | if (dragOverTabElement && !dragOverTabElement.tab.pinned) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 363 | let dragOverIndex = this.delegate_.getIndexOfTab(dragOverTabElement); |
| 364 | dragOverIndex += |
| 365 | this.shouldOffsetIndexForGroup_(dragOverTabElement) ? 1 : 0; |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 366 | this.delegate_.placeTabGroupElement(tabGroupElement, dragOverIndex); |
| 367 | return; |
| 368 | } |
| 369 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 370 | const dragOverGroupElement = /** @type {!TabGroupElement|undefined} */ ( |
| 371 | composedPath.find(isTabGroupElement)); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 372 | if (dragOverGroupElement) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 373 | let dragOverIndex = this.delegate_.getIndexOfTab( |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 374 | /** @type {!TabElement} */ (dragOverGroupElement.firstElementChild)); |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 375 | dragOverIndex += |
| 376 | this.shouldOffsetIndexForGroup_(dragOverGroupElement) ? 1 : 0; |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 377 | this.delegate_.placeTabGroupElement(tabGroupElement, dragOverIndex); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @param {!DragEvent} event |
| 383 | * @private |
| 384 | */ |
| 385 | updateForTabElement_(event) { |
| 386 | const tabElement = /** @type {!TabElement} */ (this.element_); |
| 387 | const composedPath = /** @type {!Array<!Element>} */ (event.composedPath()); |
| 388 | const dragOverTabElement = |
| 389 | /** @type {?TabElement} */ (composedPath.find(isTabElement)); |
| 390 | if (dragOverTabElement && |
| 391 | dragOverTabElement.tab.pinned !== tabElement.tab.pinned) { |
| 392 | // Can only drag between the same pinned states. |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | const previousGroupId = (tabElement.parentElement && |
| 397 | isTabGroupElement(tabElement.parentElement)) ? |
| 398 | tabElement.parentElement.dataset.groupId : |
| 399 | undefined; |
| 400 | |
| 401 | const dragOverTabGroup = |
| 402 | /** @type {?TabGroupElement} */ (composedPath.find(isTabGroupElement)); |
| 403 | if (dragOverTabGroup && |
| 404 | dragOverTabGroup.dataset.groupId !== previousGroupId) { |
| 405 | this.delegate_.placeTabElement( |
| 406 | tabElement, this.dstIndex, false, dragOverTabGroup.dataset.groupId); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | if (!dragOverTabGroup && previousGroupId) { |
| 411 | this.delegate_.placeTabElement( |
| 412 | tabElement, this.dstIndex, false, undefined); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | if (!dragOverTabElement) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | const dragOverIndex = this.delegate_.getIndexOfTab(dragOverTabElement); |
| 421 | this.delegate_.placeTabElement( |
| 422 | tabElement, dragOverIndex, tabElement.tab.pinned, previousGroupId); |
| 423 | } |
| 424 | } |
| 425 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 426 | export class DragManager { |
| 427 | /** @param {!DragManagerDelegateElement} delegate */ |
| 428 | constructor(delegate) { |
| 429 | /** @private {!DragManagerDelegateElement} */ |
| 430 | this.delegate_ = delegate; |
| 431 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 432 | /** @type {?DragSession} */ |
| 433 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 434 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 435 | /** @private {!TabsApiProxy} */ |
| 436 | this.tabsProxy_ = TabsApiProxy.getInstance(); |
| 437 | } |
| 438 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 439 | /** @private */ |
| 440 | onDragLeave_() { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 441 | if (this.dragSession_ && !this.dragSession_.isDraggingPlaceholder()) { |
| 442 | return; |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 443 | } |
| 444 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 445 | this.dragSession_.cancel(); |
| 446 | this.dragSession_ = null; |
John Lee | 9b3b01f | 2020-02-19 03:45:04 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** @param {!DragEvent} event */ |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 450 | onDragOver_(event) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 451 | event.preventDefault(); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 452 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 456 | this.dragSession_.update(event); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 457 | } |
| 458 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 459 | /** @param {!DragEvent} event */ |
| 460 | onDragStart_(event) { |
| 461 | const draggedItem = |
| 462 | /** @type {!Array<!Element>} */ (event.composedPath()).find(item => { |
| 463 | return isTabElement(item) || isTabGroupElement(item); |
| 464 | }); |
| 465 | if (!draggedItem) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 469 | this.dragSession_ = DragSession.createFromElement( |
| 470 | this.delegate_, |
| 471 | /** @type {!TabElement|!TabGroupElement} */ (draggedItem)); |
| 472 | this.dragSession_.start(event); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 473 | } |
| 474 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 475 | /** @param {!DragEvent} event */ |
| 476 | onDragEnd_(event) { |
| 477 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 481 | this.dragSession_.cancel(); |
| 482 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 483 | } |
| 484 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 485 | /** @param {!DragEvent} event */ |
| 486 | onDragEnter_(event) { |
| 487 | if (this.dragSession_) { |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | this.dragSession_ = DragSession.createFromEvent(this.delegate_, event); |
| 492 | } |
| 493 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 494 | /** |
| 495 | * @param {!DragEvent} event |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 496 | */ |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 497 | onDrop_(event) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 498 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 502 | this.dragSession_.finish(event); |
| 503 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 504 | } |
| 505 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 506 | startObserving() { |
| 507 | this.delegate_.addEventListener( |
| 508 | 'dragstart', e => this.onDragStart_(/** @type {!DragEvent} */ (e))); |
| 509 | this.delegate_.addEventListener( |
| 510 | 'dragend', e => this.onDragEnd_(/** @type {!DragEvent} */ (e))); |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 511 | this.delegate_.addEventListener( |
| 512 | 'dragenter', (e) => this.onDragEnter_(/** @type {!DragEvent} */ (e))); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 513 | this.delegate_.addEventListener('dragleave', () => this.onDragLeave_()); |
| 514 | this.delegate_.addEventListener( |
| 515 | 'dragover', e => this.onDragOver_(/** @type {!DragEvent} */ (e))); |
| 516 | this.delegate_.addEventListener( |
| 517 | 'drop', e => this.onDrop_(/** @type {!DragEvent} */ (e))); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 518 | } |
| 519 | } |