-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathLane.js
355 lines (323 loc) · 10 KB
/
Lane.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import React, {Component} from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
import isEqual from 'lodash/isEqual'
import cloneDeep from 'lodash/cloneDeep'
import pick from 'lodash/pick'
import uuidv1 from 'uuid/v1'
import Container from 'rt/dnd/Container'
import Draggable from 'rt/dnd/Draggable'
import * as laneActions from 'rt/actions/LaneActions'
class Lane extends Component {
state = {
loading: false,
currentPage: this.props.currentPage,
addCardMode: false,
collapsed: false,
isDraggingOver: false
}
handleScroll = evt => {
const node = evt.target
const elemScrollPosition = node.scrollHeight - node.scrollTop - node.clientHeight
const {onLaneScroll} = this.props
// In some browsers and/or screen sizes a decimal rest value between 0 and 1 exists, so it should be checked on < 1 instead of < 0
if (elemScrollPosition < 1 && onLaneScroll && !this.state.loading) {
const {currentPage} = this.state
this.setState({loading: true})
const nextPage = currentPage + 1
onLaneScroll(nextPage, this.props.id).then(moreCards => {
if ((moreCards || []).length > 0) {
this.props.actions.paginateLane({
laneId: this.props.id,
newCards: moreCards,
nextPage: nextPage
})
}
this.setState({loading: false})
})
}
}
sortCards(cards, sortFunction) {
if (!cards) return []
if (!sortFunction) return cards
return cards.concat().sort(function (card1, card2) {
return sortFunction(card1, card2)
})
}
laneDidMount = node => {
if (node) {
node.addEventListener('scroll', this.handleScroll)
}
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (!isEqual(this.props.cards, nextProps.cards)) {
this.setState({
currentPage: nextProps.currentPage
})
}
}
removeCard = cardId => {
if (this.props.onBeforeCardDelete && typeof this.props.onBeforeCardDelete === 'function') {
this.props.onBeforeCardDelete(() => {
this.props.onCardDelete && this.props.onCardDelete(cardId, this.props.id)
this.props.actions.removeCard({laneId: this.props.id, cardId: cardId})
})
} else {
this.props.onCardDelete && this.props.onCardDelete(cardId, this.props.id)
this.props.actions.removeCard({laneId: this.props.id, cardId: cardId})
}
}
handleCardClick = (e, card) => {
const {onCardClick} = this.props
onCardClick && onCardClick(card.id, card.metadata, card.laneId)
e.stopPropagation()
}
showEditableCard = () => {
this.setState({addCardMode: true})
}
hideEditableCard = () => {
this.setState({addCardMode: false})
}
addNewCard = params => {
const laneId = this.props.id
const id = uuidv1()
this.hideEditableCard()
let card = {id, ...params}
this.props.actions.addCard({laneId, card})
this.props.onCardAdd(card, laneId)
}
onDragStart = ({payload}) => {
const {handleDragStart} = this.props
handleDragStart && handleDragStart(payload.id, payload.laneId)
}
shouldAcceptDrop = sourceContainerOptions => {
return this.props.droppable && sourceContainerOptions.groupName === this.groupName
}
get groupName() {
const {boardId} = this.props
return `TrelloBoard${boardId}Lane`
}
onDragEnd = (laneId, result) => {
const {handleDragEnd} = this.props
const {addedIndex, payload} = result
if (this.state.isDraggingOver) {
this.setState({isDraggingOver: false})
}
if (addedIndex != null) {
const newCard = {...cloneDeep(payload), laneId}
const response = handleDragEnd ? handleDragEnd(payload.id, payload.laneId, laneId, addedIndex, newCard) : true
if (response === undefined || !!response) {
this.props.actions.moveCardAcrossLanes({
fromLaneId: payload.laneId,
toLaneId: laneId,
cardId: payload.id,
index: addedIndex
})
this.props.onCardMoveAcrossLanes(payload.laneId, laneId, payload.id, addedIndex)
}
return response
}
}
updateCard = updatedCard => {
this.props.actions.updateCard({laneId: this.props.id, card: updatedCard})
this.props.onCardUpdate(this.props.id, updatedCard)
}
renderDragContainer = isDraggingOver => {
const {
id,
cards,
laneSortFunction,
editable,
hideCardDeleteIcon,
cardDraggable,
cardDragClass,
cardDropClass,
tagStyle,
cardStyle,
components,
t
} = this.props
const {addCardMode, collapsed} = this.state
const showableCards = collapsed ? [] : cards
const cardList = this.sortCards(showableCards, laneSortFunction).map((card, idx) => {
const onDeleteCard = () => this.removeCard(card.id)
const cardToRender = (
<components.Card
key={card.id}
index={idx}
style={card.style || cardStyle}
className="react-trello-card"
onDelete={onDeleteCard}
onClick={e => this.handleCardClick(e, card)}
onChange={updatedCard => this.updateCard(updatedCard)}
showDeleteButton={!hideCardDeleteIcon}
tagStyle={tagStyle}
cardDraggable={cardDraggable}
editable={editable}
t={t}
{...card}
/>
)
return cardDraggable && (!card.hasOwnProperty('draggable') || card.draggable) ? (
<Draggable key={card.id}>{cardToRender}</Draggable>
) : (
<span key={card.id}>{cardToRender}</span>
)
})
return (
<components.ScrollableLane ref={this.laneDidMount} isDraggingOver={isDraggingOver}>
<Container
orientation="vertical"
groupName={this.groupName}
dragClass={cardDragClass}
dropClass={cardDropClass}
onDragStart={this.onDragStart}
onDrop={e => this.onDragEnd(id, e)}
onDragEnter={() => this.setState({isDraggingOver: true})}
onDragLeave={() => this.setState({isDraggingOver: false})}
shouldAcceptDrop={this.shouldAcceptDrop}
getChildPayload={index => this.props.getCardDetails(id, index)}>
{cardList}
</Container>
{editable && !addCardMode && <components.AddCardLink onClick={this.showEditableCard} t={t} laneId={id} />}
{addCardMode && (
<components.NewCardForm onCancel={this.hideEditableCard} t={t} laneId={id} onAdd={this.addNewCard} />
)}
</components.ScrollableLane>
)
}
removeLane = () => {
const {id} = this.props
this.props.actions.removeLane({laneId: id})
this.props.onLaneDelete(id)
}
updateTitle = value => {
this.props.actions.updateLane({id: this.props.id, title: value})
this.props.onLaneUpdate(this.props.id, {title: value})
}
renderHeader = pickedProps => {
const {components} = this.props
return (
<components.LaneHeader
{...pickedProps}
onDelete={this.removeLane}
onDoubleClick={this.toggleLaneCollapsed}
updateTitle={this.updateTitle}
/>
)
}
toggleLaneCollapsed = () => {
this.props.collapsibleLanes && this.setState(state => ({collapsed: !state.collapsed}))
}
render() {
const {loading, isDraggingOver, collapsed} = this.state
const {
actions,
id,
boardId,
title,
index,
laneSortFunction,
style,
cardStyle,
tagStyle,
titleStyle,
labelStyle,
cards,
label,
currentPage,
draggable,
collapsibleLanes,
droppable,
onCardMoveAcrossLanes,
onCardClick,
onBeforeCardDelete,
onCardDelete,
onCardAdd,
onCardUpdate,
onLaneDelete,
onLaneUpdate,
onLaneClick,
onLaneScroll,
editable,
laneDraggable,
cardDraggable,
cardDragClass,
cardDropClass,
canAddLanes,
t,
components,
className,
...otherProps
} = this.props
const allClassNames = classNames('react-trello-lane', this.props.className || '')
const showFooter = collapsibleLanes && cards.length > 0
return (
<components.Section
{...otherProps}
key={id}
onClick={() => onLaneClick && onLaneClick(id)}
draggable={false}
className={allClassNames}>
{this.renderHeader({id, cards, t, ...otherProps})}
{this.renderDragContainer(isDraggingOver)}
{loading && <components.Loader />}
{showFooter && <components.LaneFooter onClick={this.toggleLaneCollapsed} collapsed={collapsed} />}
</components.Section>
)
}
}
Lane.propTypes = {
actions: PropTypes.object,
id: PropTypes.string.isRequired,
boardId: PropTypes.string,
title: PropTypes.node,
index: PropTypes.number,
laneSortFunction: PropTypes.func,
style: PropTypes.object,
cardStyle: PropTypes.object,
tagStyle: PropTypes.object,
titleStyle: PropTypes.object,
labelStyle: PropTypes.object,
cards: PropTypes.array,
label: PropTypes.string,
currentPage: PropTypes.number,
draggable: PropTypes.bool,
collapsibleLanes: PropTypes.bool,
droppable: PropTypes.bool,
onCardMoveAcrossLanes: PropTypes.func,
onCardClick: PropTypes.func,
onBeforeCardDelete: PropTypes.func,
onCardDelete: PropTypes.func,
onCardAdd: PropTypes.func,
onCardUpdate: PropTypes.func,
onLaneDelete: PropTypes.func,
onLaneUpdate: PropTypes.func,
onLaneClick: PropTypes.func,
onLaneScroll: PropTypes.func,
editable: PropTypes.bool,
laneDraggable: PropTypes.bool,
cardDraggable: PropTypes.bool,
cardDragClass: PropTypes.string,
cardDropClass: PropTypes.string,
canAddLanes: PropTypes.bool,
t: PropTypes.func.isRequired,
components: PropTypes.object,
className: PropTypes.string
}
Lane.defaultProps = {
style: {},
titleStyle: {},
labelStyle: {},
label: undefined,
editable: false,
onLaneUpdate: () => {},
onCardAdd: () => {},
onCardUpdate: () => {}
}
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators(laneActions, dispatch)
})
export default connect(null, mapDispatchToProps)(Lane)