ContactsMenu.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <!--
  2. - SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  3. - SPDX-License-Identifier: AGPL-3.0-or-later
  4. -->
  5. <template>
  6. <NcHeaderMenu id="contactsmenu"
  7. class="contactsmenu"
  8. :aria-label="t('core', 'Search contacts')"
  9. @open="handleOpen">
  10. <template #trigger>
  11. <Contacts class="contactsmenu__trigger-icon" :size="20" />
  12. </template>
  13. <div class="contactsmenu__menu">
  14. <div class="contactsmenu__menu__input-wrapper">
  15. <NcTextField id="contactsmenu__menu__search"
  16. ref="contactsMenuInput"
  17. :value.sync="searchTerm"
  18. trailing-button-icon="close"
  19. :label="t('core', 'Search contacts')"
  20. :trailing-button-label="t('core','Reset search')"
  21. :show-trailing-button="searchTerm !== ''"
  22. :placeholder="t('core', 'Search contacts …')"
  23. class="contactsmenu__menu__search"
  24. @input="onInputDebounced"
  25. @trailing-button-click="onReset" />
  26. </div>
  27. <NcEmptyContent v-if="error" :name="t('core', 'Could not load your contacts')">
  28. <template #icon>
  29. <Magnify />
  30. </template>
  31. </NcEmptyContent>
  32. <NcEmptyContent v-else-if="loadingText" :name="loadingText">
  33. <template #icon>
  34. <NcLoadingIcon />
  35. </template>
  36. </NcEmptyContent>
  37. <NcEmptyContent v-else-if="contacts.length === 0" :name="t('core', 'No contacts found')">
  38. <template #icon>
  39. <Magnify />
  40. </template>
  41. </NcEmptyContent>
  42. <div v-else class="contactsmenu__menu__content">
  43. <div id="contactsmenu-contacts">
  44. <ul>
  45. <Contact v-for="contact in contacts" :key="contact.id" :contact="contact" />
  46. </ul>
  47. </div>
  48. <div v-if="contactsAppEnabled" class="contactsmenu__menu__content__footer">
  49. <NcButton type="tertiary" :href="contactsAppURL">
  50. {{ t('core', 'Show all contacts') }}
  51. </NcButton>
  52. </div>
  53. <div v-else-if="canInstallApp" class="contactsmenu__menu__content__footer">
  54. <NcButton type="tertiary" :href="contactsAppMgmtURL">
  55. {{ t('core', 'Install the Contacts app') }}
  56. </NcButton>
  57. </div>
  58. </div>
  59. </div>
  60. </NcHeaderMenu>
  61. </template>
  62. <script>
  63. import axios from '@nextcloud/axios'
  64. import Contacts from 'vue-material-design-icons/Contacts.vue'
  65. import debounce from 'debounce'
  66. import { getCurrentUser } from '@nextcloud/auth'
  67. import { generateUrl } from '@nextcloud/router'
  68. import Magnify from 'vue-material-design-icons/Magnify.vue'
  69. import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
  70. import NcEmptyContent from '@nextcloud/vue/dist/Components/NcEmptyContent.js'
  71. import NcHeaderMenu from '@nextcloud/vue/dist/Components/NcHeaderMenu.js'
  72. import NcLoadingIcon from '@nextcloud/vue/dist/Components/NcLoadingIcon.js'
  73. import { translate as t } from '@nextcloud/l10n'
  74. import Contact from '../components/ContactsMenu/Contact.vue'
  75. import logger from '../logger.js'
  76. import Nextcloud from '../mixins/Nextcloud.js'
  77. import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
  78. export default {
  79. name: 'ContactsMenu',
  80. components: {
  81. Contact,
  82. Contacts,
  83. Magnify,
  84. NcButton,
  85. NcEmptyContent,
  86. NcHeaderMenu,
  87. NcLoadingIcon,
  88. NcTextField,
  89. },
  90. mixins: [Nextcloud],
  91. data() {
  92. const user = getCurrentUser()
  93. return {
  94. contactsAppEnabled: false,
  95. contactsAppURL: generateUrl('/apps/contacts'),
  96. contactsAppMgmtURL: generateUrl('/settings/apps/social/contacts'),
  97. canInstallApp: user.isAdmin,
  98. contacts: [],
  99. loadingText: undefined,
  100. error: false,
  101. searchTerm: '',
  102. }
  103. },
  104. methods: {
  105. async handleOpen() {
  106. await this.getContacts('')
  107. },
  108. async getContacts(searchTerm) {
  109. if (searchTerm === '') {
  110. this.loadingText = t('core', 'Loading your contacts …')
  111. } else {
  112. this.loadingText = t('core', 'Looking for {term} …', {
  113. term: searchTerm,
  114. })
  115. }
  116. // Let the user try a different query if the previous one failed
  117. this.error = false
  118. try {
  119. const { data: { contacts, contactsAppEnabled } } = await axios.post(generateUrl('/contactsmenu/contacts'), {
  120. filter: searchTerm,
  121. })
  122. this.contacts = contacts
  123. this.contactsAppEnabled = contactsAppEnabled
  124. this.loadingText = undefined
  125. } catch (error) {
  126. logger.error('could not load contacts', {
  127. error,
  128. searchTerm,
  129. })
  130. this.error = true
  131. }
  132. },
  133. onInputDebounced: debounce(function() {
  134. this.getContacts(this.searchTerm)
  135. }, 500),
  136. /**
  137. * Reset the search state
  138. */
  139. onReset() {
  140. this.searchTerm = ''
  141. this.contacts = []
  142. this.focusInput()
  143. },
  144. /**
  145. * Focus the search input on next tick
  146. */
  147. focusInput() {
  148. this.$nextTick(() => {
  149. this.$refs.contactsMenuInput.focus()
  150. this.$refs.contactsMenuInput.select()
  151. })
  152. },
  153. },
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .contactsmenu {
  158. overflow-y: hidden;
  159. &__trigger-icon {
  160. color: var(--color-background-plain-text) !important;
  161. }
  162. &__menu {
  163. display: flex;
  164. flex-direction: column;
  165. overflow: hidden;
  166. height: calc(50px * 6 + 2px + 26px);
  167. max-height: inherit;
  168. label[for="contactsmenu__menu__search"] {
  169. font-weight: bold;
  170. font-size: 19px;
  171. margin-inline-start: 13px;
  172. }
  173. &__input-wrapper {
  174. padding: 10px;
  175. z-index: 2;
  176. top: 0;
  177. }
  178. &__search {
  179. width: 100%;
  180. height: 34px;
  181. margin-top: 0!important;
  182. }
  183. &__content {
  184. overflow-y: auto;
  185. margin-top: 10px;
  186. flex: 1 1 auto;
  187. &__footer {
  188. display: flex;
  189. flex-direction: column;
  190. align-items: center;
  191. }
  192. }
  193. a {
  194. &:focus-visible {
  195. box-shadow: inset 0 0 0 2px var(--color-main-text) !important; // override rule in core/css/headers.scss #header a:focus-visible
  196. }
  197. }
  198. }
  199. :deep(.empty-content) {
  200. margin: 0 !important;
  201. }
  202. }
  203. </style>