Skip to content

Commit

Permalink
Merge pull request #1122 from Patternslib/fix-sortable-3
Browse files Browse the repository at this point in the history
feat(pat-sortable): Support initialization after injection.
  • Loading branch information
thet authored Dec 22, 2022
2 parents 67cb3b7 + afaf388 commit 5472ac4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/pat/sortable/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ <h2>Horizontal sorting</h2>

<h2>Vertical sorting with pat-clone</h2>
<button class="clone-trigger-1">Add item</button>
<ul class="pat-sortable pat-clone"
<a class="pat-inject" data-pat-inject="source: .clone-template-1; target: #clone-inject-example::after">Add item via injection</a>
<ul id="clone-inject-example" class="pat-sortable pat-clone"
data-pat-clone="template: .clone-template-1; trigger-element: .clone-trigger-1">
</ul>
<template class="clone-template-1">
Expand Down
17 changes: 2 additions & 15 deletions src/pat/sortable/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,18 @@ export default Base.extend({
this.options = parser.parse(this.$el, false);
this.recordPositions().initScrolling();
this.$el.on("pat-update", this.onPatternUpdate.bind(this));
this.$el.on("patterns-injected", this.recordPositions.bind(this));
},

onPatternUpdate: function (ev, data) {
/* Handler which gets called when pat-update is triggered within
* the .pat-sortable element.
*/
if (data?.pattern !== "clone" || data?.action !== "added" || !data?.dom) {
if (data?.pattern !== "clone" || data?.action !== "added") {
// Nothing to do.
return;
}

this.recordPositions();

events.add_event_listener(
data.dom,
"dragstart",
"pat-sortable--dragstart",
this.onDragStart.bind(this)
);
events.add_event_listener(
data.dom,
"dragend",
"pat-sortable--dragend",
this.onDragEnd.bind(this)
);
},

recordPositions: function () {
Expand Down
33 changes: 33 additions & 0 deletions src/pat/sortable/sortable.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import events from "../../core/events";
import utils from "../../core/utils";
import $ from "jquery";
import Sortable from "./sortable";

Expand Down Expand Up @@ -180,4 +181,36 @@ describe("pat-sortable", function () {

expect(cloned.classList.contains(sortable.options.dragClass)).toBe(true);
});

it("6 - Initializes sorting behavior on pat-injected elements.", async function () {
const Inject = (await import("../inject/inject")).default;

document.body.innerHTML = `
<ul class="pat-sortable">
</ul>
<a class="pat-inject"
data-pat-inject="
source: .inject-template;
target: .pat-sortable::after">Inject</a>
<template class="inject-template">
<li>item</li>
</template>
`;
const el = document.querySelector(".pat-sortable");
const sortable = new Sortable(el);
const el_inject = document.querySelector(".pat-inject");
Inject.init($(el_inject));

el_inject.click();
await utils.timeout(1);

const injected = el.querySelector("li");
expect(injected).toBeTruthy();

const drag_handle = injected.querySelector(".sortable-handle");
expect(drag_handle).toBeTruthy();
drag_handle.dispatchEvent(new Event("dragstart"));

expect(injected.classList.contains(sortable.options.dragClass)).toBe(true);
});
});

0 comments on commit 5472ac4

Please sign in to comment.