2020-03-19 22:19:40 +08:00
|
|
|
import React from 'react';
|
2017-01-27 23:41:11 +08:00
|
|
|
import cx from 'classnames';
|
2019-01-04 01:14:45 +08:00
|
|
|
import Button from '/imports/ui/components/button/component';
|
2017-01-27 23:41:11 +08:00
|
|
|
import Toggle from '/imports/ui/components/switch/component';
|
2017-04-11 05:11:48 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2017-10-06 03:18:30 +08:00
|
|
|
import BaseMenu from '../base/component';
|
2018-01-08 14:17:18 +08:00
|
|
|
import { styles } from '../styles';
|
2016-12-14 11:40:32 +08:00
|
|
|
|
2017-02-25 04:19:53 +08:00
|
|
|
const MIN_FONTSIZE = 0;
|
|
|
|
|
2017-04-11 05:11:48 +08:00
|
|
|
const intlMessages = defineMessages({
|
|
|
|
applicationSectionTitle: {
|
|
|
|
id: 'app.submenu.application.applicationSectionTitle',
|
|
|
|
description: 'Application section title',
|
|
|
|
},
|
2019-01-25 00:16:23 +08:00
|
|
|
animationsLabel: {
|
|
|
|
id: 'app.submenu.application.animationsLabel',
|
|
|
|
description: 'animations label',
|
|
|
|
},
|
2017-04-11 05:11:48 +08:00
|
|
|
fontSizeControlLabel: {
|
|
|
|
id: 'app.submenu.application.fontSizeControlLabel',
|
2017-04-11 05:21:54 +08:00
|
|
|
description: 'label for font size ontrol',
|
2017-04-11 05:11:48 +08:00
|
|
|
},
|
|
|
|
increaseFontBtnLabel: {
|
|
|
|
id: 'app.submenu.application.increaseFontBtnLabel',
|
2017-04-11 05:21:54 +08:00
|
|
|
description: 'label for button to increase font size',
|
2017-04-11 05:11:48 +08:00
|
|
|
},
|
|
|
|
increaseFontBtnDesc: {
|
|
|
|
id: 'app.submenu.application.increaseFontBtnDesc',
|
2017-04-11 05:21:54 +08:00
|
|
|
description: 'adds descriptive context to increase font size button',
|
2017-04-11 05:11:48 +08:00
|
|
|
},
|
|
|
|
decreaseFontBtnLabel: {
|
|
|
|
id: 'app.submenu.application.decreaseFontBtnLabel',
|
2017-04-11 05:21:54 +08:00
|
|
|
description: 'label for button to reduce font size',
|
2017-04-11 05:11:48 +08:00
|
|
|
},
|
|
|
|
decreaseFontBtnDesc: {
|
|
|
|
id: 'app.submenu.application.decreaseFontBtnDesc',
|
2017-04-11 05:21:54 +08:00
|
|
|
description: 'adds descriptive context to decrease font size button',
|
2017-04-11 05:11:48 +08:00
|
|
|
},
|
2017-04-22 03:04:46 +08:00
|
|
|
languageLabel: {
|
|
|
|
id: 'app.submenu.application.languageLabel',
|
|
|
|
description: 'displayed label for changing application locale',
|
|
|
|
},
|
2019-05-24 05:50:36 +08:00
|
|
|
currentValue: {
|
|
|
|
id: 'app.submenu.application.currentSize',
|
|
|
|
description: 'current value label',
|
|
|
|
},
|
2017-06-03 03:25:02 +08:00
|
|
|
languageOptionLabel: {
|
2017-04-22 03:04:46 +08:00
|
|
|
id: 'app.submenu.application.languageOptionLabel',
|
|
|
|
description: 'default change language option when locales are available',
|
|
|
|
},
|
|
|
|
noLocaleOptionLabel: {
|
|
|
|
id: 'app.submenu.application.noLocaleOptionLabel',
|
|
|
|
description: 'default change language option when no locales available',
|
|
|
|
},
|
2017-04-11 05:11:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
class ApplicationMenu extends BaseMenu {
|
2019-01-04 01:14:45 +08:00
|
|
|
static setHtmlFontSize(size) {
|
|
|
|
document.getElementsByTagName('html')[0].style.fontSize = size;
|
|
|
|
}
|
|
|
|
|
2016-05-06 05:14:39 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-01-31 00:41:45 +08:00
|
|
|
|
2017-02-25 04:19:53 +08:00
|
|
|
this.state = {
|
|
|
|
settingsName: 'application',
|
|
|
|
settings: props.settings,
|
2018-03-02 01:21:19 +08:00
|
|
|
isLargestFontSize: false,
|
|
|
|
isSmallestFontSize: false,
|
2019-01-04 01:14:45 +08:00
|
|
|
fontSizes: [
|
|
|
|
'12px',
|
|
|
|
'14px',
|
|
|
|
'16px',
|
|
|
|
'18px',
|
|
|
|
'20px',
|
|
|
|
],
|
2017-02-25 04:19:53 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-04 01:14:45 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.setInitialFontSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
setInitialFontSize() {
|
|
|
|
const { fontSizes } = this.state;
|
|
|
|
const clientFont = document.getElementsByTagName('html')[0].style.fontSize;
|
|
|
|
const hasFont = fontSizes.includes(clientFont);
|
|
|
|
if (!hasFont) {
|
|
|
|
fontSizes.push(clientFont);
|
|
|
|
fontSizes.sort();
|
|
|
|
}
|
|
|
|
const fontIndex = fontSizes.indexOf(clientFont);
|
|
|
|
this.changeFontSize(clientFont);
|
|
|
|
this.setState({
|
|
|
|
isSmallestFontSize: fontIndex <= MIN_FONTSIZE,
|
|
|
|
isLargestFontSize: fontIndex >= (fontSizes.length - 1),
|
|
|
|
fontSizes,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-25 04:19:53 +08:00
|
|
|
handleUpdateFontSize(size) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const obj = this.state;
|
2017-02-25 04:19:53 +08:00
|
|
|
obj.settings.fontSize = size;
|
|
|
|
this.handleUpdateSettings(this.state.settingsName, obj.settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
changeFontSize(size) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const obj = this.state;
|
2017-02-25 04:19:53 +08:00
|
|
|
obj.settings.fontSize = size;
|
|
|
|
this.setState(obj, () => {
|
2019-01-04 01:14:45 +08:00
|
|
|
ApplicationMenu.setHtmlFontSize(this.state.settings.fontSize);
|
2017-02-25 04:19:53 +08:00
|
|
|
this.handleUpdateFontSize(this.state.settings.fontSize);
|
|
|
|
});
|
2016-05-06 05:14:39 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 04:19:53 +08:00
|
|
|
handleIncreaseFontSize() {
|
|
|
|
const currentFontSize = this.state.settings.fontSize;
|
2019-01-04 01:14:45 +08:00
|
|
|
const availableFontSizes = this.state.fontSizes;
|
|
|
|
const maxFontSize = availableFontSizes.length - 1;
|
|
|
|
const canIncreaseFontSize = availableFontSizes.indexOf(currentFontSize) < maxFontSize;
|
|
|
|
const fs = canIncreaseFontSize ? availableFontSizes.indexOf(currentFontSize) + 1 : maxFontSize;
|
2017-02-25 04:19:53 +08:00
|
|
|
this.changeFontSize(availableFontSizes[fs]);
|
2019-01-04 01:14:45 +08:00
|
|
|
if (fs === maxFontSize) this.setState({ isLargestFontSize: true });
|
2018-03-02 01:21:19 +08:00
|
|
|
this.setState({ isSmallestFontSize: false });
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2017-02-25 04:19:53 +08:00
|
|
|
|
|
|
|
handleDecreaseFontSize() {
|
|
|
|
const currentFontSize = this.state.settings.fontSize;
|
2019-01-04 01:14:45 +08:00
|
|
|
const availableFontSizes = this.state.fontSizes;
|
2017-02-25 04:19:53 +08:00
|
|
|
const canDecreaseFontSize = availableFontSizes.indexOf(currentFontSize) > MIN_FONTSIZE;
|
2018-01-30 19:20:51 +08:00
|
|
|
const fs = canDecreaseFontSize ? availableFontSizes.indexOf(currentFontSize) - 1 : MIN_FONTSIZE;
|
2017-02-25 04:19:53 +08:00
|
|
|
this.changeFontSize(availableFontSizes[fs]);
|
2018-03-02 01:21:19 +08:00
|
|
|
if (fs === MIN_FONTSIZE) this.setState({ isSmallestFontSize: true });
|
|
|
|
this.setState({ isLargestFontSize: false });
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2017-02-25 04:19:53 +08:00
|
|
|
|
2017-04-06 20:36:59 +08:00
|
|
|
handleSelectChange(fieldname, options, e) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const obj = this.state;
|
2018-04-17 01:41:23 +08:00
|
|
|
obj.settings[fieldname] = e.target.value.toLowerCase().replace('_', '-');
|
2017-04-06 20:36:59 +08:00
|
|
|
this.handleUpdateSettings('application', obj.settings);
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:41:11 +08:00
|
|
|
render() {
|
2018-01-30 19:20:51 +08:00
|
|
|
const { availableLocales, intl } = this.props;
|
2019-05-24 05:50:36 +08:00
|
|
|
const { isLargestFontSize, isSmallestFontSize, settings } = this.state;
|
2019-05-08 23:11:55 +08:00
|
|
|
|
|
|
|
// conversions can be found at http://pxtoem.com
|
2019-05-03 21:43:02 +08:00
|
|
|
const pixelPercentage = {
|
2019-05-08 23:11:55 +08:00
|
|
|
'12px': '75%',
|
|
|
|
// 14px is actually 87.5%, rounding up to show more friendly value
|
|
|
|
'14px': '90%',
|
|
|
|
'16px': '100%',
|
|
|
|
// 18px is actually 112.5%, rounding down to show more friendly value
|
|
|
|
'18px': '110%',
|
|
|
|
'20px': '125%',
|
2019-05-03 21:43:02 +08:00
|
|
|
};
|
2017-04-06 20:36:59 +08:00
|
|
|
|
2019-05-24 05:50:36 +08:00
|
|
|
const ariaValueLabel = intl.formatMessage(intlMessages.currentValue, { 0: `${pixelPercentage[settings.fontSize]}` });
|
|
|
|
|
2016-05-06 05:14:39 +08:00
|
|
|
return (
|
2018-12-06 01:42:31 +08:00
|
|
|
<div>
|
|
|
|
<div>
|
2017-04-19 03:14:39 +08:00
|
|
|
<h3 className={styles.title}>
|
|
|
|
{intl.formatMessage(intlMessages.applicationSectionTitle)}
|
|
|
|
</h3>
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
|
|
|
<div className={styles.form}>
|
2019-01-25 00:16:23 +08:00
|
|
|
|
|
|
|
<div className={styles.row}>
|
|
|
|
<div className={styles.col} aria-hidden="true">
|
|
|
|
<div className={styles.formElement}>
|
|
|
|
<label className={styles.label}>
|
|
|
|
{intl.formatMessage(intlMessages.animationsLabel)}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.col}>
|
|
|
|
<div className={cx(styles.formElement, styles.pullContentRight)}>
|
|
|
|
<Toggle
|
|
|
|
icons={false}
|
|
|
|
defaultChecked={this.state.settings.animations}
|
|
|
|
onChange={() => this.handleToggle('animations')}
|
|
|
|
ariaLabel={intl.formatMessage(intlMessages.animationsLabel)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2017-04-06 20:36:59 +08:00
|
|
|
<div className={styles.row}>
|
2018-03-02 05:15:18 +08:00
|
|
|
<div className={styles.col} aria-hidden="true">
|
2017-04-06 20:36:59 +08:00
|
|
|
<div className={styles.formElement}>
|
2019-05-29 00:28:53 +08:00
|
|
|
<label
|
|
|
|
className={styles.label}
|
|
|
|
htmlFor="langSelector"
|
2019-06-26 11:03:08 +08:00
|
|
|
aria-label={intl.formatMessage(intlMessages.languageLabel)}
|
2019-05-29 00:28:53 +08:00
|
|
|
>
|
2017-04-22 03:04:46 +08:00
|
|
|
{intl.formatMessage(intlMessages.languageLabel)}
|
2017-04-06 20:36:59 +08:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.col}>
|
2019-05-29 00:28:53 +08:00
|
|
|
<span className={cx(styles.formElement, styles.pullContentRight)}>
|
2018-01-30 19:20:51 +08:00
|
|
|
{availableLocales && availableLocales.length > 0 ? (
|
2017-11-25 04:26:03 +08:00
|
|
|
<select
|
2019-05-29 00:28:53 +08:00
|
|
|
id="langSelector"
|
2018-04-17 01:41:23 +08:00
|
|
|
defaultValue={this.state.settings.locale}
|
2019-05-24 05:55:34 +08:00
|
|
|
lang={this.state.settings.locale}
|
2017-11-25 04:26:03 +08:00
|
|
|
className={styles.select}
|
|
|
|
onChange={this.handleSelectChange.bind(this, 'locale', availableLocales)}
|
|
|
|
>
|
2018-01-30 19:20:51 +08:00
|
|
|
<option disabled>{intl.formatMessage(intlMessages.languageOptionLabel)}</option>
|
|
|
|
{availableLocales.map((locale, index) => (
|
2019-07-06 03:44:12 +08:00
|
|
|
<option key={index} value={locale.locale} lang={locale.locale}>
|
2017-11-25 04:26:03 +08:00
|
|
|
{locale.name}
|
2018-01-30 19:20:51 +08:00
|
|
|
</option>
|
|
|
|
))}
|
2017-11-25 04:26:03 +08:00
|
|
|
</select>
|
2018-01-30 19:20:51 +08:00
|
|
|
) : null}
|
2019-05-29 00:28:53 +08:00
|
|
|
</span>
|
2017-04-06 20:36:59 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2017-06-03 03:25:02 +08:00
|
|
|
<hr className={styles.separator} />
|
2017-01-27 23:41:11 +08:00
|
|
|
<div className={styles.row}>
|
|
|
|
<div className={styles.col}>
|
|
|
|
<div className={styles.formElement}>
|
|
|
|
<label className={styles.label}>
|
2017-04-11 05:11:48 +08:00
|
|
|
{intl.formatMessage(intlMessages.fontSizeControlLabel)}
|
2017-01-27 23:41:11 +08:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.col}>
|
2019-05-24 05:50:36 +08:00
|
|
|
<div aria-hidden className={cx(styles.formElement, styles.pullContentCenter)}>
|
2017-01-27 23:41:11 +08:00
|
|
|
<label className={cx(styles.label, styles.bold)}>
|
2019-05-03 21:43:02 +08:00
|
|
|
{`${pixelPercentage[this.state.settings.fontSize]}`}
|
2017-01-27 23:41:11 +08:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.col}>
|
|
|
|
<div className={cx(styles.formElement, styles.pullContentRight)}>
|
2017-03-09 22:34:33 +08:00
|
|
|
<div className={styles.pullContentRight}>
|
|
|
|
<div className={styles.col}>
|
|
|
|
<Button
|
2018-12-14 05:47:43 +08:00
|
|
|
onClick={() => this.handleDecreaseFontSize()}
|
2017-12-01 03:17:03 +08:00
|
|
|
color="primary"
|
2018-12-14 05:47:43 +08:00
|
|
|
icon="substract"
|
2017-06-03 03:25:02 +08:00
|
|
|
circle
|
|
|
|
hideLabel
|
2018-12-14 05:47:43 +08:00
|
|
|
label={intl.formatMessage(intlMessages.decreaseFontBtnLabel)}
|
2019-05-24 05:50:36 +08:00
|
|
|
aria-label={`${intl.formatMessage(intlMessages.decreaseFontBtnLabel)}, ${ariaValueLabel}`}
|
2018-12-14 05:47:43 +08:00
|
|
|
disabled={isSmallestFontSize}
|
2017-03-09 22:34:33 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.col}>
|
|
|
|
<Button
|
2018-12-14 05:47:43 +08:00
|
|
|
onClick={() => this.handleIncreaseFontSize()}
|
2017-12-01 03:17:03 +08:00
|
|
|
color="primary"
|
2018-12-14 05:47:43 +08:00
|
|
|
icon="add"
|
2017-06-03 03:25:02 +08:00
|
|
|
circle
|
|
|
|
hideLabel
|
2018-12-14 05:47:43 +08:00
|
|
|
label={intl.formatMessage(intlMessages.increaseFontBtnLabel)}
|
2019-05-24 05:50:36 +08:00
|
|
|
aria-label={`${intl.formatMessage(intlMessages.increaseFontBtnLabel)}, ${ariaValueLabel}`}
|
2018-12-14 05:47:43 +08:00
|
|
|
disabled={isLargestFontSize}
|
2017-03-09 22:34:33 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-01-27 23:41:11 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2016-05-06 05:14:39 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2017-04-11 05:11:48 +08:00
|
|
|
|
|
|
|
export default injectIntl(ApplicationMenu);
|