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 /** | 5 /** |
6 * @fileoverview DragWrapper | 6 * @fileoverview DragWrapper |
7 * A class for simplifying HTML5 drag and drop. Classes should use this to | 7 * A class for simplifying HTML5 drag and drop. Classes should use this to |
8 * handle the nitty gritty of nested drag enters and leaves. | 8 * handle the nitty gritty of nested drag enters and leaves. |
9 */ | 9 */ |
10 var DragWrapper = (function() { | 10 var DragWrapper = (function() { |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 this.target_ = target; | 32 this.target_ = target; |
33 this.handler_ = handler; | 33 this.handler_ = handler; |
34 }, | 34 }, |
35 | 35 |
36 /** | 36 /** |
37 * The number of un-paired dragenter events that have fired on |this|. This | 37 * The number of un-paired dragenter events that have fired on |this|. This |
38 * is incremented by |onDragEnter_| and decremented by |onDragLeave_|. This | 38 * is incremented by |onDragEnter_| and decremented by |onDragLeave_|. This |
39 * is necessary because dragging over child widgets will fire additional | 39 * is necessary because dragging over child widgets will fire additional |
40 * enter and leave events on |this|. A non-zero value does not necessarily | 40 * enter and leave events on |this|. A non-zero value does not necessarily |
41 * indicate that |isCurrentDragTarget_| is true. | 41 * indicate that |isCurrentDragTarget()| is true. |
42 * @type {number} | 42 * @type {number} |
43 * @private | 43 * @private |
44 */ | 44 */ |
45 dragEnters_: 0, | 45 dragEnters_: 0, |
46 | 46 |
47 /** | 47 /** |
48 * Whether the tile page is currently being dragged over with data it can | 48 * Whether the tile page is currently being dragged over with data it can |
49 * accept. | 49 * accept. |
50 * @type {boolean} | 50 * @type {boolean} |
51 * @private | |
52 */ | 51 */ |
53 isCurrentDragTarget_: false, | |
54 get isCurrentDragTarget() { | 52 get isCurrentDragTarget() { |
55 return this.isCurrentDragTarget_; | 53 return this.target_.classList.contains('drag-target'); |
56 }, | 54 }, |
57 | 55 |
58 /** | 56 /** |
59 * Handler for dragenter events fired on |target_|. | 57 * Handler for dragenter events fired on |target_|. |
60 * @param {Event} e A MouseEvent for the drag. | 58 * @param {Event} e A MouseEvent for the drag. |
61 * @private | 59 * @private |
62 */ | 60 */ |
63 onDragEnter_: function(e) { | 61 onDragEnter_: function(e) { |
64 if (++this.dragEnters_ == 1) { | 62 if (++this.dragEnters_ == 1) { |
65 if (this.handler_.shouldAcceptDrag(e)) { | 63 if (this.handler_.shouldAcceptDrag(e)) { |
66 this.isCurrentDragTarget_ = true; | 64 this.target_.classList.add('drag-target'); |
67 this.handler_.doDragEnter(e); | 65 this.handler_.doDragEnter(e); |
68 } | 66 } |
69 } else { | 67 } else { |
70 // Sometimes we'll get an enter event over a child element without an | 68 // Sometimes we'll get an enter event over a child element without an |
71 // over event following it. In this case we have to still call the | 69 // over event following it. In this case we have to still call the |
72 // drag over handler so that we make the necessary updates (one visible | 70 // drag over handler so that we make the necessary updates (one visible |
73 // symptom of not doing this is that the cursor's drag state will | 71 // symptom of not doing this is that the cursor's drag state will |
74 // flicker during drags). | 72 // flicker during drags). |
75 this.onDragOver_(e); | 73 this.onDragOver_(e); |
76 } | 74 } |
77 }, | 75 }, |
78 | 76 |
79 /** | 77 /** |
80 * Thunk for dragover events fired on |target_|. | 78 * Thunk for dragover events fired on |target_|. |
81 * @param {Event} e A MouseEvent for the drag. | 79 * @param {Event} e A MouseEvent for the drag. |
82 * @private | 80 * @private |
83 */ | 81 */ |
84 onDragOver_: function(e) { | 82 onDragOver_: function(e) { |
85 if (!this.isCurrentDragTarget_) | 83 if (!this.target_.classList.contains('drag-target')) |
86 return; | 84 return; |
87 this.handler_.doDragOver(e); | 85 this.handler_.doDragOver(e); |
88 }, | 86 }, |
89 | 87 |
90 /** | 88 /** |
91 * Thunk for drop events fired on |target_|. | 89 * Thunk for drop events fired on |target_|. |
92 * @param {Event} e A MouseEvent for the drag. | 90 * @param {Event} e A MouseEvent for the drag. |
93 * @private | 91 * @private |
94 */ | 92 */ |
95 onDrop_: function(e) { | 93 onDrop_: function(e) { |
96 this.dragEnters_ = 0; | 94 this.dragEnters_ = 0; |
97 if (!this.isCurrentDragTarget_) | 95 if (!this.target_.classList.contains('drag-target')) |
98 return; | 96 return; |
99 this.isCurrentDragTarget_ = false; | 97 this.target_.classList.remove('drag-target'); |
100 this.handler_.doDrop(e); | 98 this.handler_.doDrop(e); |
101 }, | 99 }, |
102 | 100 |
103 /** | 101 /** |
104 * Thunk for dragleave events fired on |target_|. | 102 * Thunk for dragleave events fired on |target_|. |
105 * @param {Event} e A MouseEvent for the drag. | 103 * @param {Event} e A MouseEvent for the drag. |
106 * @private | 104 * @private |
107 */ | 105 */ |
108 onDragLeave_: function(e) { | 106 onDragLeave_: function(e) { |
109 if (--this.dragEnters_ > 0) | 107 if (--this.dragEnters_ > 0) |
110 return; | 108 return; |
111 | 109 |
112 this.isCurrentDragTarget_ = false; | 110 this.target_.classList.remove('drag-target'); |
113 this.handler_.doDragLeave(); | 111 this.handler_.doDragLeave(); |
114 }, | 112 }, |
115 }; | 113 }; |
116 | 114 |
117 return DragWrapper; | 115 return DragWrapper; |
118 })(); | 116 })(); |
OLD | NEW |