summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/stores/repo_store.js
blob: 39e1b4e584978d31fba847e27afaf3a413d1fc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import Helper from '../helpers/repo_helper';
import Service from '../services/repo_service';

const RepoStore = {
  monacoLoading: false,
  service: '',
  canCommit: false,
  onTopOfBranch: false,
  editMode: false,
  isRoot: null,
  isInitialRoot: null,
  prevURL: '',
  projectId: '',
  projectName: '',
  projectUrl: '',
  branchUrl: '',
  blobRaw: '',
  currentBlobView: 'repo-preview',
  openedFiles: [],
  submitCommitsLoading: false,
  dialog: {
    open: false,
    title: '',
    status: false,
  },
  showNewBranchDialog: false,
  activeFile: Helper.getDefaultActiveFile(),
  activeFileIndex: 0,
  activeLine: -1,
  activeFileLabel: 'Raw',
  files: [],
  isCommitable: false,
  binary: false,
  currentBranch: '',
  startNewMR: false,
  currentHash: '',
  currentShortHash: '',
  customBranchURL: '',
  newMrTemplateUrl: '',
  branchChanged: false,
  commitMessage: '',
  loading: {
    tree: false,
    blob: false,
  },

  setBranchHash() {
    return Service.getBranch()
      .then((data) => {
        if (RepoStore.currentHash !== '' && data.commit.id !== RepoStore.currentHash) {
          RepoStore.branchChanged = true;
        }
        RepoStore.currentHash = data.commit.id;
        RepoStore.currentShortHash = data.commit.short_id;
      });
  },

  // mutations
  checkIsCommitable() {
    RepoStore.isCommitable = RepoStore.onTopOfBranch && RepoStore.canCommit;
  },

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

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

    RepoStore.setActiveToRaw();

    if (file.binary) {
      RepoStore.blobRaw = file.base64;
    } else if (file.newContent || file.plain) {
      RepoStore.blobRaw = file.newContent || file.plain;
    } else {
      Service.getRaw(file.raw_path)
        .then((rawResponse) => {
          RepoStore.blobRaw = rawResponse.data;
          Helper.findOpenedFileFromActive().plain = rawResponse.data;
        }).catch(Helper.loadingError);
    }

    if (!file.loading) Helper.updateHistoryEntry(file.url, file.pageTitle || file.name);
    RepoStore.binary = file.binary;
    RepoStore.setActiveLine(-1);
  },

  setFileActivity(file, openedFile, i) {
    const activeFile = openedFile;
    activeFile.active = file.url === activeFile.url;

    if (activeFile.active) RepoStore.setActiveFile(activeFile, i);

    return activeFile;
  },

  setActiveFile(activeFile, i) {
    RepoStore.activeFile = Object.assign({}, RepoStore.activeFile, activeFile);
    RepoStore.activeFileIndex = i;
  },

  setActiveLine(activeLine) {
    if (!isNaN(activeLine)) RepoStore.activeLine = activeLine;
  },

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

  removeFromOpenedFiles(file) {
    if (file.type === 'tree') return;
    let foundIndex;
    RepoStore.openedFiles = RepoStore.openedFiles.filter((openedFile, i) => {
      if (openedFile.path === file.path) foundIndex = i;
      return openedFile.path !== file.path;
    });

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

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

    if (foundIndex && foundIndex > 0) {
      RepoStore.setActiveFiles(RepoStore.openedFiles[foundIndex - 1]);
    }
  },

  addToOpenedFiles(file) {
    const openFile = file;

    const openedFilesAlreadyExists = RepoStore.openedFiles
      .some(openedFile => openedFile.path === openFile.path);

    if (openedFilesAlreadyExists) return;

    openFile.changed = false;
    openFile.active = true;
    RepoStore.openedFiles.push(openFile);
  },

  setActiveFileContents(contents) {
    if (!RepoStore.editMode) return;
    const currentFile = RepoStore.openedFiles[RepoStore.activeFileIndex];
    RepoStore.activeFile.newContent = contents;
    RepoStore.activeFile.changed = RepoStore.activeFile.plain !== RepoStore.activeFile.newContent;
    currentFile.changed = RepoStore.activeFile.changed;
    currentFile.newContent = contents;
  },

  toggleBlobView() {
    RepoStore.currentBlobView = RepoStore.isPreviewView() ? 'repo-editor' : 'repo-preview';
  },

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

  // getters

  isActiveFile(file) {
    return file && file.url === RepoStore.activeFile.url;
  },

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

export default RepoStore;