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

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

Issue 8497008: Implement Bookmark All Tabs Dialog with WebUI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review fix Created 9 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 const BookmarkTree = bmm.BookmarkTree;
6 const TreeItem = cr.ui.TreeItem;
7
8 // Delay time (ms) of updating hash after changing current folder.
9 const NAVIGATE_HASH_UPDATE_DELAY = 250;
10 // Delay time (ms) of selecting new folder after creation of it.
11 const NEWFOLDER_SELECTION_DELAY = 50;
12
13 // Sometimes the extension API is not initialized.
14 if (!chrome.bookmarks)
15 console.error('Bookmarks extension API is not available');
16
17 cr.enablePlatformSpecificCSSRules();
18
19 /**
20 * The local strings object which is used to do the translation.
21 * @type {!LocalStrings}
22 */
23 var localStrings = new LocalStrings;
24
25 // Get the localized strings from the backend.
26 chrome.experimental.bookmarkManager.getStrings(function(data) {
27 // The strings may contain & which we need to strip.
28 for (var key in data) {
29 data[key] = data[key].replace(/&/, '');
30 }
31
32 localStrings.templateData = data;
33 i18nTemplate.process(document, data);
34 });
35
36 /**
37 * The id of the bookmark root.
38 * @type {number}
39 */
40 const ROOT_ID = '0';
41
42 var searchTreeItem = new TreeItem({
43 icon: 'images/bookmark_manager_search.png',
44 bookmarkId: 'q='
45 });
46 bmm.treeLookup[searchTreeItem.bookmarkId] = searchTreeItem;
47
48 var recentTreeItem = new TreeItem({
49 icon: 'images/bookmark_manager_recent.png',
50 bookmarkId: 'recent'
51 });
52 bmm.treeLookup[recentTreeItem.bookmarkId] = recentTreeItem;
53
54 BookmarkTree.decorate(tree);
55 tree.reload();
56 bmm.addBookmarkModelListeners();
57
58 tree.addEventListener('change', function() {
59 if (tree.selectedItem)
60 navigateTo(tree.selectedItem.bookmarkId);
61 });
62
63 /**
64 * Add an event listener to a node that will remove itself after firing once.
65 * @param {!Element} node The DOM node to add the listener to.
66 * @param {string} name The name of the event listener to add to.
67 * @param {function(Event)} handler Function called when the event fires.
68 */
69 function addOneShotEventListener(node, name, handler) {
70 var f = function(e) {
71 handler(e);
72 node.removeEventListener(name, f);
73 };
74 node.addEventListener(name, f);
75 }
76
77 /**
78 * Updates the location hash to reflect the current state of the application.
79 */
80 function updateHash() {
81 window.location.hash = tree.selectedItem.bookmarkId;
82 }
83
84 /**
85 * Navigates to a bookmark ID.
86 * @param {string} id The ID to navigate to.
87 * @param {boolean=} opt_updateHashNow Whether to immediately update the
88 * location.hash. If false then it is updated in a timeout.
89 */
90 function navigateTo(id, opt_updateHashNow) {
91 // Update the location hash using a timer to prevent reentrancy. This is how
92 // often we add history entries and the time here is a bit arbitrary but was
93 // picked as the smallest time a human perceives as instant.
94
95 clearTimeout(navigateTo.timer_);
96 if (opt_updateHashNow)
97 updateHash();
98 else
99 navigateTo.timer_ = setTimeout(updateHash, NAVIGATE_HASH_UPDATE_DELAY);
100
101 updateParentId(id);
102 }
103
104 /**
105 * Checks disabilities of the buttons.
106 */
107 function checkDisabilities() {
108 var selected = tree.selectedItem;
109 var newNameText = document.querySelector('#new-name').value;
110
111 document.querySelector('#new-folder').disabled = !selected;
112 document.querySelector('#save').disabled = !selected || !newNameText;
113 }
114 document.querySelector('#new-name')
115 .addEventListener('input', checkDisabilities);
116 tree.addEventListener('change', checkDisabilities);
117 checkDisabilities();
118
119 /**
120 * Callback for the new folder command. This creates a new folder and starts
121 * a rename of it.
122 */
123 function newFolder() {
124 var parentId = tree.selectedItem.bookmarkId;
125 chrome.bookmarks.create({
126 title: localStrings.getString('new_folder_name'),
127 parentId: parentId
128 }, function(newNode) {
129 // This callback happens before the event that triggers the tree/list to
130 // get updated so delay the work so that the tree/list gets updated first.
131 setTimeout(function() {
132 var newItem = bmm.treeLookup[newNode.id];
133 tree.selectedItem = newItem;
134 newItem.editing = true;
135 }, NEWFOLDER_SELECTION_DELAY);
136 });
137 }
138
139 /**
140 * Close window without bookmark all tabs.
141 */
142 function cancel() {
143 window.close();
144 }
145
146 /**
147 * Updates the parent ID of the bookmark list and selects the correct tree item.
148 * @param {string} id The id.
149 */
150 function updateParentId(id) {
151 if (id in bmm.treeLookup)
152 tree.selectedItem = bmm.treeLookup[id];
153 }
154
155 /**
156 * Stores the information of all the tabs when window opens.
157 * @type {array of tab}
158 */
159 var allTabs = {};
160 chrome.windows.getCurrent(function (win) {
161 chrome.tabs.getAllInWindow(win.id, function (tabs) {
162 allTabs = tabs;
163 });
164 });
165
166 /**
167 * Creates a folder and Adds tabs as bookmarks under the folder.
168 */
169 function bookmarkAllTabs() {
170 var parentBookmark = tree.selectedItem;
171 var newNameTextbox = document.querySelector('#new-name');
172
173 if (!parentBookmark || !newNameTextbox)
174 return;
175
176 var parentId = parentBookmark.bookmarkId;
177 var newFolderName = newNameTextbox.value;
178
179 if (parentId == 0 || !newFolderName)
180 return;
181
182 chrome.bookmarks.create({
183 title: newFolderName,
184 parentId: parentId.toString()
185 }, function(newNode) {
186 for (var i in allTabs) {
187 var tab = allTabs[i];
188 chrome.bookmarks.create({'parentId': newNode.id.toString(),
189 'title': tab.title,
190 'url': tab.url});
191 }
192 window.close();
193 });
194 }
195
196 /**
197 * Adds handlers to buttons.
198 */
199 document.querySelector('#new-folder').addEventListener('click', newFolder);
200 document.querySelector('#save').addEventListener('click', bookmarkAllTabs);
201 document.querySelector('#cancel').addEventListener('click', cancel);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698