app.es6 6.1 KB

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