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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 */ | 123 */ |
124 function addOneShotEventListener(node, name, handler) { | 124 function addOneShotEventListener(node, name, handler) { |
125 var f = function(e) { | 125 var f = function(e) { |
126 handler(e); | 126 handler(e); |
127 node.removeEventListener(name, f); | 127 node.removeEventListener(name, f); |
128 }; | 128 }; |
129 node.addEventListener(name, f); | 129 node.addEventListener(name, f); |
130 } | 130 } |
131 | 131 |
132 /** | 132 /** |
133 * Update the locaiton hash to reflect the current state of the application. | |
flackr
2011/10/24 19:16:01
s/Update/Updates (as per style guide statement tha
Rick Byers
2011/10/24 20:02:42
Done, thanks for the style pointer!
| |
134 */ | |
135 function updateHash() { | |
136 window.location.hash = tree.selectedItem.bookmarkId; | |
137 } | |
138 | |
139 /** | |
133 * Navigates to a bookmark ID. | 140 * Navigates to a bookmark ID. |
134 * @param {string} id The ID to navigate to. | 141 * @param {string} id The ID to navigate to. |
135 * @param {boolean=} opt_updateHashNow Whether to immediately update the | 142 * @param {boolean=} opt_updateHashNow Whether to immediately update the |
136 * location.hash. If false then it is updated in a timeout. | 143 * location.hash. If false then it is updated in a timeout. |
137 */ | 144 */ |
138 function navigateTo(id, opt_updateHashNow) { | 145 function navigateTo(id, opt_updateHashNow) { |
139 console.info('navigateTo', 'from', window.location.hash, 'to', id); | 146 // console.info('navigateTo', 'from', window.location.hash, 'to', id); |
140 // Update the location hash using a timer to prevent reentrancy. This is how | 147 // Update the location hash using a timer to prevent reentrancy. This is how |
141 // often we add history entries and the time here is a bit arbitrary but was | 148 // often we add history entries and the time here is a bit arbitrary but was |
142 // picked as the smallest time a human perceives as instant. | 149 // picked as the smallest time a human perceives as instant. |
143 | 150 |
144 function f() { | |
145 window.location.hash = tree.selectedItem.bookmarkId; | |
146 } | |
147 | |
148 clearTimeout(navigateTo.timer_); | 151 clearTimeout(navigateTo.timer_); |
149 if (opt_updateHashNow) | 152 if (opt_updateHashNow) |
150 f(); | 153 updateHash(); |
151 else | 154 else |
152 navigateTo.timer_ = setTimeout(f, 250); | 155 navigateTo.timer_ = setTimeout(updateHash, 250); |
153 | 156 |
154 updateParentId(id); | 157 updateParentId(id); |
155 } | 158 } |
156 | 159 |
157 /** | 160 /** |
158 * Updates the parent ID of the bookmark list and selects the correct tree item. | 161 * Updates the parent ID of the bookmark list and selects the correct tree item. |
159 * @param {string} id The id. | 162 * @param {string} id The id. |
160 */ | 163 */ |
161 function updateParentId(id) { | 164 function updateParentId(id) { |
162 list.parentId = id; | 165 list.parentId = id; |
163 if (id in bmm.treeLookup) | 166 if (id in bmm.treeLookup) |
164 tree.selectedItem = bmm.treeLookup[id]; | 167 tree.selectedItem = bmm.treeLookup[id]; |
165 } | 168 } |
166 | 169 |
167 // Process the location hash. This is called onhashchange and when the page is | 170 // Process the location hash. This is called onhashchange and when the page is |
168 // first loaded. | 171 // first loaded. |
169 function processHash() { | 172 function processHash() { |
170 var id = window.location.hash.slice(1); | 173 var id = window.location.hash.slice(1); |
171 if (!id) { | 174 if (!id) { |
172 // If we do not have a hash select first item in the tree. | 175 // If we do not have a hash select first item in the tree. |
173 id = tree.items[0].bookmarkId; | 176 id = tree.items[0].bookmarkId; |
174 } | 177 } |
175 | 178 |
176 var valid = false; | 179 var valid = false; |
177 if (/^[ae]=/.test(id)) { | 180 if (/^e=/.test(id)) { |
178 var command = id[0]; | |
179 id = id.slice(2); | 181 id = id.slice(2); |
180 if (command == 'e') { | 182 |
181 // If hash contains e= edit the item specified. | 183 // If hash contains e= edit the item specified. |
182 chrome.bookmarks.get(id, function(bookmarkNodes) { | 184 chrome.bookmarks.get(id, function(bookmarkNodes) { |
183 // Verify the node to edit is a valid node. | 185 // Verify the node to edit is a valid node. |
184 if (!bookmarkNodes || bookmarkNodes.length != 1) | 186 if (!bookmarkNodes || bookmarkNodes.length != 1) |
185 return; | 187 return; |
186 var bookmarkNode = bookmarkNodes[0]; | 188 var bookmarkNode = bookmarkNodes[0]; |
187 // After the list reloads edit the desired bookmark. | 189 |
188 var editBookmark = function(e) { | 190 // After the list reloads edit the desired bookmark. |
189 var index = list.dataModel.findIndexById(bookmarkNode.id); | 191 var editBookmark = function(e) { |
190 if (index != -1) { | 192 var index = list.dataModel.findIndexById(bookmarkNode.id); |
191 var sm = list.selectionModel; | 193 if (index != -1) { |
192 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = index; | 194 var sm = list.selectionModel; |
193 scrollIntoViewAndMakeEditable(index); | 195 sm.anchorIndex = sm.leadIndex = sm.selectedIndex = index; |
194 } | 196 scrollIntoViewAndMakeEditable(index); |
195 } | 197 } |
198 }; | |
196 | 199 |
197 if (list.parentId == bookmarkNode.parentId) | 200 if (list.parentId == bookmarkNode.parentId) { |
198 editBookmark(); | 201 // Clear the e= from the hash so that future attemps to edit the same |
199 else { | 202 // entry will show up as a hash change. |
200 // Navigate to the parent folder, once it's loaded edit the bookmark. | 203 updateHash(); |
201 addOneShotEventListener(list, 'load', editBookmark); | 204 editBookmark(); |
202 updateParentId(bookmarkNode.parentId); | 205 } else { |
203 } | 206 // Navigate to the parent folder (which will update the hash), once |
204 }); | 207 // it's loaded edit the bookmark. |
205 // We handle the two cases of navigating to the bookmark to be edited | 208 addOneShotEventListener(list, 'load', editBookmark); |
206 // above, don't run the standard navigation code below. | 209 updateParentId(bookmarkNode.parentId); |
207 return; | 210 } |
208 } else if (command == 'a') { | 211 }); |
209 // Once the parent folder is loaded add a page bookmark. | 212 |
210 addOneShotEventListener(list, 'load', addPage); | 213 // We handle the two cases of navigating to the bookmark to be edited |
211 } | 214 // above, don't run the standard navigation code below. |
215 return; | |
212 } else if (/^q=/.test(id)) { | 216 } else if (/^q=/.test(id)) { |
213 // In case we got a search hash update the text input and the | 217 // In case we got a search hash update the text input and the |
214 // bmm.treeLookup to use the new id. | 218 // bmm.treeLookup to use the new id. |
215 setSearch(id.slice(2)); | 219 setSearch(id.slice(2)); |
216 valid = true; | 220 valid = true; |
217 } else if (id == 'recent') { | 221 } else if (id == 'recent') { |
218 valid = true; | 222 valid = true; |
219 } | 223 } |
220 | 224 |
221 // Navigate to bookmark 'id' (which may be a query of the form q=query). | 225 // Navigate to bookmark 'id' (which may be a query of the form q=query). |
222 if (valid) { | 226 if (valid) { |
223 updateParentId(id); | 227 updateParentId(id); |
224 } else { | 228 } else { |
225 // We need to verify that this is a correct ID. | 229 // We need to verify that this is a correct ID. |
226 chrome.bookmarks.get(id, function(items) { | 230 chrome.bookmarks.get(id, function(items) { |
227 if (items && items.length == 1) | 231 if (items && items.length == 1) |
228 updateParentId(id); | 232 updateParentId(id); |
229 }); | 233 }); |
230 } | 234 } |
231 }; | 235 }; |
232 | 236 |
233 // We listen to hashchange so that we can update the currently shown folder when | 237 // We listen to hashchange so that we can update the currently shown folder when |
234 // the user goes back and forward in the history. | 238 // the user goes back and forward in the history. |
235 window.onhashchange = function(e) { | 239 window.onhashchange = function(e) { |
240 // console.info('onhashchange', e.oldURL, e.newURL); | |
236 processHash(); | 241 processHash(); |
237 }; | 242 }; |
238 | 243 |
239 // Activate is handled by the open-in-same-window-command. | 244 // Activate is handled by the open-in-same-window-command. |
240 list.addEventListener('dblclick', function(e) { | 245 list.addEventListener('dblclick', function(e) { |
241 if (e.button == 0) | 246 if (e.button == 0) |
242 $('open-in-same-window-command').execute(); | 247 $('open-in-same-window-command').execute(); |
243 }); | 248 }); |
244 | 249 |
245 // The list dispatches an event when the user clicks on the URL or the Show in | 250 // The list dispatches an event when the user clicks on the URL or the Show in |
(...skipping 1414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1660 document.addEventListener('copy', handle('copy-command')); | 1665 document.addEventListener('copy', handle('copy-command')); |
1661 document.addEventListener('cut', handle('cut-command')); | 1666 document.addEventListener('cut', handle('cut-command')); |
1662 | 1667 |
1663 var pasteHandler = handle('paste-command'); | 1668 var pasteHandler = handle('paste-command'); |
1664 document.addEventListener('paste', function(e) { | 1669 document.addEventListener('paste', function(e) { |
1665 // Paste is a bit special since we need to do an async call to see if we can | 1670 // Paste is a bit special since we need to do an async call to see if we can |
1666 // paste because the paste command might not be up to date. | 1671 // paste because the paste command might not be up to date. |
1667 updatePasteCommand(pasteHandler); | 1672 updatePasteCommand(pasteHandler); |
1668 }); | 1673 }); |
1669 })(); | 1674 })(); |
OLD | NEW |