2020-10-02 15:10:00 +00:00
|
|
|
var _ = require('underscore');
|
2021-07-15 15:50:28 +00:00
|
|
|
var dispose = require('app/client/lib/dispose');
|
|
|
|
var TextEditor = require('app/client/widgets/TextEditor');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
const {Autocomplete} = require('app/client/lib/autocomplete');
|
|
|
|
const {ACIndexImpl, buildHighlightedDom} = require('app/client/lib/ACIndex');
|
2024-04-26 20:34:16 +00:00
|
|
|
const {makeT} = require('app/client/lib/localization');
|
|
|
|
const {
|
|
|
|
buildDropdownConditionFilter,
|
|
|
|
ChoiceItem,
|
|
|
|
cssChoiceList,
|
|
|
|
cssMatchText,
|
|
|
|
cssPlusButton,
|
|
|
|
cssPlusIcon,
|
|
|
|
} = require('app/client/widgets/ChoiceListEditor');
|
|
|
|
const {icon} = require('app/client/ui2018/icons');
|
2021-07-08 21:35:16 +00:00
|
|
|
const {menuCssClass} = require('app/client/ui2018/menus');
|
2023-09-21 16:57:58 +00:00
|
|
|
const {testId, theme} = require('app/client/ui2018/cssVars');
|
2021-07-15 15:50:28 +00:00
|
|
|
const {choiceToken, cssChoiceACItem} = require('app/client/widgets/ChoiceToken');
|
2023-06-13 17:14:01 +00:00
|
|
|
const {dom, styled} = require('grainjs');
|
2024-04-26 20:34:16 +00:00
|
|
|
|
|
|
|
const t = makeT('ChoiceEditor');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ChoiceEditor - TextEditor with a dropdown for possible choices.
|
|
|
|
*/
|
|
|
|
function ChoiceEditor(options) {
|
|
|
|
TextEditor.call(this, options);
|
|
|
|
|
2024-04-26 20:34:16 +00:00
|
|
|
this.widgetOptionsJson = options.field.widgetOptionsJson;
|
|
|
|
this.choices = this.widgetOptionsJson.peek().choices || [];
|
|
|
|
this.choicesSet = new Set(this.choices);
|
|
|
|
this.choiceOptions = this.widgetOptionsJson.peek().choiceOptions || {};
|
|
|
|
|
|
|
|
this.hasDropdownCondition = Boolean(options.field.dropdownCondition.peek()?.text);
|
|
|
|
this.dropdownConditionError;
|
|
|
|
|
|
|
|
let acItems = this.choices.map(c => new ChoiceItem(c, false, false));
|
|
|
|
if (this.hasDropdownCondition) {
|
|
|
|
try {
|
|
|
|
const dropdownConditionFilter = this.buildDropdownConditionFilter();
|
|
|
|
acItems = acItems.filter((item) => dropdownConditionFilter(item));
|
|
|
|
} catch (e) {
|
|
|
|
acItems = [];
|
|
|
|
this.dropdownConditionError = e.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const acIndex = new ACIndexImpl(acItems);
|
|
|
|
this._acOptions = {
|
|
|
|
popperOptions: {
|
|
|
|
placement: 'bottom'
|
|
|
|
},
|
|
|
|
menuCssClass: `${menuCssClass} ${cssChoiceList.className} test-autocomplete`,
|
|
|
|
buildNoItemsMessage: this.buildNoItemsMessage.bind(this),
|
|
|
|
search: (term) => this.maybeShowAddNew(acIndex.search(term), term),
|
|
|
|
renderItem: (item, highlightFunc) => this.renderACItem(item, highlightFunc),
|
|
|
|
getItemText: (item) => item.label,
|
|
|
|
onClick: () => this.options.commands.fieldEditSave(),
|
|
|
|
};
|
|
|
|
|
2023-06-13 17:14:01 +00:00
|
|
|
if (!options.readonly && options.field.viewSection().parentKey() === "single") {
|
|
|
|
this.cellEditorDiv.classList.add(cssChoiceEditor.className);
|
|
|
|
this.cellEditorDiv.appendChild(cssChoiceEditIcon('Dropdown'));
|
|
|
|
}
|
2024-04-26 20:34:16 +00:00
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
// Whether to include a button to show a new choice.
|
|
|
|
// TODO: Disable when the user cannot change column configuration.
|
2024-04-26 20:34:16 +00:00
|
|
|
this.enableAddNew = !this.hasDropdownCondition;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-07-08 21:35:16 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
dispose.makeDisposable(ChoiceEditor);
|
|
|
|
_.extend(ChoiceEditor.prototype, TextEditor.prototype);
|
|
|
|
|
2021-07-08 21:35:16 +00:00
|
|
|
ChoiceEditor.prototype.getCellValue = function() {
|
|
|
|
const selectedItem = this.autocomplete && this.autocomplete.getSelectedItem();
|
2023-09-21 16:57:58 +00:00
|
|
|
if (selectedItem) {
|
|
|
|
return selectedItem.label;
|
|
|
|
} else if (this.textInput.value.trim() === '') {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return TextEditor.prototype.getCellValue.call(this);
|
|
|
|
}
|
2021-07-08 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ChoiceEditor.prototype.renderACItem = function(item, highlightFunc) {
|
|
|
|
const options = this.choiceOptions[item.label];
|
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
return cssChoiceACItem(
|
|
|
|
(item.isNew ?
|
|
|
|
[cssChoiceACItem.cls('-new'), cssPlusButton(cssPlusIcon('Plus')), testId('choice-editor-new-item')] :
|
|
|
|
[cssChoiceACItem.cls('-with-new', this.showAddNew)]
|
|
|
|
),
|
|
|
|
choiceToken(
|
2021-07-08 21:35:16 +00:00
|
|
|
buildHighlightedDom(item.label, highlightFunc, cssMatchText),
|
2021-07-15 15:50:28 +00:00
|
|
|
options || {},
|
|
|
|
dom.style('max-width', '100%'),
|
2021-07-08 21:35:16 +00:00
|
|
|
testId('choice-editor-item-label')
|
|
|
|
),
|
|
|
|
testId('choice-editor-item'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChoiceEditor.prototype.attach = function(cellElem) {
|
|
|
|
TextEditor.prototype.attach.call(this, cellElem);
|
2021-07-15 15:50:28 +00:00
|
|
|
// Don't create autocomplete if readonly.
|
|
|
|
if (this.options.readonly) { return; }
|
2021-07-08 21:35:16 +00:00
|
|
|
|
2024-04-26 20:34:16 +00:00
|
|
|
this.autocomplete = Autocomplete.create(this, this.textInput, this._acOptions);
|
2021-07-08 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
/**
|
|
|
|
* Updates list of valid choices with any new ones that may have been
|
|
|
|
* added from directly inside the editor (via the "add new" item in autocomplete).
|
|
|
|
*/
|
|
|
|
ChoiceEditor.prototype.prepForSave = async function() {
|
|
|
|
const selectedItem = this.autocomplete && this.autocomplete.getSelectedItem();
|
|
|
|
if (selectedItem && selectedItem.isNew) {
|
2024-04-26 20:34:16 +00:00
|
|
|
const choices = this.widgetOptionsJson.prop('choices');
|
2021-07-21 10:59:45 +00:00
|
|
|
await choices.saveOnly([...(choices.peek() || []), selectedItem.label]);
|
2021-07-15 15:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 20:34:16 +00:00
|
|
|
ChoiceEditor.prototype.buildDropdownConditionFilter = function() {
|
|
|
|
const dropdownConditionCompiled = this.options.field.dropdownConditionCompiled.get();
|
|
|
|
if (dropdownConditionCompiled?.kind !== 'success') {
|
|
|
|
throw new Error('Dropdown condition is not compiled');
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildDropdownConditionFilter({
|
|
|
|
dropdownConditionCompiled: dropdownConditionCompiled.result,
|
|
|
|
docData: this.options.gristDoc.docData,
|
|
|
|
tableId: this.options.field.tableId(),
|
|
|
|
rowId: this.options.rowId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ChoiceEditor.prototype.buildNoItemsMessage = function() {
|
|
|
|
if (this.dropdownConditionError) {
|
|
|
|
return t('Error in dropdown condition');
|
|
|
|
} else if (this.hasDropdownCondition) {
|
|
|
|
return t('No choices matching condition');
|
|
|
|
} else {
|
|
|
|
return t('No choices to select');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
ChoiceEditor.prototype.maybeShowAddNew = function(result, text) {
|
|
|
|
// TODO: This logic is also mostly duplicated in ChoiceListEditor and ReferenceEditor.
|
|
|
|
// See if there's anything common we can factor out and re-use.
|
|
|
|
this.showAddNew = false;
|
2024-04-26 20:34:16 +00:00
|
|
|
if (!this.enableAddNew) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:50:28 +00:00
|
|
|
const trimmedText = text.trim();
|
2024-04-26 20:34:16 +00:00
|
|
|
if (!trimmedText || this.choicesSet.has(trimmedText)) {
|
|
|
|
return result;
|
|
|
|
}
|
2021-07-15 15:50:28 +00:00
|
|
|
|
2023-09-21 16:57:58 +00:00
|
|
|
const addNewItem = new ChoiceItem(trimmedText, false, false, true);
|
2021-07-15 15:50:28 +00:00
|
|
|
if (result.items.find((item) => item.cleanText === addNewItem.cleanText)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-26 20:34:16 +00:00
|
|
|
result.extraItems.push(addNewItem);
|
2021-07-15 15:50:28 +00:00
|
|
|
this.showAddNew = true;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-06-13 17:14:01 +00:00
|
|
|
const cssChoiceEditIcon = styled(icon, `
|
2023-09-21 16:57:58 +00:00
|
|
|
background-color: ${theme.lightText};
|
2023-06-13 17:14:01 +00:00
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
margin: 3px 3px 0 3px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssChoiceEditor = styled('div', `
|
|
|
|
& > .celleditor_text_editor, & > .celleditor_content_measure {
|
|
|
|
padding-left: 18px;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
module.exports = ChoiceEditor;
|