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');
|
2021-07-15 15:50:28 +00:00
|
|
|
const {ChoiceItem, cssChoiceList, cssMatchText, cssPlusButton,
|
|
|
|
cssPlusIcon} = require('app/client/widgets/ChoiceListEditor');
|
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');
|
|
|
|
const {icon} = require('../ui2018/icons');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ChoiceEditor - TextEditor with a dropdown for possible choices.
|
|
|
|
*/
|
|
|
|
function ChoiceEditor(options) {
|
|
|
|
TextEditor.call(this, options);
|
|
|
|
|
|
|
|
this.choices = options.field.widgetOptionsJson.peek().choices || [];
|
2021-07-08 21:35:16 +00:00
|
|
|
this.choiceOptions = options.field.widgetOptionsJson.peek().choiceOptions || {};
|
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'));
|
|
|
|
}
|
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.
|
|
|
|
this.enableAddNew = true;
|
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
|
|
|
|
2023-09-21 16:57:58 +00:00
|
|
|
const acItems = this.choices.map(c => new ChoiceItem(c, false, false));
|
2021-07-08 21:35:16 +00:00
|
|
|
const acIndex = new ACIndexImpl(acItems);
|
|
|
|
const acOptions = {
|
|
|
|
popperOptions: {
|
|
|
|
placement: 'bottom'
|
|
|
|
},
|
2021-07-15 15:50:28 +00:00
|
|
|
menuCssClass: `${menuCssClass} ${cssChoiceList.className} test-autocomplete`,
|
|
|
|
search: (term) => this.maybeShowAddNew(acIndex.search(term), term),
|
2021-07-08 21:35:16 +00:00
|
|
|
renderItem: (item, highlightFunc) => this.renderACItem(item, highlightFunc),
|
|
|
|
getItemText: (item) => item.label,
|
|
|
|
onClick: () => this.options.commands.fieldEditSave(),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.autocomplete = Autocomplete.create(this, this.textInput, acOptions);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const choices = this.options.field.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
const trimmedText = text.trim();
|
|
|
|
if (!this.enableAddNew || !trimmedText) { return result; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.items.push(addNewItem);
|
|
|
|
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;
|