Skip to content

Commit d1b7f5d

Browse files
author
Matt Zabriskie
committed
Improving comments
1 parent 1143011 commit d1b7f5d

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lib/draggable.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,30 @@ module.exports = React.createClass({
239239

240240
getInitialState: function () {
241241
return {
242+
// Whether or not currently dragging
242243
dragging: false,
244+
245+
// Start top/left of this.getDOMNode()
243246
startX: 0, startY: 0,
247+
248+
// Offset between start top/left and mouse top/left
244249
offsetX: 0, offsetY: 0,
250+
251+
// Current top/left of this.getDOMNode()
245252
clientX: 0, clientY: 0
246253
};
247254
},
248255

249256
handleMouseDown: function (e) {
250257
var node = this.getDOMNode();
251258

259+
// Short circuit if handle or cancel prop was provided and selector doesn't match
252260
if ((this.props.handle && !matchesSelector(e.target, this.props.handle)) ||
253261
(this.props.cancel && matchesSelector(e.target, this.props.cancel))) {
254262
return;
255263
}
256264

265+
// Initiate dragging
257266
this.setState({
258267
dragging: true,
259268
offsetX: e.clientX,
@@ -262,31 +271,39 @@ module.exports = React.createClass({
262271
startY: parseInt(node.style.top, 10) || 0
263272
});
264273

274+
// Call event handler
265275
this.props.onStart(e, createUIEvent(this));
266276

277+
// Add event handlers
267278
addEvent(window, 'mousemove', this.handleMouseMove);
268279
addEvent(window, 'mouseup', this.handleMouseUp);
269280
},
270281

271282
handleMouseUp: function (e) {
283+
// Short circuit if not currently dragging
272284
if (!this.state.dragging) {
273285
return;
274286
}
275287

288+
// Turn off dragging
276289
this.setState({
277290
dragging: false
278291
});
279292

293+
// Call event handler
280294
this.props.onStop(e, createUIEvent(this));
281295

296+
// Remove event handlers
282297
removeEvent(window, 'mousemove', this.handleMouseMove);
283298
removeEvent(window, 'mouseup', this.handleMouseUp);
284299
},
285300

286301
handleMouseMove: function (e) {
302+
// Calculate top and left
287303
var clientX = (this.state.startX + (e.clientX - this.state.offsetX));
288304
var clientY = (this.state.startY + (e.clientY - this.state.offsetY));
289305

306+
// Snap to grid if prop has been provided
290307
if (Array.isArray(this.props.grid)) {
291308
clientX = Math.abs(clientX - this.state.clientX) >= this.props.grid[0]
292309
? clientX
@@ -297,20 +314,30 @@ module.exports = React.createClass({
297314
: this.state.clientY;
298315
}
299316

317+
// Update top and left
300318
this.setState({
301319
clientX: clientX,
302320
clientY: clientY
303321
});
304322

323+
// Call event handler
305324
this.props.onDrag(e, createUIEvent(this));
306325
},
307326

308327
render: function () {
309328
var style = {
310-
top: canDragY(this) ? this.state.clientY : this.state.startY,
311-
left: canDragX(this) ? this.state.clientX : this.state.startX
329+
// Set top if vertical drag is enabled
330+
top: canDragY(this)
331+
? this.state.clientY
332+
: this.state.startY,
333+
334+
// Set left if horizontal drag is enabled
335+
left: canDragX(this)
336+
? this.state.clientX
337+
: this.state.startX
312338
};
313339

340+
// Set zIndex if currently dragging and prop has been provided
314341
if (this.state.dragging && !isNaN(this.props.zIndex)) {
315342
style.zIndex = this.props.zIndex;
316343
}

0 commit comments

Comments
 (0)