file.js 7.24 KB
Newer Older
1
import { __ } from '../../../locale';
Phil Hughes's avatar
Phil Hughes committed
2
import { normalizeHeaders } from '../../../lib/utils/common_utils';
Phil Hughes's avatar
Phil Hughes committed
3 4 5 6 7
import eventHub from '../../eventhub';
import service from '../../services';
import * as types from '../mutation_types';
import router from '../../ide_router';
import { setPageTitle } from '../utils';
8
import { viewerTypes } from '../../constants';
Phil Hughes's avatar
Phil Hughes committed
9

Phil Hughes's avatar
Phil Hughes committed
10
export const closeFile = ({ commit, state, dispatch }, file) => {
11
  const { path } = file;
Phil Hughes's avatar
Phil Hughes committed
12
  const indexOfClosedFile = state.openFiles.findIndex(f => f.key === file.key);
Phil Hughes's avatar
Phil Hughes committed
13 14
  const fileWasActive = file.active;

Phil Hughes's avatar
Phil Hughes committed
15 16 17 18 19
  if (file.pending) {
    commit(types.REMOVE_PENDING_TAB, file);
  } else {
    commit(types.TOGGLE_FILE_OPEN, path);
    commit(types.SET_FILE_ACTIVE, { path, active: false });
20
  }
Phil Hughes's avatar
Phil Hughes committed
21 22 23

  if (state.openFiles.length > 0 && fileWasActive) {
    const nextIndexToOpen = indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
Phil Hughes's avatar
Phil Hughes committed
24
    const nextFileToOpen = state.openFiles[nextIndexToOpen];
Phil Hughes's avatar
Phil Hughes committed
25

26
    if (nextFileToOpen.pending) {
27
      dispatch('updateViewer', viewerTypes.diff);
28 29 30 31
      dispatch('openPendingTab', {
        file: nextFileToOpen,
        keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged',
      });
32 33
    } else {
      router.push(`/project${nextFileToOpen.url}`);
Phil Hughes's avatar
Phil Hughes committed
34
    }
Phil Hughes's avatar
Phil Hughes committed
35 36 37 38
  } else if (!state.openFiles.length) {
    router.push(`/project/${file.projectId}/tree/${file.branchId}/`);
  }

39
  eventHub.$emit(`editor.update.model.dispose.${file.key}`);
Phil Hughes's avatar
Phil Hughes committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
};

export const setFileActive = ({ commit, state, getters, dispatch }, path) => {
  const file = state.entries[path];
  const currentActiveFile = getters.activeFile;

  if (file.active) return;

  if (currentActiveFile) {
    commit(types.SET_FILE_ACTIVE, {
      path: currentActiveFile.path,
      active: false,
    });
  }

  commit(types.SET_FILE_ACTIVE, { path, active: true });
  dispatch('scrollToTab');

  commit(types.SET_CURRENT_PROJECT, file.projectId);
  commit(types.SET_CURRENT_BRANCH, file.branchId);
};

Tim Zallmann's avatar
Tim Zallmann committed
62
export const getFileData = ({ state, commit, dispatch }, { path, makeFileActive = true }) => {
Tim Zallmann's avatar
Tim Zallmann committed
63
  const file = state.entries[path];
64 65 66

  if (file.raw || file.tempFile) return Promise.resolve();

Phil Hughes's avatar
Phil Hughes committed
67
  commit(types.TOGGLE_LOADING, { entry: file });
68

Phil Hughes's avatar
Phil Hughes committed
69
  return service
70 71 72
    .getFileData(
      `${gon.relative_url_root ? gon.relative_url_root : ''}${file.url.replace('/-/', '/')}`,
    )
73
    .then(({ data, headers }) => {
Phil Hughes's avatar
Phil Hughes committed
74 75
      const normalizedHeaders = normalizeHeaders(headers);
      setPageTitle(decodeURI(normalizedHeaders['PAGE-TITLE']));
Phil Hughes's avatar
Phil Hughes committed
76 77

      commit(types.SET_FILE_DATA, { data, file });
78
      if (makeFileActive) commit(types.TOGGLE_FILE_OPEN, path);
Tim Zallmann's avatar
Tim Zallmann committed
79
      if (makeFileActive) dispatch('setFileActive', path);
Phil Hughes's avatar
Phil Hughes committed
80 81 82 83
      commit(types.TOGGLE_LOADING, { entry: file });
    })
    .catch(() => {
      commit(types.TOGGLE_LOADING, { entry: file });
84 85 86 87 88 89 90
      dispatch('setErrorMessage', {
        text: __('An error occured whilst loading the file.'),
        action: payload =>
          dispatch('getFileData', payload).then(() => dispatch('setErrorMessage', null)),
        actionText: __('Please try again'),
        actionPayload: { path, makeFileActive },
      });
Phil Hughes's avatar
Phil Hughes committed
91 92 93
    });
};

Lukas Eipert's avatar
Lukas Eipert committed
94
export const setFileMrChange = ({ commit }, { file, mrChange }) => {
Tim Zallmann's avatar
Tim Zallmann committed
95
  commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file, mrChange });
Tim Zallmann's avatar
Tim Zallmann committed
96 97
};

98
export const getRawFileData = ({ state, commit, dispatch }, { path, baseSha }) => {
Tim Zallmann's avatar
Tim Zallmann committed
99
  const file = state.entries[path];
Tim Zallmann's avatar
Tim Zallmann committed
100 101 102 103
  return new Promise((resolve, reject) => {
    service
      .getRawFileData(file)
      .then(raw => {
104
        if (!file.tempFile) commit(types.SET_FILE_RAW_DATA, { file, raw });
Tim Zallmann's avatar
Tim Zallmann committed
105
        if (file.mrChange && file.mrChange.new_file === false) {
Tim Zallmann's avatar
Tim Zallmann committed
106 107 108 109 110 111 112 113 114 115 116 117
          service
            .getBaseRawFileData(file, baseSha)
            .then(baseRaw => {
              commit(types.SET_FILE_BASE_RAW_DATA, {
                file,
                baseRaw,
              });
              resolve(raw);
            })
            .catch(e => {
              reject(e);
            });
Tim Zallmann's avatar
Tim Zallmann committed
118 119 120 121 122
        } else {
          resolve(raw);
        }
      })
      .catch(() => {
123 124 125 126 127 128 129
        dispatch('setErrorMessage', {
          text: __('An error occured whilst loading the file content.'),
          action: payload =>
            dispatch('getRawFileData', payload).then(() => dispatch('setErrorMessage', null)),
          actionText: __('Please try again'),
          actionPayload: { path, baseSha },
        });
Tim Zallmann's avatar
Tim Zallmann committed
130 131 132 133
        reject();
      });
  });
};
Phil Hughes's avatar
Phil Hughes committed
134

135
export const changeFileContent = ({ commit, dispatch, state }, { path, content }) => {
Phil Hughes's avatar
Phil Hughes committed
136 137 138 139 140 141 142 143 144 145
  const file = state.entries[path];
  commit(types.UPDATE_FILE_CONTENT, { path, content });

  const indexOfChangedFile = state.changedFiles.findIndex(f => f.path === path);

  if (file.changed && indexOfChangedFile === -1) {
    commit(types.ADD_FILE_TO_CHANGED, path);
  } else if (!file.changed && indexOfChangedFile !== -1) {
    commit(types.REMOVE_FILE_FROM_CHANGED, path);
  }
146 147

  dispatch('burstUnusedSeal', {}, { root: true });
Phil Hughes's avatar
Phil Hughes committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161
};

export const setFileLanguage = ({ getters, commit }, { fileLanguage }) => {
  if (getters.activeFile) {
    commit(types.SET_FILE_LANGUAGE, { file: getters.activeFile, fileLanguage });
  }
};

export const setFileEOL = ({ getters, commit }, { eol }) => {
  if (getters.activeFile) {
    commit(types.SET_FILE_EOL, { file: getters.activeFile, eol });
  }
};

Tim Zallmann's avatar
Tim Zallmann committed
162
export const setEditorPosition = ({ getters, commit }, { editorRow, editorColumn }) => {
Phil Hughes's avatar
Phil Hughes committed
163 164 165 166 167 168 169 170 171
  if (getters.activeFile) {
    commit(types.SET_FILE_POSITION, {
      file: getters.activeFile,
      editorRow,
      editorColumn,
    });
  }
};

Lukas Eipert's avatar
Lukas Eipert committed
172
export const setFileViewMode = ({ commit }, { file, viewMode }) => {
Tim Zallmann's avatar
Tim Zallmann committed
173 174 175
  commit(types.SET_FILE_VIEWMODE, { file, viewMode });
};

176
export const discardFileChanges = ({ dispatch, state, commit, getters }, path) => {
Phil Hughes's avatar
Phil Hughes committed
177 178 179 180 181 182 183
  const file = state.entries[path];

  commit(types.DISCARD_FILE_CHANGES, path);
  commit(types.REMOVE_FILE_FROM_CHANGED, path);

  if (file.tempFile && file.opened) {
    commit(types.TOGGLE_FILE_OPEN, path);
184 185 186 187 188 189 190 191
  } else if (getters.activeFile && file.path === getters.activeFile.path) {
    dispatch('updateDelayViewerUpdated', true)
      .then(() => {
        router.push(`/project${file.url}`);
      })
      .catch(e => {
        throw e;
      });
Phil Hughes's avatar
Phil Hughes committed
192 193
  }

Phil Hughes's avatar
Phil Hughes committed
194
  eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content);
195
  eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
Phil Hughes's avatar
Phil Hughes committed
196
};
197

198 199 200
export const stageChange = ({ commit, state }, path) => {
  const stagedFile = state.stagedFiles.find(f => f.path === path);

201
  commit(types.STAGE_CHANGE, path);
Phil Hughes's avatar
Phil Hughes committed
202
  commit(types.SET_LAST_COMMIT_MSG, '');
203 204

  if (stagedFile) {
205
    eventHub.$emit(`editor.update.model.new.content.staged-${stagedFile.key}`, stagedFile.content);
206
  }
207 208
};

209 210
export const unstageChange = ({ commit }, path) => {
  commit(types.UNSTAGE_CHANGE, path);
211
};
Phil Hughes's avatar
Phil Hughes committed
212

213
export const openPendingTab = ({ commit, getters, dispatch, state }, { file, keyPrefix }) => {
214 215
  if (getters.activeFile && getters.activeFile.key === `${keyPrefix}-${file.key}`) return false;

216 217
  state.openFiles.forEach(f => eventHub.$emit(`editor.update.model.dispose.${f.key}`));

218
  commit(types.ADD_PENDING_TAB, { file, keyPrefix });
219

220 221
  dispatch('scrollToTab');

222
  router.push(`/project/${file.projectId}/tree/${state.currentBranchId}/`);
223 224

  return true;
225 226 227 228
};

export const removePendingTab = ({ commit }, file) => {
  commit(types.REMOVE_PENDING_TAB, file);
229 230

  eventHub.$emit(`editor.update.model.dispose.${file.key}`);
Phil Hughes's avatar
Phil Hughes committed
231
};