Dropbox.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. class DropboxPlugin {
  2. constructor() {
  3. this.connect = this.connect.bind(this);
  4. this.render = this.render.bind(this);
  5. this.files = [];
  6. this.currentDir = '/';
  7. }
  8. connect(target) {
  9. this._target = document.getElementById(target);
  10. this.client = new Dropbox.Client({ key: 'b7dzc9ei5dv5hcv', token: '' });
  11. this.client.authDriver(new Dropbox.AuthDriver.Redirect());
  12. this.client.authenticate();
  13. if (this.client.credentials().token) {
  14. this.getDirectory();
  15. }
  16. }
  17. authenticate() {
  18. }
  19. addFile() {
  20. }
  21. getDirectory() {
  22. return this.client.readdir(this.currentDir, (error, entries, stat, statFiles) => {
  23. if (error) {
  24. return showError(error); // Something went wrong.
  25. }
  26. return this.render(statFiles);
  27. });
  28. }
  29. run() {
  30. }
  31. render(files) {
  32. // for each file in the directory, create a list item element
  33. const elems = files.map((file, i) => {
  34. const icon = (file.isFolder) ? 'Folder' : 'File'
  35. return `<li data-type="${icon}" data-name="${file.name}"><span>${icon} : </span><span> ${file.name}</span></li>`
  36. })
  37. // appends the list items to the target
  38. this._target.innerHTML = elems.sort().join('');
  39. if (this.currentDir.length > 1) {
  40. const back = document.createElement('LI');
  41. back.setAttribute('data-type', 'back');
  42. back.innerHTML = '<span>...</span>';
  43. this._target.appendChild(back);
  44. }
  45. // add an onClick to each list item
  46. const fileElems = this._target.querySelectorAll('li');
  47. Array.prototype.forEach.call(fileElems, element => {
  48. var type = element.getAttribute('data-type');
  49. if (type === 'File') {
  50. element.addEventListener('click', e => {
  51. this.files.push(element.getAttribute('data-name'));
  52. });
  53. } else {
  54. element.addEventListener('dblclick', e => {
  55. console.log(type);
  56. console.log(this.currentDir.split('/').slice(0, length - 2))
  57. console.log(this.currentDir.split('/').slice(0, length - 2).join('/'));
  58. const length = this.currentDir.split('/').length;
  59. this.currentDir = (type === 'Folder') ?
  60. `${this.currentDir}${element.getAttribute('data-name')}/` :
  61. this.currentDir.split('/').slice(0, length - 2).join('/')
  62. console.log(this.currentDir);
  63. this.getDirectory();
  64. })
  65. }
  66. })
  67. }
  68. }
  69. export default new DropboxPlugin()