-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcolumn-resize-handle.js
81 lines (61 loc) · 2.21 KB
/
column-resize-handle.js
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
73
74
75
76
77
78
79
80
81
import Ember from 'ember';
import $ from 'jquery';
const { computed, uuid } = Ember;
export default Ember.Component.extend({
classNames: ['resize-handle'],
column: null,
eventNamespace: computed({
get() {
return `.rc${uuid()}`;
}
}),
didInsertElement() {
const eventNamespace = this.get('eventNamespace');
const eventString = combineEventsWithNamespace(['mousedown', 'touchstart'], eventNamespace);
this.$().on(eventString, this._onPointerDown.bind(this));
},
willDestroyElement() {
const eventNamespace = this.get('eventNamespace');
this.$().off(eventNamespace);
},
_onPointerDown(event) {
// Ignore everything except left-click dragging
if (event.which !== 1) {
return;
}
this.startX = getPointerX(event);
this.startWidth = this.get('column.width');
const ownerDocument = $(this.element.ownerDocument);
const eventNamespace = this.get('eventNamespace');
const moveEventString = combineEventsWithNamespace(['mousemove', 'touchmove'], eventNamespace);
const upEventString = combineEventsWithNamespace(['mouseup', 'touchend'], eventNamespace);
ownerDocument.on(moveEventString, this._onPointerMove.bind(this));
ownerDocument.on(upEventString, this._onPointerUp.bind(this));
},
_onPointerMove(event) {
const minWidth = this.get('column.minWidth');
let diff = getPointerX(event) - this.startX;
let newWidth = this.startWidth + diff;
if (newWidth < minWidth) {
newWidth = minWidth;
}
this.get('column').set('width', newWidth);
this.attrs.onColumnWidthChange();
},
_onPointerUp(/* event */) {
const ownerDocument = $(this.element.ownerDocument);
const eventNamespace = this.get('eventNamespace');
const events = ['mouseup', 'touchend', 'mousemove', 'touchmove'];
ownerDocument.off(combineEventsWithNamespace(events, eventNamespace));
}
});
function getPointerX(event) {
if (event.type.indexOf('touch') === 0) {
return (event.originalEvent.touches[0] || event.originalEvent.changedTouches[0]).pageX;
}
return event.pageX;
}
function combineEventsWithNamespace(events, namespace) {
let eventString = events.join(`${namespace} `);
return `${eventString}${namespace}`;
}