2022-08-09 13:43:44 +00:00
|
|
|
import { ACResults, buildHighlightedDom, cleanText as clean, HighlightFunc } from 'app/client/lib/ACIndex';
|
2021-11-01 15:48:08 +00:00
|
|
|
import { Autocomplete } from 'app/client/lib/autocomplete';
|
|
|
|
import { ICellItem } from 'app/client/models/ColumnACIndexes';
|
|
|
|
import { reportError } from 'app/client/models/errors';
|
|
|
|
import { colors, testId, vars } from 'app/client/ui2018/cssVars';
|
|
|
|
import { icon } from 'app/client/ui2018/icons';
|
|
|
|
import { menuCssClass } from 'app/client/ui2018/menus';
|
2022-08-08 13:32:50 +00:00
|
|
|
import { FieldOptions } from 'app/client/widgets/NewBaseEditor';
|
2021-11-01 15:48:08 +00:00
|
|
|
import { NTextEditor } from 'app/client/widgets/NTextEditor';
|
|
|
|
import { nocaseEqual, ReferenceUtils } from 'app/client/lib/ReferenceUtils';
|
|
|
|
import { undef } from 'app/common/gutil';
|
|
|
|
import { styled } from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A ReferenceEditor offers an autocomplete of choices from the referenced table.
|
|
|
|
*/
|
|
|
|
export class ReferenceEditor extends NTextEditor {
|
|
|
|
private _enableAddNew: boolean;
|
|
|
|
private _showAddNew: boolean = false;
|
|
|
|
private _autocomplete?: Autocomplete<ICellItem>;
|
2021-11-01 15:48:08 +00:00
|
|
|
private _utils: ReferenceUtils;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-08-08 13:32:50 +00:00
|
|
|
constructor(options: FieldOptions) {
|
2020-10-02 15:10:00 +00:00
|
|
|
super(options);
|
|
|
|
|
|
|
|
const docData = options.gristDoc.docData;
|
2021-11-01 15:48:08 +00:00
|
|
|
this._utils = new ReferenceUtils(options.field, docData);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-11-01 15:48:08 +00:00
|
|
|
const vcol = this._utils.visibleColModel;
|
2021-07-15 15:50:28 +00:00
|
|
|
this._enableAddNew = vcol && !vcol.isRealFormula() && !!vcol.colId();
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Decorate the editor to look like a reference column value (with a "link" icon).
|
2021-06-17 16:41:07 +00:00
|
|
|
// But not on readonly mode - here we will reuse default decoration
|
|
|
|
if (!options.readonly) {
|
|
|
|
this.cellEditorDiv.classList.add(cssRefEditor.className);
|
|
|
|
this.cellEditorDiv.appendChild(cssRefEditIcon('FieldReference'));
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:48:08 +00:00
|
|
|
this.textInput.value = undef(options.state, options.editValue, this._idToText());
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-11-01 15:48:08 +00:00
|
|
|
const needReload = (options.editValue === undefined && !this._utils.tableData.isLoaded);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// The referenced table has probably already been fetched (because there must already be a
|
|
|
|
// Reference widget instantiated), but it's better to avoid this assumption.
|
2021-11-01 15:48:08 +00:00
|
|
|
docData.fetchTable(this._utils.refTableId).then(() => {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
if (needReload && this.textInput.value === '') {
|
2021-11-01 15:48:08 +00:00
|
|
|
this.textInput.value = undef(options.state, options.editValue, this._idToText());
|
2020-10-02 15:10:00 +00:00
|
|
|
this.resizeInput();
|
|
|
|
}
|
|
|
|
if (this._autocomplete) {
|
|
|
|
if (options.editValue === undefined) {
|
|
|
|
this._autocomplete.search((items) => items.findIndex((item) => item.rowId === options.cellValue));
|
|
|
|
} else {
|
|
|
|
this._autocomplete.search();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reportError);
|
|
|
|
}
|
|
|
|
|
2021-02-04 03:17:17 +00:00
|
|
|
public attach(cellElem: Element): void {
|
|
|
|
super.attach(cellElem);
|
2021-06-17 16:41:07 +00:00
|
|
|
// don't create autocomplete for readonly mode
|
|
|
|
if (this.options.readonly) { return; }
|
2020-10-02 15:10:00 +00:00
|
|
|
this._autocomplete = this.autoDispose(new Autocomplete<ICellItem>(this.textInput, {
|
|
|
|
menuCssClass: menuCssClass + ' ' + cssRefList.className,
|
|
|
|
search: this._doSearch.bind(this),
|
|
|
|
renderItem: this._renderItem.bind(this),
|
|
|
|
getItemText: (item) => item.text,
|
|
|
|
onClick: () => this.options.commands.fieldEditSaveHere(),
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the 'new' item is saved, add it to the referenced table first. See _buildSourceList
|
|
|
|
*/
|
|
|
|
public async prepForSave() {
|
|
|
|
const selectedItem = this._autocomplete && this._autocomplete.getSelectedItem();
|
|
|
|
if (selectedItem &&
|
|
|
|
selectedItem.rowId === 'new' &&
|
|
|
|
selectedItem.text === this.textInput.value) {
|
2021-11-01 15:48:08 +00:00
|
|
|
const colInfo = {[this._utils.visibleColId]: this.textInput.value};
|
|
|
|
selectedItem.rowId = await this._utils.tableData.sendTableAction(["AddRecord", null, colInfo]);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCellValue() {
|
|
|
|
const selectedItem = this._autocomplete && this._autocomplete.getSelectedItem();
|
|
|
|
|
|
|
|
if (selectedItem) {
|
|
|
|
// Selected from the autocomplete dropdown; so we know the *value* (i.e. rowId).
|
|
|
|
return selectedItem.rowId;
|
2021-11-01 15:48:08 +00:00
|
|
|
} else if (nocaseEqual(this.textInput.value, this._idToText())) {
|
2020-10-02 15:10:00 +00:00
|
|
|
// Unchanged from what's already in the cell.
|
|
|
|
return this.options.cellValue;
|
|
|
|
}
|
|
|
|
|
2021-11-01 15:48:08 +00:00
|
|
|
return super.getCellValue();
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 15:48:08 +00:00
|
|
|
private _idToText() {
|
|
|
|
return this._utils.idToText(this.options.cellValue);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
/**
|
|
|
|
* If the search text does not match anything exactly, adds 'new' item to it.
|
|
|
|
*
|
|
|
|
* Also see: prepForSave.
|
|
|
|
*/
|
2020-10-02 15:10:00 +00:00
|
|
|
private async _doSearch(text: string): Promise<ACResults<ICellItem>> {
|
2021-11-01 15:48:08 +00:00
|
|
|
const result = this._utils.autocompleteSearch(text);
|
2021-07-15 15:50:28 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this._showAddNew = false;
|
2021-07-15 15:50:28 +00:00
|
|
|
if (!this._enableAddNew || !text) { return result; }
|
|
|
|
|
2022-08-09 13:43:44 +00:00
|
|
|
const cleanText = clean(text);
|
2021-07-15 15:50:28 +00:00
|
|
|
if (result.items.find((item) => item.cleanText === cleanText)) {
|
|
|
|
return result;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-07-15 15:50:28 +00:00
|
|
|
|
|
|
|
result.items.push({rowId: 'new', text, cleanText});
|
|
|
|
this._showAddNew = true;
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _renderItem(item: ICellItem, highlightFunc: HighlightFunc) {
|
2021-05-12 14:34:49 +00:00
|
|
|
return renderACItem(item.text, highlightFunc, item.rowId === 'new', this._showAddNew);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderACItem(text: string, highlightFunc: HighlightFunc, isAddNew: boolean, withSpaceForNew: boolean) {
|
|
|
|
if (isAddNew) {
|
|
|
|
return cssRefItem(cssRefItem.cls('-new'),
|
|
|
|
cssPlusButton(cssPlusIcon('Plus')), text,
|
|
|
|
testId('ref-editor-item'), testId('ref-editor-new-item'),
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
}
|
2021-05-12 14:34:49 +00:00
|
|
|
return cssRefItem(cssRefItem.cls('-with-new', withSpaceForNew),
|
|
|
|
buildHighlightedDom(text, highlightFunc, cssMatchText),
|
|
|
|
testId('ref-editor-item'),
|
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const cssRefEditor = styled('div', `
|
|
|
|
& > .celleditor_text_editor, & > .celleditor_content_measure {
|
2021-02-04 03:17:17 +00:00
|
|
|
padding-left: 18px;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2021-08-12 18:06:40 +00:00
|
|
|
// Set z-index to be higher than the 1000 set for .cell_editor.
|
|
|
|
export const cssRefList = styled('div', `
|
|
|
|
z-index: 1001;
|
2020-10-02 15:10:00 +00:00
|
|
|
overflow-y: auto;
|
|
|
|
padding: 8px 0 0 0;
|
|
|
|
--weaseljs-menu-item-padding: 8px 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
// We need to now the height of the sticky "+" element.
|
|
|
|
const addNewHeight = '37px';
|
|
|
|
|
|
|
|
const cssRefItem = styled('li', `
|
|
|
|
display: block;
|
|
|
|
font-family: ${vars.fontFamily};
|
2021-07-15 15:50:28 +00:00
|
|
|
white-space: pre;
|
2020-10-02 15:10:00 +00:00
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
outline: none;
|
|
|
|
padding: var(--weaseljs-menu-item-padding, 8px 24px);
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
&.selected {
|
|
|
|
background-color: var(--weaseljs-selected-background-color, #5AC09C);
|
|
|
|
color: var(--weaseljs-selected-color, white);
|
|
|
|
}
|
|
|
|
&-with-new {
|
|
|
|
scroll-margin-bottom: ${addNewHeight};
|
|
|
|
}
|
|
|
|
&-new {
|
|
|
|
color: ${colors.slate};
|
|
|
|
position: sticky;
|
|
|
|
bottom: 0px;
|
|
|
|
height: ${addNewHeight};
|
|
|
|
background-color: white;
|
|
|
|
border-top: 1px solid ${colors.mediumGrey};
|
|
|
|
scroll-margin-bottom: initial;
|
|
|
|
}
|
|
|
|
&-new.selected {
|
|
|
|
color: ${colors.lightGrey};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2021-08-12 18:06:40 +00:00
|
|
|
export const cssPlusButton = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
display: inline-block;
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
border-radius: 20px;
|
|
|
|
margin-right: 8px;
|
|
|
|
text-align: center;
|
|
|
|
background-color: ${colors.lightGreen};
|
|
|
|
color: ${colors.light};
|
|
|
|
|
|
|
|
.selected > & {
|
|
|
|
background-color: ${colors.darkGreen};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2021-08-12 18:06:40 +00:00
|
|
|
export const cssPlusIcon = styled(icon, `
|
2020-10-02 15:10:00 +00:00
|
|
|
background-color: ${colors.light};
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssRefEditIcon = styled(icon, `
|
|
|
|
background-color: ${colors.slate};
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
2021-02-04 03:17:17 +00:00
|
|
|
margin: 3px 3px 0 3px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMatchText = styled('span', `
|
|
|
|
color: ${colors.lightGreen};
|
|
|
|
.selected > & {
|
|
|
|
color: ${colors.lighterGreen};
|
|
|
|
}
|
|
|
|
`);
|