-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortableItemMixin.js
More file actions
72 lines (70 loc) · 2.38 KB
/
SortableItemMixin.js
File metadata and controls
72 lines (70 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var cx = require('classnames');
/**
* Elements with 'is-isolated' in the class list will not trigger on mouse down events.
*/
module.exports = {
getDefaultProps: function() {
return {
sortableStyle: {},
onSortableItemMount: function(){},
onSortableItemMouseDown: function(){},
isDraggable: true,
// Used by the Sortable component
_isPlaceholder: false,
_isDragging: false
}
},
handleSortableItemMouseDown: function(e) {
var evt = {
pageX: e.pageX,
pageY: e.pageY,
offset: this.getPosition()
};
if (!e.target.classList.contains('is-isolated') && this.props.isDraggable) {
this.props.onSortableItemMouseDown(evt, this.props.sortableIndex);
e.stopPropagation();
}
},
outerHeight: function() {
var element = ReactDOM.findDOMNode(this);
var style = getComputedStyle(element);
return element.offsetHeight + parseInt(style.marginTop) + parseInt(style.marginBottom);
},
outerWidth: function() {
var element = ReactDOM.findDOMNode(this);
var style = getComputedStyle(element);
return element.offsetWidth + parseInt(style.marginLeft) + parseInt(style.marginRight);
},
getPosition: function() {
return {
left: ReactDOM.findDOMNode(this).offsetLeft,
top: ReactDOM.findDOMNode(this).offsetTop
}
},
componentDidMount: function(){
this.props.onSortableItemMount(this.getPosition(), this.outerWidth(), this.outerHeight(), this.props.sortableIndex);
},
componentDidUpdate: function(){
this.props.onSortableItemMount(this.getPosition(), this.outerWidth(), this.outerHeight(), this.props.sortableIndex);
},
renderWithSortable: function(item){
var classNames = cx({
'SortableItem': true,
'is-dragging': this.props._isDragging,
'is-undraggable': !this.props.isDraggable,
'is-placeholder': this.props._isPlaceholder
});
return React.cloneElement(
this.props._isPlaceholder && this.getPlaceholderContent && Object.prototype.toString.call(this.getPlaceholderContent) === '[object Function]'
? this.getPlaceholderContent() : item, {
className: classNames,
style: this.props.sortableStyle,
key: this.props.sortableIndex,
onMouseDown: this.handleSortableItemMouseDown,
onMouseUp: this.handleSortableItemMouseUp
});
}
};