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>
3030
3131 < h2 > Vertical sorting with pat-clone</ h2 >
3232 < 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 "
3435 data-pat-clone ="template: .clone-template-1; trigger-element: .clone-trigger-1 ">
3536 </ ul >
3637 < template class ="clone-template-1 ">
Original file line number Diff line number Diff line change @@ -23,31 +23,18 @@ export default Base.extend({
2323 this . options = parser . parse ( this . $el , false ) ;
2424 this . recordPositions ( ) . initScrolling ( ) ;
2525 this . $el . on ( "pat-update" , this . onPatternUpdate . bind ( this ) ) ;
26+ this . $el . on ( "patterns-injected" , this . recordPositions . bind ( this ) ) ;
2627 } ,
2728
2829 onPatternUpdate : function ( ev , data ) {
2930 /* Handler which gets called when pat-update is triggered within
3031 * the .pat-sortable element.
3132 */
32- if ( data ?. pattern !== "clone" || data ?. action !== "added" || ! data ?. dom ) {
33+ if ( data ?. pattern !== "clone" || data ?. action !== "added" ) {
3334 // Nothing to do.
3435 return ;
3536 }
36-
3737 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- ) ;
5138 } ,
5239
5340 recordPositions : function ( ) {
Original file line number Diff line number Diff line change 11import events from "../../core/events" ;
2+ import utils from "../../core/utils" ;
23import $ from "jquery" ;
34import Sortable from "./sortable" ;
45
@@ -180,4 +181,36 @@ describe("pat-sortable", function () {
180181
181182 expect ( cloned . classList . contains ( sortable . options . dragClass ) ) . toBe ( true ) ;
182183 } ) ;
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+ } ) ;
183216} ) ;
You can’t perform that action at this time.
0 commit comments