import React from 'react';
import PropTypes from 'prop-types';
import {
Modal, ModalHeader, ModalBody, ModalFooter,
} from 'reactstrap';
import { withTranslation } from 'react-i18next';
import geu from './GridEditorUtil';
import BootstrapGrid from '../../models/BootstrapGrid';
const resSizes = BootstrapGrid.ResponsiveSize;
const resSizeObj = {
[resSizes.XS_SIZE]: { iconClass: 'icon-screen-smartphone', displayText: 'grid_edit.smart_no' },
[resSizes.SM_SIZE]: { iconClass: 'icon-screen-tablet', displayText: 'tablet' },
[resSizes.MD_SIZE]: { iconClass: 'icon-screen-desktop', displayText: 'desktop' },
};
class GridEditModal extends React.Component {
constructor(props) {
super(props);
this.state = {
colsRatios: [6, 6],
responsiveSize: BootstrapGrid.ResponsiveSize.XS_SIZE,
show: false,
// use when re-edit grid
// gridHtml: '',
};
this.checkResposiveSize = this.checkResposiveSize.bind(this);
this.checkColsRatios = this.checkColsRatios.bind(this);
// use when re-edit grid
// this.init = this.init.bind(this);
this.show = this.show.bind(this);
this.hide = this.hide.bind(this);
this.cancel = this.cancel.bind(this);
this.pasteCodedGrid = this.pasteCodedGrid.bind(this);
this.renderSelectedGridPattern = this.renderSelectedGridPattern.bind(this);
this.renderBreakPointSetting = this.renderBreakPointSetting.bind(this);
}
async checkResposiveSize(rs) {
await this.setState({ responsiveSize: rs });
}
async checkColsRatios(cr) {
await this.setState({ colsRatios: cr });
}
// use when re-edit grid
// init(gridHtml) {
// const initGridHtml = gridHtml;
// this.setState({ gridHtml: initGridHtml });
// }
show(gridHtml) {
// use when re-edit grid
// this.init(gridHtml);
this.setState({ show: true });
}
hide() {
this.setState({ show: false });
}
cancel() {
this.hide();
}
pasteCodedGrid() {
const { colsRatios, responsiveSize } = this.state;
const convertedHTML = geu.convertRatiosAndSizeToHTML(colsRatios, responsiveSize);
const spaceTab = ' ';
const pastedGridData = `::: editable-row\n
\n${spaceTab}
\n${convertedHTML}\n${spaceTab}
\n
\n:::`;
if (this.props.onSave != null) {
this.props.onSave(pastedGridData);
}
this.cancel();
}
renderSelectedGridPattern() {
const colsRatios = this.state.colsRatios;
return colsRatios.join(' - ');
}
renderBreakPointSetting() {
const { t } = this.props;
const output = Object.entries(resSizeObj).map((responsiveSizeForMap) => {
return (
this.checkResposiveSize(responsiveSizeForMap[0])}
/>
);
});
return output;
}
renderGridDivisionMenu() {
const gridDivisions = geu.mappingAllGridDivisionPatterns;
const { t } = this.props;
return (
{gridDivisions.map((gridDivision) => {
const numOfDivisions = gridDivision.numberOfGridDivisions;
return (
{numOfDivisions} {t('grid_edit.division')}
{gridDivision.mapping.map((gridOneDivision) => {
const keyOfRow = `${numOfDivisions}-divisions-${gridOneDivision.join('-')}`;
return (
);
})}
);
})}
);
}
renderPreview() {
const { t } = this.props;
const isMdSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.MD_SIZE;
const isXsSelected = this.state.responsiveSize === BootstrapGrid.ResponsiveSize.XS_SIZE;
return (
{this.renderGridPreview(!isXsSelected)}
{this.renderGridPreview(isMdSelected)}
{this.renderGridPreview(false)}
);
}
renderGridPreview(isBreakEnabled) {
const { colsRatios } = this.state;
const convertedHTML = colsRatios.map((colsRatio, i) => {
const ratio = isBreakEnabled ? 12 : colsRatio;
const key = `grid-preview-col-${i}`;
const className = `col-${ratio} border`;
return (
);
});
return (
{convertedHTML}
);
}
render() {
const { t } = this.props;
return (
{t('grid_edit.create_bootstrap_4_grid')}
{t('grid_edit.grid_settings')}
{t('preview')}
{this.renderPreview()}
);
}
}
GridEditModal.propTypes = {
onSave: PropTypes.func,
t: PropTypes.func.isRequired,
};
export default withTranslation('translation', { withRef: true })(GridEditModal);