app.es6 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. const RTL_LOCALES = ['ar_SA', 'fa_IR', 'he_IL']
  19. if (typeof window !== 'undefined' && typeof window.Uppy === 'undefined') {
  20. window.Uppy = {
  21. locales: {}
  22. }
  23. }
  24. function uppyInit () {
  25. if (window.uppy) {
  26. window.uppy.close()
  27. }
  28. const opts = window.uppyOptions
  29. const uppy = new Uppy({
  30. logger: Uppy.debugLogger
  31. })
  32. uppy.use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/', resume: true })
  33. uppy.on('complete', result => {
  34. console.log('successful files:')
  35. console.log(result.successful)
  36. console.log('failed files:')
  37. console.log(result.failed)
  38. })
  39. uppy.use(Dashboard, {
  40. trigger: '.UppyModalOpenerBtn',
  41. target: opts.DashboardInline ? '.DashboardContainer' : 'body',
  42. inline: opts.DashboardInline,
  43. replaceTargetContent: 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. }
  68. window.uppy.setOptions({
  69. autoProceed: opts.autoProceed,
  70. restrictions: opts.restrictions ? restrictions : defaultNullRestrictions
  71. })
  72. window.uppy.getPlugin('Dashboard').setOptions({
  73. note: opts.restrictions ? 'Images and video only, 2–3 files, up to 1 MB' : '',
  74. theme: opts.darkMode ? 'dark' : 'light'
  75. })
  76. const googleDriveInstance = window.uppy.getPlugin('GoogleDrive')
  77. if (opts.GoogleDrive && !googleDriveInstance) {
  78. window.uppy.use(GoogleDrive, { target: Dashboard, companionUrl: COMPANION })
  79. }
  80. if (!opts.GoogleDrive && googleDriveInstance) {
  81. window.uppy.removePlugin(googleDriveInstance)
  82. }
  83. const dropboxInstance = window.uppy.getPlugin('Dropbox')
  84. if (opts.Dropbox && !dropboxInstance) {
  85. window.uppy.use(Dropbox, { target: Dashboard, companionUrl: COMPANION })
  86. }
  87. if (!opts.Dropbox && dropboxInstance) {
  88. window.uppy.removePlugin(dropboxInstance)
  89. }
  90. const instagramInstance = window.uppy.getPlugin('Instagram')
  91. if (opts.Instagram && !instagramInstance) {
  92. window.uppy.use(Instagram, { target: Dashboard, companionUrl: COMPANION })
  93. }
  94. if (!opts.Instagram && instagramInstance) {
  95. window.uppy.removePlugin(instagramInstance)
  96. }
  97. const urlInstance = window.uppy.getPlugin('Url')
  98. if (opts.Url && !urlInstance) {
  99. window.uppy.use(Url, { target: Dashboard, companionUrl: COMPANION })
  100. }
  101. if (!opts.Url && urlInstance) {
  102. window.uppy.removePlugin(urlInstance)
  103. }
  104. const facebookInstance = window.uppy.getPlugin('Facebook')
  105. if (opts.Facebook && !facebookInstance) {
  106. window.uppy.use(Facebook, { target: Dashboard, companionUrl: COMPANION })
  107. }
  108. if (!opts.Facebook && facebookInstance) {
  109. window.uppy.removePlugin(facebookInstance)
  110. }
  111. const oneDriveInstance = window.uppy.getPlugin('OneDrive')
  112. if (opts.OneDrive && !oneDriveInstance) {
  113. window.uppy.use(OneDrive, { target: Dashboard, companionUrl: COMPANION })
  114. }
  115. if (!opts.OneDrive && oneDriveInstance) {
  116. window.uppy.removePlugin(oneDriveInstance)
  117. }
  118. const zoomInstance = window.uppy.getPlugin('Zoom')
  119. if (opts.Zoom && !zoomInstance) {
  120. window.uppy.use(Zoom, { target: Dashboard, companionUrl: 'https://intense-meadow-61813.herokuapp.com/' })
  121. }
  122. if (!opts.Zoom && zoomInstance) {
  123. window.uppy.removePlugin(zoomInstance)
  124. }
  125. const webcamInstance = window.uppy.getPlugin('Webcam')
  126. if (opts.Webcam && !webcamInstance) {
  127. window.uppy.use(Webcam, {
  128. target: Dashboard,
  129. showVideoSourceDropdown: true
  130. })
  131. }
  132. if (!opts.Webcam && webcamInstance) {
  133. window.uppy.removePlugin(webcamInstance)
  134. }
  135. const screenCaptureInstance = window.uppy.getPlugin('ScreenCapture')
  136. if (opts.ScreenCapture && !screenCaptureInstance) {
  137. window.uppy.use(ScreenCapture, { target: Dashboard })
  138. }
  139. if (!opts.ScreenCapture && screenCaptureInstance) {
  140. window.uppy.removePlugin(screenCaptureInstance)
  141. }
  142. const imageEditorInstance = window.uppy.getPlugin('ImageEditor')
  143. if (opts.imageEditor && !imageEditorInstance) {
  144. window.uppy.use(ImageEditor, { target: Dashboard })
  145. }
  146. if (!opts.imageEditor && imageEditorInstance) {
  147. window.uppy.removePlugin(imageEditorInstance)
  148. }
  149. }
  150. function whenLocaleAvailable (localeName, callback) {
  151. var interval = 100 // ms
  152. var loop = setInterval(function () {
  153. if (window.Uppy && window.Uppy.locales && window.Uppy.locales[localeName]) {
  154. clearInterval(loop)
  155. callback(window.Uppy.locales[localeName])
  156. }
  157. }, interval)
  158. }
  159. function loadLocaleFromCDN (localeName) {
  160. var head = document.getElementsByTagName('head')[0]
  161. var js = document.createElement('script')
  162. js.type = 'text/javascript'
  163. js.src = `https://releases.transloadit.com/uppy/locales/v1.17.1/${localeName}.min.js`
  164. head.appendChild(js)
  165. }
  166. function setLocale (localeName) {
  167. if (typeof window.Uppy.locales[localeName] === 'undefined') {
  168. loadLocaleFromCDN(localeName)
  169. }
  170. whenLocaleAvailable(localeName, (localeObj) => {
  171. const direction = RTL_LOCALES.indexOf(localeName) !== -1
  172. ? 'rtl'
  173. : 'ltr'
  174. window.uppy.setOptions({
  175. locale: localeObj
  176. })
  177. window.uppy.getPlugin('Dashboard').setOptions({
  178. direction
  179. })
  180. })
  181. }
  182. function populateLocaleSelect () {
  183. const localeSelect = document.getElementById('localeList')
  184. Object.keys(localeList).forEach(localeName => {
  185. if (localeName === 'en_US') return
  186. localeSelect.innerHTML += `<option value="${localeName}">${localeList[localeName]} — (${localeName})</option>`
  187. })
  188. localeSelect.addEventListener('change', (event) => {
  189. const localeName = event.target.value
  190. setLocale(localeName)
  191. })
  192. }
  193. window.uppySetOptions = uppySetOptions
  194. window.uppyInit = uppyInit
  195. window.uppySetLocale = setLocale
  196. populateLocaleSelect()
  197. uppyInit()
  198. uppySetOptions()