BigW Consortium Gitlab

repo_store.js 5.21 KB
Newer Older
1
/* global Flash */
2 3
import Helper from '../helpers/repo_helper';
import Service from '../services/repo_service';
4

5
const RepoStore = {
6
  monaco: {},
Jacob Schatz committed
7
  monacoLoading: false,
8
  service: '',
Jacob Schatz committed
9 10
  canCommit: false,
  onTopOfBranch: false,
11
  editMode: false,
12
  isTree: false,
13
  isRoot: false,
Jacob Schatz committed
14
  prevURL: '',
15
  projectId: '',
16
  projectName: '',
17
  projectUrl: '',
18
  blobRaw: '',
19
  currentBlobView: 'repo-preview',
20
  openedFiles: [],
Jacob Schatz committed
21
  submitCommitsLoading: false,
22 23 24 25 26
  dialog: {
    open: false,
    title: '',
    status: false,
  },
27
  activeFile: Helper.getDefaultActiveFile(),
28
  activeFileIndex: 0,
Jacob Schatz committed
29
  activeLine: 0,
30
  activeFileLabel: 'Raw',
31
  files: [],
Jacob Schatz committed
32
  isCommitable: false,
33
  binary: false,
Jacob Schatz committed
34
  currentBranch: '',
35
  commitMessage: '',
36
  binaryTypes: {
37
    png: false,
38 39 40
    md: false,
    svg: false,
    unknown: false,
41 42 43
  },
  loading: {
    tree: false,
44 45
    blob: false,
  },
46

47
  resetBinaryTypes() {
48 49 50
    Object.keys(RepoStore.binaryTypes).forEach((key) => {
      RepoStore.binaryTypes[key] = false;
    });
51 52
  },

53
  // mutations
Jacob Schatz committed
54
  checkIsCommitable() {
Jacob Schatz committed
55
    RepoStore.isCommitable = RepoStore.onTopOfBranch && RepoStore.canCommit;
Jacob Schatz committed
56 57
  },

58
  addFilesToDirectory(inDirectory, currentList, newList) {
59
    RepoStore.files = Helper.getNewMergedList(inDirectory, currentList, newList);
60 61 62
  },

  toggleRawPreview() {
63 64
    RepoStore.activeFile.raw = !RepoStore.activeFile.raw;
    RepoStore.activeFileLabel = RepoStore.activeFile.raw ? 'Display rendered file' : 'Display source';
65 66 67
  },

  setActiveFiles(file) {
68
    if (RepoStore.isActiveFile(file)) return;
69
    RepoStore.openedFiles = RepoStore.openedFiles
70
      .map((openedFile, i) => RepoStore.setFileActivity(file, openedFile, i));
71

72
    RepoStore.setActiveToRaw();
73 74

    if (file.binary) {
75
      RepoStore.blobRaw = file.base64;
76
    } else if (file.newContent || file.plain) {
77
      RepoStore.blobRaw = file.newContent || file.plain;
78
    } else {
79
      Service.getRaw(file.raw_path)
80 81
        .then((rawResponse) => {
          RepoStore.blobRaw = rawResponse.data;
82 83
          Helper.findOpenedFileFromActive().plain = rawResponse.data;
        }).catch(Helper.loadingError);
84 85
    }

86
    if (!file.loading) Helper.updateHistoryEntry(file.url, file.pageTitle || file.name);
87
    RepoStore.binary = file.binary;
88 89
  },

90 91 92
  setFileActivity(file, openedFile, i) {
    const activeFile = openedFile;
    activeFile.active = file.url === activeFile.url;
93

94
    if (activeFile.active) RepoStore.setActiveFile(activeFile, i);
95 96 97 98 99

    return activeFile;
  },

  setActiveFile(activeFile, i) {
100
    RepoStore.activeFile = Object.assign({}, RepoStore.activeFile, activeFile);
101
    RepoStore.activeFileIndex = i;
102 103 104
  },

  setActiveToRaw() {
105 106 107
    RepoStore.activeFile.raw = false;
    // can't get vue to listen to raw for some reason so RepoStore for now.
    RepoStore.activeFileLabel = 'Display source';
108 109 110 111
  },

  removeChildFilesOfTree(tree) {
    let foundTree = false;
112
    const treeToClose = tree;
Jacob Schatz committed
113
    let canStopSearching = false;
114
    RepoStore.files = RepoStore.files.filter((file) => {
115 116
      const isItTheTreeWeWant = file.url === treeToClose.url;
      // if it's the next tree
117
      if (foundTree && file.type === 'tree' && !isItTheTreeWeWant && file.level === treeToClose.level) {
Jacob Schatz committed
118
        canStopSearching = true;
119 120
        return true;
      }
Jacob Schatz committed
121
      if (canStopSearching) return true;
122

123
      if (isItTheTreeWeWant) foundTree = true;
124

125
      if (foundTree) return file.level <= treeToClose.level;
126 127 128
      return true;
    });

129 130 131
    treeToClose.opened = false;
    treeToClose.icon = 'fa-folder';
    return treeToClose;
132 133 134 135
  },

  removeFromOpenedFiles(file) {
    if (file.type === 'tree') return;
136 137
    let foundIndex;
    RepoStore.openedFiles = RepoStore.openedFiles.filter((openedFile, i) => {
Jacob Schatz committed
138 139
      if (openedFile.path === file.path) foundIndex = i;
      return openedFile.path !== file.path;
140 141 142
    });

    // now activate the right tab based on what you closed.
143
    if (RepoStore.openedFiles.length === 0) {
144 145 146
      RepoStore.activeFile = {};
      return;
    }
147 148

    if (RepoStore.openedFiles.length === 1 || foundIndex === 0) {
149 150 151
      RepoStore.setActiveFiles(RepoStore.openedFiles[0]);
      return;
    }
152

Jacob Schatz committed
153 154
    if (foundIndex && foundIndex > 0) {
      RepoStore.setActiveFiles(RepoStore.openedFiles[foundIndex - 1]);
155
    }
156 157 158 159 160
  },

  addToOpenedFiles(file) {
    const openFile = file;

161
    const openedFilesAlreadyExists = RepoStore.openedFiles
Jacob Schatz committed
162
      .some(openedFile => openedFile.path === openFile.path);
163 164 165 166

    if (openedFilesAlreadyExists) return;

    openFile.changed = false;
167
    RepoStore.openedFiles.push(openFile);
168 169 170
  },

  setActiveFileContents(contents) {
171
    if (!RepoStore.editMode) return;
172
    const currentFile = RepoStore.openedFiles[RepoStore.activeFileIndex];
173 174
    RepoStore.activeFile.newContent = contents;
    RepoStore.activeFile.changed = RepoStore.activeFile.plain !== RepoStore.activeFile.newContent;
175 176
    currentFile.changed = RepoStore.activeFile.changed;
    currentFile.newContent = contents;
177 178
  },

179
  toggleBlobView() {
180 181 182 183 184
    RepoStore.currentBlobView = RepoStore.isPreviewView() ? 'repo-editor' : 'repo-preview';
  },

  setViewToPreview() {
    RepoStore.currentBlobView = 'repo-preview';
185 186
  },

187 188 189
  // getters

  isActiveFile(file) {
190
    return file && file.url === RepoStore.activeFile.url;
191
  },
192 193 194 195

  isPreviewView() {
    return RepoStore.currentBlobView === 'repo-preview';
  },
196
};
197

198
export default RepoStore;