app.es6 7.7 KB

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