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
3 // found in the LICENSE file.
6 /** @const */ var BookmarkList
= bmm
.BookmarkList
;
7 /** @const */ var BookmarkTree
= bmm
.BookmarkTree
;
8 /** @const */ var Command
= cr
.ui
.Command
;
9 /** @const */ var CommandBinding
= cr
.ui
.CommandBinding
;
10 /** @const */ var LinkKind
= cr
.LinkKind
;
11 /** @const */ var ListItem
= cr
.ui
.ListItem
;
12 /** @const */ var Menu
= cr
.ui
.Menu
;
13 /** @const */ var MenuButton
= cr
.ui
.MenuButton
;
14 /** @const */ var Promise
= cr
.Promise
;
15 /** @const */ var Splitter
= cr
.ui
.Splitter
;
16 /** @const */ var TreeItem
= cr
.ui
.TreeItem
;
19 * An array containing the BookmarkTreeNodes that were deleted in the last
20 * deletion action. This is used for implementing undo.
21 * @type {Array.<BookmarkTreeNode>}
27 * Holds the last DOMTimeStamp when mouse pointer hovers on folder in tree
28 * view. Zero means pointer doesn't hover on folder.
31 var lastHoverOnFolderTimeStamp
= 0;
34 * Holds a function that will undo that last action, if global undo is enabled.
37 var performGlobalUndo
;
40 * Holds a link controller singleton. Use getLinkController() rarther than
41 * accessing this variabie.
42 * @type {LinkController}
47 * New Windows are not allowed in Windows 8 metro mode.
49 var canOpenNewWindows
= true;
52 * Incognito mode availability can take the following values: ,
53 * - 'enabled' for when both normal and incognito modes are available;
54 * - 'disabled' for when incognito mode is disabled;
55 * - 'forced' for when incognito mode is forced (normal mode is unavailable).
57 var incognitoModeAvailability
= 'enabled';
60 * Whether bookmarks can be modified.
69 var searchTreeItem
= new TreeItem({
74 * Command shortcut mapping.
77 var commandShortcutMap
= cr
.isMac
? {
79 // On Mac we also allow Meta+Backspace.
80 'delete': 'U+007F U+0008 Meta-U+0008',
81 'open-in-background-tab': 'Meta-Enter',
82 'open-in-new-tab': 'Shift-Meta-Enter',
83 'open-in-same-window': 'Meta-Down',
84 'open-in-new-window': 'Shift-Enter',
85 'rename-folder': 'Enter',
86 // Global undo is Command-Z. It is not in any menu.
87 'undo': 'Meta-U+005A',
91 'open-in-background-tab': 'Ctrl-Enter',
92 'open-in-new-tab': 'Shift-Ctrl-Enter',
93 'open-in-same-window': 'Enter',
94 'open-in-new-window': 'Shift-Enter',
95 'rename-folder': 'F2',
96 // Global undo is Ctrl-Z. It is not in any menu.
97 'undo': 'Ctrl-U+005A',
101 * Mapping for folder id to suffix of UMA. These names will be appeared
102 * after "BookmarkManager_NavigateTo_" in UMA dashboard.
105 var folderMetricsNameMap
= {
110 'subfolder': 'SubFolder',
114 * Adds an event listener to a node that will remove itself after firing once.
115 * @param {!Element} node The DOM node to add the listener to.
116 * @param {string} name The name of the event listener to add to.
117 * @param {function(Event)} handler Function called when the event fires.
119 function addOneShotEventListener(node
, name
, handler
) {
120 var f = function(e
) {
122 node
.removeEventListener(name
, f
);
124 node
.addEventListener(name
, f
);
127 // Get the localized strings from the backend via bookmakrManagerPrivate API.
128 function loadLocalizedStrings(data
) {
129 // The strings may contain & which we need to strip.
130 for (var key
in data
) {
131 data
[key
] = data
[key
].replace(/&/, '');
134 loadTimeData
.data
= data
;
135 i18nTemplate
.process(document
, loadTimeData
);
137 searchTreeItem
.label
= loadTimeData
.getString('search');
138 searchTreeItem
.icon
= isRTL() ? 'images/bookmark_manager_search_rtl.png' :
139 'images/bookmark_manager_search.png';
143 * Updates the location hash to reflect the current state of the application.
145 function updateHash() {
146 window
.location
.hash
= tree
.selectedItem
.bookmarkId
;
151 * Navigates to a bookmark ID.
152 * @param {string} id The ID to navigate to.
153 * @param {function()} callback Function called when list view loaded or
154 * displayed specified folder.
156 function navigateTo(id
, callback
) {
159 if (list
.parentId
== id
) {
164 var metricsId
= folderMetricsNameMap
[id
.replace(/^q=.*/, 'q=')] ||
165 folderMetricsNameMap
['subfolder'];
166 chrome
.metricsPrivate
.recordUserAction(
167 'BookmarkManager_NavigateTo_' + metricsId
);
169 addOneShotEventListener(list
, 'load', callback
);
174 * Updates the parent ID of the bookmark list and selects the correct tree item.
175 * @param {string} id The id.
177 function updateParentId(id
) {
178 // Setting list.parentId fires 'load' event.
181 // When tree.selectedItem changed, tree view calls navigatTo() then it
182 // calls updateHash() when list view displayed specified folder.
183 tree
.selectedItem
= bmm
.treeLookup
[id
] || tree
.selectedItem
;
186 // Process the location hash. This is called by onhashchange and when the page
188 function processHash() {
189 var id
= window
.location
.hash
.slice(1);
191 // If we do not have a hash, select first item in the tree.
192 id
= tree
.items
[0].bookmarkId
;
196 if (/^e=/.test(id
)) {
199 // If hash contains e=, edit the item specified.
200 chrome
.bookmarks
.get(id
, function(bookmarkNodes
) {
201 // Verify the node to edit is a valid node.
202 if (!bookmarkNodes
|| bookmarkNodes
.length
!= 1)
204 var bookmarkNode
= bookmarkNodes
[0];
206 // After the list reloads, edit the desired bookmark.
207 var editBookmark = function(e
) {
208 var index
= list
.dataModel
.findIndexById(bookmarkNode
.id
);
210 var sm
= list
.selectionModel
;
211 sm
.anchorIndex
= sm
.leadIndex
= sm
.selectedIndex
= index
;
212 scrollIntoViewAndMakeEditable(index
);
216 navigateTo(bookmarkNode
.parentId
, editBookmark
);
219 // We handle the two cases of navigating to the bookmark to be edited
220 // above. Don't run the standard navigation code below.
222 } else if (/^q=/.test(id
)) {
223 // In case we got a search hash, update the text input and the
224 // bmm.treeLookup to use the new id.
225 setSearch(id
.slice(2));
229 // Navigate to bookmark 'id' (which may be a query of the form q=query).
233 // We need to verify that this is a correct ID.
234 chrome
.bookmarks
.get(id
, function(items
) {
235 if (items
&& items
.length
== 1)
241 // Activate is handled by the open-in-same-window-command.
242 function handleDoubleClickForList(e
) {
244 $('open-in-same-window-command').execute();
247 // The list dispatches an event when the user clicks on the URL or the Show in
249 function handleUrlClickedForList(e
) {
250 getLinkController().openUrlFromEvent(e
.url
, e
.originalEvent
);
251 chrome
.bookmarkManagerPrivate
.recordLaunch();
254 function handleSearch(e
) {
255 setSearch(this.value
);
259 * Navigates to the search results for the search text.
260 * @param {string} searchText The text to search for.
262 function setSearch(searchText
) {
264 // Only update search item if we have a search term. We never want the
265 // search item to be for an empty search.
266 delete bmm
.treeLookup
[searchTreeItem
.bookmarkId
];
267 var id
= searchTreeItem
.bookmarkId
= 'q=' + searchText
;
268 bmm
.treeLookup
[searchTreeItem
.bookmarkId
] = searchTreeItem
;
271 var input
= $('term');
272 // Do not update the input if the user is actively using the text input.
273 if (document
.activeElement
!= input
)
274 input
.value
= searchText
;
277 tree
.add(searchTreeItem
);
278 tree
.selectedItem
= searchTreeItem
;
281 tree
.selectedItem
= tree
.items
[0];
282 id
= tree
.selectedItem
.bookmarkId
;
285 // Navigate now and update hash immediately.
286 navigateTo(id
, updateHash
);
289 // Handle the logo button UI.
290 // When the user clicks the button we should navigate "home" and focus the list.
291 function handleClickOnLogoButton(e
) {
297 * This returns the user visible path to the folder where the bookmark is
299 * @param {number} parentId The ID of the parent folder.
300 * @return {string} The path to the the bookmark,
302 function getFolder(parentId
) {
303 var parentNode
= tree
.getBookmarkNodeById(parentId
);
305 var s
= parentNode
.title
;
306 if (parentNode
.parentId
!= bmm
.ROOT_ID
) {
307 return getFolder(parentNode
.parentId
) + '/' + s
;
313 function handleLoadForTree(e
) {
317 function getAllUrls(nodes
) {
320 // Adds the node and all its direct children.
321 function addNodes(node
) {
322 if (node
.id
== 'new')
326 node
.children
.forEach(function(child
) {
327 if (!bmm
.isFolder(child
))
328 urls
.push(child
.url
);
335 // Get a future promise for the nodes.
336 var promises
= nodes
.map(function(node
) {
337 if (bmm
.isFolder(node
))
338 return bmm
.loadSubtree(node
.id
);
339 // Not a folder so we already have all the data we need.
340 return new Promise(node
);
343 var urlsPromise
= new Promise();
345 var p
= Promise
.all
.apply(null, promises
);
346 p
.addListener(function(nodes
) {
347 nodes
.forEach(function(node
) {
350 urlsPromise
.value
= urls
;
357 * Returns the nodes (non recursive) to use for the open commands.
358 * @param {HTMLElement} target .
359 * @return {Array.<BookmarkTreeNode>} .
361 function getNodesForOpen(target
) {
362 if (target
== tree
) {
363 if (tree
.selectedItem
!= searchTreeItem
)
364 return tree
.selectedFolders
;
365 // Fall through to use all nodes in the list.
367 var items
= list
.selectedItems
;
372 // The list starts off with a null dataModel. We can get here during startup.
376 // Return an array based on the dataModel.
377 return list
.dataModel
.slice();
381 * Returns a promise that will contain all URLs of all the selected bookmarks
382 * and the nested bookmarks for use with the open commands.
383 * @param {HTMLElement} target The target list or tree.
384 * @return {Promise} .
386 function getUrlsForOpenCommands(target
) {
387 return getAllUrls(getNodesForOpen(target
));
390 function notNewNode(node
) {
391 return node
.id
!= 'new';
395 * Helper function that updates the canExecute and labels for the open-like
397 * @param {!cr.ui.CanExecuteEvent} e The event fired by the command system.
398 * @param {!cr.ui.Command} command The command we are currently processing.
399 * @param {string} singularId The string id of singular form of the menu label.
400 * @param {string} pluralId The string id of menu label if the singular form is
402 * @param {boolean} commandDisabled Whether the menu item should be disabled
403 no matter what bookmarks are selected.
405 function updateOpenCommand(e
, command
, singularId
, pluralId
, commandDisabled
) {
407 // The command label reflects the selection which might not reflect
408 // how many bookmarks will be opened. For example if you right click an
409 // empty area in a folder with 1 bookmark the text should still say "all".
410 var selectedNodes
= getSelectedBookmarkNodes(e
.target
).filter(notNewNode
);
411 var singular
= selectedNodes
.length
== 1 && !bmm
.isFolder(selectedNodes
[0]);
412 command
.label
= loadTimeData
.getString(singular
? singularId
: pluralId
);
415 if (commandDisabled
) {
416 command
.disabled
= true;
417 e
.canExecute
= false;
421 getUrlsForOpenCommands(e
.target
).addListener(function(urls
) {
422 var disabled
= !urls
.length
;
423 command
.disabled
= disabled
;
424 e
.canExecute
= !disabled
;
429 * Calls the backend to figure out if we can paste the clipboard into the active
431 * @param {Function=} opt_f Function to call after the state has been updated.
433 function updatePasteCommand(opt_f
) {
434 function update(canPaste
) {
435 var organizeMenuCommand
= $('paste-from-organize-menu-command');
436 var contextMenuCommand
= $('paste-from-context-menu-command');
437 organizeMenuCommand
.disabled
= !canPaste
;
438 contextMenuCommand
.disabled
= !canPaste
;
442 // We cannot paste into search view.
446 chrome
.bookmarkManagerPrivate
.canPaste(list
.parentId
, update
);
449 function handleCanExecuteForDocument(e
) {
450 var command
= e
.command
;
451 switch (command
.id
) {
452 case 'import-menu-command':
453 e
.canExecute
= canEdit
;
455 case 'export-menu-command':
456 // We can always execute the export-menu command.
460 e
.canExecute
= !list
.isSearch() &&
461 list
.dataModel
&& list
.dataModel
.length
> 1;
464 // The global undo command has no visible UI, so always enable it, and
465 // just make it a no-op if undo is not possible.
469 canExecuteForList(e
);
475 * Helper function for handling canExecute for the list and the tree.
476 * @param {!Event} e Can execute event object.
477 * @param {boolean} isSearch Whether the user is trying to do a command on
480 function canExecuteShared(e
, isSearch
) {
481 var command
= e
.command
;
482 var commandId
= command
.id
;
484 case 'paste-from-organize-menu-command':
485 case 'paste-from-context-menu-command':
486 updatePasteCommand();
489 case 'add-new-bookmark-command':
490 case 'new-folder-command':
491 e
.canExecute
= !isSearch
&& canEdit
;
494 case 'open-in-new-tab-command':
495 updateOpenCommand(e
, command
, 'open_in_new_tab', 'open_all', false);
497 case 'open-in-background-tab-command':
498 updateOpenCommand(e
, command
, '', '', false);
500 case 'open-in-new-window-command':
501 updateOpenCommand(e
, command
,
502 'open_in_new_window', 'open_all_new_window',
503 // Disabled when incognito is forced.
504 incognitoModeAvailability
== 'forced' || !canOpenNewWindows
);
506 case 'open-incognito-window-command':
507 updateOpenCommand(e
, command
,
508 'open_incognito', 'open_all_incognito',
509 // Not available when incognito is disabled.
510 incognitoModeAvailability
== 'disabled');
513 case 'undo-delete-command':
514 e
.canExecute
= !!lastDeletedNodes
;
520 * Helper function for handling canExecute for the list and document.
521 * @param {!Event} e Can execute event object.
523 function canExecuteForList(e
) {
524 var command
= e
.command
;
525 var commandId
= command
.id
;
527 function hasSelected() {
528 return !!list
.selectedItem
;
531 function hasSingleSelected() {
532 return list
.selectedItems
.length
== 1;
535 function canCopyItem(item
) {
536 return item
.id
!= 'new';
539 function canCopyItems() {
540 var selectedItems
= list
.selectedItems
;
541 return selectedItems
&& selectedItems
.some(canCopyItem
);
544 function isSearch() {
545 return list
.isSearch();
549 case 'rename-folder-command':
550 // Show rename if a single folder is selected.
551 var items
= list
.selectedItems
;
552 if (items
.length
!= 1) {
553 e
.canExecute
= false;
554 command
.hidden
= true;
556 var isFolder
= bmm
.isFolder(items
[0]);
557 e
.canExecute
= isFolder
&& canEdit
;
558 command
.hidden
= !isFolder
;
563 // Show the edit command if not a folder.
564 var items
= list
.selectedItems
;
565 if (items
.length
!= 1) {
566 e
.canExecute
= false;
567 command
.hidden
= false;
569 var isFolder
= bmm
.isFolder(items
[0]);
570 e
.canExecute
= !isFolder
&& canEdit
;
571 command
.hidden
= isFolder
;
575 case 'show-in-folder-command':
576 e
.canExecute
= isSearch() && hasSingleSelected();
579 case 'delete-command':
581 e
.canExecute
= canCopyItems() && canEdit
;
585 e
.canExecute
= canCopyItems();
588 case 'open-in-same-window-command':
589 e
.canExecute
= hasSelected();
593 canExecuteShared(e
, isSearch());
597 // Update canExecute for the commands when the list is the active element.
598 function handleCanExecuteForList(e
) {
599 if (e
.target
!= list
) return;
600 canExecuteForList(e
);
603 // Update canExecute for the commands when the tree is the active element.
604 function handleCanExecuteForTree(e
) {
605 if (e
.target
!= tree
) return;
607 var command
= e
.command
;
608 var commandId
= command
.id
;
610 function hasSelected() {
611 return !!e
.target
.selectedItem
;
614 function isSearch() {
615 var item
= e
.target
.selectedItem
;
616 return item
== searchTreeItem
;
619 function isTopLevelItem() {
620 return e
.target
.selectedItem
.parentNode
== tree
;
624 case 'rename-folder-command':
625 command
.hidden
= false;
626 e
.canExecute
= hasSelected() && !isTopLevelItem() && canEdit
;
630 command
.hidden
= true;
631 e
.canExecute
= false;
634 case 'delete-command':
636 e
.canExecute
= hasSelected() && !isTopLevelItem() && canEdit
;
640 e
.canExecute
= hasSelected() && !isTopLevelItem();
644 canExecuteShared(e
, isSearch());
649 * Update the canExecute state of all the commands.
651 function updateAllCommands() {
652 var commands
= document
.querySelectorAll('command');
653 for (var i
= 0; i
< commands
.length
; i
++) {
654 commands
[i
].canExecuteChange();
658 function updateEditingCommands() {
659 var editingCommands
= ['cut', 'delete', 'rename-folder', 'edit',
660 'add-new-bookmark', 'new-folder', 'sort',
661 'paste-from-context-menu', 'paste-from-organize-menu'];
663 chrome
.bookmarkManagerPrivate
.canEdit(function(result
) {
664 if (result
!= canEdit
) {
666 editingCommands
.forEach(function(baseId
) {
667 $(baseId
+ '-command').canExecuteChange();
673 function handleChangeForTree(e
) {
675 navigateTo(tree
.selectedItem
.bookmarkId
, updateHash
);
678 function handleOrganizeButtonClick(e
) {
679 updateEditingCommands();
680 $('add-new-bookmark-command').canExecuteChange();
681 $('new-folder-command').canExecuteChange();
682 $('sort-command').canExecuteChange();
685 function handleRename(e
) {
687 var bookmarkNode
= item
.bookmarkNode
;
688 chrome
.bookmarks
.update(bookmarkNode
.id
, {title
: item
.label
});
689 performGlobalUndo
= null; // This can't be undone, so disable global undo.
692 function handleEdit(e
) {
694 var bookmarkNode
= item
.bookmarkNode
;
696 title
: bookmarkNode
.title
698 if (!bmm
.isFolder(bookmarkNode
))
699 context
.url
= bookmarkNode
.url
;
701 if (bookmarkNode
.id
== 'new') {
702 selectItemsAfterUserAction(list
);
705 context
.parentId
= bookmarkNode
.parentId
;
706 chrome
.bookmarks
.create(context
, function(node
) {
707 // A new node was created and will get added to the list due to the
709 var dataModel
= list
.dataModel
;
710 var index
= dataModel
.indexOf(bookmarkNode
);
711 dataModel
.splice(index
, 1);
714 var newIndex
= dataModel
.findIndexById(node
.id
);
715 if (newIndex
!= -1) {
716 var sm
= list
.selectionModel
;
717 list
.scrollIndexIntoView(newIndex
);
718 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= newIndex
;
723 chrome
.bookmarks
.update(bookmarkNode
.id
, context
);
725 performGlobalUndo
= null; // This can't be undone, so disable global undo.
728 function handleCancelEdit(e
) {
730 var bookmarkNode
= item
.bookmarkNode
;
731 if (bookmarkNode
.id
== 'new') {
732 var dataModel
= list
.dataModel
;
733 var index
= dataModel
.findIndexById('new');
734 dataModel
.splice(index
, 1);
739 * Navigates to the folder that the selected item is in and selects it. This is
740 * used for the show-in-folder command.
742 function showInFolder() {
743 var bookmarkNode
= list
.selectedItem
;
746 var parentId
= bookmarkNode
.parentId
;
748 // After the list is loaded we should select the revealed item.
749 function selectItem() {
750 var index
= list
.dataModel
.findIndexById(bookmarkNode
.id
);
753 var sm
= list
.selectionModel
;
754 sm
.anchorIndex
= sm
.leadIndex
= sm
.selectedIndex
= index
;
755 list
.scrollIndexIntoView(index
);
758 var treeItem
= bmm
.treeLookup
[parentId
];
761 navigateTo(parentId
, selectItem
);
765 * @return {!cr.LinkController} The link controller used to open links based on
766 * user clicks and keyboard actions.
768 function getLinkController() {
769 return linkController
||
770 (linkController
= new cr
.LinkController(loadTimeData
));
774 * Returns the selected bookmark nodes of the provided tree or list.
775 * If |opt_target| is not provided or null the active element is used.
776 * Only call this if the list or the tree is focused.
777 * @param {BookmarkList|BookmarkTree} opt_target The target list or tree.
778 * @return {!Array} Array of bookmark nodes.
780 function getSelectedBookmarkNodes(opt_target
) {
781 return (opt_target
|| document
.activeElement
) == tree
?
782 tree
.selectedFolders
: list
.selectedItems
;
786 * @return {!Array.<string>} An array of the selected bookmark IDs.
788 function getSelectedBookmarkIds() {
789 var selectedNodes
= getSelectedBookmarkNodes();
790 selectedNodes
.sort(function(a
, b
) { return a
.index
- b
.index
});
791 return selectedNodes
.map(function(node
) {
797 * Opens the selected bookmarks.
798 * @param {LinkKind} kind The kind of link we want to open.
799 * @param {HTMLElement} opt_eventTarget The target of the user initiated event.
801 function openBookmarks(kind
, opt_eventTarget
) {
802 // If we have selected any folders, we need to find all the bookmarks one
803 // level down. We use multiple async calls to getSubtree instead of getting
804 // the whole tree since we would like to minimize the amount of data sent.
806 var urlsP
= getUrlsForOpenCommands(opt_eventTarget
);
807 urlsP
.addListener(function(urls
) {
808 getLinkController().openUrls(urls
, kind
);
809 chrome
.bookmarkManagerPrivate
.recordLaunch();
814 * Opens an item in the list.
816 function openItem() {
817 var bookmarkNodes
= getSelectedBookmarkNodes();
818 // If we double clicked or pressed enter on a single folder, navigate to it.
819 if (bookmarkNodes
.length
== 1 && bmm
.isFolder(bookmarkNodes
[0])) {
820 navigateTo(bookmarkNodes
[0].id
, updateHash
);
822 openBookmarks(LinkKind
.FOREGROUND_TAB
);
827 * Deletes the selected bookmarks. The bookmarks are saved in memory in case
828 * the user needs to undo the deletion.
830 function deleteBookmarks() {
831 var selectedIds
= getSelectedBookmarkIds();
832 lastDeletedNodes
= [];
834 function performDelete() {
835 chrome
.bookmarkManagerPrivate
.removeTrees(selectedIds
);
836 $('undo-delete-command').canExecuteChange();
837 performGlobalUndo
= undoDelete
;
840 // First, store information about the bookmarks being deleted.
841 selectedIds
.forEach(function(id
) {
842 chrome
.bookmarks
.getSubTree(id
, function(results
) {
843 lastDeletedNodes
.push(results
);
845 // When all nodes have been saved, perform the deletion.
846 if (lastDeletedNodes
.length
=== selectedIds
.length
)
853 * Restores a tree of bookmarks under a specified folder.
854 * @param {BookmarkTreeNode} node The node to restore.
855 * @param {=string} parentId The ID of the folder to restore under. If not
856 * specified, the original parentId of the node will be used.
858 function restoreTree(node
, parentId
) {
860 parentId
: parentId
|| node
.parentId
,
866 chrome
.bookmarks
.create(bookmarkInfo
, function(result
) {
868 console
.error('Failed to restore bookmark.');
873 // Restore the children using the new ID for this node.
874 node
.children
.forEach(function(child
) {
875 restoreTree(child
, result
.id
);
882 * Restores the last set of bookmarks that was deleted.
884 function undoDelete() {
885 lastDeletedNodes
.forEach(function(arr
) {
886 arr
.forEach(restoreTree
);
888 lastDeletedNodes
= null;
889 $('undo-delete-command').canExecuteChange();
891 // Only a single level of undo is supported, so disable global undo now.
892 performGlobalUndo
= null;
896 * Computes folder for "Add Page" and "Add Folder".
897 * @return {string} The id of folder node where we'll create new page/folder.
899 function computeParentFolderForNewItem() {
900 if (document
.activeElement
== tree
)
901 return list
.parentId
;
902 var selectedItem
= list
.selectedItem
;
903 return selectedItem
&& bmm
.isFolder(selectedItem
) ?
904 selectedItem
.id
: list
.parentId
;
908 * Callback for rename folder and edit command. This starts editing for
911 function editSelectedItem() {
912 if (document
.activeElement
== tree
) {
913 tree
.selectedItem
.editing
= true;
915 var li
= list
.getListItem(list
.selectedItem
);
922 * Callback for the new folder command. This creates a new folder and starts
925 function newFolder() {
926 performGlobalUndo
= null; // This can't be undone, so disable global undo.
928 var parentId
= computeParentFolderForNewItem();
930 // Callback is called after tree and list data model updated.
931 function createFolder(callback
) {
932 chrome
.bookmarks
.create({
933 title
: loadTimeData
.getString('new_folder_name'),
938 if (document
.activeElement
== tree
) {
939 createFolder(function(newNode
) {
940 navigateTo(newNode
.id
, function() {
941 bmm
.treeLookup
[newNode
.id
].editing
= true;
947 function editNewFolderInList() {
948 createFolder(function() {
949 var index
= list
.dataModel
.length
- 1;
950 var sm
= list
.selectionModel
;
951 sm
.anchorIndex
= sm
.leadIndex
= sm
.selectedIndex
= index
;
952 scrollIntoViewAndMakeEditable(index
);
956 navigateTo(parentId
, editNewFolderInList
);
960 * Scrolls the list item into view and makes it editable.
961 * @param {number} index The index of the item to make editable.
963 function scrollIntoViewAndMakeEditable(index
) {
964 list
.scrollIndexIntoView(index
);
965 // onscroll is now dispatched asynchronously so we have to postpone
967 setTimeout(function() {
968 var item
= list
.getListItemByIndex(index
);
975 * Adds a page to the current folder. This is called by the
976 * add-new-bookmark-command handler.
979 var parentId
= computeParentFolderForNewItem();
981 function editNewBookmark() {
988 var dataModel
= list
.dataModel
;
989 var length
= dataModel
.length
;
990 dataModel
.splice(length
, 0, fakeNode
);
991 var sm
= list
.selectionModel
;
992 sm
.anchorIndex
= sm
.leadIndex
= sm
.selectedIndex
= length
;
993 scrollIntoViewAndMakeEditable(length
);
996 navigateTo(parentId
, editNewBookmark
);
1000 * This function is used to select items after a user action such as paste, drop
1002 * @param {BookmarkList|BookmarkTree} target The target of the user action.
1003 * @param {=string} opt_selectedTreeId If provided, then select that tree id.
1005 function selectItemsAfterUserAction(target
, opt_selectedTreeId
) {
1006 // We get one onCreated event per item so we delay the handling until we get
1007 // no more events coming.
1012 function handle(id
, bookmarkNode
) {
1013 clearTimeout(timer
);
1014 if (opt_selectedTreeId
|| list
.parentId
== bookmarkNode
.parentId
)
1016 timer
= setTimeout(handleTimeout
, 50);
1019 function handleTimeout() {
1020 chrome
.bookmarks
.onCreated
.removeListener(handle
);
1021 chrome
.bookmarks
.onMoved
.removeListener(handle
);
1023 if (opt_selectedTreeId
&& ids
.indexOf(opt_selectedTreeId
) != -1) {
1024 var index
= ids
.indexOf(opt_selectedTreeId
);
1025 if (index
!= -1 && opt_selectedTreeId
in bmm
.treeLookup
) {
1026 tree
.selectedItem
= bmm
.treeLookup
[opt_selectedTreeId
];
1028 } else if (target
== list
) {
1029 var dataModel
= list
.dataModel
;
1030 var firstIndex
= dataModel
.findIndexById(ids
[0]);
1031 var lastIndex
= dataModel
.findIndexById(ids
[ids
.length
- 1]);
1032 if (firstIndex
!= -1 && lastIndex
!= -1) {
1033 var selectionModel
= list
.selectionModel
;
1034 selectionModel
.selectedIndex
= -1;
1035 selectionModel
.selectRange(firstIndex
, lastIndex
);
1036 selectionModel
.anchorIndex
= selectionModel
.leadIndex
= lastIndex
;
1041 list
.endBatchUpdates();
1044 list
.startBatchUpdates();
1046 chrome
.bookmarks
.onCreated
.addListener(handle
);
1047 chrome
.bookmarks
.onMoved
.addListener(handle
);
1048 timer
= setTimeout(handleTimeout
, 300);
1052 * Record user action.
1053 * @param {string} name An user action name.
1055 function recordUserAction(name
) {
1056 chrome
.metricsPrivate
.recordUserAction('BookmarkManager_Command_' + name
);
1060 * The currently selected bookmark, based on where the user is clicking.
1061 * @return {string} The ID of the currently selected bookmark (could be from
1062 * tree view or list view).
1064 function getSelectedId() {
1065 if (document
.activeElement
== tree
)
1066 return tree
.selectedItem
.bookmarkId
;
1067 var selectedItem
= list
.selectedItem
;
1068 return selectedItem
&& bmm
.isFolder(selectedItem
) ?
1069 selectedItem
.id
: tree
.selectedItem
.bookmarkId
;
1073 * Pastes the copied/cutted bookmark into the right location depending whether
1074 * if it was called from Organize Menu or from Context Menu.
1075 * @param {string} id The id of the element being pasted from.
1077 function pasteBookmark(id
) {
1078 recordUserAction('Paste');
1079 selectItemsAfterUserAction(list
);
1080 chrome
.bookmarkManagerPrivate
.paste(id
, getSelectedBookmarkIds());
1084 * Handler for the command event. This is used for context menu of list/tree
1085 * and organized menu.
1086 * @param {!Event} e The event object.
1088 function handleCommand(e
) {
1089 var command
= e
.command
;
1090 var commandId
= command
.id
;
1091 switch (commandId
) {
1092 case 'import-menu-command':
1093 recordUserAction('Import');
1094 chrome
.bookmarks
.import();
1096 case 'export-menu-command':
1097 recordUserAction('Export');
1098 chrome
.bookmarks
.export();
1100 case 'undo-command':
1101 if (performGlobalUndo
) {
1102 recordUserAction('UndoGlobal');
1103 performGlobalUndo();
1105 recordUserAction('UndoNone');
1108 case 'show-in-folder-command':
1109 recordUserAction('ShowInFolder');
1112 case 'open-in-new-tab-command':
1113 case 'open-in-background-tab-command':
1114 recordUserAction('OpenInNewTab');
1115 openBookmarks(LinkKind
.BACKGROUND_TAB
, e
.target
);
1117 case 'open-in-new-window-command':
1118 recordUserAction('OpenInNewWindow');
1119 openBookmarks(LinkKind
.WINDOW
, e
.target
);
1121 case 'open-incognito-window-command':
1122 recordUserAction('OpenIncognito');
1123 openBookmarks(LinkKind
.INCOGNITO
, e
.target
);
1125 case 'delete-command':
1126 recordUserAction('Delete');
1129 case 'copy-command':
1130 recordUserAction('Copy');
1131 chrome
.bookmarkManagerPrivate
.copy(getSelectedBookmarkIds(),
1132 updatePasteCommand
);
1135 recordUserAction('Cut');
1136 chrome
.bookmarkManagerPrivate
.cut(getSelectedBookmarkIds(),
1137 updatePasteCommand
);
1139 case 'paste-from-organize-menu-command':
1140 pasteBookmark(list
.parentId
);
1142 case 'paste-from-context-menu-command':
1143 pasteBookmark(getSelectedId());
1145 case 'sort-command':
1146 recordUserAction('Sort');
1147 chrome
.bookmarkManagerPrivate
.sortChildren(list
.parentId
);
1149 case 'rename-folder-command':
1152 case 'edit-command':
1153 recordUserAction('Edit');
1156 case 'new-folder-command':
1157 recordUserAction('NewFolder');
1160 case 'add-new-bookmark-command':
1161 recordUserAction('AddPage');
1164 case 'open-in-same-window-command':
1165 recordUserAction('OpenInSame');
1168 case 'undo-delete-command':
1169 recordUserAction('UndoDelete');
1175 // Execute the copy, cut and paste commands when those events are dispatched by
1176 // the browser. This allows us to rely on the browser to handle the keyboard
1177 // shortcuts for these commands.
1178 function installEventHandlerForCommand(eventName
, commandId
) {
1179 function handle(e
) {
1180 if (document
.activeElement
!= list
&& document
.activeElement
!= tree
)
1182 var command
= $(commandId
);
1183 if (!command
.disabled
) {
1186 e
.preventDefault(); // Prevent the system beep.
1189 if (eventName
== 'paste') {
1190 // Paste is a bit special since we need to do an async call to see if we
1191 // can paste because the paste command might not be up to date.
1192 document
.addEventListener(eventName
, function(e
) {
1193 updatePasteCommand(handle
);
1196 document
.addEventListener(eventName
, handle
);
1200 function initializeSplitter() {
1201 var splitter
= document
.querySelector('.main > .splitter');
1202 Splitter
.decorate(splitter
);
1204 // The splitter persists the size of the left component in the local store.
1205 if ('treeWidth' in localStorage
)
1206 splitter
.previousElementSibling
.style
.width
= localStorage
['treeWidth'];
1208 splitter
.addEventListener('resize', function(e
) {
1209 localStorage
['treeWidth'] = splitter
.previousElementSibling
.style
.width
;
1213 function initializeBookmarkManager() {
1214 // Sometimes the extension API is not initialized.
1215 if (!chrome
.bookmarks
)
1216 console
.error('Bookmarks extension API is not available');
1218 chrome
.bookmarkManagerPrivate
.getStrings(continueInitializeBookmarkManager
);
1221 function continueInitializeBookmarkManager(localizedStrings
) {
1222 loadLocalizedStrings(localizedStrings
);
1224 bmm
.treeLookup
[searchTreeItem
.bookmarkId
] = searchTreeItem
;
1226 cr
.ui
.decorate('menu', Menu
);
1227 cr
.ui
.decorate('button[menu]', MenuButton
);
1228 cr
.ui
.decorate('command', Command
);
1229 BookmarkList
.decorate(list
);
1230 BookmarkTree
.decorate(tree
);
1232 list
.addEventListener('canceledit', handleCancelEdit
);
1233 list
.addEventListener('canExecute', handleCanExecuteForList
);
1234 list
.addEventListener('change', updateAllCommands
);
1235 list
.addEventListener('contextmenu', updateEditingCommands
);
1236 list
.addEventListener('dblclick', handleDoubleClickForList
);
1237 list
.addEventListener('edit', handleEdit
);
1238 list
.addEventListener('rename', handleRename
);
1239 list
.addEventListener('urlClicked', handleUrlClickedForList
);
1241 tree
.addEventListener('canExecute', handleCanExecuteForTree
);
1242 tree
.addEventListener('change', handleChangeForTree
);
1243 tree
.addEventListener('contextmenu', updateEditingCommands
);
1244 tree
.addEventListener('rename', handleRename
);
1245 tree
.addEventListener('load', handleLoadForTree
);
1247 cr
.ui
.contextMenuHandler
.addContextMenuProperty(tree
);
1248 list
.contextMenu
= $('context-menu');
1249 tree
.contextMenu
= $('context-menu');
1251 // We listen to hashchange so that we can update the currently shown folder
1252 // when // the user goes back and forward in the history.
1253 window
.addEventListener('hashchange', processHash
);
1255 document
.querySelector('.header form').onsubmit = function(e
) {
1256 setSearch($('term').value
);
1260 $('term').addEventListener('search', handleSearch
);
1262 document
.querySelector('.summary > button').addEventListener(
1263 'click', handleOrganizeButtonClick
);
1265 document
.querySelector('button.logo').addEventListener(
1266 'click', handleClickOnLogoButton
);
1268 document
.addEventListener('canExecute', handleCanExecuteForDocument
);
1269 document
.addEventListener('command', handleCommand
);
1271 // Listen to copy, cut and paste events and execute the associated commands.
1272 installEventHandlerForCommand('copy', 'copy-command');
1273 installEventHandlerForCommand('cut', 'cut-command');
1274 installEventHandlerForCommand('paste', 'paste-from-organize-menu-command');
1276 // Install shortcuts
1277 for (var name
in commandShortcutMap
) {
1278 $(name
+ '-command').shortcut
= commandShortcutMap
[name
];
1281 // Disable almost all commands at startup.
1282 var commands
= document
.querySelectorAll('command');
1283 for (var i
= 0, command
; command
= commands
[i
]; ++i
) {
1284 if (command
.id
!= 'import-menu-command' &&
1285 command
.id
!= 'export-menu-command') {
1286 command
.disabled
= true;
1290 chrome
.bookmarkManagerPrivate
.canEdit(function(result
) {
1294 chrome
.systemPrivate
.getIncognitoModeAvailability(function(result
) {
1295 // TODO(rustema): propagate policy value to the bookmark manager when it
1297 incognitoModeAvailability
= result
;
1300 chrome
.bookmarkManagerPrivate
.canOpenNewWindows(function(result
) {
1301 canOpenNewWindows
= result
;
1304 cr
.ui
.FocusOutlineManager
.forDocument(document
);
1305 initializeSplitter();
1306 bmm
.addBookmarkModelListeners();
1307 dnd
.init(selectItemsAfterUserAction
);
1311 initializeBookmarkManager();