Last active
July 27, 2023 04:00
-
-
Save 3cp/5c64eb01ed667128d405d58e275871b7 to your computer and use it in GitHub Desktop.
aurelia2-dnd example: move box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Dumber Gist</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"> | |
<base href="/"> | |
</head> | |
<!-- | |
Dumber Gist uses dumber bundler, the default bundle file | |
is /dist/entry-bundle.js. | |
The starting module is pointed to "main" (data-main attribute on script) | |
which is your src/main.js. | |
--> | |
<body> | |
<my-app></my-app> | |
<script src="/dist/entry-bundle.js" data-main="main"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"aurelia": "latest", | |
"aurelia2-dnd": "^0.3.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box { | |
position: absolute; | |
cursor: pointer; | |
box-sizing: border-box; | |
width: 80px; | |
height: 40px; | |
border: 1px solid #555; | |
background: white; | |
padding: 5px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div | |
ref="dndElement" | |
class="box" | |
style.bind="positionCss" | |
show.bind="!draggingMe" | |
> | |
${item.name} | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {inject, bindable} from 'aurelia'; | |
import {DndService} from 'aurelia2-dnd'; | |
@inject(DndService) | |
export class Box { | |
@bindable item; | |
constructor(dndService) { | |
this.dndService = dndService; | |
} | |
attached() { | |
this.dndService.addSource(this); | |
} | |
detached() { | |
this.dndService.removeSource(this); | |
} | |
dndModel() { | |
return { | |
type: 'moveItem', | |
item: this.item | |
}; | |
} | |
get positionCss() { | |
const x = (this.item && this.item.x) || 0; | |
const y = (this.item && this.item.y) || 0; | |
return { | |
left: x + 'px', | |
top: y + 'px' | |
}; | |
} | |
get draggingMe() { | |
return this.dndService.isProcessing && | |
this.dndService.model.item === this.item; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.container { | |
position: relative; | |
box-sizing: border-box; | |
width: 300px; | |
height: 300px; | |
outline: 1px solid #555; | |
overflow: hidden; | |
margin: 10px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<import from="./box"></import> | |
<div ref="dndElement" class="container"> | |
<box repeat.for="item of items" item.bind="item"></box> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {inject} from 'aurelia'; | |
import {DndService} from 'aurelia2-dnd'; | |
@inject(DndService) | |
export class Container { | |
items = [ | |
{name: 'A', x: 20, y: 20}, | |
{name: 'B', x: 50, y: 200}, | |
{name: 'C', x: 200, y: 100} | |
]; | |
constructor(dndService) { | |
this.dndService = dndService; | |
} | |
attached() { | |
this.dndService.addTarget(this); | |
} | |
detached() { | |
this.dndService.removeTarget(this); | |
} | |
dndCanDrop(model) { | |
return model.type === 'moveItem'; | |
} | |
dndDrop(location) { | |
const {item} = this.dnd.model; | |
const {previewElementRect, targetElementRect} = location; | |
const newLoc = { | |
x: previewElementRect.x - targetElementRect.x, | |
y: previewElementRect.y - targetElementRect.y | |
}; | |
item.x = newLoc.x; | |
item.y = newLoc.y; | |
// move the item to end of array, in order to show it above others | |
const idx = this.items.indexOf(item); | |
if (idx >= 0) { | |
this.items.splice(idx, 1); | |
this.items.push(item); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Aurelia from 'aurelia'; | |
import { MyApp } from './my-app'; | |
Aurelia.app(MyApp).start(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<import from="./container"></import> | |
<container></container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MyApp { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment