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.                 
  38.                 if (currentElement.length) {
  39.                     files = currentElement[0].files; 
  40.                     if (files.length) {
  41.                         for (var i = 0; i < files.length; i++) {
  42.                             var fileName = files[i].name;
  43.                             
  44.                             if (files[i].size > this.upload_max_filesize) {
  45.                                 isError = true;
  46.                                 break;
  47.                             }
  48.                             // Validating Form Size
  49.                             var formSize = 0
  50.                             var formData = new FormData(currentElement.parents('form')[0])
  51.                             
  52.                             for (var pair of formData.entries()) {
  53.                                 if (pair[1] instanceof Blob) {
  54.                                     formSize += pair[1].size
  55.                                 } else {
  56.                                     formSize += pair[1].length
  57.                                 }
  58.                             }
  59.                             if (formSize > this.max_post_size) {
  60.                                 isError = true
  61.                             }
  62.                             attachmentBlock.append(this.labelTemplate({'fileName': fileName}));
  63.                         }
  64.                     }
  65.                 }
  66.                 
  67.                 if (isError) {
  68.                     attachmentBlock.parents('.attachment-block').append(this.getDefaultErrorMessage())
  69.                     attachmentBlock.remove()
  70.                     return
  71.                 }
  72.                 attachmentBlock.show()
  73.             },
  74.             removeFile: function(e) {
  75.                 this.removeError(e)
  76.                 Backbone.$(e.currentTarget).parents('.uv-added-attachment').remove()
  77.             },
  78.             getDefaultErrorMessage: function() {
  79.                 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>'
  80.             },
  81.             removeError: function(e) {
  82.                 Backbone.$(e.currentTarget).parents('.attachment-block').find('.uv-field-message').remove()
  83.             }   
  84.         });
  85.         
  86.         var fileView = new FileView();
  87.     });
  88. </script>