Skip to content

Commit

Permalink
feat(sortable): new sortableMove event to trigger during sortable i…
Browse files Browse the repository at this point in the history
…tem drag
  • Loading branch information
nolimits4web committed Dec 2, 2021
1 parent 6fe117a commit ae055b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/core/components/sortable/sortable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export namespace Sortable {
/** Event will be triggered when sortable mode is disabled */
sortableDisable: (listEl: HTMLElement) => void;
/** Event will be triggered after user release currently sorting element in new position. indexes is an object with from and to properties with from/to index numbers of sorted list item */
sortableSort: (listEl: HTMLElement, indexes: SortIndexes) => void;
sortableSort: (itemEl: HTMLElement, indexes: SortIndexes, listEl: HTMLElement) => void;
/** Event will be triggered on every list item move during sorting */
sortableMove: (itemEl: HTMLElement, listEl: HTMLElement) => void;
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/core/components/sortable/sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,37 @@ const Sortable = {
const currentElHeight = $currentEl.height();
const sortingElOffset = sortingElOffsetLocal + translate;

let currentTranslate;
const prevTranslate = $currentEl[0].f7Translate;

if (
sortingElOffset >= currentElOffset - currentElHeight / 2 &&
$sortingEl.index() < $currentEl.index()
) {
$currentEl.transform(`translate3d(0, ${-sortingElHeight}px,0)`);
currentTranslate = -sortingElHeight;
$currentEl.transform(`translate3d(0, ${currentTranslate}px,0)`);
$insertAfterEl = $currentEl;
$insertBeforeEl = undefined;
} else if (
sortingElOffset <= currentElOffset + currentElHeight / 2 &&
$sortingEl.index() > $currentEl.index()
) {
$currentEl.transform(`translate3d(0, ${sortingElHeight}px,0)`);
currentTranslate = sortingElHeight;
$currentEl[0].f7Translate = currentTranslate;
$currentEl.transform(`translate3d(0, ${currentTranslate}px,0)`);
$insertAfterEl = undefined;
if (!$insertBeforeEl) $insertBeforeEl = $currentEl;
} else {
currentTranslate = undefined;
$currentEl.transform('translate3d(0, 0%,0)');
}

if (prevTranslate !== currentTranslate) {
$currentEl.trigger('sortable:move');
app.emit('sortableMove', $sortableContainer[0], $currentEl[0]);
}

$currentEl[0].f7Translate = currentTranslate;
});
}
function handleTouchEnd() {
Expand Down
8 changes: 8 additions & 0 deletions src/react/components/list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { VirtualList } from 'framework7/types';
onSortableEnable? : (...args: any[]) => void
onSortableDisable? : (...args: any[]) => void
onSortableSort? : (sortData?: any) => void
onSortableMove? : (itemEl?: any) => void
onTabShow? : (el?: HTMLElement) => void
onTabHide? : (el?: HTMLElement) => void
ref?: React.MutableRefObject<{el: HTMLElement | null; f7VirtualList: () => VirtualList.VirtualList}>;
Expand Down Expand Up @@ -124,6 +125,11 @@ const List = forwardRef((props, ref) => {
emit(props, 'sortableSort', sortData);
};

const onSortableMove = (el, listEl) => {
if (elRef.current !== listEl) return;
emit(props, 'sortableMove', el);
};

useImperativeHandle(ref, () => ({
el: elRef.current,
f7VirtualList: () => f7VirtualList.current,
Expand All @@ -136,6 +142,7 @@ const List = forwardRef((props, ref) => {
f7.on('sortableEnable', onSortableEnable);
f7.on('sortableDisable', onSortableDisable);
f7.on('sortableSort', onSortableSort);
f7.on('sortableMove', onSortableMove);
});
};

Expand All @@ -144,6 +151,7 @@ const List = forwardRef((props, ref) => {
f7.off('sortableEnable', onSortableEnable);
f7.off('sortableDisable', onSortableDisable);
f7.off('sortableSort', onSortableSort);
f7.off('sortableMove', onSortableMove);
};

const onMount = () => {
Expand Down
6 changes: 6 additions & 0 deletions src/svelte/components/list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
if (listEl !== el) return;
emit('sortableSort', [sortData]);
}
function onSortableMove(listItemEl, listEl) {
if (listEl !== el) return;
emit('sortableMove', [listEl]);
}
useTab(() => el, emit);
Expand All @@ -139,6 +143,7 @@
app.f7.on('sortableEnable', onSortableEnable);
app.f7.on('sortableDisable', onSortableDisable);
app.f7.on('sortableSort', onSortableSort);
app.f7.on('sortableMove', onSortableMove);
if (!virtualList) return;
const vlParams = virtualListParams || {};
Expand Down Expand Up @@ -178,6 +183,7 @@
app.f7.off('sortableEnable', onSortableEnable);
app.f7.off('sortableDisable', onSortableDisable);
app.f7.off('sortableSort', onSortableSort);
app.f7.off('sortableMove', onSortableMove);
if (f7VirtualList && f7VirtualList.destroy) {
f7VirtualList.destroy();
Expand Down
7 changes: 7 additions & 0 deletions src/vue/components/list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default {
'sortable:enable',
'sortable:disable',
'sortable:sort',
'sortable:move',
'virtual:itembeforeinsert',
'virtual:beforeclear',
'virtual:itemsbeforeinsert',
Expand All @@ -88,6 +89,10 @@ export default {
if (elRef.value !== listEl) return;
emit('sortable:sort', sortData);
};
const onSortableMove = (el, listEl) => {
if (elRef.value !== listEl) return;
emit(props, 'sortable:move', el);
};
useTab(elRef, emit);
Expand All @@ -96,6 +101,7 @@ export default {
f7.on('sortableEnable', onSortableEnable);
f7.on('sortableDisable', onSortableDisable);
f7.on('sortableSort', onSortableSort);
f7.on('sortableMove', onSortableMove);
if (!props.virtualList) return;
const vlParams = props.virtualListParams || {};
Expand Down Expand Up @@ -136,6 +142,7 @@ export default {
f7.off('sortableEnable', onSortableEnable);
f7.off('sortableDisable', onSortableDisable);
f7.off('sortableSort', onSortableSort);
f7.off('sortableMove', onSortableMove);
if (!(props.virtualList && f7VirtualList)) return;
if (f7VirtualList.destroy) f7VirtualList.destroy();
Expand Down

0 comments on commit ae055b2

Please sign in to comment.