You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewNodeDialog.vue 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!--
  2. - @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  3. -
  4. - @author Ferdinand Thiessen <opensource@fthiessen.de>
  5. -
  6. - @license AGPL-3.0-or-later
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <NcDialog :name="name"
  24. :open="open"
  25. close-on-click-outside
  26. out-transition
  27. @update:open="onClose">
  28. <template #actions>
  29. <NcButton type="primary"
  30. :disabled="!isUniqueName"
  31. @click="onCreate">
  32. {{ t('files', 'Create') }}
  33. </NcButton>
  34. </template>
  35. <form @submit.prevent="onCreate">
  36. <NcTextField ref="input"
  37. :error="!isUniqueName"
  38. :helper-text="errorMessage"
  39. :label="label"
  40. :value.sync="localDefaultName" />
  41. </form>
  42. </NcDialog>
  43. </template>
  44. <script lang="ts">
  45. import type { PropType } from 'vue'
  46. import { defineComponent } from 'vue'
  47. import { translate as t } from '@nextcloud/l10n'
  48. import { getUniqueName } from '../utils/fileUtils'
  49. import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
  50. import NcDialog from '@nextcloud/vue/dist/Components/NcDialog.js'
  51. import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
  52. interface ICanFocus {
  53. focus: () => void
  54. }
  55. export default defineComponent({
  56. name: 'NewNodeDialog',
  57. components: {
  58. NcButton,
  59. NcDialog,
  60. NcTextField,
  61. },
  62. props: {
  63. /**
  64. * The name to be used by default
  65. */
  66. defaultName: {
  67. type: String,
  68. default: t('files', 'New folder'),
  69. },
  70. /**
  71. * Other files that are in the current directory
  72. */
  73. otherNames: {
  74. type: Array as PropType<string[]>,
  75. default: () => [],
  76. },
  77. /**
  78. * Open state of the dialog
  79. */
  80. open: {
  81. type: Boolean,
  82. default: true,
  83. },
  84. /**
  85. * Dialog name
  86. */
  87. name: {
  88. type: String,
  89. default: t('files', 'Create new folder'),
  90. },
  91. /**
  92. * Input label
  93. */
  94. label: {
  95. type: String,
  96. default: t('files', 'Folder name'),
  97. },
  98. },
  99. emits: {
  100. close: (name: string|null) => name === null || name,
  101. },
  102. data() {
  103. return {
  104. localDefaultName: this.defaultName || t('files', 'New folder'),
  105. }
  106. },
  107. computed: {
  108. errorMessage() {
  109. if (this.isUniqueName) {
  110. return ''
  111. } else {
  112. return t('files', 'A file or folder with that name already exists.')
  113. }
  114. },
  115. uniqueName() {
  116. return getUniqueName(this.localDefaultName, this.otherNames)
  117. },
  118. isUniqueName() {
  119. return this.localDefaultName === this.uniqueName
  120. },
  121. },
  122. watch: {
  123. defaultName() {
  124. this.localDefaultName = this.defaultName || t('files', 'New folder')
  125. },
  126. /**
  127. * Ensure the input is focussed even if the dialog is already mounted but not open
  128. */
  129. open() {
  130. this.$nextTick(() => this.focusInput())
  131. },
  132. },
  133. mounted() {
  134. // on mounted lets use the unique name
  135. this.localDefaultName = this.uniqueName
  136. this.$nextTick(() => this.focusInput())
  137. },
  138. methods: {
  139. t,
  140. /**
  141. * Focus the filename input field
  142. */
  143. focusInput() {
  144. if (this.open) {
  145. this.$nextTick(() => (this.$refs.input as unknown as ICanFocus)?.focus?.())
  146. }
  147. },
  148. onCreate() {
  149. this.$emit('close', this.localDefaultName)
  150. },
  151. onClose(state: boolean) {
  152. if (!state) {
  153. this.$emit('close', null)
  154. }
  155. },
  156. },
  157. })
  158. </script>