Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

files_drop.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function ($) {
  11. var TEMPLATE =
  12. '<li data-toggle="tooltip" title="{{name}}" data-name="{{name}}">' +
  13. '{{#if isUploading}}' +
  14. '<span class="icon-loading-small"></span> {{name}}' +
  15. '{{else}}' +
  16. '<img src="{{iconSrc}}"/> {{name}}' +
  17. '{{/if}}' +
  18. '</li>';
  19. var Drop = {
  20. /** @type {Function} **/
  21. _template: undefined,
  22. addFileToUpload: function(e, data) {
  23. var errors = [];
  24. var output = this.template();
  25. var filesClient = new OC.Files.Client({
  26. host: OC.getHost(),
  27. port: OC.getPort(),
  28. userName: $('#sharingToken').val(),
  29. // note: password not be required, the endpoint
  30. // will recognize previous validation from the session
  31. root: OC.getRootPath() + '/public.php/webdav',
  32. useHTTPS: OC.getProtocol() === 'https'
  33. });
  34. var name = data.files[0].name;
  35. try {
  36. // FIXME: not so elegant... need to refactor that method to return a value
  37. Files.isFileNameValid(name);
  38. }
  39. catch (errorMessage) {
  40. OC.Notification.show(errorMessage, {type: 'error'});
  41. return false;
  42. }
  43. var base = OC.getProtocol() + '://' + OC.getHost();
  44. data.url = base + OC.getRootPath() + '/public.php/webdav/' + encodeURI(name);
  45. data.multipart = false;
  46. if (!data.headers) {
  47. data.headers = {};
  48. }
  49. var userName = filesClient.getUserName();
  50. var password = filesClient.getPassword();
  51. if (userName) {
  52. // copy username/password from DAV client
  53. data.headers['Authorization'] =
  54. 'Basic ' + btoa(userName + ':' + (password || ''));
  55. }
  56. $('#drop-upload-done-indicator').addClass('hidden');
  57. $('#drop-upload-progress-indicator').removeClass('hidden');
  58. $('#public-upload ul').append(output({isUploading: true, name: data.files[0].name}));
  59. $('[data-toggle="tooltip"]').tooltip();
  60. data.submit();
  61. return true;
  62. },
  63. updateFileItem: function (fileName, fileItem) {
  64. $('#public-upload ul li[data-name="' + fileName + '"]').replaceWith(fileItem);
  65. $('[data-toggle="tooltip"]').tooltip();
  66. },
  67. initialize: function () {
  68. $(document).bind('drop dragover', function (e) {
  69. // Prevent the default browser drop action:
  70. e.preventDefault();
  71. });
  72. var output = this.template();
  73. $('#public-upload').fileupload({
  74. type: 'PUT',
  75. dropZone: $('#public-upload'),
  76. sequentialUploads: true,
  77. add: function(e, data) {
  78. Drop.addFileToUpload(e, data);
  79. //we return true to keep trying to upload next file even
  80. //if addFileToUpload did not like the privious one
  81. return true;
  82. },
  83. done: function(e, data) {
  84. // Created
  85. var mimeTypeUrl = OC.MimeType.getIconUrl(data.files[0].type);
  86. var fileItem = output({isUploading: false, iconSrc: mimeTypeUrl, name: data.files[0].name});
  87. Drop.updateFileItem(data.files[0].name, fileItem);
  88. },
  89. fail: function(e, data) {
  90. OC.Notification.showTemporary(OC.L10N.translate(
  91. 'files_sharing',
  92. 'Could not upload "{filename}"',
  93. {filename: data.files[0].name}
  94. ));
  95. var errorIconSrc = OC.imagePath('core', 'actions/error.svg');
  96. var fileItem = output({isUploading: false, iconSrc: errorIconSrc, name: data.files[0].name});
  97. Drop.updateFileItem(data.files[0].name, fileItem);
  98. },
  99. progressall: function (e, data) {
  100. var progress = parseInt(data.loaded / data.total * 100, 10);
  101. if(progress === 100) {
  102. $('#drop-upload-done-indicator').removeClass('hidden');
  103. $('#drop-upload-progress-indicator').addClass('hidden');
  104. } else {
  105. $('#drop-upload-done-indicator').addClass('hidden');
  106. $('#drop-upload-progress-indicator').removeClass('hidden');
  107. }
  108. }
  109. });
  110. $('#public-upload .button.icon-upload').click(function(e) {
  111. e.preventDefault();
  112. $('#public-upload #emptycontent input').focus().trigger('click');
  113. });
  114. },
  115. /**
  116. * @returns {Function} from Handlebars
  117. * @private
  118. */
  119. template: function () {
  120. if (!this._template) {
  121. this._template = Handlebars.compile(TEMPLATE);
  122. }
  123. return this._template;
  124. }
  125. };
  126. OCA.FilesSharingDrop = Drop;
  127. $(document).ready(function() {
  128. if($('#upload-only-interface').val() === "1") {
  129. $('.avatardiv').avatar($('#sharingUserId').val(), 128, true);
  130. }
  131. OCA.FilesSharingDrop.initialize();
  132. });
  133. })(jQuery);