Skip to content

Commit

Permalink
Add a way to temporary disable field labels
Browse files Browse the repository at this point in the history
Fixes #1330
  • Loading branch information
maxpatiiuk committed Jul 8, 2022
1 parent c26c3cb commit 23a2df4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
1 change: 1 addition & 0 deletions specifyweb/frontend/js_src/lib/cachedefinitions.ts
Expand Up @@ -38,6 +38,7 @@ export type CacheDefinitions = {
readonly [TABLE_NAME in keyof Tables]?: boolean;
};
readonly carryForwardShowHidden: boolean;
readonly useFieldLabels: boolean;
};
readonly wbPlanViewUi: {
/** Whether to show less commonly used tables when selected base table */
Expand Down
51 changes: 40 additions & 11 deletions specifyweb/frontend/js_src/lib/components/formdefinition.tsx
Expand Up @@ -34,6 +34,20 @@ function FormDefinitionDialog({
readonly model: SpecifyModel;
readonly onClose: () => void;
}): JSX.Element {
return (
<Dialog
header={commonText('formDefinition')}
buttons={commonText('close')}
onClose={handleClose}
>
<UseAutoForm model={model} />
<UseLabels />
<EditFormDefinition />
</Dialog>
);
}

function UseAutoForm({ model }: { readonly model: SpecifyModel }): JSX.Element {
const [globalConfig = {}, setGlobalConfig] = useCachedState({
category: 'forms',
key: 'useCustomForm',
Expand All @@ -45,17 +59,32 @@ function FormDefinitionDialog({
setGlobalConfig({ ...globalConfig, [model.name]: !checked });

return (
<Dialog
header={commonText('formDefinition')}
buttons={commonText('close')}
onClose={handleClose}
>
<Label.ForCheckbox>
<Input.Checkbox checked={!useCustomForm} onValueChange={handleChange} />
{formsText('useAutoGeneratedForm')}
</Label.ForCheckbox>
<EditFormDefinition />
</Dialog>
<Label.ForCheckbox>
<Input.Checkbox checked={!useCustomForm} onValueChange={handleChange} />
{formsText('useAutoGeneratedForm')}
</Label.ForCheckbox>
);
}

function UseLabels(): JSX.Element {
const [useFieldLabels = true, setUseFieldLabels] = useCachedState({
category: 'forms',
key: 'useFieldLabels',
defaultValue: true,
staleWhileRefresh: false,
});

return (
<Label.ForCheckbox>
<Input.Checkbox
checked={useFieldLabels}
onValueChange={(checked): void => {
setUseFieldLabels(checked);
window.location.reload();
}}
/>
{formsText('useFieldLabels')}
</Label.ForCheckbox>
);
}

Expand Down
4 changes: 4 additions & 0 deletions specifyweb/frontend/js_src/lib/localization/forms.tsx
Expand Up @@ -680,6 +680,10 @@ export const formsText = createDictionary({
'en-us': 'Use Auto Generated Form',
'ru-ru': 'Использовать автоматическую схему формы',
},
useFieldLabels: {
'en-us': 'Use localized field labels',
'ru-ru': 'Использовать локализованные названия полей',
},
historyOfEdits: {
'en-us': 'History of edits',
'ru-ru': 'История изменений',
Expand Down
22 changes: 18 additions & 4 deletions specifyweb/frontend/js_src/lib/specifymodel.ts
Expand Up @@ -35,6 +35,7 @@ import {
import { isTreeResource } from './treedefinitions';
import type { IR, R, RA } from './types';
import { defined } from './types';
import { getCache } from './cache';

type FieldAlias = {
readonly vname: string;
Expand Down Expand Up @@ -219,13 +220,25 @@ export class SpecifyModel<SCHEMA extends AnySchema = AnySchema> {
model: this.Resource,
});

const useLabels = getCache('forms', 'useFieldLabels') ?? true;
this.localization = getSchemaLocalization()[this.name.toLowerCase()] ?? {
items: {},
};
if (!useLabels)
this.localization = {
...this.localization,
items: Object.fromEntries(
Object.entries(this.localization.items).map(([fieldName, data]) => [
fieldName,
{ ...data, name: fieldName },
])
),
};

(this.localization.items as R<SchemaLocalization['items'][string]>)[
tableDefinition.idFieldName.toLowerCase()
] ??= {
name: commonText('id'),
name: useLabels ? commonText('id') : tableDefinition.idFieldName,
desc: null,
format: null,
picklistname: null,
Expand All @@ -244,10 +257,11 @@ export class SpecifyModel<SCHEMA extends AnySchema = AnySchema> {
readOnly: false,
});

this.label =
typeof this.localization.name === 'string'
this.label = useLabels
? typeof this.localization.name === 'string'
? unescape(this.localization.name)
: camelToHuman(this.name);
: camelToHuman(this.name)
: this.name;

this.isHidden = this.localization.ishidden === 1;

Expand Down

0 comments on commit 23a2df4

Please sign in to comment.