File size: 5,923 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<script lang="ts">
import { computed, defineComponent, ref } from 'vue'
import { SharedData } from '@vue-devtools/shared-utils'
import { useOrientation } from '@front/features/layout/orientation'

export default defineComponent({
  props: {
    items: {
      type: Array,
      required: true,
    },

    selectedItem: {
      type: Object,
      default: () => ({}),
    },

    optionIcon: {
      type: String,
      default: null,
    },
  },
  emits: ['select'],
  setup(props, { emit }) {
    /* Open/Close */

    const isShown = ref(false)
    const isShowApplied = ref(false)

    /**
     * Delayed open should only happen on mouseover.
     * Will be overriden when clicking
     */
    const showDelayEnabled = ref(true)

    let disabled = false
    let toggleCloseEnabled = true
    let toggleCloseTimer = null

    let pendingOperation = null
    let operationTimer = null

    function queueOperation(type, delay) {
      if (disabled) {
        return
      }
      pendingOperation = type
      clearTimeout(operationTimer)
      operationTimer = setTimeout(() => applyOperation(), delay)
    }

    function applyOperation() {
      if (pendingOperation) {
        pendingOperation()
      }
      pendingOperation = null
      clearTimeout(operationTimer)
    }

    function queueOpen(delay = true) {
      queueOperation(() => {
        isShown.value = true

        toggleCloseEnabled = false
        clearTimeout(toggleCloseTimer)
        toggleCloseTimer = setTimeout(() => {
          toggleCloseEnabled = true
        }, 500)
      }, delay ? 250 : 1)
    }

    function queueClose(delay = true) {
      toggleCloseEnabled = false
      clearTimeout(toggleCloseTimer)
      queueOperation(() => {
        isShown.value = false
      }, delay ? 300 : 1)
    }

    function toggle() {
      if (isShown.value) {
        if (toggleCloseEnabled) {
          queueClose(false)
        }
      }
      else {
        // We open also when it's already open and
        // when the button close is disabled
        // so we cancel the popper autoclose
        // (the popper doesn't contain the trigger button)
        queueOpen(false)
      }
    }

    /* Select */

    function select(item) {
      disabled = true
      emit('select', item)
      // Disable menu for a short time after selecting an item
      setTimeout(() => {
        disabled = false
      }, 500)
    }

    const selectedIndex = computed(() => props.items.indexOf(props.selectedItem))

    function selectNext() {
      const index = selectedIndex.value + 1
      if (index < props.items.length) {
        select(props.items[index])
      }
    }

    function selectPrevious() {
      const index = selectedIndex.value - 1
      if (index >= 0) {
        select(props.items[index])
      }
    }

    let wheelEnabled = true

    function onMouseWheel(e: WheelEvent) {
      if (!wheelEnabled) {
        return
      }

      if (e.deltaY > 0) {
        selectNext()
      }
      else {
        selectPrevious()
      }

      if (SharedData.menuStepScrolling) {
        wheelEnabled = false
        setTimeout(() => {
          wheelEnabled = true
        }, 300)
      }
    }

    /* Layout */

    const { orientation } = useOrientation()

    return {
      isShown,
      isShowApplied,
      showDelayEnabled,
      queueOpen,
      queueClose,
      toggle,
      select,
      orientation,
      onMouseWheel,
    }
  },
})
</script>

<template>
  <VueDropdown
    v-model="isShown"
    :placement="orientation === 'landscape' ? 'right-start' : 'bottom-start'"
    :triggers="[]"
    :offset="[0, 0]"
    :delay="0"
    :auto-hide="false"
    @apply-show="isShowApplied = true"
    @apply-hide="isShowApplied = false"
  >
    <template #trigger>
      <div
        @mouseenter="queueOpen()"
        @mouseleave="queueClose()"
        @wheel="onMouseWheel"
        @click.capture="toggle()"
      >
        <slot name="trigger">
          <VueButton
            class="flat"
            :icon-left="selectedItem.icon"
            :icon-right="orientation === 'landscape' ? 'arrow_drop_down' : null"
            :class="{
              'icon-button': orientation === 'portrait',
            }"
          >
            <template v-if="orientation === 'landscape'">
              {{ selectedItem.label }}
            </template>
          </VueButton>
        </slot>
      </div>
    </template>

    <div>
      <div
        class="flex flex-col"
        @mouseenter="queueOpen()"
        @mouseleave="queueClose()"
        @wheel="onMouseWheel"
      >
        <slot name="before" />

        <VueDropdownButton
          v-for="(item, index) of items"
          :key="index"
          :icon-left="item.icon || optionIcon"
          :class="{
            selected: selectedItem === item,
          }"
          @click="select(item)"
        >
          <slot :item="item">
            {{ item.label }}
          </slot>
        </VueDropdownButton>

        <div
          v-if="$shared.showMenuScrollTip"
          class="text-xs flex items-center space-x-2 text-gray-500 pl-4 pr-1 py-1 border-t border-gray-200 dark:border-gray-700 group"
        >
          <span>Scroll to switch</span>
          <VueIcon icon="mouse" />

          <span class="flex-1" />

          <VueButton
            class="flex-none icon-button flat invisible group-hover:visible"
            icon-left="close"
            @click="$shared.showMenuScrollTip = false"
          />
        </div>
      </div>
    </div>
  </VueDropdown>
</template>

<style lang="postcss" scoped>
.selected {
  @apply bg-green-100 text-green-700 !important;

  .vue-ui-dark-mode & {
    @apply bg-gray-700 text-gray-100 !important;
  }

  :deep(svg) {
    fill: currentColor !important;
  }
}

.vue-ui-dropdown-button {
  min-height: 32px;
  height: auto;
  padding-top: 6px;
  padding-bottom: 6px;

  :deep(.default-slot) {
    flex: 1;
  }
}
</style>