UNPKG

d2-ui

Version:
221 lines (191 loc) 9.09 kB
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import TextField from 'material-ui/TextField/TextField'; import camelCaseToUnderscores from 'd2-utilizr/lib/camelCaseToUnderscores'; import RaisedButton from 'material-ui/RaisedButton/RaisedButton'; import { Observable } from 'rxjs'; import LocaleSelector from '../i18n/LocaleSelector.component'; import { getLocales, getTranslationsForModel, saveTranslations } from './translationForm.actions'; import withStateFrom from '../component-helpers/withStateFrom'; import Store from '../store/Store'; import CircularProgress from '../circular-progress/CircularProgress'; function getTranslationFormData(model) { var translationStore = Store.create(); getTranslationsForModel(model).subscribe(function (translations) { translationStore.setState(translations); }); return Observable.combineLatest(getLocales(), translationStore, function () { for (var _len = arguments.length, data = Array(_len), _key = 0; _key < _len; _key++) { data[_key] = arguments[_key]; } return Object.assign.apply(Object, [{ objectToTranslate: model, setTranslations: function setTranslations(translations) { translationStore.setState({ translations: translations }); } }].concat(data)); }); } export function getTranslationFormFor(model) { return withStateFrom(getTranslationFormData(model), TranslationForm); } var TranslationForm = function (_Component) { _inherits(TranslationForm, _Component); function TranslationForm(props, context) { _classCallCheck(this, TranslationForm); var _this = _possibleConstructorReturn(this, (TranslationForm.__proto__ || Object.getPrototypeOf(TranslationForm)).call(this, props, context)); _this.state = { loading: true, translations: {}, translationValues: {}, currentSelectedLocale: '' }; _this.setCurrentLocale = function (locale) { _this.setState({ currentSelectedLocale: locale }); }; _this.setValue = function (property, event) { var newTranslations = [].concat(_this.props.translations); var translation = newTranslations.find(function (t) { return t.locale === _this.state.currentSelectedLocale && t.property.toLowerCase() === camelCaseToUnderscores(property); }); if (translation) { if (event.target.value) { translation.value = event.target.value; } else { // Remove translation from the array newTranslations = newTranslations.filter(function (t) { return t !== translation; }); } } else { translation = { property: camelCaseToUnderscores(property).toUpperCase(), locale: _this.state.currentSelectedLocale, value: event.target.value }; newTranslations.push(translation); } _this.props.setTranslations(newTranslations); }; _this.saveTranslations = function () { saveTranslations(_this.props.objectToTranslate, _this.props.translations).subscribe(_this.props.onTranslationSaved, _this.props.onTranslationError); }; var i18n = _this.context.d2.i18n; _this.getTranslation = i18n.getTranslation.bind(i18n); return _this; } _createClass(TranslationForm, [{ key: 'getLoadingdataElement', value: function getLoadingdataElement() { return React.createElement( 'div', { style: { textAlign: 'center' } }, React.createElement(CircularProgress, { mode: 'indeterminate' }) ); } }, { key: 'renderFieldsToTranslate', value: function renderFieldsToTranslate() { var _this2 = this; return this.props.fieldsToTranslate.filter(function (fieldName) { return fieldName; }).map(function (fieldName) { return React.createElement( 'div', { key: fieldName }, React.createElement(TextField, { floatingLabelText: _this2.getTranslation(camelCaseToUnderscores(fieldName)), value: _this2.getTranslationValueFor(fieldName), fullWidth: true, onChange: _this2.setValue.bind(_this2, fieldName) }), React.createElement( 'div', null, _this2.props.objectToTranslate[fieldName] ) ); }); } }, { key: 'renderForm', value: function renderForm() { return React.createElement( 'div', null, this.renderFieldsToTranslate(), React.createElement(RaisedButton, { label: this.getTranslation('save'), primary: true, onClick: this.saveTranslations }), React.createElement(RaisedButton, { style: { marginLeft: '1rem' }, label: this.getTranslation('cancel'), onClick: this.props.onCancel }) ); } }, { key: 'renderHelpText', value: function renderHelpText() { return React.createElement( 'div', null, React.createElement( 'p', null, this.getTranslation('select_a_locale_to_enter_translations_for_that_language') ) ); } }, { key: 'render', value: function render() { if (!this.props.locales && !this.props.translations) { return this.getLoadingdataElement(); } return React.createElement( 'div', { style: { minHeight: 250 } }, React.createElement(LocaleSelector, { locales: this.props.locales, onChange: this.setCurrentLocale }), this.state.currentSelectedLocale ? this.renderForm() : this.renderHelpText() ); } }, { key: 'getTranslationValueFor', value: function getTranslationValueFor(fieldName) { var _this3 = this; var translation = this.props.translations.find(function (t) { return t.locale === _this3.state.currentSelectedLocale && t.property.toLowerCase() === camelCaseToUnderscores(fieldName); }); if (translation) { return translation.value; } } }]); return TranslationForm; }(Component); TranslationForm.propTypes = { onTranslationSaved: PropTypes.func.isRequired, onTranslationError: PropTypes.func.isRequired, objectToTranslate: PropTypes.shape({ id: PropTypes.string.isRequired }), fieldsToTranslate: PropTypes.arrayOf(PropTypes.string) }; TranslationForm.defaultProps = { fieldsToTranslate: ['name', 'shortName', 'description'] }; TranslationForm.contextTypes = { d2: PropTypes.object }; export default TranslationForm;