Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: maps, ev and er were not resizing for fullscreen or item resize in edit mode #1659

Merged
merged 7 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 10 additions & 44 deletions src/components/Item/VisualizationItem/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import {
} from '../../../reducers/itemFilters'
import { acAddVisualization } from '../../../actions/visualizations'
import { acSetSelectedItemActiveType } from '../../../actions/selected'
import {
pluginIsAvailable,
resize as pluginResize,
} from './Visualization/plugin'
import { pluginIsAvailable } from './Visualization/plugin'
import { getDataStatisticsName } from '../../../modules/itemTypes'
import { getVisualizationId, getVisualizationName } from '../../../modules/item'
import memoizeOne from '../../../modules/memoizeOne'
Expand All @@ -34,7 +31,6 @@ import {
isViewMode,
} from '../../Dashboard/dashboardModes'
import { getItemHeightPx } from '../../../modules/gridUtil'
import getGridItemDomId from '../../../modules/getGridItemDomId'

export class Item extends Component {
state = {
Expand Down Expand Up @@ -121,50 +117,17 @@ export class Item extends Component {
}
}

componentDidUpdate(prevProps) {
if (prevProps.gridWidth !== this.props.gridWidth) {
const el = document.querySelector(
`#${getGridItemDomId(this.props.item.id)}`
)
if (typeof el?.setViewportSize === 'function') {
setTimeout(
() =>
el.setViewportSize(
el.clientWidth - 5,
el.clientHeight - 5
),
10
)
}
// call resize on Map item
pluginResize(
this.props.item.id,
this.getActiveType(),
this.state.isFullscreen
)
}
}

isFullscreenSupported = () => {
const el = document.querySelector(this.itemDomElSelector)
return !!(el?.requestFullscreen || el?.webkitRequestFullscreen)
}

handleFullscreenChange = () => {
this.setState(
{
isFullscreen:
!!document.fullscreenElement ||
!!document.webkitFullscreenElement,
},
() =>
pluginResize(
this.props.item.id,
this.getActiveType(),
this.state.isFullscreen
)
)
}
handleFullscreenChange = () =>
this.setState({
isFullscreen:
!!document.fullscreenElement ||
!!document.webkitFullscreenElement,
})

onToggleFullscreen = () => {
if (!this.state.isFullscreen) {
Expand Down Expand Up @@ -230,6 +193,7 @@ export class Item extends Component {
const rect = document
.querySelector(this.itemDomElSelector)
?.getBoundingClientRect()

return rect && rect.width - this.itemContentPadding * 2
}

Expand Down Expand Up @@ -289,6 +253,8 @@ export class Item extends Component {
dimensions
)}
availableWidth={this.getAvailableWidth()}
isFullscreen={this.state.isFullscreen}
gridWidth={this.props.gridWidth}
/>
)}
</WindowDimensionsCtx.Consumer>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import DefaultPlugin from './DefaultPlugin'
import getGridItemDomId from '../../../../modules/getGridItemDomId'

const LegacyPlugin = ({
isFullscreen,
availableHeight,
availableWidth,
gridWidth,
...props
}) => {
useEffect(() => {
const el = document.querySelector(`#${getGridItemDomId(props.item.id)}`)
if (typeof el?.setViewportSize === 'function') {
setTimeout(
() =>
el.setViewportSize(el.clientWidth - 5, el.clientHeight - 5),
10
)
}
}, [availableHeight, availableWidth, isFullscreen, gridWidth])

return <DefaultPlugin {...props} />
}

LegacyPlugin.propTypes = {
activeType: PropTypes.string,
availableHeight: PropTypes.number,
availableWidth: PropTypes.number,
gridWidth: PropTypes.number,
isFullscreen: PropTypes.bool,
item: PropTypes.object,
}

export default LegacyPlugin
24 changes: 19 additions & 5 deletions src/components/Item/VisualizationItem/Visualization/MapPlugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import React from 'react'
import React, { useEffect } from 'react'
import PropTypes from 'prop-types'
import i18n from '@dhis2/d2-i18n'
import DefaultPlugin from './DefaultPlugin'
import { MAP } from '../../../../modules/itemTypes'
import { pluginIsAvailable } from './plugin'
import { pluginIsAvailable, resize } from './plugin'
import NoVisualizationMessage from './NoVisualizationMessage'

const MapPlugin = ({ applyFilters, ...props }) => {
const MapPlugin = ({
applyFilters,
isFullscreen,
availableHeight,
availableWidth,
gridWidth,
...props
}) => {
useEffect(() => {
resize(props.item.id, MAP, isFullscreen)
}, [availableHeight, availableWidth, isFullscreen, gridWidth])

if (props.item.type === MAP) {
// apply filters only to thematic and event layers
// for maps AO
Expand Down Expand Up @@ -35,7 +46,7 @@ const MapPlugin = ({ applyFilters, ...props }) => {
)
}

return pluginIsAvailable(props.activeType || props.item.type) ? (
return pluginIsAvailable(MAP) ? (
<DefaultPlugin
options={{
hideTitle: true,
Expand All @@ -50,8 +61,11 @@ const MapPlugin = ({ applyFilters, ...props }) => {
}

MapPlugin.propTypes = {
activeType: PropTypes.string,
applyFilters: PropTypes.func,
availableHeight: PropTypes.number,
availableWidth: PropTypes.number,
gridWidth: PropTypes.number,
isFullscreen: PropTypes.bool,
item: PropTypes.object,
itemFilters: PropTypes.object,
visualization: PropTypes.object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import i18n from '@dhis2/d2-i18n'

import DefaultPlugin from './DefaultPlugin'
import LegacyPlugin from './LegacyPlugin'
import MapPlugin from './MapPlugin'
import DataVisualizerPlugin from './DataVisualizerPlugin'
import NoVisualizationMessage from './NoVisualizationMessage'
Expand Down Expand Up @@ -37,8 +37,7 @@ class Visualization extends React.Component {
activeType,
item,
itemFilters,
availableHeight,
availableWidth,
...rest
} = this.props

if (!visualization) {
Expand All @@ -49,9 +48,9 @@ class Visualization extends React.Component {
)
}

const style = { height: availableHeight }
if (availableWidth) {
style.width = availableWidth
const style = { height: this.props.availableHeight }
if (this.props.availableWidth) {
style.width = this.props.availableWidth
}

const pluginProps = {
Expand All @@ -64,6 +63,7 @@ class Visualization extends React.Component {
item.type,
activeType
),
...rest,
}

switch (activeType) {
Expand Down Expand Up @@ -97,7 +97,7 @@ class Visualization extends React.Component {
return pluginIsAvailable(
pluginProps.activeType || pluginProps.item.type
) ? (
<DefaultPlugin {...pluginProps} />
<LegacyPlugin {...pluginProps} />
) : (
<NoVisualizationMessage
message={i18n.t(
Expand Down
14 changes: 7 additions & 7 deletions src/components/Item/VisualizationItem/Visualization/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const getPlugin = async type => {
}
const pluginName = itemTypeToGlobalVariable[type]

return global[pluginName]
return await global[pluginName]
}

const fetchPlugin = async (type, baseUrl) => {
Expand Down Expand Up @@ -59,14 +59,15 @@ const fetchPlugin = async (type, baseUrl) => {
const scriptsPromise = Promise.all(scripts.map(loadExternalScript)).then(
() => global[globalName] // At this point, has been replaced with the real thing
)

global[globalName] = scriptsPromise
return await scriptsPromise
}

export const pluginIsAvailable = type =>
hasIntegratedPlugin(type) || itemTypeToGlobalVariable[type]

export const loadPlugin = async (type, config, credentials) => {
const loadPlugin = async (type, config, credentials) => {
if (!pluginIsAvailable(type)) {
return
}
Expand Down Expand Up @@ -107,16 +108,15 @@ export const load = async (
await loadPlugin(type, config, credentials)
}

export const resize = (id, type, isFullscreen = false) => {
const plugin = getPlugin(type)

export const resize = async (id, type, isFullscreen = false) => {
const plugin = await getPlugin(type)
if (plugin?.resize) {
plugin.resize(getGridItemDomId(id), isFullscreen)
}
}

export const unmount = (item, activeType) => {
const plugin = getPlugin(activeType)
export const unmount = async (item, activeType) => {
const plugin = await getPlugin(activeType)

if (plugin && plugin.unmount) {
plugin.unmount(getGridItemDomId(item.id))
Expand Down