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');
|
|
|
|
const {testId} = require('app/client/ui2018/cssVars');
|
2021-07-15 15:50:28 +00:00
|
|
|
const {choiceToken, cssChoiceACItem} = require('app/client/widgets/ChoiceToken');
|
2021-07-08 21:35:16 +00:00
|
|
|
const {dom} = require('grainjs');
|
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 || {};
|
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();
|
|
|
|
return selectedItem ? selectedItem.label : TextEditor.prototype.getCellValue.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
const acItems = this.choices.map(c => new ChoiceItem(c, false));
|
|
|
|
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');
|
|
|
|
await choices.saveOnly([...choices.peek(), selectedItem.label]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; }
|
|
|
|
|
|
|
|
const addNewItem = new ChoiceItem(trimmedText, false, true);
|
|
|
|
if (result.items.find((item) => item.cleanText === addNewItem.cleanText)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.items.push(addNewItem);
|
|
|
|
this.showAddNew = true;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
module.exports = ChoiceEditor;
|