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 // The delegate interface: | 5 // The delegate interface: |
6 // dragContainer --> | 6 // dragContainer --> |
7 // element containing the draggable items | 7 // element containing the draggable items |
8 // | 8 // |
9 // transitionsDuration --> | 9 // transitionsDuration --> |
10 // length of time of transitions in ms | 10 // length of time of transitions in ms |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 el.addEventListener('dragleave', this.handleDragLeave_.bind(this)); | 48 el.addEventListener('dragleave', this.handleDragLeave_.bind(this)); |
49 el.addEventListener('drop', this.handleDrop_.bind(this)); | 49 el.addEventListener('drop', this.handleDrop_.bind(this)); |
50 el.addEventListener('dragend', this.handleDragEnd_.bind(this)); | 50 el.addEventListener('dragend', this.handleDragEnd_.bind(this)); |
51 el.addEventListener('drag', this.handleDrag_.bind(this)); | 51 el.addEventListener('drag', this.handleDrag_.bind(this)); |
52 el.addEventListener('mousedown', this.handleMouseDown_.bind(this)); | 52 el.addEventListener('mousedown', this.handleMouseDown_.bind(this)); |
53 }, | 53 }, |
54 | 54 |
55 getCoordinates_: function(e) { | 55 getCoordinates_: function(e) { |
56 var rect = this.delegate_.dragContainer.getBoundingClientRect(); | 56 var rect = this.delegate_.dragContainer.getBoundingClientRect(); |
57 var coordinates = { | 57 var coordinates = { |
58 x: e.clientX + window.scrollX - rect.left, | 58 x: e.clientX - rect.left, |
59 y: e.clientY + window.scrollY - rect.top | 59 y: e.clientY - rect.top |
60 }; | 60 }; |
61 | 61 |
62 // If we're in an RTL language, reflect the coordinates so the delegate | 62 // If we're in an RTL language, reflect the coordinates so the delegate |
63 // doesn't need to worry about it. | 63 // doesn't need to worry about it. |
64 if (isRtl()) | 64 if (isRtl()) |
65 coordinates.x = this.delegate_.dragContainer.offsetWidth - coordinates.x; | 65 coordinates.x = this.delegate_.dragContainer.offsetWidth - coordinates.x; |
66 | 66 |
67 return coordinates; | 67 return coordinates; |
68 }, | 68 }, |
69 | 69 |
70 // Listen to mousedown to get the relative position of the cursor when | 70 // Listen to mousedown to get the relative position of the cursor when |
71 // starting drag and drop. | 71 // starting drag and drop. |
72 handleMouseDown_: function(e) { | 72 handleMouseDown_: function(e) { |
73 var item = this.delegate_.getItem(e); | 73 var item = this.delegate_.getItem(e); |
74 if (!item) | 74 if (!item) { |
| 75 e.preventDefault(); |
75 return; | 76 return; |
| 77 } |
76 | 78 |
77 this.startX_ = item.offsetLeft; | 79 this.startX_ = item.offsetLeft; |
78 this.startY_ = item.offsetTop; | 80 this.startY_ = item.offsetTop; |
79 this.startScreenX_ = e.screenX; | 81 this.startScreenX_ = e.screenX; |
80 this.startScreenY_ = e.screenY; | 82 this.startScreenY_ = e.screenY; |
81 | 83 |
82 // We don't want to focus the item on mousedown. However, to prevent | 84 // We don't want to focus the item on mousedown. However, to prevent |
83 // focus one has to call preventDefault but this also prevents the drag | 85 // focus one has to call preventDefault but this also prevents the drag |
84 // and drop (sigh) so we only prevent it when the user is not doing a | 86 // and drop (sigh) so we only prevent it when the user is not doing a |
85 // left mouse button drag. | 87 // left mouse button drag. |
86 if (e.button != 0) // LEFT | 88 if (e.button != 0) // LEFT |
87 e.preventDefault(); | 89 e.preventDefault(); |
88 }, | 90 }, |
89 | 91 |
90 handleDragStart_: function(e) { | 92 handleDragStart_: function(e) { |
91 var item = this.delegate_.getItem(e); | 93 var item = this.delegate_.getItem(e); |
92 if (!item) | 94 if (!item) |
93 return; | 95 return; |
94 | 96 |
95 // Don't set data since HTML5 does not allow setting the name for | 97 // Don't set data since HTML5 does not allow setting the name for |
96 // url-list. Instead, we just rely on the dragging of link behavior. | 98 // url-list. Instead, we just rely on the dragging of link behavior. |
97 this.delegate_.dragItem = item; | 99 this.delegate_.dragItem = item; |
98 item.classList.add('dragging'); | 100 item.classList.add('dragging'); |
| 101 item.style.zIndex = 2; |
99 | 102 |
100 e.dataTransfer.effectAllowed = 'copyLinkMove'; | 103 e.dataTransfer.effectAllowed = 'copyLinkMove'; |
101 }, | 104 }, |
102 | 105 |
103 handleDragEnter_: function(e) { | 106 handleDragEnter_: function(e) { |
104 if (this.delegate_.canDropOn(this.getCoordinates_(e))) | 107 if (this.delegate_.canDropOn(this.getCoordinates_(e))) |
105 e.preventDefault(); | 108 e.preventDefault(); |
106 }, | 109 }, |
107 | 110 |
108 handleDragOver_: function(e) { | 111 handleDragOver_: function(e) { |
(...skipping 11 matching lines...) Expand all Loading... |
120 e.preventDefault(); | 123 e.preventDefault(); |
121 }, | 124 }, |
122 | 125 |
123 handleDrop_: function(e) { | 126 handleDrop_: function(e) { |
124 var dragItem = this.delegate_.dragItem; | 127 var dragItem = this.delegate_.dragItem; |
125 if (!dragItem) | 128 if (!dragItem) |
126 return; | 129 return; |
127 | 130 |
128 this.delegate_.dragItem = null; | 131 this.delegate_.dragItem = null; |
129 this.delegate_.saveDrag(); | 132 this.delegate_.saveDrag(); |
| 133 dragItem.classList.remove('dragging'); |
130 | 134 |
131 setTimeout(function() { | 135 setTimeout(function() { |
132 dragItem.classList.remove('dragging'); | 136 dragItem.style.zIndex = 0; |
133 }, this.delegate_.transitionsDuration + 10); | 137 }, this.delegate_.transitionsDuration + 10); |
134 }, | 138 }, |
135 | 139 |
136 handleDragEnd_: function(e) { | 140 handleDragEnd_: function(e) { |
137 return this.handleDrop_(e); | 141 return this.handleDrop_(e); |
138 }, | 142 }, |
139 | 143 |
140 handleDrag_: function(e) { | 144 handleDrag_: function(e) { |
141 // Moves the drag item making sure that it is not displayed outside the | 145 // Moves the drag item making sure that it is not displayed outside the |
142 // browser viewport. | 146 // browser viewport. |
(...skipping 12 matching lines...) Expand all Loading... |
155 // The shadow is 2px | 159 // The shadow is 2px |
156 y = Math.max(-rect.top, y); | 160 y = Math.max(-rect.top, y); |
157 y = Math.min(y, document.body.clientHeight - rect.top - | 161 y = Math.min(y, document.body.clientHeight - rect.top - |
158 dragItem.offsetHeight - 2); | 162 dragItem.offsetHeight - 2); |
159 | 163 |
160 // Override right in case of RTL. | 164 // Override right in case of RTL. |
161 dragItem.style.left = x + 'px'; | 165 dragItem.style.left = x + 'px'; |
162 dragItem.style.top = y + 'px'; | 166 dragItem.style.top = y + 'px'; |
163 } | 167 } |
164 }; | 168 }; |
OLD | NEW |