Skip to content

Commit

Permalink
fixed form compression (#2024)
Browse files Browse the repository at this point in the history
* fixed form compression

* solved DeepScan issue

Co-authored-by: Stefano Ricci <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 22, 2021
1 parent d133d09 commit 5500940
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 19 additions & 7 deletions core/survey/nodeDefLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ export const getRenderType = (cycle) => _getPropLayout(cycle, keys.renderType)
export const getLayoutChildren = (cycle) => _getPropLayout(cycle, keys.layoutChildren, [])

export const getLayoutChildrenCompressed =
({ cycle, hiddenDefsByUuid = {}, compressHorizontally = true }) =>
({ cycle, hiddenDefsByUuid = {} }) =>
(nodeDef) => {
const layoutChildren = getLayoutChildren(cycle)(nodeDef)
let nextY = 0
let nextX = 0
let itemPrev = { x: 0, y: 0, w: 0, h: 0, xOriginal: 0, yOriginal: 0 }
return (
[...layoutChildren]
// sort layout items from top to bottom
Expand All @@ -78,10 +77,23 @@ export const getLayoutChildrenCompressed =
const hidden = Boolean(hiddenDefsByUuid[childDefUuid])
const h = hidden ? 0 : hOriginal
const w = hidden ? 0 : wOriginal
const x = compressHorizontally ? Math.min(nextX, xOriginal) : xOriginal
const y = Math.min(nextY, yOriginal)
nextY = y + h
nextX = x + w

const sameRowOfPreviousItem = xOriginal > itemPrev.xOriginal

const x = sameRowOfPreviousItem
? // move it to xPrev + wPrev
Math.min(itemPrev.x + itemPrev.w, xOriginal)
: // item in another row, can have the same x of the previous one
Math.min(itemPrev.x, xOriginal)

const y = sameRowOfPreviousItem
? // item can have the same y of the previous one
Math.min(itemPrev.y, yOriginal)
: // item in another row, move it yPrev + hPrev
Math.min(itemPrev.y + itemPrev.h, yOriginal)

itemPrev = { x, y, h, w, xOriginal, yOriginal, hOriginal, wOriginal }

return hidden ? layoutChildrenAcc : [...layoutChildrenAcc, { ...item, h, w, x, y }]
}, [])
)
Expand Down
6 changes: 2 additions & 4 deletions webapp/store/survey/nodeDefs/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ export const putNodeDefLayoutProp =
export const compressFormItems = (nodeDef) => async (dispatch, getState) => {
const state = getState()
const cycle = SurveyState.getSurveyCycleKey(state)
const layoutChildrenCompact = NodeDefLayout.getLayoutChildrenCompressed({ cycle, compressHorizontally: false })(
nodeDef
)
const layoutChildrenCompact = NodeDefLayout.getLayoutChildrenCompressed({ cycle })(nodeDef)
dispatch(putNodeDefLayoutProp({ nodeDef, key: NodeDefLayout.keys.layoutChildren, value: layoutChildrenCompact }))
}

Expand Down Expand Up @@ -255,7 +253,7 @@ export const resetSamplingNodeDefs =
chain,
hideSamplingNodeDefsWithoutSibilings: false,
hideAreaBasedEstimate: false,
showInactiveResultVariables: true
showInactiveResultVariables: true,
})(survey).filter((_nodeDef) => NodeDef.isSampling(_nodeDef) || NodeDef.isBaseUnit(_nodeDef))

const nodeDefUuids = nodeDefs.map(NodeDef.getUuid)
Expand Down

0 comments on commit 5500940

Please sign in to comment.