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); |
| 300 | event.dataTransfer.setDragImage( |
| 301 | this.element_.getDragImage(), event.clientX - draggedItemRect.left, |
| 302 | event.clientY - draggedItemRect.top); |
| 303 | |
| 304 | if (isTabElement(this.element_)) { |
| 305 | event.dataTransfer.setData( |
| 306 | getTabIdDataType(), this.element_.tab.id.toString()); |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 307 | |
| 308 | if (this.element_.tab.pinned) { |
| 309 | event.dataTransfer.setData( |
| 310 | 'pinned', this.element_.tab.pinned.toString()); |
| 311 | } |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 312 | } else if (isTabGroupElement(this.element_)) { |
| 313 | event.dataTransfer.setData( |
| 314 | getGroupIdDataType(), this.element_.dataset.groupId); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** @param {!DragEvent} event */ |
| 319 | update(event) { |
| 320 | event.dataTransfer.dropEffect = 'move'; |
| 321 | if (isTabGroupElement(this.element_)) { |
| 322 | this.updateForTabGroupElement_(event); |
| 323 | } else if (isTabElement(this.element_)) { |
| 324 | this.updateForTabElement_(event); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * @param {!DragEvent} event |
| 330 | * @private |
| 331 | */ |
| 332 | updateForTabGroupElement_(event) { |
| 333 | const tabGroupElement = |
| 334 | /** @type {!TabGroupElement} */ (this.element_); |
| 335 | const composedPath = /** @type {!Array<!Element>} */ (event.composedPath()); |
| 336 | if (composedPath.includes(assert(this.element_))) { |
| 337 | // Dragging over itself or a child of itself. |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | const dragOverTabElement = |
| 342 | /** @type {!TabElement|undefined} */ (composedPath.find(isTabElement)); |
| 343 | if (dragOverTabElement && !dragOverTabElement.tab.pinned) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 344 | let dragOverIndex = this.delegate_.getIndexOfTab(dragOverTabElement); |
| 345 | dragOverIndex += |
| 346 | this.shouldOffsetIndexForGroup_(dragOverTabElement) ? 1 : 0; |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 347 | this.delegate_.placeTabGroupElement(tabGroupElement, dragOverIndex); |
| 348 | return; |
| 349 | } |
| 350 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 351 | const dragOverGroupElement = /** @type {!TabGroupElement|undefined} */ ( |
| 352 | composedPath.find(isTabGroupElement)); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 353 | if (dragOverGroupElement) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 354 | let dragOverIndex = this.delegate_.getIndexOfTab( |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 355 | /** @type {!TabElement} */ (dragOverGroupElement.firstElementChild)); |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 356 | dragOverIndex += |
| 357 | this.shouldOffsetIndexForGroup_(dragOverGroupElement) ? 1 : 0; |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 358 | this.delegate_.placeTabGroupElement(tabGroupElement, dragOverIndex); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @param {!DragEvent} event |
| 364 | * @private |
| 365 | */ |
| 366 | updateForTabElement_(event) { |
| 367 | const tabElement = /** @type {!TabElement} */ (this.element_); |
| 368 | const composedPath = /** @type {!Array<!Element>} */ (event.composedPath()); |
| 369 | const dragOverTabElement = |
| 370 | /** @type {?TabElement} */ (composedPath.find(isTabElement)); |
| 371 | if (dragOverTabElement && |
| 372 | dragOverTabElement.tab.pinned !== tabElement.tab.pinned) { |
| 373 | // Can only drag between the same pinned states. |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | const previousGroupId = (tabElement.parentElement && |
| 378 | isTabGroupElement(tabElement.parentElement)) ? |
| 379 | tabElement.parentElement.dataset.groupId : |
| 380 | undefined; |
| 381 | |
| 382 | const dragOverTabGroup = |
| 383 | /** @type {?TabGroupElement} */ (composedPath.find(isTabGroupElement)); |
| 384 | if (dragOverTabGroup && |
| 385 | dragOverTabGroup.dataset.groupId !== previousGroupId) { |
| 386 | this.delegate_.placeTabElement( |
| 387 | tabElement, this.dstIndex, false, dragOverTabGroup.dataset.groupId); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | if (!dragOverTabGroup && previousGroupId) { |
| 392 | this.delegate_.placeTabElement( |
| 393 | tabElement, this.dstIndex, false, undefined); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | if (!dragOverTabElement) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | const dragOverIndex = this.delegate_.getIndexOfTab(dragOverTabElement); |
| 402 | this.delegate_.placeTabElement( |
| 403 | tabElement, dragOverIndex, tabElement.tab.pinned, previousGroupId); |
| 404 | } |
| 405 | } |
| 406 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 407 | export class DragManager { |
| 408 | /** @param {!DragManagerDelegateElement} delegate */ |
| 409 | constructor(delegate) { |
| 410 | /** @private {!DragManagerDelegateElement} */ |
| 411 | this.delegate_ = delegate; |
| 412 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 413 | /** @type {?DragSession} */ |
| 414 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 415 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 416 | /** @private {!TabsApiProxy} */ |
| 417 | this.tabsProxy_ = TabsApiProxy.getInstance(); |
| 418 | } |
| 419 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 420 | /** @private */ |
| 421 | onDragLeave_() { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 422 | if (this.dragSession_ && !this.dragSession_.isDraggingPlaceholder()) { |
| 423 | return; |
John Lee | 77e94e1a | 2020-02-27 02:16:44 | [diff] [blame] | 424 | } |
| 425 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 426 | this.dragSession_.cancel(); |
| 427 | this.dragSession_ = null; |
John Lee | 9b3b01f | 2020-02-19 03:45:04 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** @param {!DragEvent} event */ |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 431 | onDragOver_(event) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 432 | event.preventDefault(); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 433 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 437 | this.dragSession_.update(event); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 438 | } |
| 439 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 440 | /** @param {!DragEvent} event */ |
| 441 | onDragStart_(event) { |
| 442 | const draggedItem = |
| 443 | /** @type {!Array<!Element>} */ (event.composedPath()).find(item => { |
| 444 | return isTabElement(item) || isTabGroupElement(item); |
| 445 | }); |
| 446 | if (!draggedItem) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 450 | this.dragSession_ = DragSession.createFromElement( |
| 451 | this.delegate_, |
| 452 | /** @type {!TabElement|!TabGroupElement} */ (draggedItem)); |
| 453 | this.dragSession_.start(event); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 454 | } |
| 455 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 456 | /** @param {!DragEvent} event */ |
| 457 | onDragEnd_(event) { |
| 458 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 462 | this.dragSession_.cancel(); |
| 463 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 464 | } |
| 465 | |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 466 | /** @param {!DragEvent} event */ |
| 467 | onDragEnter_(event) { |
| 468 | if (this.dragSession_) { |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | this.dragSession_ = DragSession.createFromEvent(this.delegate_, event); |
| 473 | } |
| 474 | |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 475 | /** |
| 476 | * @param {!DragEvent} event |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 477 | */ |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 478 | onDrop_(event) { |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 479 | if (!this.dragSession_) { |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 480 | return; |
| 481 | } |
| 482 | |
John Lee | 7b0b7a6 | 2020-03-03 06:00:50 | [diff] [blame] | 483 | this.dragSession_.finish(event); |
| 484 | this.dragSession_ = null; |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 485 | } |
| 486 | |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 487 | startObserving() { |
| 488 | this.delegate_.addEventListener( |
| 489 | 'dragstart', e => this.onDragStart_(/** @type {!DragEvent} */ (e))); |
| 490 | this.delegate_.addEventListener( |
| 491 | 'dragend', e => this.onDragEnd_(/** @type {!DragEvent} */ (e))); |
John Lee | 49ccb72 | 2020-02-26 03:30:05 | [diff] [blame] | 492 | this.delegate_.addEventListener( |
| 493 | 'dragenter', (e) => this.onDragEnter_(/** @type {!DragEvent} */ (e))); |
John Lee | aa89aa7 | 2020-02-21 01:53:29 | [diff] [blame] | 494 | this.delegate_.addEventListener('dragleave', () => this.onDragLeave_()); |
| 495 | this.delegate_.addEventListener( |
| 496 | 'dragover', e => this.onDragOver_(/** @type {!DragEvent} */ (e))); |
| 497 | this.delegate_.addEventListener( |
| 498 | 'drop', e => this.onDrop_(/** @type {!DragEvent} */ (e))); |
John Lee | c5bf0f2 | 2020-02-05 21:01:13 | [diff] [blame] | 499 | } |
| 500 | } |