Files
wekan/client/components/sidebar/sidebarCustomFields.js
T
Lauri Ojansivu 67b91c8504 Custom Fields popup: a new field appears, and the pencil is beside the name.
Two reports against the popup the Custom Fields heading opens.

A new field made from it never appeared. "Add custom field" sits OUTSIDE the list
of fields, so its data context is the popup's own - which is the CARD - and the
shared create/edit form decided insert-vs-update on whether its context had an
`_id`. A card has one, so creating a field from a card ran the UPDATE branch
against a custom field whose id was a card's: no such document, nothing written,
nothing said. From the board sidebar the same form works, because there the
context has no `_id`, which is why this only ever happened from a card.

Both halves are fixed. The popup hands the create form an empty context, since a
new field is made from nothing. And the form no longer trusts a bare `_id`: it
asks whether that id NAMES a custom field, so a context arriving from anywhere
cannot make it update one that is not there.

The pencil was on a line of its own under the field's name. Every other pop-over
list has one anchor per row, so `li` is a block and the anchor fills it - and two
anchors in a block stack. This list has two by design, the name with its checkbox
and the pencil that edits the field, so the ROW is the flex container and the
name takes the space the pencil does not. Scoped to this popup, so no other list
moves.

tests/customFieldsSectionMenu.test.cjs gains both: that the create form is handed
an empty context and decides by looking the id up, and that a row is a checkbox,
a name and a pencil on one line.

Thanks to xet7 !
2026-08-15 06:24:06 +03:00

290 lines
8.4 KiB
JavaScript

import { ReactiveCache } from '/imports/reactiveCache';
import { TAPi18n } from '/imports/i18n';
import CustomFields from '/models/customFields';
Template.customFieldsSidebar.helpers({
customFields() {
const ret = ReactiveCache.getCustomFields({
boardIds: { $in: [Session.get('currentBoard')] },
});
return ret;
},
});
Template.customFieldsSidebar.events({
'click .js-open-create-custom-field': Popup.open('createCustomField'),
'click .js-edit-custom-field': Popup.open('editCustomField'),
});
const CUSTOM_FIELD_TYPES = [
'text',
'number',
'date',
'dropdown',
'currency',
'checkbox',
'stringtemplate',
];
const CURRENCY_LIST = [
{ name: 'US Dollar', code: 'USD' },
{ name: 'Euro', code: 'EUR' },
{ name: 'Yen', code: 'JPY' },
{ name: 'Pound Sterling', code: 'GBP' },
{ name: 'Australian Dollar', code: 'AUD' },
{ name: 'Canadian Dollar', code: 'CAD' },
{ name: 'Swiss Franc', code: 'CHF' },
{ name: 'Yuan Renminbi', code: 'CNY' },
{ name: 'Hong Kong Dollar', code: 'HKD' },
{ name: 'New Zealand Dollar', code: 'NZD' },
];
function getDropdownItems(tpl) {
const items = tpl.dropdownItems.get();
Array.from(tpl.findAll('.js-field-settings-dropdown input')).forEach(
(el, index) => {
if (!items[index])
items[index] = {
_id: Random.id(6),
};
items[index].name = el.value.trim();
},
);
return items;
}
function getSettings(tpl) {
const settings = {};
switch (tpl.type.get()) {
case 'currency': {
const currencyCode = tpl.currencyCode.get();
settings.currencyCode = currencyCode;
break;
}
case 'dropdown': {
const dropdownItems = getDropdownItems(tpl).filter(
item => !!item.name.trim(),
);
settings.dropdownItems = dropdownItems;
break;
}
case 'stringtemplate': {
const stringtemplateFormat = tpl.stringtemplateFormat.get();
settings.stringtemplateFormat = stringtemplateFormat;
const stringtemplateSeparator = tpl.stringtemplateSeparator.get();
settings.stringtemplateSeparator = stringtemplateSeparator;
break;
}
}
return settings;
}
Template.createCustomFieldPopup.onCreated(function () {
const data = Template.currentData();
this.type = new ReactiveVar(
data.type ? data.type : CUSTOM_FIELD_TYPES[0],
);
this.currencyCode = new ReactiveVar(
data.settings && data.settings.currencyCode
? data.settings.currencyCode
: CURRENCY_LIST[0].code,
);
this.dropdownItems = new ReactiveVar(
data.settings && data.settings.dropdownItems
? data.settings.dropdownItems
: [],
);
this.stringtemplateFormat = new ReactiveVar(
data.settings && data.settings.stringtemplateFormat
? data.settings.stringtemplateFormat
: '',
);
this.stringtemplateSeparator = new ReactiveVar(
data.settings && data.settings.stringtemplateSeparator
? data.settings.stringtemplateSeparator
: '',
);
});
Template.createCustomFieldPopup.helpers({
types() {
const currentType = Template.currentData().type;
return CUSTOM_FIELD_TYPES.map(type => {
return {
value: type,
name: TAPi18n.__(`custom-field-${type}`),
selected: type === currentType,
};
});
},
isTypeNotSelected(type) {
return Template.instance().type.get() !== type;
},
getCurrencyCodes() {
const currentCode = Template.instance().currencyCode.get();
return CURRENCY_LIST.map(({ name, code }) => {
return {
name: `${code} - ${name}`,
value: code,
selected: code === currentCode,
};
});
},
// The template iterates `{{#each dropdownItems.get}}`. This component used to
// be a BlazeComponent, where `dropdownItems` resolved to the instance's
// ReactiveVar; after the migration to a plain Template that binding rendered
// nothing (so dropdown options never appeared and were lost on save). Expose
// the ReactiveVar as a helper again so `dropdownItems.get` works.
dropdownItems() {
return Template.instance().dropdownItems;
},
getDropdownItems() {
return getDropdownItems(Template.instance());
},
getStringtemplateFormat() {
return Template.instance().stringtemplateFormat.get();
},
getStringtemplateSeparator() {
return Template.instance().stringtemplateSeparator.get();
},
});
Template.createCustomFieldPopup.events({
'change .js-field-type'(evt, tpl) {
const value = evt.target.value;
tpl.type.set(value);
},
'change .js-field-currency'(evt, tpl) {
const value = evt.target.value;
tpl.currencyCode.set(value);
},
'keydown .js-dropdown-item.last'(evt, tpl) {
if (evt.target.value.trim() && evt.keyCode === 13) {
const items = getDropdownItems(tpl);
tpl.dropdownItems.set(items);
evt.target.value = '';
}
},
'input .js-field-stringtemplate-format'(evt, tpl) {
const value = evt.target.value;
tpl.stringtemplateFormat.set(value);
},
'input .js-field-stringtemplate-separator'(evt, tpl) {
const value = evt.target.value;
tpl.stringtemplateSeparator.set(value);
},
'click .js-field-show-on-card'(evt) {
let $target = $(evt.target);
if (!$target.hasClass('js-field-show-on-card')) {
$target = $target.parent();
}
$target.find('.materialCheckBox').toggleClass('is-checked');
$target.toggleClass('is-checked');
},
'click .js-field-automatically-on-card'(evt) {
let $target = $(evt.target);
if (!$target.hasClass('js-field-automatically-on-card')) {
$target = $target.parent();
}
$target.find('.materialCheckBox').toggleClass('is-checked');
$target.toggleClass('is-checked');
},
'click .js-field-always-on-card'(evt) {
let $target = $(evt.target);
if (!$target.hasClass('js-field-always-on-card')) {
$target = $target.parent();
}
$target.find('.materialCheckBox').toggleClass('is-checked');
$target.toggleClass('is-checked');
},
'click .js-field-showLabel-on-card'(evt) {
let $target = $(evt.target);
if (!$target.hasClass('js-field-showLabel-on-card')) {
$target = $target.parent();
}
$target.find('.materialCheckBox').toggleClass('is-checked');
$target.toggleClass('is-checked');
},
'click .js-field-show-sum-at-top-of-list'(evt) {
let $target = $(evt.target);
if (!$target.hasClass('js-field-show-sum-at-top-of-list')) {
$target = $target.parent();
}
$target.find('.materialCheckBox').toggleClass('is-checked');
$target.toggleClass('is-checked');
},
'click .primary'(evt, tpl) {
evt.preventDefault();
const data = {
name: tpl.find('.js-field-name').value.trim(),
type: tpl.type.get(),
settings: getSettings(tpl),
showOnCard: tpl.find('.js-field-show-on-card.is-checked') !== null,
showLabelOnMiniCard:
tpl.find('.js-field-showLabel-on-card.is-checked') !== null,
automaticallyOnCard:
tpl.find('.js-field-automatically-on-card.is-checked') !== null,
alwaysOnCard:
tpl.find('.js-field-always-on-card.is-checked') !== null,
showSumAtTopOfList:
tpl.find('.js-field-show-sum-at-top-of-list.is-checked') !== null,
};
const currentData = Template.currentData();
// Insert or update, decided by whether this form was opened ON a custom
// field - not by whether its context merely HAS an `_id`. Opened from the
// card's Custom Fields popup the context was the card, whose `_id` is a
// card's, and the update below then wrote to a custom field that does not
// exist: the new field was silently never created. An id that names no
// custom field is a new field.
const editing =
currentData._id && ReactiveCache.getCustomField(currentData._id);
if (!editing) {
data.boardIds = [Session.get('currentBoard')];
CustomFields.insert(data);
} else {
CustomFields.update(currentData._id, { $set: data });
}
Popup.back();
},
'click .js-delete-custom-field': Popup.afterConfirm(
'deleteCustomField',
function() {
const customField = ReactiveCache.getCustomField(this._id);
if (customField.boardIds.length > 1) {
CustomFields.update(customField._id, {
$pull: {
boardIds: Session.get('currentBoard'),
},
});
} else {
CustomFields.remove(customField._id);
}
Popup.back();
},
),
});
/*Template.deleteCustomFieldPopup.events({
'submit'(evt) {
const customFieldId = this._id;
CustomFields.remove(customFieldId);
Popup.back();
}
});*/