|
1 | 1 | import $ from "jquery"; |
2 | 2 | import Base from "../../core/base"; |
| 3 | +import dom from "../../core/dom"; |
3 | 4 | import events from "../../core/events"; |
4 | 5 | import Parser from "../../core/parser"; |
5 | 6 |
|
@@ -50,36 +51,44 @@ export default Base.extend({ |
50 | 51 | }, |
51 | 52 |
|
52 | 53 | addHandles: function () { |
53 | | - for (const sortable of [...this.$sortables].filter( |
54 | | - (it) => !it.querySelector(".sortable-handle") |
55 | | - )) { |
56 | | - // TODO: we should change to a <button>. |
57 | | - const handle = document.createElement("a"); |
58 | | - handle.textContent = "⇕"; |
59 | | - handle.classList.add("sortable-handle"); |
60 | | - handle.setAttribute("draggable", "true"); |
61 | | - handle.setAttribute("href", "#"); |
62 | | - handle.setAttribute("title", "Drag to reorder"); |
63 | | - handle.setAttribute("aria-label", "Drag to reorder"); |
64 | | - sortable.appendChild(handle); |
65 | | - |
66 | | - // TODO: remove when element is a button. |
67 | | - events.add_event_listener(handle, "click", "pat-sortable--click", (e) => { |
68 | | - e.preventDefault(); |
69 | | - }); |
70 | | - |
71 | | - events.add_event_listener( |
72 | | - handle, |
73 | | - "dragstart", |
74 | | - "pat-sortable--dragstart", |
75 | | - this.onDragStart.bind(this) |
76 | | - ); |
77 | | - events.add_event_listener( |
78 | | - handle, |
79 | | - "dragend", |
80 | | - "pat-sortable--dragend", |
81 | | - this.onDragEnd.bind(this) |
82 | | - ); |
| 54 | + for (const sortable of this.$sortables) { |
| 55 | + const handles = dom.querySelectorAllAndMe(sortable, ".sortable-handle"); |
| 56 | + if (handles.length === 0) { |
| 57 | + // TODO: we should change to a <button>. |
| 58 | + const handle = document.createElement("a"); |
| 59 | + handle.textContent = "⇕"; |
| 60 | + handle.classList.add("sortable-handle"); |
| 61 | + handle.setAttribute("draggable", "true"); |
| 62 | + handle.setAttribute("href", "#"); |
| 63 | + //handle.setAttribute("title", "Drag to reorder"); // TODO: specify if that should be kept. |
| 64 | + handle.setAttribute("aria-label", "Drag to reorder"); |
| 65 | + sortable.appendChild(handle); |
| 66 | + handles.push(handle); |
| 67 | + } |
| 68 | + |
| 69 | + for (const handle of handles) { |
| 70 | + // TODO: remove when element is a button. |
| 71 | + events.add_event_listener( |
| 72 | + handle, |
| 73 | + "click", |
| 74 | + "pat-sortable--click", |
| 75 | + (e) => { |
| 76 | + e.preventDefault(); |
| 77 | + } |
| 78 | + ); |
| 79 | + events.add_event_listener( |
| 80 | + handle, |
| 81 | + "dragstart", |
| 82 | + "pat-sortable--dragstart", |
| 83 | + this.onDragStart.bind(this) |
| 84 | + ); |
| 85 | + events.add_event_listener( |
| 86 | + handle, |
| 87 | + "dragend", |
| 88 | + "pat-sortable--dragend", |
| 89 | + this.onDragEnd.bind(this) |
| 90 | + ); |
| 91 | + } |
83 | 92 | } |
84 | 93 | }, |
85 | 94 |
|
@@ -111,7 +120,7 @@ export default Base.extend({ |
111 | 120 | }, |
112 | 121 |
|
113 | 122 | onDragEnd: function (ev) { |
114 | | - var $dragged = $(ev.target).parent(); |
| 123 | + const $dragged = $(ev.target.closest(this.options.selector)); |
115 | 124 | $dragged.removeClass(this.options.dragClass); |
116 | 125 | this.$sortables.off(".pat-sortable"); |
117 | 126 | this.$el.off(".pat-sortable"); |
@@ -156,8 +165,7 @@ export default Base.extend({ |
156 | 165 | }, |
157 | 166 |
|
158 | 167 | onDragStart: function (ev) { |
159 | | - var $handle = $(ev.target); |
160 | | - var $dragged = $handle.parent(); |
| 168 | + const $dragged = $(ev.target.closest(this.options.selector)); |
161 | 169 | var that = this; |
162 | 170 | if (ev.dataTransfer) { |
163 | 171 | // Firefox seems to need this set to any value |
|
0 commit comments