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

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

Issue 57883004: Update command state on "load" of a folder (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Changes based on code review Created 7 years, 1 month 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 (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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 searchTreeItem.label = loadTimeData.getString('search'); 137 searchTreeItem.label = loadTimeData.getString('search');
138 searchTreeItem.icon = isRTL() ? 'images/bookmark_manager_search_rtl.png' : 138 searchTreeItem.icon = isRTL() ? 'images/bookmark_manager_search_rtl.png' :
139 'images/bookmark_manager_search.png'; 139 'images/bookmark_manager_search.png';
140 } 140 }
141 141
142 /** 142 /**
143 * Updates the location hash to reflect the current state of the application. 143 * Updates the location hash to reflect the current state of the application.
144 */ 144 */
145 function updateHash() { 145 function updateHash() {
146 window.location.hash = tree.selectedItem.bookmarkId; 146 window.location.hash = tree.selectedItem.bookmarkId;
147 updateAllCommands();
147 } 148 }
148 149
149 /** 150 /**
150 * Navigates to a bookmark ID. 151 * Navigates to a bookmark ID.
151 * @param {string} id The ID to navigate to. 152 * @param {string} id The ID to navigate to.
152 * @param {function()} callback Function called when list view loaded or 153 * @param {function()} callback Function called when list view loaded or
153 * displayed specified folder. 154 * displayed specified folder.
154 */ 155 */
155 function navigateTo(id, callback) { 156 function navigateTo(id, callback) {
156 updateHash(id); 157 updateHash(id);
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 return urlsPromise; 353 return urlsPromise;
353 } 354 }
354 355
355 /** 356 /**
356 * Returns the nodes (non recursive) to use for the open commands. 357 * Returns the nodes (non recursive) to use for the open commands.
357 * @param {HTMLElement} target . 358 * @param {HTMLElement} target .
358 * @return {Array.<BookmarkTreeNode>} . 359 * @return {Array.<BookmarkTreeNode>} .
359 */ 360 */
360 function getNodesForOpen(target) { 361 function getNodesForOpen(target) {
361 if (target == tree) { 362 if (target == tree) {
362 var folderItem = tree.selectedItem; 363 if (tree.selectedItem != searchTreeItem)
363 return folderItem == searchTreeItem ? 364 return tree.selectedFolders;
364 list.dataModel.slice() : tree.selectedFolders; 365 // Fall through to use all nodes in the list.
366 } else {
367 var items = list.selectedItems;
368 if (items.length)
369 return items;
365 } 370 }
366 var items = list.selectedItems; 371
367 return items.length ? items : list.dataModel.slice(); 372 // The list starts off with a null dataModel. We can get here during startup.
373 if (!list.dataModel)
374 return [];
375
376 // Return an array based on the dataModel.
377 return list.dataModel.slice();
368 } 378 }
369 379
370 /** 380 /**
371 * Returns a promise that will contain all URLs of all the selected bookmarks 381 * Returns a promise that will contain all URLs of all the selected bookmarks
372 * and the nested bookmarks for use with the open commands. 382 * and the nested bookmarks for use with the open commands.
373 * @param {HTMLElement} target The target list or tree. 383 * @param {HTMLElement} target The target list or tree.
374 * @return {Promise} . 384 * @return {Promise} .
375 */ 385 */
376 function getUrlsForOpenCommands(target) { 386 function getUrlsForOpenCommands(target) {
377 return getAllUrls(getNodesForOpen(target)); 387 return getAllUrls(getNodesForOpen(target));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 var command = e.command; 450 var command = e.command;
441 switch (command.id) { 451 switch (command.id) {
442 case 'import-menu-command': 452 case 'import-menu-command':
443 e.canExecute = canEdit; 453 e.canExecute = canEdit;
444 break; 454 break;
445 case 'export-menu-command': 455 case 'export-menu-command':
446 // We can always execute the export-menu command. 456 // We can always execute the export-menu command.
447 e.canExecute = true; 457 e.canExecute = true;
448 break; 458 break;
449 case 'sort-command': 459 case 'sort-command':
450 e.canExecute = !list.isSearch() && list.dataModel.length > 1; 460 e.canExecute = !list.isSearch() &&
461 list.dataModel && list.dataModel.length > 1;
451 break; 462 break;
452 case 'undo-command': 463 case 'undo-command':
453 // The global undo command has no visible UI, so always enable it, and 464 // The global undo command has no visible UI, so always enable it, and
454 // just make it a no-op if undo is not possible. 465 // just make it a no-op if undo is not possible.
455 e.canExecute = true; 466 e.canExecute = true;
456 break; 467 break;
457 default: 468 default:
458 canExecuteForList(e); 469 canExecuteForList(e);
459 break; 470 break;
460 } 471 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 case 'copy-command': 639 case 'copy-command':
629 e.canExecute = hasSelected() && !isTopLevelItem(); 640 e.canExecute = hasSelected() && !isTopLevelItem();
630 break; 641 break;
631 642
632 default: 643 default:
633 canExecuteShared(e, isSearch()); 644 canExecuteShared(e, isSearch());
634 } 645 }
635 } 646 }
636 647
637 /** 648 /**
638 * Update the canExecute state of the commands when the selection changes. 649 * Update the canExecute state of all the commands.
639 * @param {Event} e The change event object.
640 */ 650 */
641 function updateCommandsBasedOnSelection(e) { 651 function updateAllCommands() {
642 if (e.target == document.activeElement) { 652 var commands = document.querySelectorAll('command');
643 // Paste only needs to be updated when the tree selection changes. 653 for (var i = 0; i < commands.length; i++) {
644 var commandNames = ['copy', 'cut', 'delete', 'rename-folder', 'edit', 654 commands[i].canExecuteChange();
645 'add-new-bookmark', 'new-folder', 'open-in-new-tab',
646 'open-in-background-tab', 'open-in-new-window', 'open-incognito-window',
647 'open-in-same-window', 'show-in-folder'];
648
649 if (e.target == tree) {
650 commandNames.push('paste-from-context-menu', 'paste-from-organize-menu',
651 'sort');
652 }
653
654 commandNames.forEach(function(baseId) {
655 $(baseId + '-command').canExecuteChange();
656 });
657 } 655 }
658 } 656 }
659 657
660 function updateEditingCommands() { 658 function updateEditingCommands() {
661 var editingCommands = ['cut', 'delete', 'rename-folder', 'edit', 659 var editingCommands = ['cut', 'delete', 'rename-folder', 'edit',
662 'add-new-bookmark', 'new-folder', 'sort', 660 'add-new-bookmark', 'new-folder', 'sort',
663 'paste-from-context-menu', 'paste-from-organize-menu']; 661 'paste-from-context-menu', 'paste-from-organize-menu'];
664 662
665 chrome.bookmarkManagerPrivate.canEdit(function(result) { 663 chrome.bookmarkManagerPrivate.canEdit(function(result) {
666 if (result != canEdit) { 664 if (result != canEdit) {
667 canEdit = result; 665 canEdit = result;
668 editingCommands.forEach(function(baseId) { 666 editingCommands.forEach(function(baseId) {
669 $(baseId + '-command').canExecuteChange(); 667 $(baseId + '-command').canExecuteChange();
670 }); 668 });
671 } 669 }
672 }); 670 });
673 } 671 }
674 672
675 function handleChangeForTree(e) { 673 function handleChangeForTree(e) {
676 updateCommandsBasedOnSelection(e); 674 updateAllCommands();
677 navigateTo(tree.selectedItem.bookmarkId, updateHash); 675 navigateTo(tree.selectedItem.bookmarkId, updateHash);
678 } 676 }
679 677
680 function handleOrganizeButtonClick(e) { 678 function handleOrganizeButtonClick(e) {
681 updateEditingCommands(); 679 updateEditingCommands();
682 $('add-new-bookmark-command').canExecuteChange(); 680 $('add-new-bookmark-command').canExecuteChange();
683 $('new-folder-command').canExecuteChange(); 681 $('new-folder-command').canExecuteChange();
684 $('sort-command').canExecuteChange(); 682 $('sort-command').canExecuteChange();
685 } 683 }
686 684
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 bmm.treeLookup[searchTreeItem.bookmarkId] = searchTreeItem; 1220 bmm.treeLookup[searchTreeItem.bookmarkId] = searchTreeItem;
1223 1221
1224 cr.ui.decorate('menu', Menu); 1222 cr.ui.decorate('menu', Menu);
1225 cr.ui.decorate('button[menu]', MenuButton); 1223 cr.ui.decorate('button[menu]', MenuButton);
1226 cr.ui.decorate('command', Command); 1224 cr.ui.decorate('command', Command);
1227 BookmarkList.decorate(list); 1225 BookmarkList.decorate(list);
1228 BookmarkTree.decorate(tree); 1226 BookmarkTree.decorate(tree);
1229 1227
1230 list.addEventListener('canceledit', handleCancelEdit); 1228 list.addEventListener('canceledit', handleCancelEdit);
1231 list.addEventListener('canExecute', handleCanExecuteForList); 1229 list.addEventListener('canExecute', handleCanExecuteForList);
1232 list.addEventListener('change', updateCommandsBasedOnSelection); 1230 list.addEventListener('change', updateAllCommands);
1233 list.addEventListener('contextmenu', updateEditingCommands); 1231 list.addEventListener('contextmenu', updateEditingCommands);
1234 list.addEventListener('dblclick', handleDoubleClickForList); 1232 list.addEventListener('dblclick', handleDoubleClickForList);
1235 list.addEventListener('edit', handleEdit); 1233 list.addEventListener('edit', handleEdit);
1236 list.addEventListener('rename', handleRename); 1234 list.addEventListener('rename', handleRename);
1237 list.addEventListener('urlClicked', handleUrlClickedForList); 1235 list.addEventListener('urlClicked', handleUrlClickedForList);
1238 1236
1239 tree.addEventListener('canExecute', handleCanExecuteForTree); 1237 tree.addEventListener('canExecute', handleCanExecuteForTree);
1240 tree.addEventListener('change', handleChangeForTree); 1238 tree.addEventListener('change', handleChangeForTree);
1241 tree.addEventListener('contextmenu', updateEditingCommands); 1239 tree.addEventListener('contextmenu', updateEditingCommands);
1242 tree.addEventListener('rename', handleRename); 1240 tree.addEventListener('rename', handleRename);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1301 1299
1302 cr.ui.FocusOutlineManager.forDocument(document); 1300 cr.ui.FocusOutlineManager.forDocument(document);
1303 initializeSplitter(); 1301 initializeSplitter();
1304 bmm.addBookmarkModelListeners(); 1302 bmm.addBookmarkModelListeners();
1305 dnd.init(selectItemsAfterUserAction); 1303 dnd.init(selectItemsAfterUserAction);
1306 tree.reload(); 1304 tree.reload();
1307 } 1305 }
1308 1306
1309 initializeBookmarkManager(); 1307 initializeBookmarkManager();
1310 })(); 1308 })();
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