forked from silverstripe/silverstripe-linkfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkField.js
More file actions
619 lines (586 loc) · 21.7 KB
/
LinkField.js
File metadata and controls
619 lines (586 loc) · 21.7 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* eslint-disable */
import React, { useState, useEffect, useRef, createContext } from 'react';
import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import {
closestCenter,
DndContext,
KeyboardCode,
KeyboardSensor,
PointerSensor,
useSensor,
useSensors
} from '@dnd-kit/core';
import { arrayMove, SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable';
import { restrictToVerticalAxis, restrictToParentElement } from '@dnd-kit/modifiers';
import fieldHolder from 'components/FieldHolder/FieldHolder';
import LinkPicker from 'components/LinkPicker/LinkPicker';
import LinkPickerTitle from 'components/LinkPicker/LinkPickerTitle';
import Loading from 'components/Loading/Loading';
import LinkModalContainer from 'containers/LinkModalContainer';
import * as toastsActions from 'state/toasts/ToastsActions';
import backend from 'lib/Backend';
import Config from 'lib/Config';
import { joinUrlPaths } from 'lib/urls';
import PropTypes from 'prop-types';
import i18n from 'i18n';
import url from 'url';
import qs from 'qs';
import classnames from 'classnames';
import versionStates from 'constants/versionStates';
export const LinkFieldContext = createContext(null);
// section used in window.ss config
const section = 'SilverStripe\\LinkField\\Controllers\\LinkFieldController';
/**
* value - ID of the Link passed from LinkField entwine
* onChange - callback function passed from LinkField entwine - used to update the underlying <input> form field
* types - types of the Link passed from LinkField entwine
* actions - object of redux actions
* isMulti - whether this field handles multiple links or not
* canCreate - whether this field can create new links or not
* readonly - whether this field is readonly or not
* disabled - whether this field is disabled or not
* ownerID - ID of the owner DataObject
* ownerClass - class name of the owner DataObject
* ownerRelation - name of the relation on the owner DataObject
* inHistoryViewer - if the field is being viewed in the context of the history viewer
*/
const LinkField = ({
value = null,
onChange = () => {},
types = {},
actions,
isMulti = false,
canCreate,
readonly,
disabled,
ownerID,
ownerClass,
ownerRelation,
excludeLinkTextField = false,
inHistoryViewer,
}) => {
const [data, setData] = useState({});
const [editingID, setEditingID] = useState(0);
const [focusOnID, setFocusOnID] = useState(0);
const [focusOnNewLinkWhenClosed, setFocusOnNewLinkWhenClosed] = useState(false);
const [focusOnNewLink, setFocusOnNewLink] = useState(false);
const [focusOnIDWhenAvailable, setFocusOnIDWhenAvailable] = useState(0);
const [focusOnLinkPicker, setFocusOnLinkPicker] = useState(false);
const [loading, setLoading] = useState(false);
const [loadingError, setLoadingError] = useState(false);
const [forceFetch, setForceFetch] = useState(0);
const [isSorting, setIsSorting] = useState(false);
const [linksClassName, setLinksClassName] = useState(classnames({'link-picker-links': true}));
const sensors = useSensors(
useSensor(PointerSensor, {
activationConstraint: {
distance: 10
}
}),
useSensor(KeyboardSensor, {
coordinateGetter: (event, args) => {
event.preventDefault();
const { active, over, droppableContainers } = args.context;
if (!active || !active.data || !active.data.current) {
return;
}
const items = active.data.current.sortable.items;
const overId = over ? over.id : active.id;
const overIndex = items.indexOf(overId);
const activeIndex = items.indexOf(active.id);
const directionUp = -1;
const directionDown = 1;
let nextIndex = overIndex;
let direction = directionDown;
switch (event.code) {
case KeyboardCode.Down:
case KeyboardCode.Right:
nextIndex = Math.min(overIndex + 1, items.length - 1);
break;
case KeyboardCode.Up:
case KeyboardCode.Left:
nextIndex = Math.max(0, overIndex - 1);
direction = directionUp;
break;
default:
return;
}
if (overIndex === nextIndex) {
return;
}
const sortedItems = arrayMove(items, activeIndex, overIndex);
const currentNodeIdAtNextIndex = sortedItems[nextIndex];
if (!droppableContainers.has(currentNodeIdAtNextIndex)) {
return;
}
const activeNode = droppableContainers.get(active.id).node.current;
if (!droppableContainers.has(active.id)) {
return;
}
const newNode = droppableContainers.get(currentNodeIdAtNextIndex).node.current;
const activeRect = activeNode.getBoundingClientRect();
const newRect = newNode.getBoundingClientRect();
const offset = direction === directionDown
? newRect.top - activeRect.bottom
: activeRect.top - newRect.bottom;
return {
x: 0,
y: activeRect.top + direction * (newRect.height + offset),
};
}
})
);
// Sometimes, the value will be given as a numeric string.
// Ensure that an actual number is used
if (typeof value === 'string') {
value = Number(value);
}
// Ensure we have a valid array
let linkIDs = value;
if (!Array.isArray(linkIDs)) {
if (typeof linkIDs === 'number' && linkIDs != 0) {
linkIDs = [linkIDs];
}
if (!linkIDs) {
linkIDs = [];
}
}
// Read data from endpoint and update component state
// This happens any time a link is added or removed and triggers a re-render
useEffect(() => {
if (!editingID && linkIDs.length > 0) {
setLoading(true);
const query = [];
for (const linkID of linkIDs) {
query.push(`itemIDs[]=${linkID}`);
}
const endpoint = `${Config.getSection(section).form.linkForm.dataUrl}?${query.join('&')}`;
backend.get(endpoint)
.then(response => response.json())
.then(responseJson => {
setData(responseJson);
})
.catch(() => {
setLoadingError(true);
})
.finally(() => {
setLoading(false);
// isSorting is set to true on drag start and only set to false here to prevent
// the loading indicator for flickering
setIsSorting(false);
})
}
}, [editingID, value && value.length, forceFetch]);
// Create a ref for the LinkPicker so it can be focused
const linkPickerRef = useRef(null);
// Create refs for each LinkPickerTitle button so they can be focused
let refCount = 0;
const linkButtonRefs = []
for (const linkID of linkIDs) {
linkButtonRefs[linkID] = useRef(null);
refCount++;
}
// Ensure the exact same number of hooks are called on every render
// If this this isn't done then a react error will be thrown when a link is deleted
while (refCount < 256) {
useRef(null);
refCount++;
}
// This sets focus after closed the modal when creating a new link with the single-linkfield
useEffect(() => {
if (!focusOnNewLink || loading) {
return;
}
if (linkIDs.length === 0) {
// User opened modal but exited without of saving
setFocusOnLinkPicker(true);
} else {
// User opened modal and created a new link
const linkID = linkIDs[0];
if (linkButtonRefs[linkID].current) {
setFocusOnIDWhenAvailable(linkID);
}
setFocusOnNewLink(false);
}
}, [focusOnNewLink, loading, linkIDs]);
// This sets focus after closing a modal for both single-linkfield and multi-linkfield
// when editing an existing link
// Note setFocusOnIDWhenAvailable is used because the .focus() must not be called immediately
// if we try to focus on the link at this point then it will immediately lose focus
useEffect(() => {
if (!focusOnID || loading) {
return;
}
setFocusOnIDWhenAvailable(focusOnID);
setFocusOnID(0);
}, [focusOnID, loading]);
// Focus on the a link when it's ready for focus
useEffect(() => {
if (focusOnIDWhenAvailable === 0 || loading) {
return;
}
linkButtonRefs[focusOnIDWhenAvailable].current.focus();
setFocusOnIDWhenAvailable(0);
}, [focusOnIDWhenAvailable, loading]);
// Focus on the link picker when it's available for focus
// The use of a useEffect block isn't strictly needed in all scenarios when focusing on the
// link picker, however it is needed when archiving a single-linkfield.link
// For the sake of consistency, only use `focusOnLinkPicker(true)` to focus on the link picker
useEffect(() => {
if (!focusOnLinkPicker || !linkPickerRef.current || loading) {
return;
}
linkPickerRef.current.focus();
setFocusOnLinkPicker(false);
}, [focusOnLinkPicker, loading, linkPickerRef]);
/**
* Unset the editing ID when the editing modal is closed
* Focus on button used to open the modal
*/
const handleModalClosed = () => {
if (editingID) {
setFocusOnID(editingID);
} else if (focusOnNewLinkWhenClosed) {
// This is called when a user uses single-linkfield to create a new link and then
// successfully saves the link
// Closing the a single-linkfield modal without saving is handled elsewhere
setFocusOnNewLink(true);
} else {
// This is called when a user uses multi-linkfield and either sucessfully save a new link
// or closes the modal without saving
setFocusOnLinkPicker(true);
}
setEditingID(0);
setFocusOnNewLinkWhenClosed(false);
};
/**
* Update the component when the modal successfully saves a link
*/
const handleModalSuccess = (value) => {
handleModalClosed();
const ids = [...linkIDs];
if (!ids.includes(value)) {
ids.push(value);
}
// Update value in the underlying <input> form field
// so that the Page (or other parent DataObject) gets the Link relation set.
// Also likely required in react context for dirty form state, etc.
onChange(isMulti ? ids : ids[0]);
// success toast
actions.toasts.success(i18n._t('LinkField.SAVE_SUCCESS', 'Saved link'));
}
const handleSelectType = () => {
if (!isMulti) {
setFocusOnNewLinkWhenClosed(true);
}
}
/**
* Update the component when the 'Delete' button in the LinkPicker is clicked
*/
const handleDelete = (linkID, deleteType) => {
const versionState = data[linkID]?.versionState || '';
const isVersioned = [
versionStates.draft,
versionStates.modified,
versionStates.published
].includes(versionState);
const deleteText = isVersioned
? i18n._t('LinkField.ARCHIVE_CONFIRM', 'Are you sure you want to archive this link?')
: i18n._t('LinkField.DELETE_CONFIRM', 'Are you sure you want to delete this link?');
if (!window.confirm(deleteText)) {
return false;
}
let endpoint = joinUrlPaths(Config.getSection(section).form.linkForm.deleteUrl, linkID.toString());
const parsedURL = url.parse(endpoint);
const parsedQs = qs.parse(parsedURL.query);
parsedQs.ownerID = ownerID;
parsedQs.ownerClass = ownerClass;
parsedQs.ownerRelation = ownerRelation;
endpoint = url.format({ ...parsedURL, search: qs.stringify(parsedQs)});
const successText = isVersioned
? i18n._t('LinkField.ARCHIVE_SUCCESS', 'Archived link')
: i18n._t('LinkField.DELETE_SUCCESS', 'Deleted link');
const failedText = isVersioned
? i18n._t('LinkField.ARCHIVE_ERROR', 'Failed to archive link')
: i18n._t('LinkField.DELETE_ERROR', 'Failed to delete link');
// CSRF token 'X-SecurityID' headers needs to be present for destructive requests
backend.delete(endpoint, {}, { 'X-SecurityID': Config.get('SecurityID') })
.then(() => actions.toasts.success(successText))
.catch(() => actions.toasts.error(failedText));
// Work out where to put focus after delete
// First create an array of linkIDs sorted by sort order
// Note - key is a linkID
const keysObj = {};
const keys = [];
for (const key in data) {
const sort = Number(data[key].sort);
keysObj[sort] = key;
}
const sorts = Object.keys(keysObj).sort((a, b) => a - b);
for (const sort of sorts) {
keys[keys.length] = Number(keysObj[sort]);
}
const index = keys.indexOf(linkID);
const isOnlyLink = keys.length === 1;
const isLastLink = index === keys.length - 1;
if (isOnlyLink) {
// If link was the only one then put focus on the picker
setFocusOnLinkPicker(true);
} else {
// If more than one link ...
if (isLastLink) {
// and link was last then focus on previous link
setFocusOnID(keys[index - 1]);
} else {
// and link was not last link then focus on next link
setFocusOnID(keys[index + 1]);
}
}
// Update component state
const newData = {...data};
delete newData[linkID];
setData(newData);
// Update parent JsonField data IDs used to update the underlying <input> form field
// Not using Object.keys() to ensure that int key sort order is retained
const newLinkIDs = [];
for (const id of linkIDs) {
if (id !== linkID) {
newLinkIDs.push(id);
}
}
onChange(isMulti ? newLinkIDs : 0);
};
/**
* Update the edit form "Publish" button state to be dirty when an link has an
* unpublished version state
*
* We do not update the state of the "Save" button because LinkField exclusively updates
* via AJAX so that there's no need to save the page to update a Link DataObject
*
* This is fairly hackish code that directly manipulates the DOM, however there's no
* clean way to do this since the publish button is not a react component, and the existing
* jQuery change tracker does not allow independently updating only the publish button
*/
const handleUnpublishedVersionedState = () => {
const cssSelector = [
// CMS page edit form publish button
'.cms-edit-form button[data-text-alternate]#Form_EditForm_action_publish',
// GridField managed DataObject edit form publish button
'.cms-edit-form button[data-text-alternate]#Form_ItemEditForm_action_doPublish'
].join(',');
const publishButton = document.querySelector(cssSelector);
if (!publishButton) {
return;
}
const dataBtnAlternateRemove = publishButton.getAttribute('data-btn-alternate-remove') || '';
dataBtnAlternateRemove.split(' ').forEach((className) => {
if (className) {
publishButton.classList.remove(className);
}
});
const dataBtnAlternateAdd = publishButton.getAttribute('data-btn-alternate-add') || '';
dataBtnAlternateAdd.split(' ').forEach((className) => {
if (className) {
publishButton.classList.add(className);
}
});
const buttonTitle = publishButton.querySelector('.btn__title');
const dataTextAlternate = publishButton.dataset.textAlternate;
if (dataTextAlternate) {
publishButton.dataset.textStandard = buttonTitle.textContent;
buttonTitle.textContent = dataTextAlternate;
}
// Icons in the child element can also be swapped out
const iconElement = publishButton.querySelector('.btn__icon');
if (!iconElement) {
return;
}
// Find the original font-icon class name (if there was one) so we can toggle back to it
let standardIcon = '';
for (const classname of iconElement.classList) {
if (classname.match(/^font-icon-/)) {
standardIcon = classname.replace(/^font-icon-/, '');
break;
}
}
if (standardIcon) {
publishButton.dataset.iconStandard = standardIcon;
iconElement.classList.remove(`font-icon-${standardIcon}`);
}
// Add alternate icon
const alternateIcon = publishButton.getAttribute('data-icon-alternate');
if (alternateIcon) {
iconElement.classList.add(`font-icon-${alternateIcon}`);
}
}
/**
* Render all of the links currently in the field data
*/
const renderLinks = () => {
const links = [];
for (let i = 0; i < linkIDs.length; i++) {
const linkID = linkIDs[i];
// Render dataless item to provide a good loading experience, except if we have single link field
const linkData = data[linkID] || {};
if (!linkData && !isMulti) {
continue;
}
const type = types.hasOwnProperty(linkData.typeKey) ?
types[linkData.typeKey] :
{icon: 'font-icon-link' };
links.push(<LinkPickerTitle
key={linkID}
id={linkID}
title={linkData.title}
description={linkData.description}
versionState={linkData.versionState}
statusFlags={linkData.statusFlags}
typeTitle={type.title || ''}
typeIcon={type.icon}
onDelete={handleDelete}
onClick={() => { setEditingID(linkID); }}
onUnpublishedVersionedState={handleUnpublishedVersionedState}
canDelete={linkData.canDelete ? true : false}
isMulti={isMulti}
isFirst={i === 0}
isLast={i === linkIDs.length - 1}
isSorting={isSorting}
canCreate={canCreate}
readonly={readonly}
disabled={disabled}
buttonRef={linkButtonRefs[linkID]}
/>);
}
return links;
};
const sortableLinks = () => {
if (isMulti && !readonly && !disabled) {
return <div className={linksClassName} onBlur={() => setIsSorting(false)}>
<DndContext modifiers={[restrictToVerticalAxis, restrictToParentElement]}
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<ol className="link-picker__list">
<SortableContext
items={linkIDs}
strategy={verticalListSortingStrategy}
>
{links}
</SortableContext>
</ol>
</DndContext>
</div>
}
return <div>{links}</div>
};
const handleDragStart = (event) => {
setLinksClassName(classnames({
'link-picker__links': true,
'link-picker__links--dragging': true,
}));
setIsSorting(true);
}
/**
* Drag and drop handler for MultiLinkField's
*/
const handleDragEnd = (event) => {
const {active, over} = event;
setLinksClassName(classnames({
'link-picker__links': true,
'link-picker__links--dragging': false,
}));
if (active.id === over.id) {
setIsSorting(false);
return;
}
// Update the local entwine state via onChange so that sorting looks correct on the frontend
// and make a request to the server to update the database
// Note that setIsSorting is not set to true here, instead it's set in the useEffect() block
// higher up in this file
const fromIndex = linkIDs.indexOf(active.id);
const toIndex = linkIDs.indexOf(over.id);
const newLinkIDs = arrayMove(linkIDs, fromIndex, toIndex);
onChange(newLinkIDs);
let endpoint = `${Config.getSection(section).form.linkForm.sortUrl}`;
// CSRF token 'X-SecurityID' headers needs to be present
backend.post(endpoint, { newLinkIDs }, { 'X-SecurityID': Config.get('SecurityID') })
.then(async () => {
onChange(newLinkIDs);
actions.toasts.success(i18n._t('LinkField.SORT_SUCCESS', 'Updated link sort order'));
// Force a rerender so that links are retched so that status flag badges are up to date
setForceFetch(forceFetch + 1);
})
.catch(() => {
actions.toasts.error(i18n._t('LinkField.SORT_ERROR', 'Failed to sort links'));
});
}
const renderLoadingError = loadingError;
const renderPicker = !loadingError && !inHistoryViewer && (isMulti || linkIDs.length === 0);
const renderModal = !loadingError && Boolean(editingID);
const loadingErrorText = i18n._t('LinkField.FAILED_TO_LOAD_LINKS', 'Failed to load link(s)');
const links = renderLinks();
return <LinkFieldContext.Provider value={{
ownerID,
ownerClass,
ownerRelation,
actions,
loading,
excludeLinkTextField,
inHistoryViewer
}}>
<div className="link-field__container">
{ renderLoadingError && <div className="link-field__loading-error">{loadingErrorText}</div> }
{ loading && !isSorting && <Loading containerClass="link-field__loading"/> }
{ renderPicker && <LinkPicker
onModalSuccess={handleModalSuccess}
onModalClosed={handleModalClosed}
types={types}
canCreate={canCreate}
readonly={readonly}
disabled={disabled}
onSelectType={handleSelectType}
dropdownToggleRef={linkPickerRef}
/> }
{sortableLinks()}
{ /* This <LinkModalContainer> is only used for editing EXISTING links */ }
{ renderModal && <LinkModalContainer
types={types}
typeKey={data[editingID]?.typeKey}
isOpen={Boolean(editingID)}
onSuccess={handleModalSuccess}
onClosed={handleModalClosed}
linkID={editingID}
/>
}
</div>
</LinkFieldContext.Provider>;
};
LinkField.propTypes = {
value: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.number, PropTypes.string]),
onChange: PropTypes.func,
types: PropTypes.object.isRequired,
actions: PropTypes.object.isRequired,
isMulti: PropTypes.bool,
canCreate: PropTypes.bool.isRequired,
readonly: PropTypes.bool.isRequired,
disabled: PropTypes.bool.isRequired,
ownerID: PropTypes.number.isRequired,
ownerClass: PropTypes.string.isRequired,
ownerRelation: PropTypes.string.isRequired,
excludeLinkTextField: PropTypes.bool,
inHistoryViewer: PropTypes.bool,
};
// redux actions loaded into props - used to get toast notifications
const mapDispatchToProps = (dispatch) => ({
actions: {
toasts: bindActionCreators(toastsActions, dispatch),
},
});
export { LinkField as Component };
export default compose(
fieldHolder,
connect(null, mapDispatchToProps)
)(LinkField);