2020-01-30 02:36:17 +08:00
|
|
|
import React, { Component } from 'react';
|
2019-02-22 05:01:39 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2020-05-26 04:00:13 +08:00
|
|
|
import { defineMessages } from 'react-intl';
|
2019-02-22 05:01:39 +08:00
|
|
|
import _ from 'lodash';
|
|
|
|
import Button from '/imports/ui/components/button/component';
|
|
|
|
import Dropdown from '/imports/ui/components/dropdown/component';
|
|
|
|
import DropdownTrigger from '/imports/ui/components/dropdown/trigger/component';
|
|
|
|
import DropdownContent from '/imports/ui/components/dropdown/content/component';
|
|
|
|
import DropdownList from '/imports/ui/components/dropdown/list/component';
|
|
|
|
import DropdownListItem from '/imports/ui/components/dropdown/list/item/component';
|
|
|
|
import { styles } from '../styles';
|
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
quickPollLabel: {
|
|
|
|
id: 'app.poll.quickPollTitle',
|
|
|
|
description: 'Quick poll button title',
|
|
|
|
},
|
|
|
|
trueOptionLabel: {
|
|
|
|
id: 'app.poll.t',
|
|
|
|
description: 'Poll true option value',
|
|
|
|
},
|
|
|
|
falseOptionLabel: {
|
|
|
|
id: 'app.poll.f',
|
|
|
|
description: 'Poll false option value',
|
|
|
|
},
|
|
|
|
yesOptionLabel: {
|
|
|
|
id: 'app.poll.y',
|
|
|
|
description: 'Poll yes option value',
|
|
|
|
},
|
|
|
|
noOptionLabel: {
|
|
|
|
id: 'app.poll.n',
|
|
|
|
description: 'Poll no option value',
|
|
|
|
},
|
2021-01-24 09:22:40 +08:00
|
|
|
abstentionOptionLabel: {
|
|
|
|
id: 'app.poll.abstention',
|
|
|
|
description: 'Poll Abstention option value',
|
|
|
|
},
|
2019-02-22 05:01:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const propTypes = {
|
2020-05-26 04:00:13 +08:00
|
|
|
intl: PropTypes.object.isRequired,
|
2019-02-22 05:01:39 +08:00
|
|
|
parseCurrentSlideContent: PropTypes.func.isRequired,
|
2019-09-07 04:28:02 +08:00
|
|
|
amIPresenter: PropTypes.bool.isRequired,
|
2019-02-22 05:01:39 +08:00
|
|
|
};
|
|
|
|
|
2021-04-06 01:50:50 +08:00
|
|
|
const getLocalizedAnswers = (type, intl) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'TF':
|
|
|
|
return [
|
|
|
|
intl.formatMessage(intlMessages.trueOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.falseOptionLabel),
|
|
|
|
];
|
|
|
|
case 'YN':
|
|
|
|
return [
|
|
|
|
intl.formatMessage(intlMessages.yesOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.noOptionLabel),
|
|
|
|
];
|
|
|
|
case 'YNA':
|
|
|
|
return [
|
|
|
|
intl.formatMessage(intlMessages.yesOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.noOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.abstentionOptionLabel),
|
|
|
|
];
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAvailableQuickPolls = (slideId, parsedSlides, startPoll, intl) => {
|
2019-03-25 01:28:01 +08:00
|
|
|
const pollItemElements = parsedSlides.map((poll) => {
|
2021-04-06 01:50:50 +08:00
|
|
|
let { poll: label, type } = poll;
|
2019-03-25 01:28:01 +08:00
|
|
|
let itemLabel = label;
|
2021-04-06 01:50:50 +08:00
|
|
|
let answers = null;
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2021-01-24 09:22:40 +08:00
|
|
|
if (type !== 'YN' && type !== 'YNA' && type !== 'TF') {
|
2019-03-25 01:28:01 +08:00
|
|
|
const { options } = itemLabel;
|
|
|
|
itemLabel = options.join('/').replace(/[\n.)]/g, '');
|
2021-04-06 01:50:50 +08:00
|
|
|
} else {
|
|
|
|
answers = getLocalizedAnswers(type, intl);
|
|
|
|
type = 'custom';
|
2019-03-25 01:28:01 +08:00
|
|
|
}
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2019-03-25 01:28:01 +08:00
|
|
|
// removes any whitespace from the label
|
|
|
|
itemLabel = itemLabel.replace(/\s+/g, '').toUpperCase();
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2019-03-25 01:28:01 +08:00
|
|
|
const numChars = {
|
|
|
|
1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E',
|
|
|
|
};
|
|
|
|
itemLabel = itemLabel.split('').map((c) => {
|
|
|
|
if (numChars[c]) return numChars[c];
|
|
|
|
return c;
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DropdownListItem
|
|
|
|
label={itemLabel}
|
|
|
|
key={_.uniqueId('quick-poll-item')}
|
2021-04-06 01:50:50 +08:00
|
|
|
onClick={() => startPoll(type, slideId, answers)}
|
2019-03-25 01:28:01 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const sizes = [];
|
|
|
|
return pollItemElements.filter((el) => {
|
|
|
|
const { label } = el.props;
|
|
|
|
if (label.length === sizes[sizes.length - 1]) return;
|
|
|
|
sizes.push(label.length);
|
|
|
|
return el;
|
|
|
|
});
|
|
|
|
};
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2020-01-30 02:36:17 +08:00
|
|
|
class QuickPollDropdown extends Component {
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
amIPresenter,
|
|
|
|
intl,
|
|
|
|
parseCurrentSlideContent,
|
|
|
|
startPoll,
|
|
|
|
currentSlide,
|
|
|
|
activePoll,
|
2020-03-05 02:46:45 +08:00
|
|
|
className,
|
2020-01-30 02:36:17 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const parsedSlide = parseCurrentSlideContent(
|
|
|
|
intl.formatMessage(intlMessages.yesOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.noOptionLabel),
|
2021-01-30 04:58:05 +08:00
|
|
|
intl.formatMessage(intlMessages.abstentionOptionLabel),
|
2020-01-30 02:36:17 +08:00
|
|
|
intl.formatMessage(intlMessages.trueOptionLabel),
|
|
|
|
intl.formatMessage(intlMessages.falseOptionLabel),
|
|
|
|
);
|
2019-02-22 05:01:39 +08:00
|
|
|
|
2020-01-30 02:36:17 +08:00
|
|
|
const { slideId, quickPollOptions } = parsedSlide;
|
2021-04-06 01:50:50 +08:00
|
|
|
const quickPolls = getAvailableQuickPolls(slideId, quickPollOptions, startPoll, intl);
|
2020-01-30 02:36:17 +08:00
|
|
|
|
|
|
|
if (quickPollOptions.length === 0) return null;
|
|
|
|
|
|
|
|
let quickPollLabel = '';
|
|
|
|
if (quickPolls.length > 0) {
|
|
|
|
const { props: pollProps } = quickPolls[0];
|
|
|
|
quickPollLabel = pollProps.label;
|
|
|
|
}
|
|
|
|
|
|
|
|
let singlePollType = null;
|
2021-04-06 01:50:50 +08:00
|
|
|
let answers = null;
|
2020-01-30 02:36:17 +08:00
|
|
|
if (quickPolls.length === 1 && quickPollOptions.length) {
|
|
|
|
const { type } = quickPollOptions[0];
|
|
|
|
singlePollType = type;
|
|
|
|
}
|
2020-01-11 04:18:43 +08:00
|
|
|
|
2021-04-06 01:50:50 +08:00
|
|
|
if (singlePollType === 'TF' || singlePollType === 'YN' || singlePollType === 'YNA') {
|
|
|
|
answers = getLocalizedAnswers(singlePollType, intl);
|
|
|
|
singlePollType = 'custom';
|
|
|
|
}
|
|
|
|
|
2020-01-30 02:36:17 +08:00
|
|
|
let btn = (
|
|
|
|
<Button
|
|
|
|
aria-label={intl.formatMessage(intlMessages.quickPollLabel)}
|
|
|
|
className={styles.quickPollBtn}
|
|
|
|
label={quickPollLabel}
|
|
|
|
tooltipLabel={intl.formatMessage(intlMessages.quickPollLabel)}
|
2021-04-06 01:50:50 +08:00
|
|
|
onClick={() => startPoll(singlePollType, currentSlide.id, answers)}
|
2020-01-30 02:36:17 +08:00
|
|
|
size="lg"
|
|
|
|
disabled={!!activePoll}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const usePollDropdown = quickPollOptions && quickPollOptions.length && quickPolls.length > 1;
|
|
|
|
let dropdown = null;
|
|
|
|
|
|
|
|
if (usePollDropdown) {
|
|
|
|
btn = (
|
|
|
|
<Button
|
|
|
|
aria-label={intl.formatMessage(intlMessages.quickPollLabel)}
|
|
|
|
className={styles.quickPollBtn}
|
|
|
|
label={quickPollLabel}
|
|
|
|
tooltipLabel={intl.formatMessage(intlMessages.quickPollLabel)}
|
|
|
|
onClick={() => null}
|
|
|
|
size="lg"
|
|
|
|
disabled={!!activePoll}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
dropdown = (
|
2020-03-05 02:46:45 +08:00
|
|
|
<Dropdown className={className}>
|
2020-01-30 02:36:17 +08:00
|
|
|
<DropdownTrigger tabIndex={0}>
|
|
|
|
{btn}
|
|
|
|
</DropdownTrigger>
|
|
|
|
<DropdownContent placement="top left">
|
|
|
|
<DropdownList>
|
|
|
|
{quickPolls}
|
|
|
|
</DropdownList>
|
|
|
|
</DropdownContent>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amIPresenter && usePollDropdown ? (
|
|
|
|
dropdown
|
|
|
|
) : (
|
|
|
|
btn
|
2020-01-14 10:29:31 +08:00
|
|
|
);
|
|
|
|
}
|
2020-01-30 02:36:17 +08:00
|
|
|
}
|
2020-01-14 10:29:31 +08:00
|
|
|
|
2019-02-22 05:01:39 +08:00
|
|
|
|
|
|
|
QuickPollDropdown.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default QuickPollDropdown;
|