Skip to content
Snippets Groups Projects
Select Git revision
  • f2d35ff48a6a0fb25fdea6d6e702544cae524d7d
  • master default protected
  • doli-script-update
  • add-info-cli
  • improve_doli_logs
  • nullmailer
  • 79-expedition-de-mails-depuis-nos-instances-dolibarr
  • 4-mettre-a-jour-le-fichier-env-sample
  • sso
  • donation-form
  • 7-agenda
11 results

doli

Blame
  • workspace.js 2.97 KiB
    import uuid from 'uuid/v4';
    import ActionTypes from '../actions/action-types';
    
    /** Check if the viewport dimensions are fully specified */
    function hasViewportPosition(viewportPosition) {
      return viewportPosition.x !== undefined
        && viewportPosition.y !== undefined
        && viewportPosition.width !== undefined
        && viewportPosition.height !== undefined;
    }
    
    /** Check if the containee is fully within the bounds on the container */
    function contains(container, containee) {
      return containee.x - containee.width / 2 > container.x - container.width / 2
        && containee.y - containee.height / 2 > container.y - container.height / 2
        && containee.x + containee.width / 2 < container.x + container.width / 2
        && containee.y + containee.height / 2 < container.y + container.height / 2;
    }
    
    /**
     * workspaceReducer
     */
    export const workspaceReducer = (
      state = { // we'll need to abstract this more, methinks.
        draggingEnabled: true,
        exposeModeOn: false,
        height: 5000,
        id: uuid(),
        viewportPosition: {
          x: 0,
          y: 0,
        },
        width: 5000,
      },
      action,
    ) => {
      let newWorkspaceDimensions;
      let viewportPosition;
      switch (action.type) {
        case ActionTypes.FOCUS_WINDOW:
          return {
            ...state,
            focusedWindowId: action.windowId,
            viewportPosition: {
              ...state.viewportPosition,
              ...action.position,
            },
          };
        case ActionTypes.ADD_WINDOW:
          return {
            ...state,
            focusedWindowId: action.window.id,
          };
        case ActionTypes.REMOVE_WINDOW:
          if (Object.keys(action.windows).length > 2) return state;
          return {
            ...state,
            focusedWindowId: Object.keys(action.windows).find(e => e !== action.windowId),
          };
        case ActionTypes.SET_WORKSPACE_FULLSCREEN:
          return { ...state, isFullscreenEnabled: action.isFullscreenEnabled };
        case ActionTypes.TOGGLE_ZOOM_CONTROLS:
          return { ...state, showZoomControls: action.showZoomControls };
        case ActionTypes.UPDATE_WORKSPACE_MOSAIC_LAYOUT:
          return { ...state, layout: action.layout };
        case ActionTypes.SET_WORKSPACE_VIEWPORT_POSITION:
          newWorkspaceDimensions = {};
    
          viewportPosition = {
            ...state.viewportPosition,
            ...action.payload.position,
          };
    
          if (
            hasViewportPosition(viewportPosition)
            && !contains({
              height: state.height, width: state.width, x: 0, y: 0,
            }, viewportPosition)
          ) {
            newWorkspaceDimensions = {
              height: state.height * 2,
              width: state.width * 2,
            };
          }
    
          return {
            ...state,
            ...newWorkspaceDimensions,
            viewportPosition,
          };
        case ActionTypes.TOGGLE_WORKSPACE_EXPOSE_MODE:
          return { ...state, exposeModeOn: !state.exposeModeOn };
        case ActionTypes.IMPORT_MIRADOR_STATE:
          return action.state.workspace;
        case ActionTypes.TOGGLE_DRAGGING:
          return { ...state, draggingEnabled: !state.draggingEnabled };
        default:
          return state;
      }
    };