File tree Expand file tree Collapse file tree 3 files changed +37
-16
lines changed Expand file tree Collapse file tree 3 files changed +37
-16
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,8 @@ <h2>Horizontal sorting</h2>
30
30
31
31
< h2 > Vertical sorting with pat-clone</ h2 >
32
32
< button class ="clone-trigger-1 "> Add item</ button >
33
- < ul class ="pat-sortable pat-clone "
33
+ < a class ="pat-inject " data-pat-inject ="source: .clone-template-1; target: #clone-inject-example::after "> Add item via injection</ a >
34
+ < ul id ="clone-inject-example " class ="pat-sortable pat-clone "
34
35
data-pat-clone ="template: .clone-template-1; trigger-element: .clone-trigger-1 ">
35
36
</ ul >
36
37
< template class ="clone-template-1 ">
Original file line number Diff line number Diff line change @@ -23,31 +23,18 @@ export default Base.extend({
23
23
this . options = parser . parse ( this . $el , false ) ;
24
24
this . recordPositions ( ) . initScrolling ( ) ;
25
25
this . $el . on ( "pat-update" , this . onPatternUpdate . bind ( this ) ) ;
26
+ this . $el . on ( "patterns-injected" , this . recordPositions . bind ( this ) ) ;
26
27
} ,
27
28
28
29
onPatternUpdate : function ( ev , data ) {
29
30
/* Handler which gets called when pat-update is triggered within
30
31
* the .pat-sortable element.
31
32
*/
32
- if ( data ?. pattern !== "clone" || data ?. action !== "added" || ! data ?. dom ) {
33
+ if ( data ?. pattern !== "clone" || data ?. action !== "added" ) {
33
34
// Nothing to do.
34
35
return ;
35
36
}
36
-
37
37
this . recordPositions ( ) ;
38
-
39
- events . add_event_listener (
40
- data . dom ,
41
- "dragstart" ,
42
- "pat-sortable--dragstart" ,
43
- this . onDragStart . bind ( this )
44
- ) ;
45
- events . add_event_listener (
46
- data . dom ,
47
- "dragend" ,
48
- "pat-sortable--dragend" ,
49
- this . onDragEnd . bind ( this )
50
- ) ;
51
38
} ,
52
39
53
40
recordPositions : function ( ) {
Original file line number Diff line number Diff line change 1
1
import events from "../../core/events" ;
2
+ import utils from "../../core/utils" ;
2
3
import $ from "jquery" ;
3
4
import Sortable from "./sortable" ;
4
5
@@ -180,4 +181,36 @@ describe("pat-sortable", function () {
180
181
181
182
expect ( cloned . classList . contains ( sortable . options . dragClass ) ) . toBe ( true ) ;
182
183
} ) ;
184
+
185
+ it ( "6 - Initializes sorting behavior on pat-injected elements." , async function ( ) {
186
+ const Inject = ( await import ( "../inject/inject" ) ) . default ;
187
+
188
+ document . body . innerHTML = `
189
+ <ul class="pat-sortable">
190
+ </ul>
191
+ <a class="pat-inject"
192
+ data-pat-inject="
193
+ source: .inject-template;
194
+ target: .pat-sortable::after">Inject</a>
195
+ <template class="inject-template">
196
+ <li>item</li>
197
+ </template>
198
+ ` ;
199
+ const el = document . querySelector ( ".pat-sortable" ) ;
200
+ const sortable = new Sortable ( el ) ;
201
+ const el_inject = document . querySelector ( ".pat-inject" ) ;
202
+ Inject . init ( $ ( el_inject ) ) ;
203
+
204
+ el_inject . click ( ) ;
205
+ await utils . timeout ( 1 ) ;
206
+
207
+ const injected = el . querySelector ( "li" ) ;
208
+ expect ( injected ) . toBeTruthy ( ) ;
209
+
210
+ const drag_handle = injected . querySelector ( ".sortable-handle" ) ;
211
+ expect ( drag_handle ) . toBeTruthy ( ) ;
212
+ drag_handle . dispatchEvent ( new Event ( "dragstart" ) ) ;
213
+
214
+ expect ( injected . classList . contains ( sortable . options . dragClass ) ) . toBe ( true ) ;
215
+ } ) ;
183
216
} ) ;
You can’t perform that action at this time.
0 commit comments