Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doubleClick support to slot selection #575

Merged
merged 4 commits into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion examples/demos/selectable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ let Selectable = React.createClass({
onSelectEvent={event => alert(event.title)}
onSelectSlot={(slotInfo) => alert(
`selected slot: \n\nstart ${slotInfo.start.toLocaleString()} ` +
`\nend: ${slotInfo.end.toLocaleString()}`
`\nend: ${slotInfo.end.toLocaleString()}` +
`\naction: ${slotInfo.action}`
)}
/>
</div>
Expand Down
43 changes: 24 additions & 19 deletions src/BackgroundCells.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ class BackgroundCells extends React.Component {
longPressThreshold: this.props.longPressThreshold,
})

let selectorClicksHandler = (point, actionType) => {
if (!isEvent(findDOMNode(this), point)) {
let rowBox = getBoundsForNode(node)
let { range, rtl } = this.props;

if (pointInBox(rowBox, point)) {
let width = slotWidth(getBoundsForNode(node), range.length);
let currentCell = getCellAtX(rowBox, point.x, width, rtl, range.length);

this._selectSlot({
startIdx: currentCell,
endIdx: currentCell,
action: actionType,
})
}
}

this._initial = {}
this.setState({ selecting: false })
};
ZhoraKhachatryanArmweb-zz marked this conversation as resolved.
Show resolved Hide resolved

selector.on('selecting', box => {
let { range, rtl } = this.props;

Expand Down Expand Up @@ -125,26 +146,10 @@ class BackgroundCells extends React.Component {
})

selector
.on('click', point => {
if (!isEvent(findDOMNode(this), point)) {
let rowBox = getBoundsForNode(node)
let { range, rtl } = this.props;

if (pointInBox(rowBox, point)) {
let width = slotWidth(getBoundsForNode(node), range.length);
let currentCell = getCellAtX(rowBox, point.x, width, rtl, range.length);

this._selectSlot({
startIdx: currentCell,
endIdx: currentCell,
action: 'click',
})
}
}
.on('click', point => selectorClicksHandler(point, 'click'))

this._initial = {}
this.setState({ selecting: false })
})
selector
.on('doubleClick', point => selectorClicksHandler(point, 'doubleClick'))

selector
.on('select', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Calendar extends React.Component {
* start: Date,
* end: Date,
* slots: Array<Date>,
* action: "select" | "click"
* action: "select" | "click" | "doubleClick"
* }
* ) => any
* ```
Expand Down
15 changes: 10 additions & 5 deletions src/DayColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ class DayColumn extends React.Component {
}
}

let selectorClicksHandler = (box, actionType) => {
if (!isEvent(findDOMNode(this), box))
this._selectSlot({ ...selectionState(box), action: actionType })

this.setState({ selecting: false })
}
ZhoraKhachatryanArmweb-zz marked this conversation as resolved.
Show resolved Hide resolved

selector.on('selecting', maybeSelect)
selector.on('selectStart', maybeSelect)

Expand All @@ -305,12 +312,10 @@ class DayColumn extends React.Component {
})

selector
.on('click', (box) => {
if (!isEvent(findDOMNode(this), box))
this._selectSlot({ ...selectionState(box), action: 'click' })
.on('click', box => selectorClicksHandler(box, 'click'))

this.setState({ selecting: false })
})
selector
.on('doubleClick', (box) => selectorClicksHandler(box, 'doubleClick'))

selector
.on('select', () => {
Expand Down
38 changes: 32 additions & 6 deletions src/Selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function getEventCoordinates(e) {
}

const clickTolerance = 5;
const clickInterval = 250;

class Selection {

Expand Down Expand Up @@ -224,7 +225,7 @@ class Selection {
}

_handleTerminatingEvent(e) {
const { pageX, pageY, clientX, clientY } = getEventCoordinates(e);
const { pageX, pageY } = getEventCoordinates(e);

this.selecting = false;

Expand All @@ -243,17 +244,42 @@ class Selection {
return this.emit('reset')
}

if(click && inRoot)
return this.emit('click', {
if(click && inRoot) {
return this._handleClickEvent(e);
}

// User drag-clicked in the Selectable area
if(!click)
return this.emit('select', bounds)
}

_handleClickEvent(e) {
const { pageX, pageY, clientX, clientY } = getEventCoordinates(e);
const now = new Date().getTime();

if (this._lastClickData && now - this._lastClickData.timestamp < clickInterval) {
// Double click event
this._lastClickData = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be cleared here? It seems like if you click 3 times really fast you'll get 1 click and 2 dblClick events instead of: click, dblClick, click

timestamp: now,
isDblClick: true,
};
return this.emit('doubleClick', {
x: pageX,
y: pageY,
clientX: clientX,
clientY: clientY,
})
}

// User drag-clicked in the Selectable area
if(!click)
return this.emit('select', bounds)
this._lastClickData = {
timestamp: now,
};
return this.emit('click', {
x: pageX,
y: pageY,
clientX: clientX,
clientY: clientY,
});
}

_handleMoveEvent(e) {
Expand Down