vendor/uvdesk/core-framework/Resources/views/Templates/attachment.html.twig line 1

Open in your IDE?
  1. <style>
  2. input.attachment {
  3. display: none;
  4. }
  5. </style>
  6. <script type="text/javascript">
  7. $(function () {
  8. var FileView = Backbone.View.extend({
  9. fileCounter: 0,
  10. max_post_size: {{ max_post_size }},
  11. max_file_uploads: {{ max_file_uploads }},
  12. upload_max_filesize: {{ upload_max_filesize }},
  13. el: '.attachment-block',
  14. events : {
  15. 'click .uv-file-label': 'createFileType',
  16. 'change .attachment': 'selectFile',
  17. 'click .uv-added-attachment span': 'removeFile',
  18. 'click .uv-field-message': 'removeError',
  19. },
  20. createFileType: function(e) {
  21. this.removeError(e)
  22. var currentElement = Backbone.$(e.currentTarget),
  23. attachmentBlock = currentElement.parents('.attachment-block')
  24. if (attachmentBlock.children('.uv-added-attachment').length + 1 > this.max_file_uploads) {
  25. attachmentBlock.append(this.getDefaultErrorMessage())
  26. return;
  27. }
  28. this.fileCounter += 1;
  29. attachmentBlock.append('<div class="uv-added-attachment" style="display: none" id="file-' + this.fileCounter + '"><div class="uv-attachment"><input type="file" name="attachments[]" class="attachment" multiple="multiple"></div><span></span></div>')
  30. $('#file-' + this.fileCounter).find('.attachment').trigger('click')
  31. },
  32. labelTemplate: _.template('<label class="file-name"><%- fileName %></label><br>'),
  33. selectFile: function(e) {
  34. var currentElement = Backbone.$(e.currentTarget);
  35. var attachmentBlock = currentElement.parents(".uv-added-attachment");
  36. var isError = false;
  37. if (currentElement.length) {
  38. files = currentElement[0].files;
  39. if (files.length) {
  40. for (var i = 0; i < files.length; i++) {
  41. var fileName = files[i].name;
  42. if (files[i].size > this.upload_max_filesize) {
  43. isError = true;
  44. break;
  45. }
  46. // Validating Form Size
  47. var formSize = 0
  48. var formData = new FormData(currentElement.parents('form')[0])
  49. for (var pair of formData.entries()) {
  50. if (pair[1] instanceof Blob) {
  51. formSize += pair[1].size
  52. } else {
  53. formSize += pair[1].length
  54. }
  55. }
  56. if (formSize > this.max_post_size) {
  57. isError = true
  58. }
  59. attachmentBlock.append(this.labelTemplate({'fileName': fileName}));
  60. }
  61. }
  62. }
  63. if (isError) {
  64. attachmentBlock.parents('.attachment-block').append(this.getDefaultErrorMessage())
  65. attachmentBlock.remove()
  66. return
  67. }
  68. attachmentBlock.show()
  69. },
  70. removeFile: function(e) {
  71. this.removeError(e)
  72. Backbone.$(e.currentTarget).parents('.uv-added-attachment').remove()
  73. },
  74. getDefaultErrorMessage: function() {
  75. return '<span class="uv-field-message">You can send up to ' + Math.floor(this.upload_max_filesize/(1024*1024)) + ' MB in attachments. If you have more than one attachment, they can\'t add up to more than ' + Math.floor(this.max_post_size/(1024*1024)) + ' MB and ' + this.max_file_uploads + ' attachments in total.</span>'
  76. },
  77. removeError: function(e) {
  78. Backbone.$(e.currentTarget).parents('.attachment-block').find('.uv-field-message').remove()
  79. }
  80. });
  81. var fileView = new FileView();
  82. });
  83. </script>