Spinner.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import Plugin from './Plugin'
  2. /**
  3. * Spinner
  4. *
  5. */
  6. export default class Spinner extends Plugin {
  7. constructor (core, opts) {
  8. super(core, opts)
  9. this.type = 'progress'
  10. // set default options
  11. const defaultOptions = {}
  12. // merge default options with the ones set by user
  13. this.opts = Object.assign({}, defaultOptions, opts)
  14. }
  15. setProgress (percentage) {
  16. if (percentage !== 100) {
  17. this.spinnerEl.classList.add('is-spinning')
  18. } else {
  19. this.spinnerEl.classList.remove('is-spinning')
  20. }
  21. }
  22. initSpinner () {
  23. const spinnerContainer = document.querySelector(this.target)
  24. spinnerContainer.innerHTML = `<div class="UppySpinner"></div>`
  25. this.spinnerEl = document.querySelector(`${this.target} .UppySpinner`)
  26. }
  27. initEvents () {
  28. this.core.emitter.on('progress', data => {
  29. const percentage = data.percentage
  30. const plugin = data.plugin
  31. this.core.log(
  32. `progress is: ${percentage}, set by ${plugin.constructor.name}`
  33. )
  34. this.setProgress(percentage)
  35. })
  36. }
  37. install () {
  38. console.log(
  39. this.getTarget(this.opts.target).spinner
  40. )
  41. this.target = this.getTarget(this.opts.target).spinner
  42. this.initSpinner()
  43. this.initEvents()
  44. return
  45. }
  46. }