aboutsummaryrefslogtreecommitdiff
path: root/src/components/settings_modal/helpers/emoji_editing_popover.vue
blob: f0465dd5d9f7c6fddd4aa70acf10332a1a15ad26 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
  <Popover
    ref="emojiPopover"
    trigger="click"
    :placement="placement"
    bound-to-selector=".emoji-list"
    popover-class="emoji-tab-edit-popover popover-default"
    :bound-to="{ x: 'container' }"
    :offset="{ y: 5 }"
    :disabled="disabled"
    :class="{'emoji-unsaved': isEdited}"
  >
    <template #trigger>
      <slot name="trigger" />
    </template>
    <template #content>
      <h3>
        {{ title }}
      </h3>

      <StillImage
        v-if="emojiPreview"
        class="emoji"
        :src="emojiPreview"
      />
      <div
        v-else
        class="emoji"
      />

      <div
        v-if="newUpload"
        class="emoji-tab-popover-input"
      >
        <input
          type="file"
          accept="image/*"
          class="emoji-tab-popover-file input"
          @change="uploadFile = $event.target.files"
        >
      </div>
      <div>
        <div class="emoji-tab-popover-input">
          <label>
            {{ $t('admin_dash.emoji.shortcode') }}
            <input
              v-model="editedShortcode"
              class="emoji-data-input input"
              :placeholder="$t('admin_dash.emoji.new_shortcode')"
            >
          </label>
        </div>

        <div class="emoji-tab-popover-input">
          <label>
            {{ $t('admin_dash.emoji.filename') }}

            <input
              v-model="editedFile"
              class="emoji-data-input input"
              :placeholder="$t('admin_dash.emoji.new_filename')"
            >
          </label>
        </div>

        <button
          class="button button-default btn"
          type="button"
          :disabled="newUpload ? uploadFile.length == 0 : !isEdited"
          @click="newUpload ? uploadEmoji() : saveEditedEmoji()"
        >
          {{ $t('admin_dash.emoji.save') }}
        </button>

        <template v-if="!newUpload">
          <button
            class="button button-default btn emoji-tab-popover-button"
            type="button"
            @click="deleteModalVisible = true"
          >
            {{ $t('admin_dash.emoji.delete') }}
          </button>
          <button
            class="button button-default btn emoji-tab-popover-button"
            type="button"
            @click="revertEmoji"
          >
            {{ $t('admin_dash.emoji.revert') }}
          </button>
          <ConfirmModal
            v-if="deleteModalVisible"
            :title="$t('admin_dash.emoji.delete_title')"
            :cancel-text="$t('status.delete_confirm_cancel_button')"
            :confirm-text="$t('status.delete_confirm_accept_button')"
            @cancelled="deleteModalVisible = false"
            @accepted="deleteEmoji"
          >
            {{ $t('admin_dash.emoji.delete_confirm', [shortcode]) }}
          </ConfirmModal>
        </template>
      </div>
    </template>
  </Popover>
</template>

<script>
import Popover from 'components/popover/popover.vue'
import ConfirmModal from 'components/confirm_modal/confirm_modal.vue'
import StillImage from 'components/still-image/still-image.vue'

export default {
  components: { Popover, ConfirmModal, StillImage },
  inject: ['emojiAddr'],
  props: {
    placement: String,
    disabled: {
      type: Boolean,
      default: false
    },

    newUpload: Boolean,

    title: String,
    packName: String,
    shortcode: {
      type: String,
      // Only exists when this is not a new upload
      default: ''
    },
    file: {
      type: String,
      // Only exists when this is not a new upload
      default: ''
    }
  },
  emits: ['updatePackFiles', 'displayError'],
  data () {
    return {
      uploadFile: [],
      editedShortcode: this.shortcode,
      editedFile: this.file,
      deleteModalVisible: false
    }
  },
  computed: {
    emojiPreview () {
      if (this.newUpload && this.uploadFile.length > 0) {
        return URL.createObjectURL(this.uploadFile[0])
      } else if (!this.newUpload) {
        return this.emojiAddr(this.file)
      }

      return null
    },
    isEdited () {
      return !this.newUpload && (this.editedShortcode !== this.shortcode || this.editedFile !== this.file)
    }
  },
  methods: {
    saveEditedEmoji () {
      if (!this.isEdited) return

      this.$store.state.api.backendInteractor.updateEmojiFile(
        { packName: this.packName, shortcode: this.shortcode, newShortcode: this.editedShortcode, newFilename: this.editedFile, force: false }
      ).then(resp => {
        if (resp.error !== undefined) {
          this.$emit('displayError', resp.error)
          return Promise.reject(resp.error)
        }

        return resp.json()
      }).then(resp => this.$emit('updatePackFiles', resp))
    },
    uploadEmoji () {
      this.$store.state.api.backendInteractor.addNewEmojiFile({
        packName: this.packName,
        file: this.uploadFile[0],
        shortcode: this.editedShortcode,
        filename: this.editedFile
      }).then(resp => resp.json()).then(resp => {
        if (resp.error !== undefined) {
          this.$emit('displayError', resp.error)
          return
        }

        this.$emit('updatePackFiles', resp)
        this.$refs.emojiPopover.hidePopover()

        this.editedFile = ''
        this.editedShortcode = ''
        this.uploadFile = []
      })
    },
    revertEmoji () {
      this.editedFile = this.file
      this.editedShortcode = this.shortcode
    },
    deleteEmoji () {
      this.deleteModalVisible = false

      this.$store.state.api.backendInteractor.deleteEmojiFile(
        { packName: this.packName, shortcode: this.shortcode }
      ).then(resp => resp.json()).then(resp => {
        if (resp.error !== undefined) {
          this.$emit('displayError', resp.error)
          return
        }

        this.$emit('updatePackFiles', resp)
      })
    }
  }
}
</script>

<style lang="scss">
  .emoji-tab-edit-popover {
    padding-left: 0.5em;
    padding-right: 0.5em;
    padding-bottom: 0.5em;

    .emoji {
      width: 32px;
      height: 32px;
    }
  }
</style>