@osimatic/helpers-js 1.1.58 → 1.1.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/sortable_list.js +38 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.1.58",
3
+ "version": "1.1.60",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -0,0 +1,38 @@
1
+ class SortableList {
2
+ static init(sortableList, clientYOffset=0) {
3
+ const items = sortableList.find('[draggable="true"]');
4
+ items.get().forEach(item => {
5
+ $(item).on('dragstart', () => {
6
+ // Adding dragging class to an item after a delay
7
+ setTimeout(() => $(item).addClass('dragging'), 0);
8
+
9
+ });
10
+
11
+ // Removing dragging class from the item on the dragend event
12
+ $(item).on('dragend', () => $(item).removeClass('dragging'));
13
+ });
14
+
15
+ const initSortableList = (e) => {
16
+ e.preventDefault();
17
+
18
+ const draggingItem = sortableList.find('.dragging');
19
+
20
+ // Getting all items except currently dragging and making an array of them
21
+ const siblings = [...sortableList.find('[draggable="true"]:not(.dragging)').get()];
22
+
23
+ // Finding the sibling after which the dragging item should be placed
24
+ let nextSibling = siblings.find(sibling => {
25
+ //console.log(e.clientY, sibling.offsetTop + sibling.offsetHeight / 2, sibling.offsetTop, sibling.offsetHeight, sibling);
26
+ return e.clientY+clientYOffset <= sibling.offsetTop + sibling.offsetHeight / 2;
27
+ });
28
+
29
+ // Inserting the dragging item before the found sibling
30
+ sortableList.get()[0].insertBefore(draggingItem.get()[0], nextSibling);
31
+ };
32
+
33
+ sortableList.on('dragover', initSortableList);
34
+ sortableList.on('dragenter', e => e.preventDefault());
35
+ }
36
+ }
37
+
38
+ module.exports = { SortableList };