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

Side by Side Diff: chrome/browser/resources/md_bookmarks/dnd_manager.js

Issue 2799593003: [MD Bookmarks] Allow dragging onto empty bookmark lists. (Closed)
Patch Set: rebase Created 3 years, 8 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
« no previous file with comments | « no previous file | chrome/browser/resources/md_bookmarks/list.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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('bookmarks', function() { 5 cr.define('bookmarks', function() {
6 /** 6 /**
7 * @param {BookmarkElement} element 7 * @param {BookmarkElement} element
8 * @return {boolean} 8 * @return {boolean}
9 */ 9 */
10 function isBookmarkItem(element) { 10 function isBookmarkItem(element) {
11 return element.tagName == 'BOOKMARKS-ITEM'; 11 return element.tagName == 'BOOKMARKS-ITEM';
12 } 12 }
13 13
14 /** 14 /**
15 * @param {BookmarkElement} element 15 * @param {BookmarkElement} element
16 * @return {boolean} 16 * @return {boolean}
17 */ 17 */
18 function isBookmarkFolderNode(element) { 18 function isBookmarkFolderNode(element) {
19 return element.tagName == 'BOOKMARKS-FOLDER-NODE'; 19 return element.tagName == 'BOOKMARKS-FOLDER-NODE';
20 } 20 }
21 21
22 /** 22 /**
23 * @param {BookmarkElement} element
24 * @return {boolean}
25 */
26 function isBookmarkList(element) {
27 return element.tagName == 'BOOKMARKS-LIST';
28 }
29
30 /**
23 * @param {Array<!Element>|undefined} path 31 * @param {Array<!Element>|undefined} path
24 * @return {BookmarkElement} 32 * @return {BookmarkElement}
25 */ 33 */
26 function getBookmarkElement(path) { 34 function getBookmarkElement(path) {
27 if (!path) 35 if (!path)
28 return null; 36 return null;
29 37
30 for (var i = 0; i < path.length; i++) { 38 for (var i = 0; i < path.length; i++) {
31 if (isBookmarkItem(path[i]) || isBookmarkFolderNode(path[i])) 39 if (isBookmarkItem(path[i]) || isBookmarkFolderNode(path[i]) ||
40 isBookmarkList(path[i]))
32 return path[i]; 41 return path[i];
33 } 42 }
34 return null; 43 return null;
35 } 44 }
36 45
37 /** 46 /**
38 * @param {BookmarkElement} bookmarkElement 47 * @param {BookmarkElement} bookmarkElement
39 * @return {BookmarkNode} 48 * @return {BookmarkNode}
40 */ 49 */
41 function getBookmarkNode(bookmarkElement) { 50 function getBookmarkNode(bookmarkElement) {
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 344
336 this.dropDestination_ = null; 345 this.dropDestination_ = null;
337 this.dropIndicator_.finish(); 346 this.dropIndicator_.finish();
338 }, 347 },
339 348
340 /** 349 /**
341 * @param {DropDestination} dropDestination 350 * @param {DropDestination} dropDestination
342 * @return {{parentId: string, index: number}} 351 * @return {{parentId: string, index: number}}
343 */ 352 */
344 calculateDropInfo_: function(dropDestination) { 353 calculateDropInfo_: function(dropDestination) {
354 if (isBookmarkList(dropDestination.element)) {
355 return {
356 index: 0,
357 parentId: bookmarks.Store.getInstance().data.selectedFolder,
358 };
359 }
360
345 var node = getBookmarkNode(dropDestination.element); 361 var node = getBookmarkNode(dropDestination.element);
346 var position = dropDestination.position; 362 var position = dropDestination.position;
347 var index = -1; 363 var index = -1;
348 var parentId = node.id; 364 var parentId = node.id;
349 365
350 if (position != DropPosition.ON) { 366 if (position != DropPosition.ON) {
351 var state = bookmarks.Store.getInstance().data; 367 var state = bookmarks.Store.getInstance().data;
352 368
353 // Drops between items in the normal list and the sidebar use the drop 369 // Drops between items in the normal list and the sidebar use the drop
354 // destination node's parent. 370 // destination node's parent.
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 /** 511 /**
496 * Determines the valid drop positions for the given target element. 512 * Determines the valid drop positions for the given target element.
497 * @private 513 * @private
498 * @param {!BookmarkElement} overElement The element that we are currently 514 * @param {!BookmarkElement} overElement The element that we are currently
499 * dragging over. 515 * dragging over.
500 * @return {DropPosition} An bit field enumeration of valid drop locations. 516 * @return {DropPosition} An bit field enumeration of valid drop locations.
501 */ 517 */
502 calculateValidDropPositions_: function(overElement) { 518 calculateValidDropPositions_: function(overElement) {
503 var dragInfo = this.dragInfo_; 519 var dragInfo = this.dragInfo_;
504 var state = bookmarks.Store.getInstance().data; 520 var state = bookmarks.Store.getInstance().data;
521 var itemId = overElement.itemId;
505 522
506 // Drags aren't allowed onto the search result list. 523 // Drags aren't allowed onto the search result list.
507 if (isBookmarkItem(overElement) && 524 if ((isBookmarkList(overElement) || isBookmarkItem(overElement)) &&
508 bookmarks.util.isShowingSearch(state)) { 525 bookmarks.util.isShowingSearch(state)) {
509 return DropPosition.NONE; 526 return DropPosition.NONE;
510 } 527 }
511 528
529 if (isBookmarkList(overElement))
530 itemId = state.selectedFolder;
531
512 // Drags of a bookmark onto itself or of a folder into its children aren't 532 // Drags of a bookmark onto itself or of a folder into its children aren't
513 // allowed. 533 // allowed.
514 if (dragInfo.isDraggingBookmark(overElement.itemId) || 534 if (dragInfo.isDraggingBookmark(itemId) ||
515 dragInfo.isDraggingFolderToDescendant( 535 dragInfo.isDraggingFolderToDescendant(itemId, state.nodes)) {
516 overElement.itemId, state.nodes)) {
517 return DropPosition.NONE; 536 return DropPosition.NONE;
518 } 537 }
519 538
520 var validDropPositions = this.calculateDropAboveBelow_(overElement); 539 var validDropPositions = this.calculateDropAboveBelow_(overElement);
521 if (this.canDropOn_(overElement)) 540 if (this.canDropOn_(overElement))
522 validDropPositions |= DropPosition.ON; 541 validDropPositions |= DropPosition.ON;
523 542
524 return validDropPositions; 543 return validDropPositions;
525 }, 544 },
526 545
527 /** 546 /**
528 * @private 547 * @private
529 * @param {BookmarkElement} overElement 548 * @param {BookmarkElement} overElement
530 * @return {DropPosition} 549 * @return {DropPosition}
531 */ 550 */
532 calculateDropAboveBelow_: function(overElement) { 551 calculateDropAboveBelow_: function(overElement) {
533 var dragInfo = this.dragInfo_; 552 var dragInfo = this.dragInfo_;
534 var state = bookmarks.Store.getInstance().data; 553 var state = bookmarks.Store.getInstance().data;
535 554
555 if (isBookmarkList(overElement))
556 return DropPosition.NONE;
557
536 // We cannot drop between Bookmarks bar and Other bookmarks. 558 // We cannot drop between Bookmarks bar and Other bookmarks.
537 if (getBookmarkNode(overElement).parentId == ROOT_NODE_ID) 559 if (getBookmarkNode(overElement).parentId == ROOT_NODE_ID)
538 return DropPosition.NONE; 560 return DropPosition.NONE;
539 561
540 var isOverFolderNode = isBookmarkFolderNode(overElement); 562 var isOverFolderNode = isBookmarkFolderNode(overElement);
541 563
542 // We can only drop between items in the tree if we have any folders. 564 // We can only drop between items in the tree if we have any folders.
543 if (isOverFolderNode && !dragInfo.isDraggingFolders()) 565 if (isOverFolderNode && !dragInfo.isDraggingFolders())
544 return DropPosition.NONE; 566 return DropPosition.NONE;
545 567
(...skipping 26 matching lines...) Expand all
572 594
573 /** 595 /**
574 * Determine whether we can drop the dragged items on the drop target. 596 * Determine whether we can drop the dragged items on the drop target.
575 * @private 597 * @private
576 * @param {!BookmarkElement} overElement The element that we are currently 598 * @param {!BookmarkElement} overElement The element that we are currently
577 * dragging over. 599 * dragging over.
578 * @return {boolean} Whether we can drop the dragged items on the drop 600 * @return {boolean} Whether we can drop the dragged items on the drop
579 * target. 601 * target.
580 */ 602 */
581 canDropOn_: function(overElement) { 603 canDropOn_: function(overElement) {
604 // Allow dragging onto empty bookmark lists.
605 if (isBookmarkList(overElement)) {
606 var state = bookmarks.Store.getInstance().data;
607 return state.selectedFolder &&
608 state.nodes[state.selectedFolder].children.length == 0;
609 }
610
582 // We can only drop on a folder. 611 // We can only drop on a folder.
583 if (getBookmarkNode(overElement).url) 612 if (getBookmarkNode(overElement).url)
584 return false; 613 return false;
585 614
586 return !this.dragInfo_.isDraggingChildBookmark(overElement.itemId) 615 return !this.dragInfo_.isDraggingChildBookmark(overElement.itemId)
587 }, 616 },
588 }; 617 };
589 618
590 return { 619 return {
591 DNDManager: DNDManager, 620 DNDManager: DNDManager,
592 DragInfo: DragInfo, 621 DragInfo: DragInfo,
593 DropIndicator: DropIndicator, 622 DropIndicator: DropIndicator,
594 }; 623 };
595 }); 624 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/md_bookmarks/list.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698