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

Side by Side Diff: chrome/browser/resources/bookmark_manager/js/main.js

Issue 11504002: Bookmark manager: make Organized menu commands to work. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 2012-12-10T15:50 Created 8 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 * @param {!cr.ui.CanExecuteEvent} e The event fired by the command system. 894 * @param {!cr.ui.CanExecuteEvent} e The event fired by the command system.
895 * @param {!cr.ui.Command} command The command we are currently processing. 895 * @param {!cr.ui.Command} command The command we are currently processing.
896 */ 896 */
897 function updateOpenCommands(e, command) { 897 function updateOpenCommands(e, command) {
898 var selectedItem = e.target.selectedItem; 898 var selectedItem = e.target.selectedItem;
899 var selectionCount; 899 var selectionCount;
900 if (e.target == tree) { 900 if (e.target == tree) {
901 selectionCount = selectedItem ? 1 : 0; 901 selectionCount = selectedItem ? 1 : 0;
902 selectedItem = selectedItem.bookmarkNode; 902 selectedItem = selectedItem.bookmarkNode;
903 } else { 903 } else {
904 selectionCount = e.target.selectedItems.length; 904 selectionCount = list.selectedItems.length;
905 } 905 }
906 906
907 var isFolder = selectionCount == 1 && 907 var isFolder = selectionCount == 1 &&
908 selectedItem && 908 selectedItem &&
909 bmm.isFolder(selectedItem); 909 bmm.isFolder(selectedItem);
910 var multiple = selectionCount != 1 || isFolder; 910 var multiple = selectionCount != 1 || isFolder;
911 911
912 function hasBookmarks(node) { 912 function hasBookmarks(node) {
913 for (var i = 0; i < node.children.length; i++) { 913 for (var i = 0; i < node.children.length; i++) {
914 if (!bmm.isFolder(node.children[i])) 914 if (!bmm.isFolder(node.children[i]))
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 document.addEventListener('canExecute', function(e) { 972 document.addEventListener('canExecute', function(e) {
973 var command = e.command; 973 var command = e.command;
974 switch (command.id) { 974 switch (command.id) {
975 case 'import-menu-command': 975 case 'import-menu-command':
976 e.canExecute = canEdit; 976 e.canExecute = canEdit;
977 break; 977 break;
978 case 'export-menu-command': 978 case 'export-menu-command':
979 // We can always execute the export-menu command. 979 // We can always execute the export-menu command.
980 e.canExecute = true; 980 e.canExecute = true;
981 break; 981 break;
982 982 case 'sort-command':
983 case 'undo-command': 983 e.canExecute = !list.isRecent() && !list.isSearch() &&
984 // The global undo command has no visible UI, so always enable it, and 984 list.dataModel.length > 1;
985 // just make it a no-op if undo is not possible.
986 e.canExecute = true;
987 break; 985 break;
988 986 default:
989 case 'add-new-bookmark-command': 987 canExecuteForList(e);
990 case 'new-folder-command':
991 // When active element is other than list/tree view, add-new-bookmark and
992 // new-folder commands are avaiable as if list view is active.
993 canExecuteShared(e, list.isRecent() || list.isSearch());
994 break; 988 break;
995 } 989 }
996 }); 990 });
997 991
998 /** 992 /**
999 * Helper function for handling canExecute for the list and the tree. 993 * Helper function for handling canExecute for the list and the tree.
1000 * @param {!Event} e Can execute event object. 994 * @param {!Event} e Can execute event object.
1001 * @param {boolean} isRecentOrSearch Whether the user is trying to do a command 995 * @param {boolean} isRecentOrSearch Whether the user is trying to do a command
1002 * on recent or search. 996 * on recent or search.
1003 */ 997 */
1004 function canExecuteShared(e, isRecentOrSearch) { 998 function canExecuteShared(e, isRecentOrSearch) {
1005 var command = e.command; 999 var command = e.command;
1006 var commandId = command.id; 1000 var commandId = command.id;
1007 switch (commandId) { 1001 switch (commandId) {
1008 case 'paste-command': 1002 case 'paste-command':
1009 updatePasteCommand(); 1003 updatePasteCommand();
1010 break; 1004 break;
1011 1005
1012 case 'sort-command':
1013 if (isRecentOrSearch) {
1014 e.canExecute = false;
1015 } else {
1016 e.canExecute = list.dataModel.length > 0 && canEdit;
1017
1018 // The list might be loading so listen to the load event.
1019 if (canEdit) {
1020 var f = function() {
1021 list.removeEventListener('load', f);
1022 command.disabled = list.dataModel.length == 0 || !canEdit;
1023 };
1024 list.addEventListener('load', f);
1025 }
1026 }
1027 break;
1028
1029 case 'add-new-bookmark-command': 1006 case 'add-new-bookmark-command':
1030 case 'new-folder-command': 1007 case 'new-folder-command':
1031 e.canExecute = !isRecentOrSearch && canEdit; 1008 e.canExecute = !isRecentOrSearch && canEdit;
1032 break; 1009 break;
1033 1010
1034 case 'open-in-new-tab-command': 1011 case 'open-in-new-tab-command':
1035 case 'open-in-background-tab-command': 1012 case 'open-in-background-tab-command':
1036 case 'open-in-new-window-command': 1013 case 'open-in-new-window-command':
1037 case 'open-incognito-window-command': 1014 case 'open-incognito-window-command':
1038 updateOpenCommands(e, command); 1015 updateOpenCommands(e, command);
1039 break; 1016 break;
1040 1017
1041 case 'undo-delete-command': 1018 case 'undo-delete-command':
1042 e.canExecute = !!lastDeletedNodes; 1019 e.canExecute = !!lastDeletedNodes;
1043 break; 1020 break;
1044 } 1021 }
1045 } 1022 }
1046 1023
1047 // Update canExecute for the commands when the list is the active element. 1024 /**
1048 list.addEventListener('canExecute', function(e) { 1025 * Helper function for handling canExecute for the list and document.
1049 if (e.target != list) return; 1026 * @param {!Event} e Can execute event object.
1050 1027 */
1028 function canExecuteForList(e) {
1051 var command = e.command; 1029 var command = e.command;
1052 var commandId = command.id; 1030 var commandId = command.id;
1053 1031
1054 function hasSelected() { 1032 function hasSelected() {
1055 return !!e.target.selectedItem; 1033 return !!list.selectedItem;
1056 } 1034 }
1057 1035
1058 function hasSingleSelected() { 1036 function hasSingleSelected() {
1059 return e.target.selectedItems.length == 1; 1037 return list.selectedItems.length == 1;
1060 } 1038 }
1061 1039
1062 function isRecentOrSearch() { 1040 function isRecentOrSearch() {
1063 return list.isRecent() || list.isSearch(); 1041 return list.isRecent() || list.isSearch();
1064 } 1042 }
1065 1043
1066 switch (commandId) { 1044 switch (commandId) {
1067 case 'rename-folder-command': 1045 case 'rename-folder-command':
1068 // Show rename if a single folder is selected. 1046 // Show rename if a single folder is selected.
1069 var items = e.target.selectedItems; 1047 var items = list.selectedItems;
1070 if (items.length != 1) { 1048 if (items.length != 1) {
1071 e.canExecute = false; 1049 e.canExecute = false;
1072 command.hidden = true; 1050 command.hidden = true;
1073 } else { 1051 } else {
1074 var isFolder = bmm.isFolder(items[0]); 1052 var isFolder = bmm.isFolder(items[0]);
1075 e.canExecute = isFolder && canEdit; 1053 e.canExecute = isFolder && canEdit;
1076 command.hidden = !isFolder; 1054 command.hidden = !isFolder;
1077 } 1055 }
1078 break; 1056 break;
1079 1057
1080 case 'edit-command': 1058 case 'edit-command':
1081 // Show the edit command if not a folder. 1059 // Show the edit command if not a folder.
1082 var items = e.target.selectedItems; 1060 var items = list.selectedItems;
1083 if (items.length != 1) { 1061 if (items.length != 1) {
1084 e.canExecute = false; 1062 e.canExecute = false;
1085 command.hidden = false; 1063 command.hidden = false;
1086 } else { 1064 } else {
1087 var isFolder = bmm.isFolder(items[0]); 1065 var isFolder = bmm.isFolder(items[0]);
1088 e.canExecute = !isFolder && canEdit; 1066 e.canExecute = !isFolder && canEdit;
1089 command.hidden = isFolder; 1067 command.hidden = isFolder;
1090 } 1068 }
1091 break; 1069 break;
1092 1070
(...skipping 10 matching lines...) Expand all
1103 e.canExecute = hasSelected(); 1081 e.canExecute = hasSelected();
1104 break; 1082 break;
1105 1083
1106 case 'open-in-same-window-command': 1084 case 'open-in-same-window-command':
1107 e.canExecute = hasSelected(); 1085 e.canExecute = hasSelected();
1108 break; 1086 break;
1109 1087
1110 default: 1088 default:
1111 canExecuteShared(e, isRecentOrSearch()); 1089 canExecuteShared(e, isRecentOrSearch());
1112 } 1090 }
1091 }
1092
1093 // Update canExecute for the commands when the list is the active element.
1094 list.addEventListener('canExecute', function(e) {
1095 if (e.target != list) return;
1096 canExecuteForList(e);
1113 }); 1097 });
1114 1098
1115 // Update canExecute for the commands when the tree is the active element. 1099 // Update canExecute for the commands when the tree is the active element.
1116 tree.addEventListener('canExecute', function(e) { 1100 tree.addEventListener('canExecute', function(e) {
1117 if (e.target != tree) return; 1101 if (e.target != tree) return;
1118 1102
1119 var command = e.command; 1103 var command = e.command;
1120 var commandId = command.id; 1104 var commandId = command.id;
1121 1105
1122 function hasSelected() { 1106 function hasSelected() {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 }); 1178 });
1195 } 1179 }
1196 }); 1180 });
1197 } 1181 }
1198 1182
1199 var organizeButton = document.querySelector('.summary > button'); 1183 var organizeButton = document.querySelector('.summary > button');
1200 organizeButton.addEventListener('click', function(e) { 1184 organizeButton.addEventListener('click', function(e) {
1201 updateEditingCommands(); 1185 updateEditingCommands();
1202 $('add-new-bookmark-command').canExecuteChange(); 1186 $('add-new-bookmark-command').canExecuteChange();
1203 $('new-folder-command').canExecuteChange(); 1187 $('new-folder-command').canExecuteChange();
1188 $('sort-command').canExecuteChange();
1204 }); 1189 });
1205 list.addEventListener('contextmenu', updateEditingCommands); 1190 list.addEventListener('contextmenu', updateEditingCommands);
1206 tree.addEventListener('contextmenu', updateEditingCommands); 1191 tree.addEventListener('contextmenu', updateEditingCommands);
1207 1192
1208 // Handle global commands.
1209 document.addEventListener('command', function(e) {
1210 var command = e.command;
1211 switch (command.id) {
1212 case 'import-menu-command':
1213 chrome.bookmarks.import();
1214 break;
1215 case 'export-menu-command':
1216 chrome.bookmarks.export();
1217 break;
1218 case 'undo-command':
1219 if (performGlobalUndo)
1220 performGlobalUndo();
1221 break;
1222 case 'add-new-bookmark-command':
1223 case 'new-folder-command':
1224 if (e.target != list && e.target != tree)
1225 handleCommand(e);
1226 break;
1227 }
1228 });
1229
1230 function handleRename(e) { 1193 function handleRename(e) {
1231 var item = e.target; 1194 var item = e.target;
1232 var bookmarkNode = item.bookmarkNode; 1195 var bookmarkNode = item.bookmarkNode;
1233 chrome.bookmarks.update(bookmarkNode.id, {title: item.label}); 1196 chrome.bookmarks.update(bookmarkNode.id, {title: item.label});
1234 performGlobalUndo = null; // This can't be undone, so disable global undo. 1197 performGlobalUndo = null; // This can't be undone, so disable global undo.
1235 } 1198 }
1236 1199
1237 tree.addEventListener('rename', handleRename); 1200 tree.addEventListener('rename', handleRename);
1238 list.addEventListener('rename', handleRename); 1201 list.addEventListener('rename', handleRename);
1239 1202
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 return linkController || 1282 return linkController ||
1320 (linkController = new cr.LinkController(loadTimeData)); 1283 (linkController = new cr.LinkController(loadTimeData));
1321 } 1284 }
1322 1285
1323 /** 1286 /**
1324 * Returns the selected bookmark nodes of the active element. Only call this 1287 * Returns the selected bookmark nodes of the active element. Only call this
1325 * if the list or the tree is focused. 1288 * if the list or the tree is focused.
1326 * @return {!Array} Array of bookmark nodes. 1289 * @return {!Array} Array of bookmark nodes.
1327 */ 1290 */
1328 function getSelectedBookmarkNodes() { 1291 function getSelectedBookmarkNodes() {
1329 if (document.activeElement == list) { 1292 return document.activeElement == tree ? [tree.selectedItem.bookmarkNode] :
1330 return list.selectedItems; 1293 list.selectedItems;
1331 } else if (document.activeElement == tree) {
1332 return [tree.selectedItem.bookmarkNode];
1333 } else {
1334 throw Error('getSelectedBookmarkNodes called when wrong element focused.');
1335 }
1336 } 1294 }
1337 1295
1338 /** 1296 /**
1339 * @return {!Array.<string>} An array of the selected bookmark IDs. 1297 * @return {!Array.<string>} An array of the selected bookmark IDs.
1340 */ 1298 */
1341 function getSelectedBookmarkIds() { 1299 function getSelectedBookmarkIds() {
1342 return getSelectedBookmarkNodes().map(function(node) { 1300 return getSelectedBookmarkNodes().map(function(node) {
1343 return node.id; 1301 return node.id;
1344 }); 1302 });
1345 } 1303 }
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
1610 } 1568 }
1611 1569
1612 list.startBatchUpdates(); 1570 list.startBatchUpdates();
1613 1571
1614 chrome.bookmarks.onCreated.addListener(handle); 1572 chrome.bookmarks.onCreated.addListener(handle);
1615 chrome.bookmarks.onMoved.addListener(handle); 1573 chrome.bookmarks.onMoved.addListener(handle);
1616 timer = setTimeout(handleTimeout, 300); 1574 timer = setTimeout(handleTimeout, 300);
1617 } 1575 }
1618 1576
1619 /** 1577 /**
1620 * Handler for the command event. This is used both for the tree and the list. 1578 * Handler for the command event. This is used for context menu of list/tree
1579 * and organized menu.
1621 * @param {!Event} e The event object. 1580 * @param {!Event} e The event object.
1622 */ 1581 */
1623 function handleCommand(e) { 1582 function handleCommand(e) {
1624 var command = e.command; 1583 var command = e.command;
1625 var commandId = command.id; 1584 var commandId = command.id;
1626 switch (commandId) { 1585 switch (commandId) {
1586 case 'import-menu-command':
1587 chrome.bookmarks.import();
1588 break;
1589 case 'export-menu-command':
1590 chrome.bookmarks.export();
1591 break;
1592 case 'undo-command':
1593 if (performGlobalUndo)
1594 performGlobalUndo();
1595 break;
1627 case 'show-in-folder-command': 1596 case 'show-in-folder-command':
1628 showInFolder(); 1597 showInFolder();
1629 break; 1598 break;
1630 case 'open-in-new-tab-command': 1599 case 'open-in-new-tab-command':
1631 case 'open-in-background-tab-command': 1600 case 'open-in-background-tab-command':
1632 openBookmarks(LinkKind.BACKGROUND_TAB); 1601 openBookmarks(LinkKind.BACKGROUND_TAB);
1633 break; 1602 break;
1634 case 'open-in-new-window-command': 1603 case 'open-in-new-window-command':
1635 openBookmarks(LinkKind.WINDOW); 1604 openBookmarks(LinkKind.WINDOW);
1636 break; 1605 break;
(...skipping 14 matching lines...) Expand all
1651 case 'paste-command': 1620 case 'paste-command':
1652 selectItemsAfterUserAction(list); 1621 selectItemsAfterUserAction(list);
1653 chrome.bookmarkManagerPrivate.paste(list.parentId, 1622 chrome.bookmarkManagerPrivate.paste(list.parentId,
1654 getSelectedBookmarkIds()); 1623 getSelectedBookmarkIds());
1655 break; 1624 break;
1656 case 'sort-command': 1625 case 'sort-command':
1657 chrome.bookmarkManagerPrivate.sortChildren(list.parentId); 1626 chrome.bookmarkManagerPrivate.sortChildren(list.parentId);
1658 break; 1627 break;
1659 case 'rename-folder-command': 1628 case 'rename-folder-command':
1660 case 'edit-command': 1629 case 'edit-command':
1661 if (document.activeElement == list) { 1630 if (document.activeElement == tree)
1631 tree.selectedItem.editing = true;
1632 else {
1662 var li = list.getListItem(list.selectedItem); 1633 var li = list.getListItem(list.selectedItem);
1663 if (li) 1634 if (li)
1664 li.editing = true; 1635 li.editing = true;
1665 } else {
1666 document.activeElement.selectedItem.editing = true;
1667 } 1636 }
1668 break; 1637 break;
1669 case 'new-folder-command': 1638 case 'new-folder-command':
1670 newFolder(); 1639 newFolder();
1671 break; 1640 break;
1672 case 'add-new-bookmark-command': 1641 case 'add-new-bookmark-command':
1673 addPage(); 1642 addPage();
1674 break; 1643 break;
1675 case 'open-in-same-window-command': 1644 case 'open-in-same-window-command':
1676 openItem(); 1645 openItem();
(...skipping 16 matching lines...) Expand all
1693 1662
1694 $('open-in-new-window-command').shortcut = 'Shift-Enter'; 1663 $('open-in-new-window-command').shortcut = 'Shift-Enter';
1695 $('open-in-background-tab-command').shortcut = cr.isMac ? 'Meta-Enter' : 1664 $('open-in-background-tab-command').shortcut = cr.isMac ? 'Meta-Enter' :
1696 'Ctrl-Enter'; 1665 'Ctrl-Enter';
1697 $('open-in-new-tab-command').shortcut = cr.isMac ? 'Shift-Meta-Enter' : 1666 $('open-in-new-tab-command').shortcut = cr.isMac ? 'Shift-Meta-Enter' :
1698 'Shift-Ctrl-Enter'; 1667 'Shift-Ctrl-Enter';
1699 1668
1700 $('rename-folder-command').shortcut = $('edit-command').shortcut = 1669 $('rename-folder-command').shortcut = $('edit-command').shortcut =
1701 cr.isMac ? 'Enter' : 'F2'; 1670 cr.isMac ? 'Enter' : 'F2';
1702 1671
1703 list.addEventListener('command', handleCommand); 1672 document.addEventListener('command', handleCommand);
1704 tree.addEventListener('command', handleCommand);
1705 1673
1706 // Execute the copy, cut and paste commands when those events are dispatched by 1674 // Execute the copy, cut and paste commands when those events are dispatched by
1707 // the browser. This allows us to rely on the browser to handle the keyboard 1675 // the browser. This allows us to rely on the browser to handle the keyboard
1708 // shortcuts for these commands. 1676 // shortcuts for these commands.
1709 (function() { 1677 (function() {
1710 function handle(id) { 1678 function handle(id) {
1711 return function(e) { 1679 return function(e) {
1712 var command = $(id); 1680 var command = $(id);
1713 if (!command.disabled) { 1681 if (!command.disabled) {
1714 command.execute(); 1682 command.execute();
1715 if (e) e.preventDefault(); // Prevent the system beep. 1683 if (e) e.preventDefault(); // Prevent the system beep.
1716 } 1684 }
1717 }; 1685 };
1718 } 1686 }
1719 1687
1720 // Listen to copy, cut and paste events and execute the associated commands. 1688 // Listen to copy, cut and paste events and execute the associated commands.
1721 document.addEventListener('copy', handle('copy-command')); 1689 document.addEventListener('copy', handle('copy-command'));
1722 document.addEventListener('cut', handle('cut-command')); 1690 document.addEventListener('cut', handle('cut-command'));
1723 1691
1724 var pasteHandler = handle('paste-command'); 1692 var pasteHandler = handle('paste-command');
1725 document.addEventListener('paste', function(e) { 1693 document.addEventListener('paste', function(e) {
1726 // Paste is a bit special since we need to do an async call to see if we can 1694 // Paste is a bit special since we need to do an async call to see if we can
1727 // paste because the paste command might not be up to date. 1695 // paste because the paste command might not be up to date.
1728 updatePasteCommand(pasteHandler); 1696 updatePasteCommand(pasteHandler);
1729 }); 1697 });
1730 })(); 1698 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698