1import { Sortable } from 'sortablejs'; 2 3export class WaniKaniSwap { 4 static pluginName = 'wanikaniSwap' 5 static lastSwapEl = null 6 7 constructor() { 8 this.defaults = {swapClass: 'sortable-swap-highlight'} 9 } 10 11 dragStart({dragEl}) { 12 WaniKaniSwap.lastSwapEl = dragEl 13 } 14 15 dragOver({dragEl, completed, target, onMove, activeSortable, changed, cancel}) { 16 let el = this.sortable.el 17 let prevSwapEl = WaniKaniSwap.lastSwapEl; 18 if (target && target !== el && target !== dragEl && this.sortable.options.group.name == activeSortable.options.group.name) { 19 if (onMove(target) !== false) { 20 target.classList.toggle(this.options.swapClass, true); 21 WaniKaniSwap.lastSwapEl = target; 22 } else { 23 WaniKaniSwap.lastSwapEl = null; 24 } 25 26 } 27 if (target == dragEl) { 28 WaniKaniSwap.lastSwapEl = null; 29 } 30 if (prevSwapEl && prevSwapEl !== WaniKaniSwap.lastSwapEl) { 31 prevSwapEl.classList.toggle(this.options.swapClass, false); 32 } 33 changed(); 34 completed(true); 35 cancel(); 36 } 37 38 drop({ activeSortable, putSortable, dragEl }) { 39 let toSortable = (putSortable || this.sortable); 40 let options = this.options; 41 WaniKaniSwap.lastSwapEl && WaniKaniSwap.lastSwapEl.classList.toggle(this.options.swapClass, false); 42 if (WaniKaniSwap.lastSwapEl && (this.options.wanikaniSwap || putSortable && putSortable.options.swap)) { 43 if (dragEl !== WaniKaniSwap.lastSwapEl) { 44 toSortable.captureAnimationState(); 45 if (toSortable !== activeSortable) { 46 activeSortable.captureAnimationState(); 47 } 48 WaniKaniSwap.swapNodes(dragEl, WaniKaniSwap.lastSwapEl); 49 toSortable.animateAll(); 50 if (toSortable !== activeSortable) { 51 activeSortable.animateAll() 52 }; 53 } 54 } 55 } 56 57 nulling() { 58 WaniKaniSwap.lastSwapEl = null; 59 } 60 61 static swapNodes(n1, n2) { 62 let p1 = n1.parentNode, p2 = n2.parentNode; 63 if (!p1 || !p2 || p1.isEqualNode(n2) || p2.isEqualNode(n1)) return; 64 65 let i1 = Sortable.utils.index(n1); 66 let i2 = Sortable.utils.index(n2); 67 68 if (p1.isEqualNode(p2) && i1 < i2) { 69 i2++; 70 } 71 p1.insertBefore(n2, p1.children[i1]); 72 p2.insertBefore(n1, p2.children[i2]); 73 const n1Width = n1.getAttribute('width') 74 n1.setAttribute('width', n2.getAttribute('width')) 75 n2.setAttribute('width', n1Width) 76 } 77}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.