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 /** @const */ var BookmarkList = bmm.BookmarkList; | 5 /** @const */ var BookmarkList = bmm.BookmarkList; |
6 /** @const */ var BookmarkTree = bmm.BookmarkTree; | 6 /** @const */ var BookmarkTree = bmm.BookmarkTree; |
7 /** @const */ var Command = cr.ui.Command; | 7 /** @const */ var Command = cr.ui.Command; |
8 /** @const */ var CommandBinding = cr.ui.CommandBinding; | 8 /** @const */ var CommandBinding = cr.ui.CommandBinding; |
9 /** @const */ var LinkKind = cr.LinkKind; | 9 /** @const */ var LinkKind = cr.LinkKind; |
10 /** @const */ var ListItem = cr.ui.ListItem; | 10 /** @const */ var ListItem = cr.ui.ListItem; |
(...skipping 1469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1480 arr.forEach(restoreTree); | 1480 arr.forEach(restoreTree); |
1481 }); | 1481 }); |
1482 lastDeletedNodes = null; | 1482 lastDeletedNodes = null; |
1483 $('undo-delete-command').canExecuteChange(); | 1483 $('undo-delete-command').canExecuteChange(); |
1484 | 1484 |
1485 // Only a single level of undo is supported, so disable global undo now. | 1485 // Only a single level of undo is supported, so disable global undo now. |
1486 performGlobalUndo = null; | 1486 performGlobalUndo = null; |
1487 } | 1487 } |
1488 | 1488 |
1489 /** | 1489 /** |
1490 * Computes folder for "Add Page" and "Add Folder". | |
1491 * @return {string} The id of folder node where we'll create new page/folder. | |
1492 */ | |
1493 function computeParentFolderForNewItem() { | |
1494 if (document.activeElement == tree) | |
1495 return list.parentId; | |
1496 var selectedItem = list.selectedItem; | |
1497 return selectedItem && bmm.isFolder(selectedItem) ? | |
1498 selectedItem.id : list.parentId; | |
1499 } | |
1500 | |
1501 /** | |
1490 * Callback for the new folder command. This creates a new folder and starts | 1502 * Callback for the new folder command. This creates a new folder and starts |
1491 * a rename of it. | 1503 * a rename of it. |
1492 */ | 1504 */ |
1493 function newFolder() { | 1505 function newFolder() { |
1494 var parentId = list.parentId; | |
1495 var isTree = document.activeElement == tree; | |
1496 chrome.bookmarks.create({ | |
1497 title: loadTimeData.getString('new_folder_name'), | |
1498 parentId: parentId | |
1499 }, function(newNode) { | |
1500 // This callback happens before the event that triggers the tree/list to | |
1501 // get updated so delay the work so that the tree/list gets updated first. | |
1502 setTimeout(function() { | |
1503 var newItem; | |
1504 if (isTree) { | |
1505 newItem = bmm.treeLookup[newNode.id]; | |
1506 tree.selectedItem = newItem; | |
1507 newItem.editing = true; | |
1508 } else { | |
1509 var index = list.dataModel.findIndexById(newNode.id); | |
1510 var sm = list.selectionModel; | |
1511 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = index; | |
1512 scrollIntoViewAndMakeEditable(index); | |
1513 } | |
1514 }, 50); | |
1515 }); | |
1516 performGlobalUndo = null; // This can't be undone, so disable global undo. | 1506 performGlobalUndo = null; // This can't be undone, so disable global undo. |
1507 | |
1508 var parentId = computeParentFolderForNewItem(); | |
1509 | |
1510 // Callback is called after tree and list data model updated. | |
1511 function createFolder(callback) { | |
1512 chrome.bookmarks.create({ | |
1513 title: loadTimeData.getString('new_folder_name'), | |
1514 parentId: parentId | |
1515 }, callback); | |
1516 } | |
1517 | |
1518 if (document.activeElement == tree) { | |
1519 createFolder(function(newNode) { | |
1520 newItem = bmm.treeLookup[newNode.id]; | |
1521 tree.selectedItem = newItem; | |
1522 newItem.editing = true; | |
1523 }); | |
1524 return; | |
1525 } | |
1526 | |
1527 function editNewFolderInList() { | |
1528 createFolder(function() { | |
1529 var index = list.dataModel.length - 1; | |
1530 var sm = list.selectionModel; | |
1531 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = index; | |
1532 scrollIntoViewAndMakeEditable(index); | |
1533 }); | |
1534 } | |
1535 | |
1536 if (parentId == list.parentId) { | |
1537 editNewFolderInList(); | |
1538 return; | |
1539 } | |
1540 | |
1541 addOneShotEventListener(list, 'load', editNewFolderInList); | |
1542 navigateTo(parentId, true); | |
1517 } | 1543 } |
1518 | 1544 |
1519 /** | 1545 /** |
1520 * Scrolls the list item into view and makes it editable. | 1546 * Scrolls the list item into view and makes it editable. |
1521 * @param {number} index The index of the item to make editable. | 1547 * @param {number} index The index of the item to make editable. |
1522 */ | 1548 */ |
1523 function scrollIntoViewAndMakeEditable(index) { | 1549 function scrollIntoViewAndMakeEditable(index) { |
1524 list.scrollIndexIntoView(index); | 1550 list.scrollIndexIntoView(index); |
1525 // onscroll is now dispatched asynchronously so we have to postpone | 1551 // onscroll is now dispatched asynchronously so we have to postpone |
1526 // the rest. | 1552 // the rest. |
1527 setTimeout(function() { | 1553 setTimeout(function() { |
1528 var item = list.getListItemByIndex(index); | 1554 var item = list.getListItemByIndex(index); |
1529 if (item) | 1555 if (item) |
1530 item.editing = true; | 1556 item.editing = true; |
1531 }); | 1557 }); |
1532 } | 1558 } |
1533 | 1559 |
1534 /** | 1560 /** |
1535 * Adds a page to the current folder. This is called by the | 1561 * Adds a page to the current folder. This is called by the |
1536 * add-new-bookmark-command handler. | 1562 * add-new-bookmark-command handler. |
1537 */ | 1563 */ |
1538 function addPage() { | 1564 function addPage() { |
1539 var parentId = list.parentId; | 1565 var parentId = computeParentFolderForNewItem(); |
1540 var fakeNode = { | 1566 |
1541 title: '', | 1567 function editNewBookmark() { |
1542 url: '', | 1568 var fakeNode = { |
1543 parentId: parentId, | 1569 title: '', |
1544 id: 'new' | 1570 url: '', |
1571 parentId: parentId, | |
1572 id: 'new' | |
1573 }; | |
1574 var dataModel = list.dataModel; | |
1575 var length = dataModel.length; | |
1576 dataModel.splice(length, 0, fakeNode); | |
1577 var sm = list.selectionModel; | |
1578 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = length; | |
1579 scrollIntoViewAndMakeEditable(length); | |
1545 }; | 1580 }; |
1546 | 1581 |
1547 var dataModel = list.dataModel; | 1582 if (parentId == list.parentId) { |
1548 var length = dataModel.length; | 1583 editNewBookmark(); |
1549 dataModel.splice(length, 0, fakeNode); | 1584 return; |
1550 var sm = list.selectionModel; | 1585 } |
1551 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = length; | 1586 |
1552 scrollIntoViewAndMakeEditable(length); | 1587 addOneShotEventListener(list, 'load', editNewBookmark); |
1588 navigateTo(parentId, true); | |
arv (Not doing code reviews)
2012/11/26 14:50:44
Maybe we should have navigateTo take an optional c
| |
1553 } | 1589 } |
1554 | 1590 |
1555 /** | 1591 /** |
1556 * This function is used to select items after a user action such as paste, drop | 1592 * This function is used to select items after a user action such as paste, drop |
1557 * add page etc. | 1593 * add page etc. |
1558 * @param {BookmarkList|BookmarkTree} target The target of the user action. | 1594 * @param {BookmarkList|BookmarkTree} target The target of the user action. |
1559 * @param {=string} opt_selectedTreeId If provided, then select that tree id. | 1595 * @param {=string} opt_selectedTreeId If provided, then select that tree id. |
1560 */ | 1596 */ |
1561 function selectItemsAfterUserAction(target, opt_selectedTreeId) { | 1597 function selectItemsAfterUserAction(target, opt_selectedTreeId) { |
1562 // We get one onCreated event per item so we delay the handling until we get | 1598 // We get one onCreated event per item so we delay the handling until we get |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1709 document.addEventListener('copy', handle('copy-command')); | 1745 document.addEventListener('copy', handle('copy-command')); |
1710 document.addEventListener('cut', handle('cut-command')); | 1746 document.addEventListener('cut', handle('cut-command')); |
1711 | 1747 |
1712 var pasteHandler = handle('paste-command'); | 1748 var pasteHandler = handle('paste-command'); |
1713 document.addEventListener('paste', function(e) { | 1749 document.addEventListener('paste', function(e) { |
1714 // Paste is a bit special since we need to do an async call to see if we can | 1750 // Paste is a bit special since we need to do an async call to see if we can |
1715 // paste because the paste command might not be up to date. | 1751 // paste because the paste command might not be up to date. |
1716 updatePasteCommand(pasteHandler); | 1752 updatePasteCommand(pasteHandler); |
1717 }); | 1753 }); |
1718 })(); | 1754 })(); |
OLD | NEW |