summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/components/repo_sidebar.vue
blob: 09dc9ee25d734e755ae4b6be062e9edaa048a506 (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
<script>
import _ from 'underscore';
import Service from '../services/repo_service';
import Helper from '../helpers/repo_helper';
import Store from '../stores/repo_store';
import eventHub from '../event_hub';
import RepoPreviousDirectory from './repo_prev_directory.vue';
import RepoFile from './repo_file.vue';
import RepoLoadingFile from './repo_loading_file.vue';
import RepoMixin from '../mixins/repo_mixin';

export default {
  mixins: [RepoMixin],
  components: {
    'repo-previous-directory': RepoPreviousDirectory,
    'repo-file': RepoFile,
    'repo-loading-file': RepoLoadingFile,
  },
  created() {
    window.addEventListener('popstate', this.checkHistory);
  },
  destroyed() {
    eventHub.$off('fileNameClicked', this.fileClicked);
    eventHub.$off('goToPreviousDirectoryClicked', this.goToPreviousDirectoryClicked);
    window.removeEventListener('popstate', this.checkHistory);
  },
  mounted() {
    eventHub.$on('fileNameClicked', this.fileClicked);
    eventHub.$on('goToPreviousDirectoryClicked', this.goToPreviousDirectoryClicked);
  },
  data() {
    return Store;
  },
  computed: {
    flattendFiles() {
      const mapFiles = arr => (!arr.files.length ? [] : _.map(arr.files, a => [a, mapFiles(a)]));

      return _.chain(this.files)
        .map(arr => [arr, mapFiles(arr)])
        .flatten()
        .value();
    },
  },
  methods: {
    checkHistory() {
      let selectedFile = this.files.find(file => location.pathname.indexOf(file.url) > -1);
      if (!selectedFile) {
        // Maybe it is not in the current tree but in the opened tabs
        selectedFile = Helper.getFileFromPath(location.pathname);
      }

      let lineNumber = null;
      if (location.hash.indexOf('#L') > -1) lineNumber = Number(location.hash.substr(2));

      if (selectedFile) {
        if (selectedFile.url !== this.activeFile.url) {
          this.fileClicked(selectedFile, lineNumber);
        } else {
          Store.setActiveLine(lineNumber);
        }
      } else {
        // Not opened at all lets open new tab
        this.fileClicked({
          url: location.href,
        }, lineNumber);
      }
    },

    fileClicked(clickedFile, lineNumber) {
      const file = clickedFile;

      if (file.loading) return;

      if (file.type === 'tree' && file.opened) {
        Helper.setDirectoryToClosed(file);
        Store.setActiveLine(lineNumber);
      } else if (file.type === 'submodule') {
        file.loading = true;

        gl.utils.visitUrl(file.url);
      } else {
        const openFile = Helper.getFileFromPath(file.url);

        if (openFile) {
          Store.setActiveFiles(openFile);
          Store.setActiveLine(lineNumber);
        } else {
          file.loading = true;
          Service.url = file.url;
          Helper.getContent(file)
            .then(() => {
              file.loading = false;
              Helper.scrollTabsRight();
              Store.setActiveLine(lineNumber);
            })
            .catch(Helper.loadingError);
        }
      }
    },

    goToPreviousDirectoryClicked(prevURL) {
      Service.url = prevURL;
      Helper.getContent(null, true)
        .then(() => Helper.scrollTabsRight())
        .catch(Helper.loadingError);
    },
  },
};
</script>

<template>
<div id="sidebar" :class="{'sidebar-mini' : isMini}">
  <table class="table">
    <thead>
      <tr>
        <th
          v-if="isMini"
          class="repo-file-options title"
        >
          <strong class="clgray">
            {{ projectName }}
          </strong>
        </th>
        <template v-else>
          <th class="name">
            Name
          </th>
          <th class="hidden-sm hidden-xs last-commit">
            Last commit
          </th>
          <th class="hidden-xs last-update text-right">
            Last update
          </th>
        </template>
      </tr>
    </thead>
    <tbody>
      <repo-previous-directory
        v-if="!isRoot && !loading.tree"
        :prev-url="prevURL"
      />
      <repo-loading-file
        v-if="!flattendFiles.length && loading.tree"
        v-for="n in 5"
        :key="n"
      />
      <repo-file
        v-for="file in flattendFiles"
        :key="file.id"
        :file="file"
      />
    </tbody>
  </table>
</div>
</template>