app.es6 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. require('es6-promise/auto')
  2. require('whatwg-fetch')
  3. const Uppy = require('@uppy/core')
  4. const Dashboard = require('@uppy/dashboard')
  5. const GoogleDrive = require('@uppy/google-drive')
  6. const Dropbox = require('@uppy/dropbox')
  7. const Instagram = require('@uppy/instagram')
  8. const Facebook = require('@uppy/facebook')
  9. const OneDrive = require('@uppy/onedrive')
  10. const Url = require('@uppy/url')
  11. const Webcam = require('@uppy/webcam')
  12. const Tus = require('@uppy/tus')
  13. const localeList = require('../locale_list.json')
  14. const COMPANION = require('../env')
  15. if (typeof window !== 'undefined' && typeof window.Uppy === 'undefined') {
  16. window.Uppy = {
  17. locales: {}
  18. }
  19. }
  20. function uppyInit () {
  21. if (window.uppy) {
  22. window.uppy.close()
  23. }
  24. const opts = window.uppyOptions
  25. const uppy = Uppy({
  26. logger: Uppy.debugLogger
  27. })
  28. uppy.use(Tus, { endpoint: 'https://master.tus.io/files/', resume: true })
  29. uppy.on('complete', result => {
  30. console.log('successful files:')
  31. console.log(result.successful)
  32. console.log('failed files:')
  33. console.log(result.failed)
  34. })
  35. uppy.use(Dashboard, {
  36. trigger: '.UppyModalOpenerBtn',
  37. target: opts.DashboardInline ? '.DashboardContainer' : 'body',
  38. inline: opts.DashboardInline,
  39. replaceTargetContent: opts.DashboardInline,
  40. height: 470,
  41. showProgressDetails: true,
  42. metaFields: [
  43. { id: 'name', name: 'Name', placeholder: 'file name' },
  44. { id: 'caption', name: 'Caption', placeholder: 'add description' }
  45. ]
  46. })
  47. window.uppy = uppy
  48. }
  49. function uppySetOptions () {
  50. const opts = window.uppyOptions
  51. const defaultNullRestrictions = {
  52. maxFileSize: null,
  53. maxNumberOfFiles: null,
  54. minNumberOfFiles: null,
  55. allowedFileTypes: null
  56. }
  57. const restrictions = {
  58. maxFileSize: 1000000,
  59. maxNumberOfFiles: 3,
  60. minNumberOfFiles: 2,
  61. allowedFileTypes: ['image/*', 'video/*']
  62. }
  63. window.uppy.setOptions({
  64. autoProceed: opts.autoProceed,
  65. restrictions: opts.restrictions ? restrictions : defaultNullRestrictions
  66. })
  67. window.uppy.getPlugin('Dashboard').setOptions({
  68. note: opts.restrictions ? 'Images and video only, 2–3 files, up to 1 MB' : '',
  69. browserBackButtonClose: opts.browserBackButtonClose
  70. })
  71. const GoogleDriveInstance = window.uppy.getPlugin('GoogleDrive')
  72. if (opts.GoogleDrive && !GoogleDriveInstance) {
  73. window.uppy.use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION })
  74. }
  75. if (!opts.GoogleDrive && GoogleDriveInstance) {
  76. window.uppy.removePlugin(GoogleDriveInstance)
  77. }
  78. const DropboxInstance = window.uppy.getPlugin('Dropbox')
  79. if (opts.Dropbox && !DropboxInstance) {
  80. window.uppy.use(Dropbox, { target: Dashboard, companionUrl: COMPANION })
  81. }
  82. if (!opts.Dropbox && DropboxInstance) {
  83. window.uppy.removePlugin(DropboxInstance)
  84. }
  85. const InstagramInstance = window.uppy.getPlugin('Instagram')
  86. if (opts.Instagram && !InstagramInstance) {
  87. window.uppy.use(Instagram, { target: Dashboard, companionUrl: COMPANION })
  88. }
  89. if (!opts.Instagram && InstagramInstance) {
  90. window.uppy.removePlugin(InstagramInstance)
  91. }
  92. const UrlInstance = window.uppy.getPlugin('Url')
  93. if (opts.Url && !UrlInstance) {
  94. window.uppy.use(Url, { target: Dashboard, companionUrl: COMPANION })
  95. }
  96. if (!opts.Url && UrlInstance) {
  97. window.uppy.removePlugin(UrlInstance)
  98. }
  99. const FacebookInstance = window.uppy.getPlugin('Facebook')
  100. if (opts.Facebook && !FacebookInstance) {
  101. uppy.use(Facebook, { target: Dashboard, companionUrl: COMPANION })
  102. }
  103. if (!opts.Facebook && FacebookInstance) {
  104. window.uppy.removePlugin(FacebookInstance)
  105. }
  106. const OneDriveInstance = window.uppy.getPlugin('OneDrive')
  107. if (opts.OneDrive && !OneDriveInstance) {
  108. uppy.use(OneDrive, { target: Dashboard, companionUrl: COMPANION })
  109. }
  110. if (!opts.OneDrive && OneDriveInstance) {
  111. window.uppy.removePlugin(OneDriveInstance)
  112. }
  113. const WebcamInstance = window.uppy.getPlugin('Webcam')
  114. if (opts.Webcam && !WebcamInstance) {
  115. window.uppy.use(Webcam, { target: Dashboard, companionUrl: COMPANION })
  116. }
  117. if (!opts.Webcam && WebcamInstance) {
  118. window.uppy.removePlugin(WebcamInstance)
  119. }
  120. }
  121. function whenLocaleAvailable (localeName, callback) {
  122. var interval = 100 // ms
  123. var loop = setInterval(function () {
  124. if (window.Uppy && window.Uppy.locales && window.Uppy.locales[localeName]) {
  125. clearInterval(loop)
  126. callback(window.Uppy.locales[localeName])
  127. }
  128. }, interval)
  129. }
  130. function loadLocaleFromCDN (localeName) {
  131. var head = document.getElementsByTagName('head')[0]
  132. var js = document.createElement('script')
  133. js.type = 'text/javascript'
  134. js.src = `https://transloadit.edgly.net/releases/uppy/locales/v1.11.0/${localeName}.min.js`
  135. head.appendChild(js)
  136. }
  137. function setLocale (localeName) {
  138. if (typeof window.Uppy.locales[localeName] === 'undefined') {
  139. loadLocaleFromCDN(localeName)
  140. }
  141. whenLocaleAvailable(localeName, (localeObj) => {
  142. window.uppy.setOptions({
  143. locale: localeObj
  144. })
  145. })
  146. }
  147. function populateLocaleSelect () {
  148. const localeSelect = document.getElementById('localeList')
  149. Object.keys(localeList).forEach(localeName => {
  150. if (localeName === 'en_US') return
  151. localeSelect.innerHTML += `<option value="${localeName}">${localeList[localeName]} — (${localeName})</option>`
  152. })
  153. localeSelect.addEventListener('change', (event) => {
  154. const localeName = event.target.value
  155. setLocale(localeName)
  156. })
  157. }
  158. window.uppySetOptions = uppySetOptions
  159. window.uppyInit = uppyInit
  160. window.uppySetLocale = setLocale
  161. populateLocaleSelect()
  162. uppyInit()
  163. uppySetOptions()