OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 const BookmarkList = bmm.BookmarkList; | 5 const BookmarkList = bmm.BookmarkList; |
6 const BookmarkTree = bmm.BookmarkTree; | 6 const BookmarkTree = bmm.BookmarkTree; |
7 const ListItem = cr.ui.ListItem; | 7 const ListItem = cr.ui.ListItem; |
8 const TreeItem = cr.ui.TreeItem; | 8 const TreeItem = cr.ui.TreeItem; |
9 const LinkKind = cr.LinkKind; | 9 const LinkKind = cr.LinkKind; |
10 const Command = cr.ui.Command; | 10 const Command = cr.ui.Command; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 }); | 109 }); |
110 bmm.treeLookup[recentTreeItem.bookmarkId] = recentTreeItem; | 110 bmm.treeLookup[recentTreeItem.bookmarkId] = recentTreeItem; |
111 | 111 |
112 BookmarkTree.decorate(tree); | 112 BookmarkTree.decorate(tree); |
113 | 113 |
114 tree.addEventListener('change', function() { | 114 tree.addEventListener('change', function() { |
115 navigateTo(tree.selectedItem.bookmarkId); | 115 navigateTo(tree.selectedItem.bookmarkId); |
116 }); | 116 }); |
117 | 117 |
118 /** | 118 /** |
| 119 * Add an event listener to a node that will remove itself after firing once. |
| 120 * @param {!Element} node The DOM node to add the listener to. |
| 121 * @param {string} name The name of the event listener to add to. |
| 122 * @param {function(Event)} handler Function called when the event fires. |
| 123 */ |
| 124 function addOneShotEventListener(node, name, handler) { |
| 125 var f = function(e) { |
| 126 handler(e); |
| 127 node.removeEventListener(name, f); |
| 128 }; |
| 129 node.addEventListener(name, f); |
| 130 } |
| 131 |
| 132 /** |
119 * Navigates to a bookmark ID. | 133 * Navigates to a bookmark ID. |
120 * @param {string} id The ID to navigate to. | 134 * @param {string} id The ID to navigate to. |
121 * @param {boolean=} opt_updateHashNow Whether to immediately update the | 135 * @param {boolean=} opt_updateHashNow Whether to immediately update the |
122 * location.hash. If false then it is updated in a timeout. | 136 * location.hash. If false then it is updated in a timeout. |
123 */ | 137 */ |
124 function navigateTo(id, opt_updateHashNow) { | 138 function navigateTo(id, opt_updateHashNow) { |
125 console.info('navigateTo', 'from', window.location.hash, 'to', id); | 139 console.info('navigateTo', 'from', window.location.hash, 'to', id); |
126 // Update the location hash using a timer to prevent reentrancy. This is how | 140 // Update the location hash using a timer to prevent reentrancy. This is how |
127 // often we add history entries and the time here is a bit arbitrary but was | 141 // often we add history entries and the time here is a bit arbitrary but was |
128 // picked as the smallest time a human perceives as instant. | 142 // picked as the smallest time a human perceives as instant. |
(...skipping 14 matching lines...) Expand all Loading... |
143 /** | 157 /** |
144 * Updates the parent ID of the bookmark list and selects the correct tree item. | 158 * Updates the parent ID of the bookmark list and selects the correct tree item. |
145 * @param {string} id The id. | 159 * @param {string} id The id. |
146 */ | 160 */ |
147 function updateParentId(id) { | 161 function updateParentId(id) { |
148 list.parentId = id; | 162 list.parentId = id; |
149 if (id in bmm.treeLookup) | 163 if (id in bmm.treeLookup) |
150 tree.selectedItem = bmm.treeLookup[id]; | 164 tree.selectedItem = bmm.treeLookup[id]; |
151 } | 165 } |
152 | 166 |
153 // We listen to hashchange so that we can update the currently shown folder when | 167 // Process the location hash. This is called onhashchange and when the page is |
154 // the user goes back and forward in the history. | 168 // first loaded. |
155 window.onhashchange = function(e) { | 169 function processHash() { |
156 var id = window.location.hash.slice(1); | 170 var id = window.location.hash.slice(1); |
| 171 if (!id) { |
| 172 // If we do not have a hash select first item in the tree. |
| 173 id = tree.items[0].bookmarkId; |
| 174 } |
157 | 175 |
158 var valid = false; | 176 var valid = false; |
| 177 if (/^[ae]=/.test(id)) { |
| 178 var command = id[0]; |
| 179 id = id.slice(2); |
| 180 if (command == 'e') { |
| 181 // If hash contains e= edit the item specified. |
| 182 chrome.bookmarks.get(id, function(bookmarkNodes) { |
| 183 // Verify the node to edit is a valid node. |
| 184 if (!bookmarkNodes || bookmarkNodes.length != 1) |
| 185 return; |
| 186 var bookmarkNode = bookmarkNodes[0]; |
| 187 // After the list reloads edit the desired bookmark. |
| 188 var editBookmark = function(e) { |
| 189 var index = list.dataModel.findIndexById(bookmarkNode.id); |
| 190 if (index != -1) { |
| 191 var sm = list.selectionModel; |
| 192 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = index; |
| 193 scrollIntoViewAndMakeEditable(index); |
| 194 } |
| 195 } |
159 | 196 |
160 // In case we got a search hash update the text input and the bmm.treeLookup | 197 if (list.parentId == bookmarkNode.parentId) |
161 // to use the new id. | 198 editBookmark(); |
162 if (/^q=/.test(id)) { | 199 else { |
| 200 // Navigate to the parent folder, once it's loaded edit the bookmark. |
| 201 addOneShotEventListener(list, 'load', editBookmark); |
| 202 updateParentId(bookmarkNode.parentId); |
| 203 } |
| 204 }); |
| 205 // We handle the two cases of navigating to the bookmark to be edited |
| 206 // above, don't run the standard navigation code below. |
| 207 return; |
| 208 } else if (command == 'a') { |
| 209 // Once the parent folder is loaded add a page bookmark. |
| 210 addOneShotEventListener(list, 'load', addPage); |
| 211 } |
| 212 } else if (/^q=/.test(id)) { |
| 213 // In case we got a search hash update the text input and the |
| 214 // bmm.treeLookup to use the new id. |
163 setSearch(id.slice(2)); | 215 setSearch(id.slice(2)); |
164 valid = true; | 216 valid = true; |
165 } else if (id == 'recent') { | 217 } else if (id == 'recent') { |
166 valid = true; | 218 valid = true; |
167 } | 219 } |
168 | 220 |
| 221 // Navigate to bookmark 'id' (which may be a query of the form q=query). |
169 if (valid) { | 222 if (valid) { |
170 updateParentId(id); | 223 updateParentId(id); |
171 } else { | 224 } else { |
172 // We need to verify that this is a correct ID. | 225 // We need to verify that this is a correct ID. |
173 chrome.bookmarks.get(id, function(items) { | 226 chrome.bookmarks.get(id, function(items) { |
174 if (items && items.length == 1) | 227 if (items && items.length == 1) |
175 updateParentId(id); | 228 updateParentId(id); |
176 }); | 229 }); |
177 } | 230 } |
178 }; | 231 }; |
179 | 232 |
| 233 // We listen to hashchange so that we can update the currently shown folder when |
| 234 // the user goes back and forward in the history. |
| 235 window.onhashchange = function(e) { |
| 236 processHash(); |
| 237 }; |
| 238 |
180 // Activate is handled by the open-in-same-window-command. | 239 // Activate is handled by the open-in-same-window-command. |
181 list.addEventListener('dblclick', function(e) { | 240 list.addEventListener('dblclick', function(e) { |
182 if (e.button == 0) | 241 if (e.button == 0) |
183 $('open-in-same-window-command').execute(); | 242 $('open-in-same-window-command').execute(); |
184 }); | 243 }); |
185 | 244 |
186 // The list dispatches an event when the user clicks on the URL or the Show in | 245 // The list dispatches an event when the user clicks on the URL or the Show in |
187 // folder part. | 246 // folder part. |
188 list.addEventListener('urlClicked', function(e) { | 247 list.addEventListener('urlClicked', function(e) { |
189 getLinkController().openUrlFromEvent(e.url, e.originalEvent); | 248 getLinkController().openUrlFromEvent(e.url, e.originalEvent); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 if (parentNode.parentId != ROOT_ID) { | 399 if (parentNode.parentId != ROOT_ID) { |
341 return getFolder(parentNode.parentId) + '/' + s; | 400 return getFolder(parentNode.parentId) + '/' + s; |
342 } | 401 } |
343 return s; | 402 return s; |
344 } | 403 } |
345 } | 404 } |
346 | 405 |
347 tree.addEventListener('load', function(e) { | 406 tree.addEventListener('load', function(e) { |
348 // Add hard coded tree items | 407 // Add hard coded tree items |
349 tree.add(recentTreeItem); | 408 tree.add(recentTreeItem); |
350 | 409 processHash(); |
351 // Now we can select a tree item. | |
352 var hash = window.location.hash.slice(1); | |
353 if (!hash) { | |
354 // If we do not have a hash select first item in the tree. | |
355 hash = tree.items[0].bookmarkId; | |
356 } | |
357 | |
358 if (/^q=/.test(hash)) { | |
359 var searchTerm = hash.slice(2); | |
360 $('term').value = searchTerm; | |
361 setSearch(searchTerm); | |
362 } else { | |
363 navigateTo(hash); | |
364 } | |
365 }); | 410 }); |
366 | 411 |
367 tree.reload(); | 412 tree.reload(); |
368 addBookmarkModelListeners(); | 413 addBookmarkModelListeners(); |
369 | 414 |
370 var dnd = { | 415 var dnd = { |
371 dragData: null, | 416 dragData: null, |
372 | 417 |
373 getBookmarkElement: function(el) { | 418 getBookmarkElement: function(el) { |
374 while (el && !el.bookmarkNode) { | 419 while (el && !el.bookmarkNode) { |
(...skipping 1238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1613 document.addEventListener('copy', handle('copy-command')); | 1658 document.addEventListener('copy', handle('copy-command')); |
1614 document.addEventListener('cut', handle('cut-command')); | 1659 document.addEventListener('cut', handle('cut-command')); |
1615 | 1660 |
1616 var pasteHandler = handle('paste-command'); | 1661 var pasteHandler = handle('paste-command'); |
1617 document.addEventListener('paste', function(e) { | 1662 document.addEventListener('paste', function(e) { |
1618 // Paste is a bit special since we need to do an async call to see if we can | 1663 // Paste is a bit special since we need to do an async call to see if we can |
1619 // paste because the paste command might not be up to date. | 1664 // paste because the paste command might not be up to date. |
1620 updatePasteCommand(pasteHandler); | 1665 updatePasteCommand(pasteHandler); |
1621 }); | 1666 }); |
1622 })(); | 1667 })(); |
OLD | NEW |