summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/extensions/base.vue
blob: 0ac98f6c9822a7b2017774793834da5a7ecda20d (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
<script>
import { GlButton, GlLoadingIcon, GlIcon, GlLink, GlBadge, GlSafeHtmlDirective } from '@gitlab/ui';
import SmartVirtualList from '~/vue_shared/components/smart_virtual_list.vue';
import StatusIcon from '../mr_widget_status_icon.vue';

export const LOADING_STATES = {
  collapsedLoading: 'collapsedLoading',
  collapsedError: 'collapsedError',
  expandedLoading: 'expandedLoading',
};

export default {
  components: {
    GlButton,
    GlLoadingIcon,
    GlIcon,
    GlLink,
    GlBadge,
    SmartVirtualList,
    StatusIcon,
  },
  directives: {
    SafeHtml: GlSafeHtmlDirective,
  },
  data() {
    return {
      loadingState: LOADING_STATES.collapsedLoading,
      collapsedData: null,
      fullData: null,
      isCollapsed: true,
    };
  },
  computed: {
    isLoadingSummary() {
      return this.loadingState === LOADING_STATES.collapsedLoading;
    },
    isLoadingExpanded() {
      return this.loadingState === LOADING_STATES.expandedLoading;
    },
    isCollapsible() {
      if (this.isLoadingSummary) {
        return false;
      }

      return true;
    },
    statusIconName() {
      if (this.isLoadingSummary) {
        return 'loading';
      }

      if (this.loadingState === LOADING_STATES.collapsedError) {
        return 'warning';
      }

      return this.statusIcon(this.collapsedData);
    },
  },
  watch: {
    isCollapsed(newVal) {
      if (!newVal) {
        this.loadAllData();
      } else {
        this.loadingState = null;
      }
    },
  },
  mounted() {
    this.fetchCollapsedData(this.$props)
      .then((data) => {
        this.collapsedData = data;
        this.loadingState = null;
      })
      .catch((e) => {
        this.loadingState = LOADING_STATES.collapsedError;
        throw e;
      });
  },
  methods: {
    toggleCollapsed() {
      this.isCollapsed = !this.isCollapsed;
    },
    loadAllData() {
      if (this.fullData) return;

      this.loadingState = LOADING_STATES.expandedLoading;

      this.fetchFullData(this.$props)
        .then((data) => {
          this.loadingState = null;
          this.fullData = data;
        })
        .catch((e) => {
          this.loadingState = null;
          throw e;
        });
    },
  },
};
</script>

<template>
  <section class="media-section mr-widget-border-top">
    <div class="media gl-p-5">
      <status-icon :status="statusIconName" class="align-self-center" />
      <div class="media-body d-flex flex-align-self-center align-items-center">
        <div class="code-text">
          <template v-if="isLoadingSummary">
            {{ __('Loading...') }}
          </template>
          <div v-else v-safe-html="summary(collapsedData)"></div>
        </div>
        <gl-button
          v-if="isCollapsible"
          size="small"
          class="float-right align-self-center"
          @click="toggleCollapsed"
        >
          {{ isCollapsed ? __('Expand') : __('Collapse') }}
        </gl-button>
      </div>
    </div>
    <div v-if="!isCollapsed" class="mr-widget-grouped-section">
      <div v-if="isLoadingExpanded" class="report-block-container">
        <gl-loading-icon size="sm" inline /> {{ __('Loading...') }}
      </div>
      <smart-virtual-list
        v-else-if="fullData"
        :length="fullData.length"
        :remain="20"
        :size="32"
        wtag="ul"
        wclass="report-block-list"
        class="report-block-container"
      >
        <li v-for="data in fullData" :key="data.id" class="d-flex align-items-center">
          <div v-if="data.icon" :class="data.icon.class" class="d-flex">
            <gl-icon :name="data.icon.name" :size="24" />
          </div>
          <div
            class="gl-mt-2 gl-mb-2 align-content-around align-items-start flex-wrap align-self-center d-flex"
          >
            <div class="gl-mr-4">
              {{ data.text }}
            </div>
            <div v-if="data.link">
              <gl-link :href="data.link.href">{{ data.link.text }}</gl-link>
            </div>
            <gl-badge v-if="data.badge" :variant="data.badge.variant || 'info'">
              {{ data.badge.text }}
            </gl-badge>
          </div>
        </li>
      </smart-virtual-list>
    </div>
  </section>
</template>