npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webhandle/drag-sortable-list

v1.0.2

Published

Makes lists of things sortable by dragging

Downloads

1

Readme

Drag Sortable List

Creates a draggable list/queue of html elements. It can accept files and html elements dragged into the list.

While it does have a lot of functionality and extensibility, it's pretty light weight at something like 9k (unzipped) as opposed to something like sortable.js, which is great, but 300k.

Install

npm install @webhandle/drag-sortable-list

This can be used in the normal way, but there's also a compiled version at public/js/index.js

Usage

Markup

<div class="list1 webhandle-drag-sortable-list">
	<div class="cell">
		<span class="handle">↕</span>
		a 
	</div>
	<div class="cell">
		<span class="handle">↕</span>
		b 
	</div>
	<div class="cell">
		<span class="handle">↕</span>
		c 
	</div>
	<div class="cell">
		<span class="handle">↕</span>
		d
		<br>
		e 
	</div>
</div>

Script

The most basic, "just let the items get dragged up and down", script

import ListView from '@webhandle/drag-sortable-list'

let elList1 = document.querySelector('.list1')
let list1 = new ListView({
	el: elList1
	, mobileHandleSelector: '.handle'
})
list1.render()

Styling

No styling is required at all, but as an example of some really basic styling:

.list1 {
	width: 500px;
	height: 500px;
	border: solid 1px black;
	
	
	.cell {
		border-top: solid transparent 2px;
		border-bottom: solid transparent 2px;
		min-height: 10px;
		background-color: #eeeeee;
		background-clip: padding-box;
		padding: 10px;
		
		&.dragging {
			background-color: #cccccc;
		}
		
		.handle {
			display: inline-block;
			text-align: center;
			width: 40px;
			background-color: #dddddd;
		}
	}
}

Dragging HTML page objects

This list supports dragging other page objects onto the list. They'll have to be draggable="true of course, for the page to respond to the drag. Also, the drag event MUST have data starting with data:text/label, or the list (by default) won't see it as something which can be added to the list.


document.querySelectorAll('.draggable-thing').forEach(cell => {
	cell.addEventListener('dragstart', (evt) => {
		let uriListText = cell.getAttribute('data-uri-list')
		evt.dataTransfer.setData('text', uriListText)
		evt.dataTransfer.setData('text/uri-list', uriListText)
		
		let label = `data:text/label,${cell.innerText}`
		evt.dataTransfer.setData(label, label)
	})
})
<div class="draggable-thing" draggable="true" data-uri-list="http://files/file1.txt,http://files/file2.txt">
	box 1
</div>

Reordering

An event emitter is available which issues events for re-orders and drops

let emitter = list1.emitter
emitter.on('list-change', (evt) => {
	let eventLog = document.querySelector('.event-log')
	evt.cells.forEach(cell => {
		let msg = evt.type + ': ' + cell.innerText
		// do something with msg, type is: reorder, drop
		
		// get all the cells to update the order or something
		let cells = list1.getCells()
	})
})

Customization

Responding to events within the cells

You can add event handlers in the normal Backbone fashion.

import ListView from '@webhandle/drag-sortable-list'

let elList1 = document.querySelector('.list1')
let list1 = new ListView({
	el: elList1
	, mobileHandleSelector: '.cell .handle'
	, events: {
		'click a': 'linkClick'
	}
	, linkClick(evt, selected) {
		evt.preventDefault()
		console.log(selected.innerText)
	}
})
list1.render()

Options

For creating new cells from dropped items.


/**
 * Creates permanent cells for files dropped into the list
 * @param {array[FileEntry]} files 
 * @returns an array of Elements
 */
createCellsForFiles(files) 


/**
 * Creates permanent cells for resource objects dropped into the list
 * @param {array[string]} uriList 
 * @returns an array of Elements 
 */
createCellsForUriList(uriList) 


/**
 * Creates permanent cells for drops of unknown types.
 * @param {Event} evt 
 * @returns An array of elements
 */
createCellsForUnknownType(evt)