app.es6 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import Uppy, { debugLogger } from '@uppy/core'
  2. import Dashboard from '@uppy/dashboard'
  3. import GoogleDrive from '@uppy/google-drive'
  4. import Dropbox from '@uppy/dropbox'
  5. import Instagram from '@uppy/instagram'
  6. import Facebook from '@uppy/facebook'
  7. import OneDrive from '@uppy/onedrive'
  8. import Zoom from '@uppy/zoom'
  9. import Unsplash from '@uppy/unsplash'
  10. import Box from '@uppy/box'
  11. import ImageEditor from '@uppy/image-editor'
  12. import Url from '@uppy/url'
  13. import Webcam from '@uppy/webcam'
  14. import Audio from '@uppy/audio'
  15. import ScreenCapture from '@uppy/screen-capture'
  16. import Tus from '@uppy/tus'
  17. import DropTarget from '@uppy/drop-target'
  18. import GoldenRetriever from '@uppy/golden-retriever'
  19. import Compressor from '@uppy/compressor'
  20. import localeList from '../locale_list.json'
  21. import COMPANION from '../env.js'
  22. const RTL_LOCALES = ['ar_SA', 'fa_IR', 'he_IL']
  23. if (typeof window !== 'undefined' && typeof window.Uppy === 'undefined') {
  24. window.Uppy = {
  25. locales: {},
  26. }
  27. }
  28. function uppyInit () {
  29. if (window.uppy) {
  30. window.uppy.close()
  31. }
  32. const opts = window.uppyOptions
  33. const uppy = new Uppy({
  34. logger: debugLogger,
  35. })
  36. uppy.use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/' })
  37. uppy.on('complete', result => {
  38. console.log('successful files:')
  39. console.log(result.successful)
  40. console.log('failed files:')
  41. console.log(result.failed)
  42. })
  43. uppy.use(Dashboard, {
  44. trigger: '.UppyModalOpenerBtn',
  45. target: opts.DashboardInline ? '.DashboardContainer' : 'body',
  46. inline: opts.DashboardInline,
  47. height: 470,
  48. showProgressDetails: true,
  49. metaFields: [
  50. { id: 'name', name: 'Name', placeholder: 'file name' },
  51. { id: 'caption', name: 'Caption', placeholder: 'add description' },
  52. ],
  53. })
  54. window.uppy = uppy
  55. }
  56. function uppySetOptions () {
  57. const opts = window.uppyOptions
  58. const defaultNullRestrictions = {
  59. maxFileSize: null,
  60. minFileSize: null,
  61. maxNumberOfFiles: null,
  62. minNumberOfFiles: null,
  63. allowedFileTypes: null,
  64. }
  65. const restrictions = {
  66. maxFileSize: 1000000,
  67. maxNumberOfFiles: 3,
  68. minNumberOfFiles: 2,
  69. allowedFileTypes: ['image/*', 'video/*'],
  70. requiredMetaFields: ['caption'],
  71. }
  72. window.uppy.setOptions({
  73. autoProceed: opts.autoProceed,
  74. restrictions: opts.restrictions ? restrictions : defaultNullRestrictions,
  75. })
  76. window.uppy.getPlugin('Dashboard').setOptions({
  77. note: opts.restrictions ? 'Images and video only, 2–3 files, up to 1 MB' : '',
  78. theme: opts.darkMode ? 'dark' : 'light',
  79. disabled: opts.disabled,
  80. })
  81. const googleDriveInstance = window.uppy.getPlugin('GoogleDrive')
  82. if (opts.GoogleDrive && !googleDriveInstance) {
  83. window.uppy.use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION })
  84. }
  85. if (!opts.GoogleDrive && googleDriveInstance) {
  86. window.uppy.removePlugin(googleDriveInstance)
  87. }
  88. const dropboxInstance = window.uppy.getPlugin('Dropbox')
  89. if (opts.Dropbox && !dropboxInstance) {
  90. window.uppy.use(Dropbox, { target: Dashboard, companionUrl: COMPANION })
  91. }
  92. if (!opts.Dropbox && dropboxInstance) {
  93. window.uppy.removePlugin(dropboxInstance)
  94. }
  95. const instagramInstance = window.uppy.getPlugin('Instagram')
  96. if (opts.Instagram && !instagramInstance) {
  97. window.uppy.use(Instagram, { target: Dashboard, companionUrl: COMPANION })
  98. }
  99. if (!opts.Instagram && instagramInstance) {
  100. window.uppy.removePlugin(instagramInstance)
  101. }
  102. const urlInstance = window.uppy.getPlugin('Url')
  103. if (opts.Url && !urlInstance) {
  104. window.uppy.use(Url, { target: Dashboard, companionUrl: COMPANION })
  105. }
  106. if (!opts.Url && urlInstance) {
  107. window.uppy.removePlugin(urlInstance)
  108. }
  109. const facebookInstance = window.uppy.getPlugin('Facebook')
  110. if (opts.Facebook && !facebookInstance) {
  111. window.uppy.use(Facebook, { target: Dashboard, companionUrl: COMPANION })
  112. }
  113. if (!opts.Facebook && facebookInstance) {
  114. window.uppy.removePlugin(facebookInstance)
  115. }
  116. const oneDriveInstance = window.uppy.getPlugin('OneDrive')
  117. if (opts.OneDrive && !oneDriveInstance) {
  118. window.uppy.use(OneDrive, { target: Dashboard, companionUrl: COMPANION })
  119. }
  120. if (!opts.OneDrive && oneDriveInstance) {
  121. window.uppy.removePlugin(oneDriveInstance)
  122. }
  123. const unsplashInstance = window.uppy.getPlugin('Unsplash')
  124. if (opts.Unsplash && !unsplashInstance) {
  125. window.uppy.use(Unsplash, { target: Dashboard, companionUrl: COMPANION })
  126. }
  127. if (!opts.Unsplash && unsplashInstance) {
  128. window.uppy.removePlugin(unsplashInstance)
  129. }
  130. const zoomInstance = window.uppy.getPlugin('Zoom')
  131. if (opts.Zoom && !zoomInstance) {
  132. window.uppy.use(Zoom, { target: Dashboard, companionUrl: 'https://intense-meadow-61813.herokuapp.com/' })
  133. }
  134. if (!opts.Zoom && zoomInstance) {
  135. window.uppy.removePlugin(zoomInstance)
  136. }
  137. const boxInstance = window.uppy.getPlugin('Box')
  138. if (opts.Box && !boxInstance) {
  139. window.uppy.use(Box, { target: Dashboard, companionUrl: COMPANION })
  140. }
  141. if (!opts.Box && boxInstance) {
  142. window.uppy.removePlugin(boxInstance)
  143. }
  144. const webcamInstance = window.uppy.getPlugin('Webcam')
  145. if (opts.Webcam && !webcamInstance) {
  146. window.uppy.use(Webcam, {
  147. target: Dashboard,
  148. showVideoSourceDropdown: true,
  149. })
  150. }
  151. if (!opts.Webcam && webcamInstance) {
  152. window.uppy.removePlugin(webcamInstance)
  153. }
  154. const audioInstance = window.uppy.getPlugin('Audio')
  155. if (opts.Audio && !audioInstance) {
  156. window.uppy.use(Audio, {
  157. target: Dashboard,
  158. showAudioSourceDropdown: true,
  159. })
  160. }
  161. if (!opts.Audio && audioInstance) {
  162. window.uppy.removePlugin(audioInstance)
  163. }
  164. const screenCaptureInstance = window.uppy.getPlugin('ScreenCapture')
  165. if (opts.ScreenCapture && !screenCaptureInstance) {
  166. window.uppy.use(ScreenCapture, { target: Dashboard })
  167. }
  168. if (!opts.ScreenCapture && screenCaptureInstance) {
  169. window.uppy.removePlugin(screenCaptureInstance)
  170. }
  171. const imageEditorInstance = window.uppy.getPlugin('ImageEditor')
  172. if (opts.imageEditor && !imageEditorInstance) {
  173. window.uppy.use(ImageEditor, { target: Dashboard })
  174. }
  175. if (!opts.imageEditor && imageEditorInstance) {
  176. window.uppy.removePlugin(imageEditorInstance)
  177. }
  178. const dropTargetInstance = window.uppy.getPlugin('DropTarget')
  179. if (opts.DropTarget && !dropTargetInstance) {
  180. window.uppy.use(DropTarget, { target: document.body })
  181. }
  182. if (!opts.DropTarget && dropTargetInstance) {
  183. window.uppy.removePlugin(dropTargetInstance)
  184. }
  185. const goldenRetrieverInstance = window.uppy.getPlugin('GoldenRetriever')
  186. if (opts.GoldenRetriever && !goldenRetrieverInstance) {
  187. window.uppy.use(GoldenRetriever)
  188. }
  189. if (!opts.GoldenRetriever && goldenRetrieverInstance) {
  190. window.uppy.removePlugin(goldenRetrieverInstance)
  191. }
  192. const compressorInstance = window.uppy.getPlugin('Compressor')
  193. if (opts.Compressor && !compressorInstance) {
  194. window.uppy.use(Compressor)
  195. }
  196. if (!opts.Compressor && compressorInstance) {
  197. window.uppy.removePlugin(compressorInstance)
  198. }
  199. }
  200. function whenLocaleAvailable (localeName, callback) {
  201. const interval = 100 // ms
  202. const loop = setInterval(() => {
  203. if (window.Uppy && window.Uppy.locales && window.Uppy.locales[localeName]) {
  204. clearInterval(loop)
  205. callback(window.Uppy.locales[localeName])
  206. }
  207. }, interval)
  208. }
  209. function loadLocaleFromCDN (localeName) {
  210. const head = document.getElementsByTagName('head')[0]
  211. const js = document.createElement('script')
  212. js.type = 'text/javascript'
  213. js.src = `https://releases.transloadit.com/uppy/locales/v3.0.0-beta.4/${localeName}.min.js`
  214. head.appendChild(js)
  215. }
  216. function setLocale (localeName) {
  217. if (typeof window.Uppy.locales[localeName] === 'undefined') {
  218. loadLocaleFromCDN(localeName)
  219. }
  220. whenLocaleAvailable(localeName, (localeObj) => {
  221. const direction = RTL_LOCALES.indexOf(localeName) !== -1
  222. ? 'rtl'
  223. : 'ltr'
  224. window.uppy.setOptions({
  225. locale: localeObj,
  226. })
  227. window.uppy.getPlugin('Dashboard').setOptions({
  228. direction,
  229. })
  230. })
  231. }
  232. function populateLocaleSelect () {
  233. const localeSelect = document.getElementById('localeList')
  234. Object.keys(localeList).forEach(localeName => {
  235. if (localeName === 'en_US') return
  236. localeSelect.innerHTML += `<option value="${localeName}">${localeList[localeName]} — (${localeName})</option>`
  237. })
  238. localeSelect.addEventListener('change', (event) => {
  239. const localeName = event.target.value
  240. setLocale(localeName)
  241. })
  242. }
  243. window.uppySetOptions = uppySetOptions
  244. window.uppyInit = uppyInit
  245. window.uppySetLocale = setLocale
  246. populateLocaleSelect()
  247. uppyInit()
  248. uppySetOptions()