summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/static_site_editor/components/edit_meta_controls.vue
blob: 9f75c65a316e1ae5b769a04cae76480a39fa8cf3 (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
<script>
import { GlForm, GlFormGroup, GlFormInput, GlFormTextarea } from '@gitlab/ui';
import AccessorUtilities from '~/lib/utils/accessor';

export default {
  components: {
    GlForm,
    GlFormGroup,
    GlFormInput,
    GlFormTextarea,
  },
  props: {
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      editable: {
        title: this.title,
        description: this.description,
      },
    };
  },
  computed: {
    editableStorageKey() {
      return this.getId('local-storage', 'editable');
    },
    hasLocalStorage() {
      return AccessorUtilities.isLocalStorageAccessSafe();
    },
  },
  mounted() {
    this.initCachedEditable();
    this.preSelect();
  },
  methods: {
    getId(type, key) {
      return `sse-merge-request-meta-${type}-${key}`;
    },
    initCachedEditable() {
      if (this.hasLocalStorage) {
        const cachedEditable = JSON.parse(localStorage.getItem(this.editableStorageKey));
        if (cachedEditable) {
          this.editable = cachedEditable;
        }
      }
    },
    preSelect() {
      this.$nextTick(() => {
        this.$refs.title.$el.select();
      });
    },
    resetCachedEditable() {
      if (this.hasLocalStorage) {
        window.localStorage.removeItem(this.editableStorageKey);
      }
    },
    onUpdate() {
      const payload = { ...this.editable };
      this.$emit('updateSettings', payload);

      if (this.hasLocalStorage) {
        window.localStorage.setItem(this.editableStorageKey, JSON.stringify(payload));
      }
    },
  },
};
</script>

<template>
  <gl-form>
    <gl-form-group
      key="title"
      :label="__('Brief title about the change')"
      :label-for="getId('control', 'title')"
    >
      <gl-form-input
        :id="getId('control', 'title')"
        ref="title"
        v-model.lazy="editable.title"
        type="text"
        @input="onUpdate"
      />
    </gl-form-group>

    <gl-form-group
      key="description"
      :label="__('Goal of the changes and what reviewers should be aware of')"
      :label-for="getId('control', 'description')"
    >
      <gl-form-textarea
        :id="getId('control', 'description')"
        v-model.lazy="editable.description"
        @input="onUpdate"
      />
    </gl-form-group>
  </gl-form>
</template>