2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* This module export a component for editing some document settings consisting of the timezone,
|
|
|
|
* (new settings to be added here ...).
|
|
|
|
*/
|
2023-03-23 18:22:28 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
|
|
|
import {ACIndexImpl} from 'app/client/lib/ACIndex';
|
|
|
|
import {ACSelectItem, buildACSelect} from 'app/client/lib/ACSelect';
|
2023-04-28 09:20:28 +00:00
|
|
|
import {copyToClipboard} from 'app/client/lib/clipboardUtils';
|
2023-03-23 18:22:28 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
|
|
|
import {reportError} from 'app/client/models/AppModel';
|
2023-05-08 22:06:24 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
2023-03-23 18:22:28 +00:00
|
|
|
import {KoSaveableObservable} from 'app/client/models/modelUtil';
|
|
|
|
import {docListHeader} from 'app/client/ui/DocMenuCss';
|
2023-01-05 08:11:54 +00:00
|
|
|
import {showTransientTooltip} from 'app/client/ui/tooltips';
|
2023-05-08 22:06:24 +00:00
|
|
|
import {primaryButtonLink} from 'app/client/ui2018/buttons';
|
2023-01-05 08:11:54 +00:00
|
|
|
import {mediaSmall, testId, theme, vars} from 'app/client/ui2018/cssVars';
|
2021-09-15 23:35:21 +00:00
|
|
|
import {select} from 'app/client/ui2018/menus';
|
2023-03-23 18:22:28 +00:00
|
|
|
import {confirmModal} from 'app/client/ui2018/modals';
|
2021-09-23 18:09:58 +00:00
|
|
|
import {buildCurrencyPicker} from 'app/client/widgets/CurrencyPicker';
|
2021-08-26 16:35:11 +00:00
|
|
|
import {buildTZAutocomplete} from 'app/client/widgets/TZAutocomplete';
|
2021-09-15 23:35:21 +00:00
|
|
|
import {EngineCode} from 'app/common/DocumentSettings';
|
|
|
|
import {GristLoadConfig} from 'app/common/gristUrls';
|
2023-03-23 18:22:28 +00:00
|
|
|
import {propertyCompare} from 'app/common/gutil';
|
|
|
|
import {getCurrency, locales} from 'app/common/Locales';
|
|
|
|
import {Computed, Disposable, dom, fromKo, IDisposableOwner, styled} from 'grainjs';
|
|
|
|
import * as moment from 'moment-timezone';
|
2022-10-28 16:11:08 +00:00
|
|
|
|
|
|
|
const t = makeT('DocumentSettings');
|
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
export class DocSettingsPage extends Disposable {
|
|
|
|
private _docInfo = this._gristDoc.docInfo;
|
|
|
|
|
|
|
|
private _timezone = this._docInfo.timezone;
|
|
|
|
private _locale: KoSaveableObservable<string> = this._docInfo.documentSettingsJson.prop('locale');
|
|
|
|
private _currency: KoSaveableObservable<string|undefined> = this._docInfo.documentSettingsJson.prop('currency');
|
|
|
|
private _engine: Computed<EngineCode|undefined> = Computed.create(this, (
|
|
|
|
use => use(this._docInfo.documentSettingsJson.prop('engine'))
|
|
|
|
))
|
|
|
|
.onWrite(val => this._setEngine(val));
|
2021-08-26 16:35:11 +00:00
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
constructor(private _gristDoc: GristDoc) {
|
|
|
|
super();
|
|
|
|
}
|
2021-09-15 23:35:21 +00:00
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
public buildDom() {
|
2021-09-15 23:35:21 +00:00
|
|
|
const canChangeEngine = getSupportedEngineChoices().length > 0;
|
2023-01-05 08:11:54 +00:00
|
|
|
const docPageModel = this._gristDoc.docPageModel;
|
2021-08-26 16:35:11 +00:00
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
return cssContainer(
|
|
|
|
cssHeader(t('Document Settings')),
|
|
|
|
cssDataRow(t("Time Zone:")),
|
|
|
|
cssDataRow(
|
|
|
|
dom.create(buildTZAutocomplete, moment, fromKo(this._timezone), (val) => this._timezone.saveOnly(val))
|
|
|
|
),
|
|
|
|
cssDataRow(t("Locale:")),
|
|
|
|
cssDataRow(dom.create(buildLocaleSelect, this._locale)),
|
|
|
|
cssDataRow(t("Currency:")),
|
|
|
|
cssDataRow(dom.domComputed(fromKo(this._locale), (l) =>
|
|
|
|
dom.create(buildCurrencyPicker, fromKo(this._currency), (val) => this._currency.saveOnly(val),
|
|
|
|
{defaultCurrencyLabel: t("Local currency ({{currency}})", {currency: getCurrency(l)})})
|
|
|
|
)),
|
2023-01-30 14:13:56 +00:00
|
|
|
canChangeEngine ? cssDataRow([
|
2023-01-05 08:11:54 +00:00
|
|
|
// Small easter egg: you can click on the skull-and-crossbones to
|
|
|
|
// force a reload of the document.
|
|
|
|
cssDataRow(t("Engine (experimental {{span}} change at own risk):", {span:
|
|
|
|
dom('span', '☠',
|
|
|
|
dom.style('cursor', 'pointer'),
|
|
|
|
dom.on('click', async () => {
|
|
|
|
await docPageModel.appModel.api.getDocAPI(docPageModel.currentDocId.get()!).forceReload();
|
|
|
|
document.location.reload();
|
|
|
|
}))
|
2022-10-28 16:11:08 +00:00
|
|
|
})),
|
2023-01-05 08:11:54 +00:00
|
|
|
select(this._engine, getSupportedEngineChoices()),
|
2023-01-30 14:13:56 +00:00
|
|
|
]) : null,
|
2023-01-05 08:11:54 +00:00
|
|
|
cssHeader(t('API')),
|
|
|
|
cssDataRow(t("This document's ID (for API use):")),
|
|
|
|
cssDataRow(cssHoverWrapper(
|
|
|
|
dom('tt', docPageModel.currentDocId.get()),
|
|
|
|
dom.on('click', async (e, d) => {
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
showTransientTooltip(d, t("Document ID copied to clipboard"), {
|
|
|
|
key: 'copy-document-id'
|
|
|
|
});
|
|
|
|
await copyToClipboard(docPageModel.currentDocId.get()!);
|
|
|
|
}),
|
|
|
|
)),
|
2023-05-08 22:06:24 +00:00
|
|
|
cssHeader(t('Webhooks'), cssBeta('Beta')),
|
|
|
|
cssDataRow(primaryButtonLink(t('Manage Webhooks'), urlState().setLinkUrl({docPage: 'webhook'}))),
|
2023-01-05 08:11:54 +00:00
|
|
|
);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
private async _setEngine(val: EngineCode|undefined) {
|
|
|
|
confirmModal(t('Save and Reload'), t('Ok'), () => this._doSetEngine(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _doSetEngine(val: EngineCode|undefined) {
|
|
|
|
const docPageModel = this._gristDoc.docPageModel;
|
|
|
|
if (this._engine.get() !== val) {
|
|
|
|
await this._docInfo.documentSettingsJson.prop('engine').saveOnly(val);
|
|
|
|
await docPageModel.appModel.api.getDocAPI(docPageModel.currentDocId.get()!).forceReload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-26 16:35:11 +00:00
|
|
|
|
|
|
|
type LocaleItem = ACSelectItem & {locale?: string};
|
|
|
|
|
|
|
|
function buildLocaleSelect(
|
|
|
|
owner: IDisposableOwner,
|
2023-01-05 08:11:54 +00:00
|
|
|
locale: KoSaveableObservable<string>,
|
2021-08-26 16:35:11 +00:00
|
|
|
) {
|
|
|
|
const localeList: LocaleItem[] = locales.map(l => ({
|
|
|
|
value: l.name, // Use name as a value, we will translate the name into the locale on save
|
|
|
|
label: l.name,
|
|
|
|
locale: l.code,
|
|
|
|
cleanText: l.name.trim().toLowerCase(),
|
2021-09-23 18:09:58 +00:00
|
|
|
})).sort(propertyCompare("label"));
|
2021-08-26 16:35:11 +00:00
|
|
|
const acIndex = new ACIndexImpl<LocaleItem>(localeList, 200, true);
|
|
|
|
// AC select will show the value (in this case locale) not a label when something is selected.
|
|
|
|
// To show the label - create another observable that will be in sync with the value, but
|
|
|
|
// will contain text.
|
2023-01-05 08:11:54 +00:00
|
|
|
const textObs = Computed.create(owner, use => {
|
|
|
|
const localeCode = use(locale);
|
|
|
|
const localeName = locales.find(l => l.code === localeCode)?.name || localeCode;
|
|
|
|
return localeName;
|
|
|
|
});
|
2021-08-26 16:35:11 +00:00
|
|
|
return buildACSelect(owner,
|
|
|
|
{
|
|
|
|
acIndex, valueObs: textObs,
|
2023-01-05 08:11:54 +00:00
|
|
|
save(_value, item: LocaleItem | undefined) {
|
2021-08-26 16:35:11 +00:00
|
|
|
if (!item) { throw new Error("Invalid locale"); }
|
2023-01-05 08:11:54 +00:00
|
|
|
locale.saveOnly(item.locale!).catch(reportError);
|
2021-08-26 16:35:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
testId("locale-autocomplete")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-05 08:11:54 +00:00
|
|
|
const cssHeader = styled(docListHeader, `
|
|
|
|
margin-bottom: 0;
|
|
|
|
&:not(:first-of-type) {
|
|
|
|
margin-top: 40px;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssContainer = styled('div', `
|
|
|
|
overflow-y: auto;
|
|
|
|
position: relative;
|
|
|
|
height: 100%;
|
|
|
|
padding: 32px 64px 24px 64px;
|
|
|
|
@media ${mediaSmall} {
|
|
|
|
& {
|
|
|
|
padding: 32px 24px 24px 24px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssHoverWrapper = styled('div', `
|
|
|
|
display: inline-block;
|
|
|
|
cursor: default;
|
|
|
|
color: ${theme.lightText};
|
|
|
|
transition: background 0.05s;
|
|
|
|
&:hover {
|
|
|
|
background: ${theme.lightHover};
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// This matches the style used in showProfileModal in app/client/ui/AccountWidget.
|
|
|
|
const cssDataRow = styled('div', `
|
|
|
|
margin: 16px 0px;
|
|
|
|
font-size: ${vars.largeFontSize};
|
2023-01-05 08:11:54 +00:00
|
|
|
color: ${theme.text};
|
2023-01-30 14:13:56 +00:00
|
|
|
width: 360px;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
2021-09-15 23:35:21 +00:00
|
|
|
|
2023-05-08 22:06:24 +00:00
|
|
|
const cssBeta = styled('sup', `
|
|
|
|
text-transform: uppercase;
|
|
|
|
color: ${theme.text};
|
|
|
|
font-size: ${vars.smallFontSize};
|
|
|
|
margin-left: 8px;
|
|
|
|
`);
|
|
|
|
|
2021-09-15 23:35:21 +00:00
|
|
|
// Check which engines can be selected in the UI, if any.
|
|
|
|
export function getSupportedEngineChoices(): EngineCode[] {
|
|
|
|
const gristConfig: GristLoadConfig = (window as any).gristConfig || {};
|
|
|
|
return gristConfig.supportEngines || [];
|
|
|
|
}
|