OLD | NEW |
---|---|
(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 // Sometimes the extension API is not initialized. | |
9 if (!chrome.bookmarks) | |
10 console.error('Bookmarks extension API is not available'); | |
11 | |
12 cr.enablePlatformSpecificCSSRules(); | |
13 | |
14 /** | |
15 * The local strings object which is used to do the translation. | |
16 * @type {!LocalStrings} | |
17 */ | |
18 var localStrings = new LocalStrings; | |
19 | |
20 // Get the localized strings from the backend. | |
21 chrome.experimental.bookmarkManager.getStrings(function(data) { | |
22 // The strings may contain & which we need to strip. | |
23 for (var key in data) { | |
24 data[key] = data[key].replace(/&/, ''); | |
25 } | |
26 | |
27 localStrings.templateData = data; | |
28 i18nTemplate.process(document, data); | |
29 }); | |
30 | |
31 /** | |
32 * The id of the bookmark root. | |
33 * @type {number} | |
34 */ | |
35 const ROOT_ID = '0'; | |
36 | |
37 var canEdit = true; | |
38 chrome.experimental.bookmarkManager.canEdit(function(result) { | |
39 canEdit = result; | |
40 }); | |
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 // console.info('navigateTo', 'from', window.location.hash, 'to', id); | |
mazda
2011/11/09 04:49:43
Please delete this.
yoshiki
2011/11/09 15:06:39
Done.
| |
92 // Update the location hash using a timer to prevent reentrancy. This is how | |
93 // often we add history entries and the time here is a bit arbitrary but was | |
94 // picked as the smallest time a human perceives as instant. | |
95 | |
96 clearTimeout(navigateTo.timer_); | |
97 if (opt_updateHashNow) | |
98 updateHash(); | |
99 else | |
100 navigateTo.timer_ = setTimeout(updateHash, 250); | |
101 | |
102 updateParentId(id); | |
103 } | |
104 | |
105 /** | |
106 * This returns the user visible path to the folder where the bookmark is | |
107 * located. | |
108 * @param {number} parentId The ID of the parent folder. | |
109 * @return {string} The path to the the bookmark, | |
110 */ | |
111 function getFolder(parentId) { | |
112 var parentNode = tree.getBookmarkNodeById(parentId); | |
113 if (parentNode) { | |
114 var s = parentNode.title; | |
115 if (parentNode.parentId != ROOT_ID) { | |
116 return getFolder(parentNode.parentId) + '/' + s; | |
117 } | |
118 return s; | |
119 } | |
mazda
2011/11/09 04:49:43
Please add return for the case that parentNode is
yoshiki
2011/11/09 15:06:39
This function is no longer used here. Deleted.
On
| |
120 } | |
121 | |
122 // Checks disabilities of the buttons. | |
mazda
2011/11/09 04:49:43
Please format the comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
123 function checkDisabilities() { | |
124 var selected = tree.selectedItem; | |
125 var newNameText = document.querySelector('#new-name').value; | |
126 | |
127 document.querySelector('#new-folder').disabled = !selected; | |
128 document.querySelector('#ok').disabled = !selected || !newNameText; | |
129 } | |
130 document.querySelector('#new-name') | |
131 .addEventListener('input', checkDisabilities); | |
132 tree.addEventListener('change', checkDisabilities); | |
133 checkDisabilities(); | |
134 | |
135 /** | |
136 * Callback for the new folder command. This creates a new folder and starts | |
137 * a rename of it. | |
138 */ | |
139 function newFolder() { | |
140 var parentId = tree.selectedItem.bookmarkId; | |
141 chrome.bookmarks.create({ | |
142 title: localStrings.getString('new_folder_name'), | |
143 parentId: parentId | |
144 }, function(newNode) { | |
145 // This callback happens before the event that triggers the tree/list to | |
146 // get updated so delay the work so that the tree/list gets updated first. | |
147 setTimeout(function() { | |
148 var newItem = bmm.treeLookup[newNode.id]; | |
149 tree.selectedItem = newItem; | |
150 newItem.editing = true; | |
151 }, 50); | |
152 }); | |
153 } | |
154 | |
155 function cancel() { | |
mazda
2011/11/09 04:49:43
Please add comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
156 window.close(); | |
157 } | |
158 | |
159 /** | |
160 * Updates the parent ID of the bookmark list and selects the correct tree item. | |
161 * @param {string} id The id. | |
162 */ | |
163 function updateParentId(id) { | |
164 if (id in bmm.treeLookup) | |
165 tree.selectedItem = bmm.treeLookup[id]; | |
166 } | |
167 | |
168 /** | |
169 * Stores the information of all the tabs when window opens. | |
170 */ | |
171 var allTabs = {}; | |
172 | |
173 chrome.windows.getCurrent(getCurrentWindow); | |
174 | |
175 function getCurrentWindow(win) { | |
176 chrome.tabs.getAllInWindow(win.id, callbackAllTabs); | |
177 } | |
178 | |
179 function callbackAllTabs(tabs) { | |
mazda
2011/11/09 04:49:43
Ditto.
yoshiki
2011/11/09 15:06:39
This function is only used once. Made it inline.
| |
180 allTabs = tabs; | |
181 } | |
182 | |
183 /** | |
184 * Creates a folder and Adds tabs as bookmarks under the folder. | |
185 */ | |
186 function bookmarkAllTabs() { | |
187 var parentBookmark = tree.selectedItem; | |
188 var newNameTextbox = document.querySelector('#new-name'); | |
189 | |
190 if (!parentBookmark || !newNameTextbox) | |
191 return; | |
192 | |
193 var parentId = parentBookmark.bookmarkId; | |
194 var newFolderName = newNameTextbox.value; | |
195 | |
196 if (parentId == 0 || newFolderName == "") | |
mazda
2011/11/09 04:49:43
if (parentId == 0 || !newFolderName)
http://googl
yoshiki
2011/11/09 15:06:39
Done.
| |
197 return; | |
198 | |
199 chrome.bookmarks.create({ | |
200 title: newFolderName, | |
201 parentId: parentId.toString() | |
202 }, function(newNode) { | |
203 for (var i in allTabs) { | |
204 var tab = allTabs[i]; | |
205 chrome.bookmarks.create({'parentId': newNode.id.toString(), | |
206 'title': tab.title, | |
207 'url': tab.url}); | |
208 } | |
209 window.close(); | |
210 }); | |
211 } | |
212 | |
213 /** | |
214 * Adds handlers to buttons. | |
215 */ | |
216 document.querySelector('#new-folder').addEventListener('click', newFolder); | |
217 document.querySelector('#ok').addEventListener('click', bookmarkAllTabs); | |
218 document.querySelector('#cancel').addEventListener('click', cancel); | |
OLD | NEW |