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.     .uv-element-block .uv-added-attachment {
  6.         color: #333333;
  7.         margin: -14px 5px;
  8.     }
  9. </style>
  10. <script type="text/javascript">
  11.     $(function () {
  12.         var FileView = Backbone.View.extend({
  13.             fileCounter: 0,
  14.             max_post_size: {{ max_post_size }},
  15.             max_file_uploads: {{ max_file_uploads }},
  16.             upload_max_filesize: {{ upload_max_filesize }},
  17.             el: '.attachment-block',
  18.             events : {
  19.                 'click .uv-file-label': 'createFileType',
  20.                 'change .attachment': 'selectFile',
  21.                 'click .uv-added-attachment span': 'removeFile',
  22.                 'click .uv-field-message': 'removeError',
  23.             },
  24.             createFileType: function(e) {
  25.                 this.removeError(e)
  26.                 var currentElement = Backbone.$(e.currentTarget),
  27.                     attachmentBlock = currentElement.parents('.attachment-block')
  28.                 if (attachmentBlock.children('.uv-added-attachment').length + 1 > this.max_file_uploads) {
  29.                     attachmentBlock.append(this.getDefaultErrorMessage())
  30.                     return;
  31.                 }
  32.                 this.fileCounter += 1;
  33.                 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></div>')
  34.                 $('#file-' + this.fileCounter).find('.attachment').trigger('click')
  35.             },
  36.             labelTemplate: _.template('<div data-name="<%- fileName %>" class="uv-selected-attachment-item"><span></span><label class="file-name"><%- fileName %></label></div>'),
  37.             selectFile: function(e) {
  38.                 var currentElement = Backbone.$(e.currentTarget);
  39.                 var attachmentBlock = currentElement.parents(".uv-added-attachment");
  40.                 var isError = false;
  41.                 
  42.                 if (currentElement.length) {
  43.                     files = currentElement[0].files; 
  44.                     if (files.length) {
  45.                         for (var i = 0; i < files.length; i++) {
  46.                             var fileName = files[i].name;
  47.                             if (files[i].size > this.upload_max_filesize) {
  48.                                 isError = true;
  49.                                 break;
  50.                             }
  51.                             // Validating Form Size
  52.                             var formSize = 0
  53.                             var formData = new FormData(currentElement.parents('form')[0])
  54.                             
  55.                             for (var pair of formData.entries()) {
  56.                                 if (pair[1] instanceof Blob) {
  57.                                     formSize += pair[1].size
  58.                                 } else {
  59.                                     formSize += pair[1].length
  60.                                 }
  61.                             }
  62.                             if (formSize > this.max_post_size) {
  63.                                 isError = true
  64.                             }
  65.                             attachmentBlock.append(this.labelTemplate({
  66.                                 'fileName': fileName
  67.                             }));
  68.                         }
  69.                     }
  70.                 }
  71.                 
  72.                 if (isError) {
  73.                     attachmentBlock.parents('.attachment-block').append(this.getDefaultErrorMessage())
  74.                     attachmentBlock.remove()
  75.                     return
  76.                 }
  77.                 attachmentBlock.show()
  78.             },
  79.             removeFile: function(e) {
  80.                 this.removeError(e);
  81.                 let el = $(e.currentTarget).parent('.uv-selected-attachment-item');
  82.                 let inputEl = el.siblings('.uv-attachment').find('input');
  83.                 
  84.                 let fileCount = $("input[name='attachments[]']")[0].files.length;
  85.                 if (fileCount > 1) {
  86.                     console.log(fileCount);
  87.                     let fileName = el.data('name');
  88.                     let files = inputEl[0].files; 
  89.                     let dt = new DataTransfer();
  90.                     for (let i = 0; i < files.length; i++) {
  91.                         let file = files[i];
  92.                         if (el.data('name') != files[i].name) {
  93.                             dt.items.add(file);
  94.                         }
  95.                     }
  96.                     $("input[name='attachments[]']")[0].files = dt.files;
  97.                     el.remove();
  98.                     if ($("input[name='attachments[]']")[0].files.length == 0) {
  99.                         $('.uv-added-attachment').remove();
  100.                     }
  101.                 }
  102.                 Backbone.$(e.currentTarget).parents('.uv-added-attachment').remove();
  103.             },
  104.             getDefaultErrorMessage: function() {
  105.                 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>'
  106.             },
  107.             removeError: function(e) {
  108.                 Backbone.$(e.currentTarget).parents('.attachment-block').find('.uv-field-message').remove()
  109.             }   
  110.         });
  111.         
  112.         var fileView = new FileView();
  113.     });
  114. </script>