app.es6 6.5 KB

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