summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/preview/navigator.vue
blob: 4bf346946b610bb69b2accc6576b2853ef507495 (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
<script>
import { listen } from 'codesandbox-api';
import Icon from '~/vue_shared/components/icon.vue';
import LoadingIcon from '~/vue_shared/components/loading_icon.vue';

export default {
  components: {
    Icon,
    LoadingIcon,
  },
  props: {
    manager: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      currentBrowsingIndex: null,
      navigationStack: [],
      forwardNavigationStack: [],
      path: '',
      loading: true,
    };
  },
  computed: {
    backButtonDisabled() {
      return this.navigationStack.length <= 1;
    },
    forwardButtonDisabled() {
      return !this.forwardNavigationStack.length;
    },
  },
  mounted() {
    this.listener = listen(e => {
      switch (e.type) {
        case 'urlchange':
          this.onUrlChange(e);
          break;
        case 'done':
          this.loading = false;
          break;
        default:
          break;
      }
    });
  },
  beforeDestroy() {
    this.listener();
  },
  methods: {
    onUrlChange(e) {
      const lastPath = this.path;

      this.path = e.url.replace(this.manager.bundlerURL, '') || '/';

      if (lastPath !== this.path) {
        this.currentBrowsingIndex =
          this.currentBrowsingIndex === null ? 0 : this.currentBrowsingIndex + 1;
        this.navigationStack.push(this.path);
      }
    },
    back() {
      const lastPath = this.path;

      this.visitPath(this.navigationStack[this.currentBrowsingIndex - 1]);

      this.forwardNavigationStack.push(lastPath);

      if (this.currentBrowsingIndex === 1) {
        this.currentBrowsingIndex = null;
        this.navigationStack = [];
      }
    },
    forward() {
      this.visitPath(this.forwardNavigationStack.splice(0, 1)[0]);
    },
    refresh() {
      this.visitPath(this.path);
    },
    visitPath(path) {
      this.manager.iframe.src = `${this.manager.bundlerURL}${path}`;
    },
  },
};
</script>

<template>
  <header class="ide-preview-header d-flex align-items-center">
    <button
      :aria-label="s__('IDE|Back')"
      :disabled="backButtonDisabled"
      :class="{
        'disabled-content': backButtonDisabled
      }"
      type="button"
      class="ide-navigator-btn d-flex align-items-center d-transparent border-0 bg-transparent"
      @click="back"
    >
      <icon
        :size="24"
        name="chevron-left"
        class="m-auto"
      />
    </button>
    <button
      :aria-label="s__('IDE|Back')"
      :disabled="forwardButtonDisabled"
      :class="{
        'disabled-content': forwardButtonDisabled
      }"
      type="button"
      class="ide-navigator-btn d-flex align-items-center d-transparent border-0 bg-transparent"
      @click="forward"
    >
      <icon
        :size="24"
        name="chevron-right"
        class="m-auto"
      />
    </button>
    <button
      :aria-label="s__('IDE|Refresh preview')"
      type="button"
      class="ide-navigator-btn d-flex align-items-center d-transparent border-0 bg-transparent"
      @click="refresh"
    >
      <icon
        :size="18"
        name="retry"
        class="m-auto"
      />
    </button>
    <div class="position-relative w-100 prepend-left-4">
      <input
        :value="path || '/'"
        type="text"
        class="ide-navigator-location form-control bg-white"
        readonly
      />
      <loading-icon
        v-if="loading"
        class="position-absolute ide-preview-loading-icon"
      />
    </div>
  </header>
</template>