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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <!--
  2. - @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
  3. -
  4. - @author John Molakvoæ <skjnldsv@protonmail.com>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  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. <Modal v-if="opened"
  24. :clear-view-delay="-1"
  25. class="templates-picker"
  26. size="large"
  27. @close="close">
  28. <form class="templates-picker__form"
  29. :style="style"
  30. @submit.prevent.stop="onSubmit">
  31. <h2>{{ t('files', 'Pick a template for {name}', { name: nameWithoutExt }) }}</h2>
  32. <!-- Templates list -->
  33. <ul class="templates-picker__list">
  34. <TemplatePreview
  35. v-bind="emptyTemplate"
  36. :checked="checked === emptyTemplate.fileid"
  37. @check="onCheck" />
  38. <TemplatePreview
  39. v-for="template in provider.templates"
  40. :key="template.fileid"
  41. v-bind="template"
  42. :checked="checked === template.fileid"
  43. :ratio="provider.ratio"
  44. @check="onCheck" />
  45. </ul>
  46. <!-- Cancel and submit -->
  47. <div class="templates-picker__buttons">
  48. <button @click="close">
  49. {{ t('files', 'Cancel') }}
  50. </button>
  51. <input type="submit"
  52. class="primary"
  53. :value="t('files', 'Create')"
  54. :aria-label="t('files', 'Create a new file with the selected template')">
  55. </div>
  56. </form>
  57. <EmptyContent v-if="loading" class="templates-picker__loading" icon="icon-loading">
  58. {{ t('files', 'Creating file') }}
  59. </EmptyContent>
  60. </Modal>
  61. </template>
  62. <script>
  63. import { normalize } from 'path'
  64. import { showError } from '@nextcloud/dialogs'
  65. import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
  66. import Modal from '@nextcloud/vue/dist/Components/Modal'
  67. import { getCurrentDirectory } from '../utils/davUtils'
  68. import { createFromTemplate, getTemplates } from '../services/Templates'
  69. import TemplatePreview from '../components/TemplatePreview'
  70. const border = 2
  71. const margin = 8
  72. const width = margin * 20
  73. export default {
  74. name: 'TemplatePicker',
  75. components: {
  76. EmptyContent,
  77. Modal,
  78. TemplatePreview,
  79. },
  80. props: {
  81. logger: {
  82. type: Object,
  83. required: true,
  84. },
  85. },
  86. data() {
  87. return {
  88. // Check empty template by default
  89. checked: -1,
  90. loading: false,
  91. name: null,
  92. opened: false,
  93. provider: null,
  94. }
  95. },
  96. computed: {
  97. /**
  98. * Strip away extension from name
  99. * @returns {string}
  100. */
  101. nameWithoutExt() {
  102. return this.name.indexOf('.') > -1
  103. ? this.name.split('.').slice(0, -1).join('.')
  104. : this.name
  105. },
  106. emptyTemplate() {
  107. return {
  108. basename: t('files', 'Blank'),
  109. fileid: -1,
  110. filename: this.t('files', 'Blank'),
  111. hasPreview: false,
  112. mime: this.provider?.mimetypes[0] || this.provider?.mimetypes,
  113. }
  114. },
  115. selectedTemplate() {
  116. return this.provider.templates.find(template => template.fileid === this.checked)
  117. },
  118. /**
  119. * Style css vars bin,d
  120. * @returns {Object}
  121. */
  122. style() {
  123. return {
  124. '--margin': margin + 'px',
  125. '--width': width + 'px',
  126. '--border': border + 'px',
  127. '--fullwidth': width + 2 * margin + 2 * border + 'px',
  128. '--height': this.provider.ratio ? Math.round(width / this.provider.ratio) + 'px' : null,
  129. }
  130. },
  131. },
  132. methods: {
  133. /**
  134. * Open the picker
  135. * @param {string} name the file name to create
  136. * @param {object} provider the template provider picked
  137. */
  138. async open(name, provider) {
  139. this.checked = this.emptyTemplate.fileid
  140. this.name = name
  141. this.provider = provider
  142. const templates = await getTemplates()
  143. const fetchedProvider = templates.find((fetchedProvider) => fetchedProvider.app === provider.app && fetchedProvider.label === provider.label)
  144. if (fetchedProvider === null) {
  145. throw new Error('Failed to match provider in results')
  146. }
  147. this.provider = fetchedProvider
  148. // If there is no templates available, just create an empty file
  149. if (fetchedProvider.templates.length === 0) {
  150. this.onSubmit()
  151. return
  152. }
  153. // Else, open the picker
  154. this.opened = true
  155. },
  156. /**
  157. * Close the picker and reset variables
  158. */
  159. close() {
  160. this.checked = this.emptyTemplate.fileid
  161. this.loading = false
  162. this.name = null
  163. this.opened = false
  164. this.provider = null
  165. },
  166. /**
  167. * Manages the radio template picker change
  168. * @param {number} fileid the selected template file id
  169. */
  170. onCheck(fileid) {
  171. this.checked = fileid
  172. },
  173. async onSubmit() {
  174. this.loading = true
  175. const currentDirectory = getCurrentDirectory()
  176. const fileList = OCA?.Files?.App?.currentFileList
  177. // If the file doesn't have an extension, add the default one
  178. if (this.nameWithoutExt === this.name) {
  179. this.logger.debug('Fixed invalid filename', { name: this.name, extension: this.provider?.extension })
  180. this.name = this.name + this.provider?.extension
  181. }
  182. try {
  183. const fileInfo = await createFromTemplate(
  184. normalize(`${currentDirectory}/${this.name}`),
  185. this.selectedTemplate?.filename,
  186. this.selectedTemplate?.templateType,
  187. )
  188. this.logger.debug('Created new file', fileInfo)
  189. await fileList?.addAndFetchFileInfo(this.name)
  190. // Run default action
  191. const fileAction = OCA.Files.fileActions.getDefaultFileAction(fileInfo.mime, 'file', OC.PERMISSION_ALL)
  192. fileAction.action(fileInfo.basename, {
  193. $file: fileList?.findFileEl(this.name),
  194. dir: currentDirectory,
  195. fileList,
  196. fileActions: fileList?.fileActions,
  197. fileInfoModel: fileList?.getModelForFile(this.name),
  198. })
  199. this.close()
  200. } catch (error) {
  201. this.logger.error('Error while creating the new file from template')
  202. console.error(error)
  203. showError(this.t('files', 'Unable to create new file from template'))
  204. } finally {
  205. this.loading = false
  206. }
  207. },
  208. },
  209. }
  210. </script>
  211. <style lang="scss" scoped>
  212. .templates-picker {
  213. &__form {
  214. padding: calc(var(--margin) * 2);
  215. // Will be handled by the buttons
  216. padding-bottom: 0;
  217. h2 {
  218. text-align: center;
  219. font-weight: bold;
  220. margin: var(--margin) 0 calc(var(--margin) * 2);
  221. }
  222. }
  223. &__list {
  224. display: grid;
  225. grid-gap: calc(var(--margin) * 2);
  226. grid-auto-columns: 1fr;
  227. // We want maximum 5 columns. Putting 6 as we don't count the grid gap. So it will always be lower than 6
  228. max-width: calc(var(--fullwidth) * 6);
  229. grid-template-columns: repeat(auto-fit, var(--fullwidth));
  230. // Make sure all rows are the same height
  231. grid-auto-rows: 1fr;
  232. // Center the columns set
  233. justify-content: center;
  234. }
  235. &__buttons {
  236. display: flex;
  237. justify-content: space-between;
  238. padding: calc(var(--margin) * 2) var(--margin);
  239. position: sticky;
  240. bottom: 0;
  241. background-image: linear-gradient(0, var(--gradient-main-background));
  242. button, input[type='submit'] {
  243. height: 44px;
  244. }
  245. }
  246. // Make sure we're relative for the loading emptycontent on top
  247. ::v-deep .modal-container {
  248. position: relative;
  249. overflow-y: auto !important;
  250. }
  251. &__loading {
  252. position: absolute;
  253. top: 0;
  254. left: 0;
  255. justify-content: center;
  256. width: 100%;
  257. height: 100%;
  258. margin: 0;
  259. background-color: var(--color-main-background-translucent);
  260. }
  261. }
  262. </style>