| 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 (function() { | 5 (function() { |
| 6 /** @const */ var BookmarkList = bmm.BookmarkList; | 6 /** @const */ var BookmarkList = bmm.BookmarkList; |
| 7 /** @const */ var BookmarkTree = bmm.BookmarkTree; | 7 /** @const */ var BookmarkTree = bmm.BookmarkTree; |
| 8 /** @const */ var Command = cr.ui.Command; | 8 /** @const */ var Command = cr.ui.Command; |
| 9 /** @const */ var CommandBinding = cr.ui.CommandBinding; | 9 /** @const */ var CommandBinding = cr.ui.CommandBinding; |
| 10 /** @const */ var LinkKind = cr.LinkKind; | 10 /** @const */ var LinkKind = cr.LinkKind; |
| (...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 var bookmarkNodes = getSelectedBookmarkNodes(); | 817 var bookmarkNodes = getSelectedBookmarkNodes(); |
| 818 // If we double clicked or pressed enter on a single folder, navigate to it. | 818 // If we double clicked or pressed enter on a single folder, navigate to it. |
| 819 if (bookmarkNodes.length == 1 && bmm.isFolder(bookmarkNodes[0])) { | 819 if (bookmarkNodes.length == 1 && bmm.isFolder(bookmarkNodes[0])) { |
| 820 navigateTo(bookmarkNodes[0].id, updateHash); | 820 navigateTo(bookmarkNodes[0].id, updateHash); |
| 821 } else { | 821 } else { |
| 822 openBookmarks(LinkKind.FOREGROUND_TAB); | 822 openBookmarks(LinkKind.FOREGROUND_TAB); |
| 823 } | 823 } |
| 824 } | 824 } |
| 825 | 825 |
| 826 /** | 826 /** |
| 827 * Refreshes search results after delete or undo-delete. |
| 828 * This ensures children of deleted folders do not remain in results |
| 829 */ |
| 830 function updateSearchResults() { |
| 831 if (list.isSearch()) { |
| 832 list.reload(); |
| 833 } |
| 834 } |
| 835 |
| 836 /** |
| 827 * Deletes the selected bookmarks. The bookmarks are saved in memory in case | 837 * Deletes the selected bookmarks. The bookmarks are saved in memory in case |
| 828 * the user needs to undo the deletion. | 838 * the user needs to undo the deletion. |
| 829 */ | 839 */ |
| 830 function deleteBookmarks() { | 840 function deleteBookmarks() { |
| 831 var selectedIds = getSelectedBookmarkIds(); | 841 var selectedIds = getSelectedBookmarkIds(); |
| 842 var filteredIds = getFilteredSelectedBookmarkIds(); |
| 832 lastDeletedNodes = []; | 843 lastDeletedNodes = []; |
| 833 | 844 |
| 834 function performDelete() { | 845 function performDelete() { |
| 835 chrome.bookmarkManagerPrivate.removeTrees(selectedIds); | 846 // Only remove filtered ids. |
| 847 chrome.bookmarkManagerPrivate.removeTrees(filteredIds); |
| 836 $('undo-delete-command').canExecuteChange(); | 848 $('undo-delete-command').canExecuteChange(); |
| 837 performGlobalUndo = undoDelete; | 849 performGlobalUndo = undoDelete; |
| 838 } | 850 } |
| 839 | 851 |
| 840 // First, store information about the bookmarks being deleted. | 852 // First, store information about the bookmarks being deleted. |
| 853 // Store all selected ids. |
| 841 selectedIds.forEach(function(id) { | 854 selectedIds.forEach(function(id) { |
| 842 chrome.bookmarks.getSubTree(id, function(results) { | 855 chrome.bookmarks.getSubTree(id, function(results) { |
| 843 lastDeletedNodes.push(results); | 856 lastDeletedNodes.push(results); |
| 844 | 857 |
| 845 // When all nodes have been saved, perform the deletion. | 858 // When all nodes have been saved, perform the deletion. |
| 846 if (lastDeletedNodes.length === selectedIds.length) | 859 if (lastDeletedNodes.length === selectedIds.length) { |
| 847 performDelete(); | 860 performDelete(); |
| 861 updateSearchResults(); |
| 862 } |
| 848 }); | 863 }); |
| 849 }); | 864 }); |
| 850 } | 865 } |
| 851 | 866 |
| 852 /** | 867 /** |
| 853 * Restores a tree of bookmarks under a specified folder. | 868 * Restores a tree of bookmarks under a specified folder. |
| 854 * @param {BookmarkTreeNode} node The node to restore. | 869 * @param {BookmarkTreeNode} node The node to restore. |
| 855 * @param {=string} parentId The ID of the folder to restore under. If not | 870 * @param {=string} parentId The ID of the folder to restore under. If not |
| 856 * specified, the original parentId of the node will be used. | 871 * specified, the original parentId of the node will be used. |
| 857 */ | 872 */ |
| (...skipping 10 matching lines...) Expand all Loading... |
| 868 console.error('Failed to restore bookmark.'); | 883 console.error('Failed to restore bookmark.'); |
| 869 return; | 884 return; |
| 870 } | 885 } |
| 871 | 886 |
| 872 if (node.children) { | 887 if (node.children) { |
| 873 // Restore the children using the new ID for this node. | 888 // Restore the children using the new ID for this node. |
| 874 node.children.forEach(function(child) { | 889 node.children.forEach(function(child) { |
| 875 restoreTree(child, result.id); | 890 restoreTree(child, result.id); |
| 876 }); | 891 }); |
| 877 } | 892 } |
| 893 |
| 894 updateSearchResults(); |
| 878 }); | 895 }); |
| 879 } | 896 } |
| 880 | 897 |
| 881 /** | 898 /** |
| 882 * Restores the last set of bookmarks that was deleted. | 899 * Restores the last set of bookmarks that was deleted. |
| 883 */ | 900 */ |
| 884 function undoDelete() { | 901 function undoDelete() { |
| 885 lastDeletedNodes.forEach(function(arr) { | 902 lastDeletedNodes.forEach(function(arr) { |
| 886 arr.forEach(restoreTree); | 903 arr.forEach(restoreTree); |
| 887 }); | 904 }); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1074 * if it was called from Organize Menu or from Context Menu. | 1091 * if it was called from Organize Menu or from Context Menu. |
| 1075 * @param {string} id The id of the element being pasted from. | 1092 * @param {string} id The id of the element being pasted from. |
| 1076 */ | 1093 */ |
| 1077 function pasteBookmark(id) { | 1094 function pasteBookmark(id) { |
| 1078 recordUserAction('Paste'); | 1095 recordUserAction('Paste'); |
| 1079 selectItemsAfterUserAction(list); | 1096 selectItemsAfterUserAction(list); |
| 1080 chrome.bookmarkManagerPrivate.paste(id, getSelectedBookmarkIds()); | 1097 chrome.bookmarkManagerPrivate.paste(id, getSelectedBookmarkIds()); |
| 1081 } | 1098 } |
| 1082 | 1099 |
| 1083 /** | 1100 /** |
| 1101 * Returns true if child is contained in another selected folder. |
| 1102 * Traces parent nodes up the tree until a selected ancestor or root is found. |
| 1103 */ |
| 1104 function hasSelectedAncestor(parentNode) { |
| 1105 function contains(arr, item) { |
| 1106 for (var i = 0; i < arr.length; i++) |
| 1107 if (arr[i] === item) |
| 1108 return true; |
| 1109 return false; |
| 1110 } |
| 1111 |
| 1112 // Don't search top level, cannot select permanent nodes in search. |
| 1113 if (parentNode == null || parentNode.id <= 2) |
| 1114 return false; |
| 1115 |
| 1116 // Found selected ancestor. |
| 1117 if (contains(getSelectedBookmarkNodes(), parentNode)) |
| 1118 return true; |
| 1119 |
| 1120 // Keep digging. |
| 1121 return hasSelectedAncestor(tree.getBookmarkNodeById(parentNode.parentId)); |
| 1122 } |
| 1123 |
| 1124 function getFilteredSelectedBookmarkIds() { |
| 1125 // Remove duplicates from filteredIds and return. |
| 1126 var filteredIds = new Array(); |
| 1127 // Selected nodes to iterate through for matches. |
| 1128 var nodes = getSelectedBookmarkNodes(); |
| 1129 |
| 1130 for (var i = 0; i < nodes.length; i++) |
| 1131 if (!hasSelectedAncestor(tree.getBookmarkNodeById(nodes[i].parentId))) |
| 1132 filteredIds.splice(0, 0, nodes[i].id); |
| 1133 |
| 1134 return filteredIds; |
| 1135 } |
| 1136 |
| 1137 /** |
| 1084 * Handler for the command event. This is used for context menu of list/tree | 1138 * Handler for the command event. This is used for context menu of list/tree |
| 1085 * and organized menu. | 1139 * and organized menu. |
| 1086 * @param {!Event} e The event object. | 1140 * @param {!Event} e The event object. |
| 1087 */ | 1141 */ |
| 1088 function handleCommand(e) { | 1142 function handleCommand(e) { |
| 1089 var command = e.command; | 1143 var command = e.command; |
| 1090 var commandId = command.id; | 1144 var commandId = command.id; |
| 1091 switch (commandId) { | 1145 switch (commandId) { |
| 1092 case 'import-menu-command': | 1146 case 'import-menu-command': |
| 1093 recordUserAction('Import'); | 1147 recordUserAction('Import'); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1127 deleteBookmarks(); | 1181 deleteBookmarks(); |
| 1128 break; | 1182 break; |
| 1129 case 'copy-command': | 1183 case 'copy-command': |
| 1130 recordUserAction('Copy'); | 1184 recordUserAction('Copy'); |
| 1131 chrome.bookmarkManagerPrivate.copy(getSelectedBookmarkIds(), | 1185 chrome.bookmarkManagerPrivate.copy(getSelectedBookmarkIds(), |
| 1132 updatePasteCommand); | 1186 updatePasteCommand); |
| 1133 break; | 1187 break; |
| 1134 case 'cut-command': | 1188 case 'cut-command': |
| 1135 recordUserAction('Cut'); | 1189 recordUserAction('Cut'); |
| 1136 chrome.bookmarkManagerPrivate.cut(getSelectedBookmarkIds(), | 1190 chrome.bookmarkManagerPrivate.cut(getSelectedBookmarkIds(), |
| 1137 updatePasteCommand); | 1191 function() { |
| 1192 updatePasteCommand(); |
| 1193 updateSearchResults(); |
| 1194 }); |
| 1138 break; | 1195 break; |
| 1139 case 'paste-from-organize-menu-command': | 1196 case 'paste-from-organize-menu-command': |
| 1140 pasteBookmark(list.parentId); | 1197 pasteBookmark(list.parentId); |
| 1141 break; | 1198 break; |
| 1142 case 'paste-from-context-menu-command': | 1199 case 'paste-from-context-menu-command': |
| 1143 pasteBookmark(getSelectedId()); | 1200 pasteBookmark(getSelectedId()); |
| 1144 break; | 1201 break; |
| 1145 case 'sort-command': | 1202 case 'sort-command': |
| 1146 recordUserAction('Sort'); | 1203 recordUserAction('Sort'); |
| 1147 chrome.bookmarkManagerPrivate.sortChildren(list.parentId); | 1204 chrome.bookmarkManagerPrivate.sortChildren(list.parentId); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1303 | 1360 |
| 1304 cr.ui.FocusOutlineManager.forDocument(document); | 1361 cr.ui.FocusOutlineManager.forDocument(document); |
| 1305 initializeSplitter(); | 1362 initializeSplitter(); |
| 1306 bmm.addBookmarkModelListeners(); | 1363 bmm.addBookmarkModelListeners(); |
| 1307 dnd.init(selectItemsAfterUserAction); | 1364 dnd.init(selectItemsAfterUserAction); |
| 1308 tree.reload(); | 1365 tree.reload(); |
| 1309 } | 1366 } |
| 1310 | 1367 |
| 1311 initializeBookmarkManager(); | 1368 initializeBookmarkManager(); |
| 1312 })(); | 1369 })(); |
| OLD | NEW |