Skip to content

Commit af222a1

Browse files
committed
All tests passing on rewrite.
1 parent 49c822b commit af222a1

File tree

15 files changed

+211
-158
lines changed

15 files changed

+211
-158
lines changed

.eslintrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"parser": "babel-eslint",
33
"rules": {
44
"strict": 0,
5-
"quotes": ["single"],
6-
"curly": "multi-line",
5+
"quotes": [1, "single"],
6+
"curly": [1, "multi-line"],
77
"camelcase": 0,
88
"comma-dangle": 1,
9-
"no-use-before-define": "nofunc",
9+
"no-use-before-define": [1, "nofunc"],
1010
"no-underscore-dangle": 0,
1111
"no-unused-vars": 1,
1212
"new-cap": 0,

Makefile

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22
# Thanks @andreypopp
33

44
BIN = ./node_modules/.bin
5-
SRC = $(wildcard lib/*.js)
6-
LIB = $(SRC:lib/%.js=dist/%.js)
7-
MIN = $(SRC:lib/%.js=dist/%.min.js)
5+
DIST = dist
6+
LIB = $(DIST)/react-draggable.js
7+
MIN = $(DIST)/react-draggable.min.js
88

9-
.PHONY: test dev
9+
.PHONY: test dev build clean
10+
11+
clean:
12+
rm -rf dist
1013

1114
build: $(LIB) $(MIN)
1215

1316
# Allows usage of `make install`, `make link`
1417
install link:
1518
@npm $@
1619

17-
# FIXME
18-
dist/%.min.js: $(BIN)
19-
@$(BIN)/uglifyjs dist/react-draggable.js \
20-
--output dist/react-draggable.min.js \
21-
--source-map dist/react-draggable.min.map \
22-
--source-map-url react-draggable.min.map \
23-
--in-source-map dist/react-draggable.map \
20+
dist/%.min.js: $(LIB) $(BIN)
21+
@$(BIN)/uglifyjs $< \
22+
--output $@ \
23+
--source-map $@.map \
24+
--source-map-url $(basename $@.map) \
25+
--in-source-map $<.map \
2426
--compress warnings=false
2527

2628
dist/%.js: $(BIN)
@@ -59,6 +61,6 @@ release-minor: test build
5961
release-major: test build
6062
@$(call release,major)
6163

62-
publish:
64+
publish: build
6365
git push --tags origin HEAD:master
6466
npm publish

karma.conf.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ module.exports = function(config) {
2020
cache: true,
2121
module: {
2222
loaders: [
23-
{test: /\.js$/, loader: 'jsx-loader'}
23+
{
24+
test: /\.(?:js|es).?$/,
25+
loader: 'babel-loader',
26+
exclude: /(node_modules)/
27+
}
2428
]
29+
},
30+
resolve: {
31+
extensions: ["", ".webpack.js", ".web.js", ".js", ".es6"]
2532
}
2633
},
2734

lib/Draggable.es6

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
'use strict';
2-
31
import React from 'react';
42
import classNames from 'classnames';
53
import assign from 'object-assign';
64
import {autobind} from './utils/shims';
75
import {createUIEvent, createCSSTransform} from './utils/domFns';
86
import {canDragX, canDragY, getBoundPosition, snapToGrid} from './utils/positionFns';
97
import DraggableCore from './DraggableCore';
8+
import log from './utils/log';
109

1110
//
1211
// Define <Draggable>
@@ -27,10 +26,10 @@ export default class Draggable extends DraggableCore {
2726
}
2827

2928
onDragStart(e, coreEvent) {
30-
console.log('Draggable: onDragStart: %j', coreEvent.position);
29+
log('Draggable: onDragStart: %j', coreEvent.position);
3130

3231
// Short-circuit if user's callback killed it.
33-
let shouldStart = this.props.onStart(e, coreEvent);
32+
let shouldStart = this.props.onStart(e, createUIEvent(this, coreEvent));
3433
// Kills start event on core as well, so move handlers are never bound.
3534
if (shouldStart === false) return false;
3635

@@ -41,39 +40,42 @@ export default class Draggable extends DraggableCore {
4140

4241
onDrag(e, coreEvent) {
4342
if (!this.state.dragging) return false;
44-
console.log('Draggable: onDrag: %j', coreEvent.position);
43+
log('Draggable: onDrag: %j', coreEvent.position);
4544

4645
// Short-circuit if user's callback killed it.
47-
let shouldUpdate = this.props.onDrag(e, coreEvent);
46+
let shouldUpdate = this.props.onDrag(e, createUIEvent(this, coreEvent));
4847
if (shouldUpdate === false) return false;
4948

50-
var clientX = this.state.clientX + coreEvent.position.deltaX;
51-
var clientY = this.state.clientY + coreEvent.position.deltaY;
49+
let newState = {
50+
clientX: this.state.clientX + coreEvent.position.deltaX,
51+
clientY: this.state.clientY + coreEvent.position.deltaY
52+
};
5253

5354
// Snap to grid if prop has been provided
5455
if (Array.isArray(this.props.grid)) {
55-
[clientX, clientY] = snapToGrid(this.props.grid, clientX, clientY);
56+
newState.lastX = (this.state.lastX || newState.clientX) + coreEvent.position.deltaX;
57+
newState.lastY = (this.state.lastY || newState.clientY) + coreEvent.position.deltaY;
58+
// Eslint bug, it thinks newState.clientY is undefined
59+
/*eslint no-undef:0*/
60+
[newState.clientX, newState.clientY] = snapToGrid(this.props.grid, newState.lastX, newState.lastY);
5661
}
5762

5863
// Keep within bounds.
5964
if (this.props.bounds) {
60-
[clientX, clientY] = getBoundPosition(this, clientX, clientY);
65+
[newState.clientX, newState.clientY] = getBoundPosition(this, newState.clientX, newState.clientY);
6166
}
6267

63-
// TODO create drag event using createUIEvent and call back with it
64-
// this.props.onDrag(createUIEvent(e));
65-
66-
this.setState({clientX, clientY});
68+
this.setState(newState);
6769
}
6870

69-
onDragEnd(e, coreEvent) {
71+
onDragStop(e, coreEvent) {
7072
if (!this.state.dragging) return false;
7173

7274
// Short-circuit if user's callback killed it.
73-
let shouldStop = this.props.onStop(e, coreEvent);
75+
let shouldStop = this.props.onStop(e, createUIEvent(this, coreEvent));
7476
if (shouldStop === false) return false;
7577

76-
console.log('Draggable: onDragEnd: %j', coreEvent.position);
78+
log('Draggable: onDragStop: %j', coreEvent.position);
7779

7880
this.setState({
7981
dragging: false
@@ -86,7 +88,7 @@ export default class Draggable extends DraggableCore {
8688
// without worrying about whether or not it is relatively or absolutely positioned.
8789
// If the item you are dragging already has a transform set, wrap it in a <span> so <Draggable>
8890
// has a clean slate.
89-
var style = createCSSTransform({
91+
let style = createCSSTransform({
9092
// Set left if horizontal drag is enabled
9193
x: canDragX(this) ?
9294
this.state.clientX :
@@ -104,7 +106,7 @@ export default class Draggable extends DraggableCore {
104106
}
105107

106108
// Mark with class while dragging
107-
var className = classNames((this.props.children.props.className || ''), 'react-draggable', {
109+
let className = classNames((this.props.children.props.className || ''), 'react-draggable', {
108110
'react-draggable-dragging': this.state.dragging,
109111
'react-draggable-dragged': this.state.dragged
110112
});
@@ -113,7 +115,7 @@ export default class Draggable extends DraggableCore {
113115
// This makes it flexible to use whatever element is wanted (div, ul, etc)
114116
return (
115117
<DraggableCore {...this.props} style={style} className={className}
116-
onStart={this.onDragStart} onDrag={this.onDrag} onStop={this.onDragEnd}>
118+
onStart={this.onDragStart} onDrag={this.onDrag} onStop={this.onDragStop}>
117119
{React.Children.only(this.props.children)}
118120
</DraggableCore>
119121
);
@@ -147,7 +149,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
147149
* Example:
148150
*
149151
* ```jsx
150-
* var App = React.createClass({
152+
* let App = React.createClass({
151153
* render: function () {
152154
* return (
153155
* <Draggable bounds={{right: 300, bottom: 300}}>
@@ -174,7 +176,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
174176
* Example:
175177
*
176178
* ```jsx
177-
* var App = React.createClass({
179+
* let App = React.createClass({
178180
* render: function () {
179181
* return (
180182
* <Draggable grid={[25, 25]}>
@@ -193,7 +195,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
193195
* Example:
194196
*
195197
* ```jsx
196-
* var App = React.createClass({
198+
* let App = React.createClass({
197199
* render: function () {
198200
* return (
199201
* <Draggable start={{x: 25, y: 25}}>
@@ -215,7 +217,7 @@ Draggable.propTypes = assign({}, DraggableCore.propTypes, {
215217
* Example:
216218
*
217219
* ```jsx
218-
* var App = React.createClass({
220+
* let App = React.createClass({
219221
* render: function () {
220222
* return (
221223
* <Draggable zIndex={100}>

0 commit comments

Comments
 (0)