Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Scaled drag #173

Merged
merged 4 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/drag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="/dist/kd.css">
<style>
body, html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
font-family: Roboto, -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Segoe UI", "Oxygen", "Ubuntu", "Cantarell", "Open Sans", sans-serif;
}
h1 {
font-size: 36px;
line-height: 48px;
text-align: center;
}
.foo {
background: pink;
width: 800px;
height: 800px;
transform: matrix(.5,0,0,.5,0,0);
}
.bar {
background: gray;
width: 200px;
height: 200px;
}
.baz {
background: cyan;
width: 50px;
height: 50px;
}
</style>
</head>
<body>

<script src="/example/bundle.js"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/drag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var kd = require('../lib/index')

var main = new kd.View({ cssClass: 'lobilo' })
main.appendToDomBody()

var header = new kd.CustomHTMLView({
partial: 'Drag scaled items',
tagName: 'h1',
})
var foo = new kd.View({ cssClass: 'foo', draggable: true })
var bar = new kd.View({ cssClass: 'bar', draggable: { containment: true } })
var baz = new kd.View({ cssClass: 'baz', draggable: { containment: true } })

bar.addSubView(baz)
foo.addSubView(bar)

main.addSubView(header)
main.addSubView(foo)
2 changes: 1 addition & 1 deletion example/index.html → examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="/dist/kd.css">
</head>
<body>

<script src="/example/bundle.js"></script>
</body>
</html>
File renamed without changes.
31 changes: 23 additions & 8 deletions lib/core/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ module.exports = class KDView extends KDObject

getDomElement:-> @domElement

getElement:-> @getDomElement()[0]
getElement:-> @element ? @element = @domElement[0]

getTagName:-> @options.tagName || 'div'

Expand Down Expand Up @@ -322,12 +322,20 @@ module.exports = class KDView extends KDObject
@getElement().classList.contains cssClass

getBounds: ->
$el = @domElement
rect = @getElement().getBoundingClientRect()

x : @getX()
y : @getY()
w : @getWidth()
h : @getHeight()
n : @constructor.name
return {
x : rect.left
y : rect.top
w : $el.outerWidth no
h : $el.outerHeight no
n : @constructor.name
}

getScale: ->
el = @getElement()
return el.getBoundingClientRect().width / el.offsetWidth

setRandomBG:->@getDomElement().css "background-color", KD.utils.getRandomRGB()

Expand All @@ -346,7 +354,7 @@ module.exports = class KDView extends KDObject
positionOptions.position = "absolute"
@$().css positionOptions

getWidth:-> @$().outerWidth no
getWidth:-> @getDomElement().outerWidth no

setWidth:(w, unit = "px")->
@getElement().style.width = "#{w}#{unit}"
Expand Down Expand Up @@ -708,6 +716,9 @@ module.exports = class KDView extends KDObject

dragState = @dragState

if (ps = @parent.getScale()) isnt 1
@dragState.parentScale = ps

if options.containment

dragState.containment = {}
Expand Down Expand Up @@ -757,7 +768,7 @@ module.exports = class KDView extends KDObject

drag:(event, delta)->

{directionX, directionY, axis, containment} = @dragState
{directionX, directionY, axis, containment, parentScale} = @dragState

{x, y} = delta
dragPos = @dragState.position
Expand All @@ -769,6 +780,10 @@ module.exports = class KDView extends KDObject
dragCurDir = dragDir.current
{axis} = @getOptions().draggable

if parentScale
x = x * 1 / parentScale
y = y * 1 / parentScale

draggedDistance = if axis
if axis is "x" then Math.abs x else Math.abs y
else Math.max Math.abs(x), Math.abs(y)
Expand Down