FormBuilderFile.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div class="form-builder-file"
  3. :class="customClass">
  4. <div class="outsideLabel">{{ placeholder ? label : null }}</div>
  5. <q-file :id="inputUid"
  6. ref="input"
  7. v-model="file"
  8. :name="name"
  9. :label="placeholder ? null : label"
  10. :stack-label="!!placeholder"
  11. :placeholder="placeholder"
  12. :disable="disable"
  13. :filled="filled"
  14. :capture="capture"
  15. :error="error"
  16. :error-message="errorMessage"
  17. :accept="accept"
  18. :readonly="readonly"
  19. :loading="loading"
  20. clearable
  21. :model-value="file"
  22. :rules="rules"
  23. :lazy-rules="lazyRules"
  24. :class="customClass"
  25. :input-class="customClass"
  26. @update:model-value="change($event)"
  27. @clear="onClearInputFile"
  28. @click="onClick" />
  29. <label :for="inputUid"
  30. class="cursor-pointer"
  31. @click="downloadFile()">
  32. <q-img v-if="previewMode === 'photo'"
  33. :src="imageSource"
  34. placeholder-src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWBAMAAADOL2zRAAAAG1BMVEXMzMyWlpaqqqq3t7fFxcW+vr6xsbGjo6OcnJyLKnDGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABAElEQVRoge3SMW+DMBiE4YsxJqMJtHOTITPeOsLQnaodGImEUMZEkZhRUqn92f0MaTubtfeMh/QGHANEREREREREREREtIJJ0xbH299kp8l8FaGtLdTQ19HjofxZlJ0m1+eBKZcikd9PWtXC5DoDotRO04B9YOvFIXmXLy2jEbiqE6Df7DTleA5socLqvEFVxtJyrpZFWz/pHM2CVte0lS8g2eDe6prOyqPglhzROL+Xye4tmT4WvRcQ2/m81p+/rdguOi8Hc5L/8Qk4vhZzy08DduGt9eVQyP2qoTM1zi0/uf4hvBWf5c77e69Gf798y08L7j0RERERERERERH9P99ZpSVRivB/rgAAAABJRU5ErkJggg=="
  35. class="cursor-pointer"
  36. :class="customClass" />
  37. <q-icon v-if="previewMode === 'audio'"
  38. size="2rem"
  39. name="audiotrack"
  40. class="cursor-pointer"
  41. :class="customClass" />
  42. <q-icon v-if="previewMode === 'pdf'"
  43. size="100px"
  44. name="picture_as_pdf"
  45. class="cursor-pointer"
  46. :class="customClass" />
  47. <q-icon v-if="previewMode === 'office/word'"
  48. size="100px"
  49. name="description"
  50. class="cursor-pointer"
  51. :class="customClass" />
  52. <q-icon v-if="previewMode === 'office/excel'"
  53. size="100px"
  54. name="table_view"
  55. class="cursor-pointer"
  56. :class="customClass" />
  57. </label>
  58. <div v-if="caption"
  59. class="caption text-grey-8">{{ caption }}</div>
  60. </div>
  61. </template>
  62. <script>
  63. import { uid } from 'quasar'
  64. import { inputMixin } from 'quasar-form-builder'
  65. export default {
  66. name: 'FormBuilderFile',
  67. mixins: [inputMixin],
  68. props: {
  69. name: {
  70. default: '',
  71. type: String
  72. },
  73. value: {
  74. default: '',
  75. type: [String, Object]
  76. },
  77. caption: {
  78. default: null,
  79. type: String
  80. },
  81. capture: {
  82. default: undefined,
  83. type: String
  84. },
  85. sendNull: {
  86. default: false,
  87. type: Boolean
  88. },
  89. accept: {
  90. default: undefined,
  91. type: String
  92. }
  93. },
  94. data() {
  95. return {
  96. file: null,
  97. inputUid: null,
  98. url: null
  99. }
  100. },
  101. computed: {
  102. imageSource() {
  103. if (this.isValidURL(this.inputData)) {
  104. return this.inputData
  105. } else if (this.inputData instanceof File) {
  106. return this.createObjectURL()
  107. } else if (this.inputData === null) {
  108. return this.url
  109. } else {
  110. return null
  111. }
  112. },
  113. previewMode() {
  114. let extension = ''
  115. if (this.inputData === null) {
  116. extension = this.getSourceExtension(this.url)
  117. } else {
  118. extension = this.getSourceExtension(this.inputData)
  119. }
  120. return extension
  121. }
  122. },
  123. watch: {
  124. value(newValue) {
  125. this.inputData = newValue
  126. if (this.isFile(newValue)) {
  127. this.file = newValue
  128. }
  129. if (newValue === null && this.sendNull) {
  130. this.url = null
  131. this.file = newValue
  132. }
  133. },
  134. inputData(newValue) {
  135. if (this.isValidURL(newValue) && this.getSourceExtension(newValue) === 'photo') {
  136. this.url = newValue
  137. }
  138. },
  139. file(newValue) {
  140. if (newValue === null) {
  141. this.inputData = this.url
  142. } else {
  143. this.inputData = newValue
  144. }
  145. }
  146. },
  147. created() {
  148. this.inputUid = this.getUid()
  149. },
  150. methods: {
  151. downloadFile () {
  152. window.open(this.value, '_blank')
  153. },
  154. onClearInputFile() {
  155. // this.inputData = this.url
  156. },
  157. getSourceExtension(filePath) {
  158. if (this.isValidURL(filePath)) {
  159. return this.getFileExtension(filePath)
  160. } else if (this.isFile(filePath)) {
  161. return this.getFileExtension(filePath.name)
  162. } else {
  163. return false
  164. }
  165. },
  166. getFileExtension(filePath) {
  167. if (!filePath || typeof filePath !== 'string') {
  168. return false
  169. }
  170. const photoExtensions = /\.(jpeg|jpg|gif|png)$/,
  171. audioExtensions = /\.(mp3|ogg)$/,
  172. officeWordExtensions = /\.(doc|docx)$/,
  173. officeExcelExtensions = /\.(xls|xlsx)$/,
  174. pdfExtensions = /\.(pdf|PDF)$/
  175. if (filePath.match(photoExtensions) != null) {
  176. return 'photo'
  177. } else if (filePath.match(audioExtensions) != null) {
  178. return 'audio'
  179. } else if (filePath.match(pdfExtensions) != null) {
  180. return 'pdf'
  181. } else if (filePath.match(officeWordExtensions) != null) {
  182. return 'office/word'
  183. } else if (filePath.match(officeExcelExtensions) != null) {
  184. return 'office/excel'
  185. } else {
  186. return false
  187. }
  188. },
  189. isFile(file) {
  190. return typeof File !== 'undefined' && file instanceof File && typeof file.type === 'string'
  191. },
  192. isValidURL(str) {
  193. if (!str || typeof str !== 'string' || this.isFile(str)) {
  194. return false
  195. }
  196. const a = document.createElement('a')
  197. a.href = str
  198. return a.host !== ''
  199. },
  200. createObjectURL() {
  201. return window.URL.createObjectURL(this.inputData)
  202. },
  203. getUid() {
  204. return uid()
  205. }
  206. }
  207. }
  208. </script>
  209. <style scoped></style>