// Edit account parameters. import React from 'react'; import { FormattedMessage } from 'react-intl'; import HashNavigation from '../lib/navigation.js'; import { WALLPAPER_DEFAULTS } from '../config.js'; import { wallpaperNameFromUrl, wallpaperTypeFromUrl } from '../lib/utils.js'; import imageIndex from '../../img/bkg/index.json'; export default class WallpapersView extends React.PureComponent { constructor(props) { super(props); const type = wallpaperTypeFromUrl(this.props.wallpaper); const name = wallpaperNameFromUrl(this.props.wallpaper); this.state = { tab: type || WALLPAPER_DEFAULTS.type, // currently selected tab: 'patt' | 'img' selectedType: type || WALLPAPER_DEFAULTS.type, // type of the selected wallpaper. wallpaper: name || imageIndex[WALLPAPER_DEFAULTS.type][WALLPAPER_DEFAULTS.index].name, blur: this.props.wallpaperBlur | 0, }; this.blurValues = [0, 1, 2, 4, 8, 16]; this.handleTabClick = this.handleTabClick.bind(this); this.handleWallpaperSelected = this.handleWallpaperSelected.bind(this); this.handleBlurChanged = this.handleBlurChanged.bind(this); this.hasChanged = this.hasChanged.bind(this); } handleTabClick(e) { e.preventDefault(); HashNavigation.navigateTo(HashNavigation.addUrlParam(window.location.hash, 'tab', e.currentTarget.dataset.id)); this.setState({tab: e.currentTarget.dataset.id}); } handleWallpaperSelected(e) { let index, type, blur if (!e) { if (!this.hasChanged()) { return; } // Restore default. type = WALLPAPER_DEFAULTS.type; index = WALLPAPER_DEFAULTS.index; blur = 0; } else { e.preventDefault(); type = this.state.tab; index = e.currentTarget.dataset.id; blur = type == 'patt' ? 0 : this.state.blur; } const fname = imageIndex[type][index].name; const size = type == 'patt' ? imageIndex[type][index].size : 0; this.setState({tab: type, wallpaper: fname, selectedType: type, blur: blur}); this.props.onWallpaperSelected(`../${WALLPAPER_DEFAULTS.path}${fname}`, size, this.blurValues[blur]); } handleBlurChanged(e) { e.preventDefault(); const blur = e.currentTarget.value; this.setState({blur: blur}); if (this.state.selectedType == 'img') { // Apply blur to patterns only. Path relative to CSS file. this.props.onWallpaperSelected(`../${WALLPAPER_DEFAULTS.path}${this.state.wallpaper}`, 0, this.blurValues[blur]); } } hasChanged() { return this.state.selectedType != WALLPAPER_DEFAULTS.type || this.state.wallpaper != imageIndex[WALLPAPER_DEFAULTS.type][WALLPAPER_DEFAULTS.index].name || this.state.blur != 0; } render() { return (