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 searchTreeItem = new TreeItem({ | |
38 icon: 'images/bookmark_manager_search.png', | |
39 bookmarkId: 'q=' | |
40 }); | |
41 bmm.treeLookup[searchTreeItem.bookmarkId] = searchTreeItem; | |
42 | |
43 var recentTreeItem = new TreeItem({ | |
44 icon: 'images/bookmark_manager_recent.png', | |
45 bookmarkId: 'recent' | |
46 }); | |
47 bmm.treeLookup[recentTreeItem.bookmarkId] = recentTreeItem; | |
48 | |
49 BookmarkTree.decorate(tree); | |
50 tree.reload(); | |
51 bmm.addBookmarkModelListeners(); | |
52 | |
53 tree.addEventListener('change', function() { | |
54 if (tree.selectedItem) | |
55 navigateTo(tree.selectedItem.bookmarkId); | |
56 }); | |
57 | |
58 /** | |
59 * Add an event listener to a node that will remove itself after firing once. | |
60 * @param {!Element} node The DOM node to add the listener to. | |
61 * @param {string} name The name of the event listener to add to. | |
62 * @param {function(Event)} handler Function called when the event fires. | |
63 */ | |
64 function addOneShotEventListener(node, name, handler) { | |
65 var f = function(e) { | |
66 handler(e); | |
67 node.removeEventListener(name, f); | |
68 }; | |
69 node.addEventListener(name, f); | |
70 } | |
71 | |
72 /** | |
73 * Updates the location hash to reflect the current state of the application. | |
74 */ | |
75 function updateHash() { | |
76 window.location.hash = tree.selectedItem.bookmarkId; | |
77 } | |
78 | |
79 /** | |
80 * Navigates to a bookmark ID. | |
81 * @param {string} id The ID to navigate to. | |
82 * @param {boolean=} opt_updateHashNow Whether to immediately update the | |
83 * location.hash. If false then it is updated in a timeout. | |
84 */ | |
85 function navigateTo(id, opt_updateHashNow) { | |
86 // Update the location hash using a timer to prevent reentrancy. This is how | |
87 // often we add history entries and the time here is a bit arbitrary but was | |
88 // picked as the smallest time a human perceives as instant. | |
89 | |
90 clearTimeout(navigateTo.timer_); | |
91 if (opt_updateHashNow) | |
92 updateHash(); | |
93 else | |
94 navigateTo.timer_ = setTimeout(updateHash, 250); | |
mazda
2011/11/10 06:03:46
Please make a constant variable.
yoshiki
2011/11/10 07:54:38
Done.
| |
95 | |
96 updateParentId(id); | |
97 } | |
98 | |
99 /** | |
100 * Checks disabilities of the buttons. | |
101 */ | |
102 function checkDisabilities() { | |
103 var selected = tree.selectedItem; | |
104 var newNameText = document.querySelector('#new-name').value; | |
105 | |
106 document.querySelector('#new-folder').disabled = !selected; | |
107 document.querySelector('#save').disabled = !selected || !newNameText; | |
108 } | |
109 document.querySelector('#new-name') | |
110 .addEventListener('input', checkDisabilities); | |
111 tree.addEventListener('change', checkDisabilities); | |
112 checkDisabilities(); | |
113 | |
114 /** | |
115 * Callback for the new folder command. This creates a new folder and starts | |
116 * a rename of it. | |
117 */ | |
118 function newFolder() { | |
119 var parentId = tree.selectedItem.bookmarkId; | |
120 chrome.bookmarks.create({ | |
121 title: localStrings.getString('new_folder_name'), | |
122 parentId: parentId | |
123 }, function(newNode) { | |
124 // This callback happens before the event that triggers the tree/list to | |
125 // get updated so delay the work so that the tree/list gets updated first. | |
126 setTimeout(function() { | |
127 var newItem = bmm.treeLookup[newNode.id]; | |
128 tree.selectedItem = newItem; | |
129 newItem.editing = true; | |
130 }, 50); | |
mazda
2011/11/10 06:03:46
Please make a constant variable.
yoshiki
2011/11/10 07:54:38
Done.
| |
131 }); | |
132 } | |
133 | |
134 /** | |
135 * Close window without bookmark all tabs. | |
136 */ | |
137 function cancel() { | |
138 window.close(); | |
139 } | |
140 | |
141 /** | |
142 * Updates the parent ID of the bookmark list and selects the correct tree item. | |
143 * @param {string} id The id. | |
144 */ | |
145 function updateParentId(id) { | |
146 if (id in bmm.treeLookup) | |
147 tree.selectedItem = bmm.treeLookup[id]; | |
148 } | |
149 | |
150 /** | |
151 * Stores the information of all the tabs when window opens. | |
152 * @type {array of tab} | |
153 */ | |
154 var allTabs = {}; | |
155 chrome.windows.getCurrent(function (win) { | |
156 chrome.tabs.getAllInWindow(win.id, function (tabs) { | |
157 allTabs = tabs; | |
158 }); | |
159 }); | |
160 | |
161 /** | |
162 * Creates a folder and Adds tabs as bookmarks under the folder. | |
163 */ | |
164 function bookmarkAllTabs() { | |
165 var parentBookmark = tree.selectedItem; | |
166 var newNameTextbox = document.querySelector('#new-name'); | |
167 | |
168 if (!parentBookmark || !newNameTextbox) | |
169 return; | |
170 | |
171 var parentId = parentBookmark.bookmarkId; | |
172 var newFolderName = newNameTextbox.value; | |
173 | |
174 if (parentId == 0 || !newFolderName) | |
175 return; | |
176 | |
177 chrome.bookmarks.create({ | |
178 title: newFolderName, | |
179 parentId: parentId.toString() | |
180 }, function(newNode) { | |
181 for (var i in allTabs) { | |
182 var tab = allTabs[i]; | |
183 chrome.bookmarks.create({'parentId': newNode.id.toString(), | |
184 'title': tab.title, | |
185 'url': tab.url}); | |
186 } | |
187 window.close(); | |
188 }); | |
189 } | |
190 | |
191 /** | |
192 * Adds handlers to buttons. | |
193 */ | |
194 document.querySelector('#new-folder').addEventListener('click', newFolder); | |
195 document.querySelector('#save').addEventListener('click', bookmarkAllTabs); | |
196 document.querySelector('#cancel').addEventListener('click', cancel); | |
OLD | NEW |