import classNames from 'classnames';
import {connect} from 'react-redux';
import {compose} from 'redux';
import {defineMessages, FormattedMessage, injectIntl, intlShape} from 'react-intl';
import PropTypes from 'prop-types';
import bindAll from 'lodash.bindall';
import bowser from 'bowser';
import React from 'react';
import VM from 'scratch-vm';
import Box from '../box/box.jsx';
import Button from '../button/button.jsx';
import CommunityButton from './community-button.jsx';
import ShareButton from './share-button.jsx';
import {ComingSoonTooltip} from '../coming-soon/coming-soon.jsx';
import Divider from '../divider/divider.jsx';
import SaveStatus from './save-status.jsx';
import ProjectWatcher from '../../containers/project-watcher.jsx';
import MenuBarMenu from './menu-bar-menu.jsx';
import {MenuItem, MenuSection} from '../menu/menu.jsx';
import ProjectTitleInput from './project-title-input.jsx';
import AuthorInfo from './author-info.jsx';
import AccountNav from '../../containers/account-nav.jsx';
import LoginDropdown from './login-dropdown.jsx';
import SB3Downloader from '../../containers/sb3-downloader.jsx';
import DeletionRestorer from '../../containers/deletion-restorer.jsx';
import TurboMode from '../../containers/turbo-mode.jsx';
import MenuBarHOC from '../../containers/menu-bar-hoc.jsx';
import SettingsMenu from './settings-menu.jsx';
import {openTipsLibrary, openDebugModal} from '../../reducers/modals';
import {setPlayer} from '../../reducers/mode';
import {
isTimeTravel220022BC,
isTimeTravel1920,
isTimeTravel1990,
isTimeTravel2020,
isTimeTravelNow,
setTimeTravel
} from '../../reducers/time-travel';
import {
autoUpdateProject,
getIsUpdating,
getIsShowingProject,
manualUpdateProject,
requestNewProject,
remixProject,
saveProjectAsCopy
} from '../../reducers/project-state';
import {
openAboutMenu,
closeAboutMenu,
aboutMenuOpen,
openAccountMenu,
closeAccountMenu,
accountMenuOpen,
openFileMenu,
closeFileMenu,
fileMenuOpen,
openEditMenu,
closeEditMenu,
editMenuOpen,
openLoginMenu,
closeLoginMenu,
loginMenuOpen,
openModeMenu,
closeModeMenu,
modeMenuOpen,
settingsMenuOpen,
openSettingsMenu,
closeSettingsMenu
} from '../../reducers/menus';
import collectMetadata from '../../lib/collect-metadata';
import styles from './menu-bar.css';
import helpIcon from '../../lib/assets/icon--tutorials.svg';
import mystuffIcon from './icon--mystuff.png';
import profileIcon from './icon--profile.png';
import remixIcon from './icon--remix.svg';
import dropdownCaret from './dropdown-caret.svg';
import aboutIcon from './icon--about.svg';
import fileIcon from './icon--file.svg';
import editIcon from './icon--edit.svg';
import debugIcon from '../debug-modal/icons/icon--debug.svg';
import scratchLogo from './scratch-logo.svg';
import ninetiesLogo from './nineties_logo.svg';
import catLogo from './cat_logo.svg';
import prehistoricLogo from './prehistoric-logo.svg';
import oldtimeyLogo from './oldtimey-logo.svg';
import sharedMessages from '../../lib/shared-messages';
const ariaMessages = defineMessages({
tutorials: {
id: 'gui.menuBar.tutorialsLibrary',
defaultMessage: 'Tutorials',
description: 'accessibility text for the tutorials button'
},
debug: {
id: 'gui.menuBar.debug',
defaultMessage: 'Debug',
description: 'accessibility text for the debug button'
}
});
const MenuBarItemTooltip = ({
children,
className,
enable,
id,
place = 'bottom'
}) => {
if (enable) {
return (
{children}
);
}
return (
{children}
);
};
MenuBarItemTooltip.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
enable: PropTypes.bool,
id: PropTypes.string,
place: PropTypes.oneOf(['top', 'bottom', 'left', 'right'])
};
const MenuItemTooltip = ({id, isRtl, children, className}) => (
{children}
);
MenuItemTooltip.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
id: PropTypes.string,
isRtl: PropTypes.bool
};
const AboutButton = props => (
);
AboutButton.propTypes = {
onClick: PropTypes.func.isRequired
};
class MenuBar extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleClickNew',
'handleClickRemix',
'handleClickSave',
'handleClickSaveAsCopy',
'handleClickSeeCommunity',
'handleClickShare',
'handleSetMode',
'handleKeyPress',
'handleRestoreOption',
'getSaveToComputerHandler',
'restoreOptionMessage'
]);
}
componentDidMount () {
document.addEventListener('keydown', this.handleKeyPress);
}
componentWillUnmount () {
document.removeEventListener('keydown', this.handleKeyPress);
}
handleClickNew () {
// if the project is dirty, and user owns the project, we will autosave.
// but if they are not logged in and can't save, user should consider
// downloading or logging in first.
// Note that if user is logged in and editing someone else's project,
// they'll lose their work.
const readyToReplaceProject = this.props.confirmReadyToReplaceProject(
this.props.intl.formatMessage(sharedMessages.replaceProjectWarning)
);
this.props.onRequestCloseFile();
if (readyToReplaceProject) {
this.props.onClickNew(this.props.canSave && this.props.canCreateNew);
}
this.props.onRequestCloseFile();
}
handleClickRemix () {
this.props.onClickRemix();
this.props.onRequestCloseFile();
}
handleClickSave () {
this.props.onClickSave();
this.props.onRequestCloseFile();
}
handleClickSaveAsCopy () {
this.props.onClickSaveAsCopy();
this.props.onRequestCloseFile();
}
handleClickSeeCommunity (waitForUpdate) {
if (this.props.shouldSaveBeforeTransition()) {
this.props.autoUpdateProject(); // save before transitioning to project page
waitForUpdate(true); // queue the transition to project page
} else {
waitForUpdate(false); // immediately transition to project page
}
}
handleClickShare (waitForUpdate) {
if (!this.props.isShared) {
if (this.props.canShare) { // save before transitioning to project page
this.props.onShare();
}
if (this.props.canSave) { // save before transitioning to project page
this.props.autoUpdateProject();
waitForUpdate(true); // queue the transition to project page
} else {
waitForUpdate(false); // immediately transition to project page
}
}
}
handleSetMode (mode) {
return () => {
// Turn on/off filters for modes.
if (mode === '1920') {
document.documentElement.style.filter = 'brightness(.9)contrast(.8)sepia(1.0)';
document.documentElement.style.height = '100%';
} else if (mode === '1990') {
document.documentElement.style.filter = 'hue-rotate(40deg)';
document.documentElement.style.height = '100%';
} else {
document.documentElement.style.filter = '';
document.documentElement.style.height = '';
}
// Change logo for modes
if (mode === '1990') {
document.getElementById('logo_img').src = ninetiesLogo;
} else if (mode === '2020') {
document.getElementById('logo_img').src = catLogo;
} else if (mode === '1920') {
document.getElementById('logo_img').src = oldtimeyLogo;
} else if (mode === '220022BC') {
document.getElementById('logo_img').src = prehistoricLogo;
} else {
document.getElementById('logo_img').src = this.props.logo;
}
this.props.onSetTimeTravelMode(mode);
};
}
handleRestoreOption (restoreFun) {
return () => {
restoreFun();
this.props.onRequestCloseEdit();
};
}
handleKeyPress (event) {
const modifier = bowser.mac ? event.metaKey : event.ctrlKey;
if (modifier && event.key === 's') {
this.props.onClickSave();
event.preventDefault();
}
}
getSaveToComputerHandler (downloadProjectCallback) {
return () => {
this.props.onRequestCloseFile();
downloadProjectCallback();
if (this.props.onProjectTelemetryEvent) {
const metadata = collectMetadata(this.props.vm, this.props.projectTitle, this.props.locale);
this.props.onProjectTelemetryEvent('projectDidSave', metadata);
}
};
}
restoreOptionMessage (deletedItem) {
switch (deletedItem) {
case 'Sprite':
return ();
case 'Sound':
return ();
case 'Costume':
return ();
default: {
return ();
}
}
}
buildAboutMenu (onClickAbout) {
if (!onClickAbout) {
// hide the button
return null;
}
if (typeof onClickAbout === 'function') {
// make a button which calls a function
return ;
}
// assume it's an array of objects
// each item must have a 'title' FormattedMessage and a 'handleClick' function
// generate a menu with items for each object in the array
return (
{
onClickAbout.map(itemProps => (
))
}
);
}
wrapAboutMenuCallback (callback) {
return () => {
callback();
this.props.onRequestCloseAbout();
};
}
render () {
const saveNowMessage = (
);
const createCopyMessage = (
);
const remixMessage = (
);
const newProjectMessage = (
);
const remixButton = (
);
// Show the About button only if we have a handler for it (like in the desktop app)
const aboutButton = this.buildAboutMenu(this.props.onClickAbout);
return (
{(this.props.canChangeTheme || this.props.canChangeLanguage) && (
)}
{(this.props.canManageFiles) && (
{(this.props.canSave || this.props.canCreateCopy || this.props.canRemix) && (
{this.props.canSave && (
)}
{this.props.canCreateCopy && (
)}
{this.props.canRemix && (
)}
)}
{(className, downloadProjectCallback) => (
)}
)}
{(handleRestore, {restorable, deletedItem}) => (
)}
{(toggleTurboMode, {turboMode}) => (
)}
{this.props.isTotallyNormal && (
)}
{this.props.canEditTitle ? (
) : ((this.props.authorUsername && this.props.authorUsername !== this.props.username) ? (
) : null)}
{this.props.canShare ? (
(this.props.isShowingProject || this.props.isUpdating) && (
{
waitForUpdate => (
{
this.handleClickShare(waitForUpdate);
}}
/* eslint-enable react/jsx-no-bind */
/>
)
}
)
) : (
this.props.showComingSoon ? (
) : []
)}
{this.props.canRemix ? remixButton : []}
{this.props.enableCommunity ? (
(this.props.isShowingProject || this.props.isUpdating) && (
{
waitForUpdate => (
{
this.handleClickSeeCommunity(waitForUpdate);
}}
/* eslint-enable react/jsx-no-bind */
/>
)
}
)
) : (this.props.showComingSoon ? (
) : [])}
{/* show the proper UI in the account menu, given whether the user is
logged in, and whether a session is available to log in with */}
{this.props.canSave && (
)}
{this.props.sessionExists ? (
this.props.username ? (
// ************ user is logged in ************
) : (
// ********* user not logged in, but a session exists
// ********* so they can choose to log in
)
) : (
// ******** no login session is available, so don't show login stuff
{this.props.showComingSoon ? (
{'scratch-cat'}
) : []}
)}
{aboutButton}
);
}
}
MenuBar.propTypes = {
aboutMenuOpen: PropTypes.bool,
accountMenuOpen: PropTypes.bool,
authorId: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
authorThumbnailUrl: PropTypes.string,
authorUsername: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
autoUpdateProject: PropTypes.func,
canChangeLanguage: PropTypes.bool,
canChangeTheme: PropTypes.bool,
canCreateCopy: PropTypes.bool,
canCreateNew: PropTypes.bool,
canEditTitle: PropTypes.bool,
canManageFiles: PropTypes.bool,
canRemix: PropTypes.bool,
canSave: PropTypes.bool,
canShare: PropTypes.bool,
className: PropTypes.string,
confirmReadyToReplaceProject: PropTypes.func,
currentLocale: PropTypes.string.isRequired,
editMenuOpen: PropTypes.bool,
enableCommunity: PropTypes.bool,
fileMenuOpen: PropTypes.bool,
intl: intlShape,
isRtl: PropTypes.bool,
isShared: PropTypes.bool,
isShowingProject: PropTypes.bool,
isTotallyNormal: PropTypes.bool,
isUpdating: PropTypes.bool,
locale: PropTypes.string.isRequired,
loginMenuOpen: PropTypes.bool,
logo: PropTypes.string,
mode1920: PropTypes.bool,
mode1990: PropTypes.bool,
mode2020: PropTypes.bool,
mode220022BC: PropTypes.bool,
modeMenuOpen: PropTypes.bool,
modeNow: PropTypes.bool,
onClickAbout: PropTypes.oneOfType([
PropTypes.func, // button mode: call this callback when the About button is clicked
PropTypes.arrayOf( // menu mode: list of items in the About menu
PropTypes.shape({
title: PropTypes.string, // text for the menu item
onClick: PropTypes.func // call this callback when the menu item is clicked
})
)
]),
onClickAccount: PropTypes.func,
onClickEdit: PropTypes.func,
onClickFile: PropTypes.func,
onClickLogin: PropTypes.func,
onClickLogo: PropTypes.func,
onClickMode: PropTypes.func,
onClickNew: PropTypes.func,
onClickRemix: PropTypes.func,
onClickSave: PropTypes.func,
onClickSaveAsCopy: PropTypes.func,
onClickSettings: PropTypes.func,
onLogOut: PropTypes.func,
onOpenRegistration: PropTypes.func,
onOpenTipLibrary: PropTypes.func,
onOpenDebugModal: PropTypes.func,
onProjectTelemetryEvent: PropTypes.func,
onRequestCloseAbout: PropTypes.func,
onRequestCloseAccount: PropTypes.func,
onRequestCloseEdit: PropTypes.func,
onRequestCloseFile: PropTypes.func,
onRequestCloseLogin: PropTypes.func,
onRequestCloseMode: PropTypes.func,
onRequestCloseSettings: PropTypes.func,
onRequestOpenAbout: PropTypes.func,
onSeeCommunity: PropTypes.func,
onSetTimeTravelMode: PropTypes.func,
onShare: PropTypes.func,
onStartSelectingFileUpload: PropTypes.func,
onToggleLoginOpen: PropTypes.func,
projectTitle: PropTypes.string,
renderLogin: PropTypes.func,
sessionExists: PropTypes.bool,
settingsMenuOpen: PropTypes.bool,
shouldSaveBeforeTransition: PropTypes.func,
showComingSoon: PropTypes.bool,
username: PropTypes.string,
userOwnsProject: PropTypes.bool,
vm: PropTypes.instanceOf(VM).isRequired
};
MenuBar.defaultProps = {
logo: scratchLogo,
onShare: () => {}
};
const mapStateToProps = (state, ownProps) => {
const loadingState = state.scratchGui.projectState.loadingState;
const user = state.session && state.session.session && state.session.session.user;
return {
aboutMenuOpen: aboutMenuOpen(state),
accountMenuOpen: accountMenuOpen(state),
currentLocale: state.locales.locale,
fileMenuOpen: fileMenuOpen(state),
editMenuOpen: editMenuOpen(state),
isRtl: state.locales.isRtl,
isUpdating: getIsUpdating(loadingState),
isShowingProject: getIsShowingProject(loadingState),
locale: state.locales.locale,
loginMenuOpen: loginMenuOpen(state),
modeMenuOpen: modeMenuOpen(state),
projectTitle: state.scratchGui.projectTitle,
sessionExists: state.session && typeof state.session.session !== 'undefined',
settingsMenuOpen: settingsMenuOpen(state),
username: user ? user.username : null,
userOwnsProject: ownProps.authorUsername && user &&
(ownProps.authorUsername === user.username),
vm: state.scratchGui.vm,
mode220022BC: isTimeTravel220022BC(state),
mode1920: isTimeTravel1920(state),
mode1990: isTimeTravel1990(state),
mode2020: isTimeTravel2020(state),
modeNow: isTimeTravelNow(state)
};
};
const mapDispatchToProps = dispatch => ({
autoUpdateProject: () => dispatch(autoUpdateProject()),
onOpenTipLibrary: () => dispatch(openTipsLibrary()),
onOpenDebugModal: () => dispatch(openDebugModal()),
onClickAccount: () => dispatch(openAccountMenu()),
onRequestCloseAccount: () => dispatch(closeAccountMenu()),
onClickFile: () => dispatch(openFileMenu()),
onRequestCloseFile: () => dispatch(closeFileMenu()),
onClickEdit: () => dispatch(openEditMenu()),
onRequestCloseEdit: () => dispatch(closeEditMenu()),
onClickLogin: () => dispatch(openLoginMenu()),
onRequestCloseLogin: () => dispatch(closeLoginMenu()),
onClickMode: () => dispatch(openModeMenu()),
onRequestCloseMode: () => dispatch(closeModeMenu()),
onRequestOpenAbout: () => dispatch(openAboutMenu()),
onRequestCloseAbout: () => dispatch(closeAboutMenu()),
onClickSettings: () => dispatch(openSettingsMenu()),
onRequestCloseSettings: () => dispatch(closeSettingsMenu()),
onClickNew: needSave => dispatch(requestNewProject(needSave)),
onClickRemix: () => dispatch(remixProject()),
onClickSave: () => dispatch(manualUpdateProject()),
onClickSaveAsCopy: () => dispatch(saveProjectAsCopy()),
onSeeCommunity: () => dispatch(setPlayer(true)),
onSetTimeTravelMode: mode => dispatch(setTimeTravel(mode))
});
export default compose(
injectIntl,
MenuBarHOC,
connect(
mapStateToProps,
mapDispatchToProps
)
)(MenuBar);