OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 cr.define('bookmarks', function() { | 5 cr.define('bookmarks', function() { |
6 /** | 6 /** |
7 * @param {BookmarkElement} element | 7 * @param {BookmarkElement} element |
8 * @return {boolean} | 8 * @return {boolean} |
9 */ | 9 */ |
10 function isBookmarkItem(element) { | 10 function isBookmarkItem(element) { |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 179 |
180 /** | 180 /** |
181 * Encapsulates the behavior of the drag and drop indicator which puts a line | 181 * Encapsulates the behavior of the drag and drop indicator which puts a line |
182 * between items or highlights folders which are valid drop targets. | 182 * between items or highlights folders which are valid drop targets. |
183 * @constructor | 183 * @constructor |
184 */ | 184 */ |
185 function DropIndicator() { | 185 function DropIndicator() { |
186 /** | 186 /** |
187 * @private {number|null} Timer id used to help minimize flicker. | 187 * @private {number|null} Timer id used to help minimize flicker. |
188 */ | 188 */ |
189 this.removeDropIndicatorTimer_ = null; | 189 this.removeDropIndicatorTimeoutId_ = null; |
190 | 190 |
191 /** | 191 /** |
192 * The element that had a style applied it to indicate the drop location. | 192 * The element that had a style applied it to indicate the drop location. |
193 * This is used to easily remove the style when necessary. | 193 * This is used to easily remove the style when necessary. |
194 * @private {BookmarkElement|null} | 194 * @private {BookmarkElement|null} |
195 */ | 195 */ |
196 this.lastIndicatorElement_ = null; | 196 this.lastIndicatorElement_ = null; |
197 | 197 |
198 /** | 198 /** |
199 * The style that was applied to indicate the drop location. | 199 * The style that was applied to indicate the drop location. |
200 * @private {?string|null} | 200 * @private {?string|null} |
201 */ | 201 */ |
202 this.lastIndicatorClassName_ = null; | 202 this.lastIndicatorClassName_ = null; |
203 | 203 |
204 /** | 204 /** |
205 * Used to instantly remove the indicator style in tests. | 205 * Used to instantly remove the indicator style in tests. |
206 * @private {function((Function|null|string), number)} | 206 * @private {bookmarks.TimerProxy} |
207 */ | 207 */ |
208 this.setTimeout = window.setTimeout.bind(window); | 208 this.timerProxy = new bookmarks.TimerProxy(); |
209 } | 209 } |
210 | 210 |
211 DropIndicator.prototype = { | 211 DropIndicator.prototype = { |
212 /** | 212 /** |
213 * Applies the drop indicator style on the target element and stores that | 213 * Applies the drop indicator style on the target element and stores that |
214 * information to easily remove the style in the future. | 214 * information to easily remove the style in the future. |
215 * @param {HTMLElement} indicatorElement | 215 * @param {HTMLElement} indicatorElement |
216 * @param {DropPosition} position | 216 * @param {DropPosition} position |
217 */ | 217 */ |
218 addDropIndicatorStyle: function(indicatorElement, position) { | 218 addDropIndicatorStyle: function(indicatorElement, position) { |
(...skipping 18 matching lines...) Expand all Loading... |
237 this.lastIndicatorElement_ = null; | 237 this.lastIndicatorElement_ = null; |
238 this.lastIndicatorClassName_ = null; | 238 this.lastIndicatorClassName_ = null; |
239 }, | 239 }, |
240 | 240 |
241 /** | 241 /** |
242 * Displays the drop indicator on the current drop target to give the | 242 * Displays the drop indicator on the current drop target to give the |
243 * user feedback on where the drop will occur. | 243 * user feedback on where the drop will occur. |
244 * @param {DropDestination} dropDest | 244 * @param {DropDestination} dropDest |
245 */ | 245 */ |
246 update: function(dropDest) { | 246 update: function(dropDest) { |
247 window.clearTimeout(this.removeDropIndicatorTimer_); | 247 this.timerProxy.clearTimeout(this.removeDropIndicatorTimeoutId_); |
248 | 248 |
249 var indicatorElement = dropDest.element.getDropTarget(); | 249 var indicatorElement = dropDest.element.getDropTarget(); |
250 var position = dropDest.position; | 250 var position = dropDest.position; |
251 | 251 |
252 this.removeDropIndicatorStyle(); | 252 this.removeDropIndicatorStyle(); |
253 this.addDropIndicatorStyle(indicatorElement, position); | 253 this.addDropIndicatorStyle(indicatorElement, position); |
254 }, | 254 }, |
255 | 255 |
256 /** | 256 /** |
257 * Stop displaying the drop indicator. | 257 * Stop displaying the drop indicator. |
258 */ | 258 */ |
259 finish: function() { | 259 finish: function() { |
260 // The use of a timeout is in order to reduce flickering as we move | 260 // The use of a timeout is in order to reduce flickering as we move |
261 // between valid drop targets. | 261 // between valid drop targets. |
262 window.clearTimeout(this.removeDropIndicatorTimer_); | 262 this.timerProxy.clearTimeout(this.removeDropIndicatorTimeoutId_); |
263 this.removeDropIndicatorTimer_ = this.setTimeout(function() { | 263 this.removeDropIndicatorTimeoutId_ = |
264 this.removeDropIndicatorStyle(); | 264 this.timerProxy.setTimeout(function() { |
265 }.bind(this), 100); | 265 this.removeDropIndicatorStyle(); |
| 266 }.bind(this), 100); |
266 }, | 267 }, |
267 }; | 268 }; |
268 | 269 |
269 /** | 270 /** |
270 * Manages drag and drop events for the bookmarks-app. | 271 * Manages drag and drop events for the bookmarks-app. |
271 * @constructor | 272 * @constructor |
272 */ | 273 */ |
273 function DNDManager() { | 274 function DNDManager() { |
274 /** @private {bookmarks.DragInfo} */ | 275 /** @private {bookmarks.DragInfo} */ |
275 this.dragInfo_ = null; | 276 this.dragInfo_ = null; |
276 | 277 |
277 /** @private {?DropDestination} */ | 278 /** @private {?DropDestination} */ |
278 this.dropDestination_ = null; | 279 this.dropDestination_ = null; |
279 | 280 |
280 /** @private {bookmarks.DropIndicator} */ | 281 /** @private {bookmarks.DropIndicator} */ |
281 this.dropIndicator_ = null; | 282 this.dropIndicator_ = null; |
282 | 283 |
283 /** @private {Object<string, function(!Event)>} */ | 284 /** @private {Object<string, function(!Event)>} */ |
284 this.documentListeners_ = null; | 285 this.documentListeners_ = null; |
285 | 286 |
286 /** | 287 /** |
287 * Used to instantly clearDragData in tests. | 288 * Used to instantly clearDragData in tests. |
288 * @private {function((Function|null|string), number)} | 289 * @private {bookmarks.TimerProxy} |
289 */ | 290 */ |
290 this.setTimeout_ = window.setTimeout.bind(window); | 291 this.timerProxy_ = new bookmarks.TimerProxy(); |
291 } | 292 } |
292 | 293 |
293 DNDManager.prototype = { | 294 DNDManager.prototype = { |
294 init: function() { | 295 init: function() { |
295 this.dragInfo_ = new DragInfo(); | 296 this.dragInfo_ = new DragInfo(); |
296 this.dropIndicator_ = new DropIndicator(); | 297 this.dropIndicator_ = new DropIndicator(); |
297 this.autoExpander_ = new AutoExpander(); | 298 this.autoExpander_ = new AutoExpander(); |
298 | 299 |
299 this.documentListeners_ = { | 300 this.documentListeners_ = { |
300 'dragstart': this.onDragStart_.bind(this), | 301 'dragstart': this.onDragStart_.bind(this), |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 index: index, | 380 index: index, |
380 parentId: parentId, | 381 parentId: parentId, |
381 }; | 382 }; |
382 }, | 383 }, |
383 | 384 |
384 /** @private */ | 385 /** @private */ |
385 clearDragData_: function() { | 386 clearDragData_: function() { |
386 // Defer the clearing of the data so that the bookmark manager API's drop | 387 // Defer the clearing of the data so that the bookmark manager API's drop |
387 // event doesn't clear the drop data before the web drop event has a | 388 // event doesn't clear the drop data before the web drop event has a |
388 // chance to execute (on Mac). | 389 // chance to execute (on Mac). |
389 this.setTimeout_(function() { | 390 this.timerProxy_.setTimeout(function() { |
390 this.dragInfo_.clearDragData(); | 391 this.dragInfo_.clearDragData(); |
391 this.dropDestination_ = null; | 392 this.dropDestination_ = null; |
392 this.dropIndicator_.finish(); | 393 this.dropIndicator_.finish(); |
393 }.bind(this), 0); | 394 }.bind(this), 0); |
394 }, | 395 }, |
395 | 396 |
396 /** | 397 /** |
397 * @private | 398 * @private |
398 * @param {Event} e | 399 * @param {Event} e |
399 */ | 400 */ |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 state.nodes[state.selectedFolder].children.length == 0; | 632 state.nodes[state.selectedFolder].children.length == 0; |
632 } | 633 } |
633 | 634 |
634 // We can only drop on a folder. | 635 // We can only drop on a folder. |
635 if (getBookmarkNode(overElement).url) | 636 if (getBookmarkNode(overElement).url) |
636 return false; | 637 return false; |
637 | 638 |
638 return !this.dragInfo_.isDraggingChildBookmark(overElement.itemId); | 639 return !this.dragInfo_.isDraggingChildBookmark(overElement.itemId); |
639 }, | 640 }, |
640 | 641 |
641 disableTimeoutsForTesting: function() { | 642 /** @param {bookmarks.TimerProxy} timerProxy */ |
642 this.setTimeout_ = function(fn) { | 643 setTimerProxyForTesting: function(timerProxy) { |
643 fn(); | 644 this.timerProxy_ = timerProxy; |
644 }; | 645 this.dropIndicator_.timerProxy = timerProxy; |
645 this.dropIndicator_.setTimeout = this.setTimeout_; | |
646 } | 646 } |
647 }; | 647 }; |
648 | 648 |
649 return { | 649 return { |
650 DNDManager: DNDManager, | 650 DNDManager: DNDManager, |
651 DragInfo: DragInfo, | 651 DragInfo: DragInfo, |
652 DropIndicator: DropIndicator, | 652 DropIndicator: DropIndicator, |
653 }; | 653 }; |
654 }); | 654 }); |
OLD | NEW |