1/** 2 * @file 3 * JavaScript behaviors for managed file uploads. 4 */ 5 6(function ($, Drupal, once) { 7 /** 8 * Track file uploads and display confirm dialog when an file upload is in progress. 9 * 10 * @type {Drupal~behavior} 11 */ 12 Drupal.behaviors.webformManagedFileAutoUpload = { 13 attach: function attach(context) { 14 // Add submit handler to file upload form. 15 $(once('webform-auto-file-upload', 'form', context)) 16 .on('submit', function (event) { 17 var $form = $(this); 18 if ($form.data('webform-auto-file-uploads') > 0 && Drupal.webformManagedFileBlockSubmit($form)) { 19 event.preventDefault(); 20 return false; 21 } 22 return true; 23 }); 24 25 // Add submit handler to form.beforeSend. 26 // Update Drupal.Ajax.prototype.beforeSend only once. 27 if (typeof Drupal.Ajax !== 'undefined' && typeof Drupal.Ajax.prototype.beforeSubmitWebformManagedFileAutoUploadOriginal === 'undefined') { 28 Drupal.Ajax.prototype.beforeSubmitWebformManagedFileAutoUploadOriginal = Drupal.Ajax.prototype.beforeSubmit; 29 Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) { 30 var $form = this.$form; 31 var $element = $(this.element); 32 33 // Determine if the triggering element is within .form-actions. 34 var isFormActions = $element 35 .closest('.form-actions').length; 36 37 // Determine if the triggering element is within a multiple element. 38 var isMultipleUpload = $element 39 .parents('.js-form-type-webform-multiple, .js-form-type-webform-custom-composite') 40 .find('.js-form-managed-file').length; 41 42 // Determine if the triggering element is not within a 43 // managed file element. 44 var isManagedUploadButton = $element.parents('.js-form-managed-file').length; 45 46 // Only trigger block submit for .form-actions and multiple element 47 // with file upload. 48 if ($form.data('webform-auto-file-uploads') > 0 && 49 (isFormActions || (isMultipleUpload && !isManagedUploadButton)) && 50 Drupal.webformManagedFileBlockSubmit($form)) { 51 this.ajaxing = false; 52 return false; 53 } 54 return this.beforeSubmitWebformManagedFileAutoUploadOriginal.apply(this, arguments); 55 }; 56 } 57 58 $(once('webform-auto-file-upload', 'input[type="file"]', context)).on('change', function () { 59 // Track file upload. 60 $(this).data('webform-auto-file-upload', true); 61 62 // Increment form file uploads. 63 var $form = $(this.form); 64 var fileUploads = ($form.data('webform-auto-file-uploads') || 0); 65 $form.data('webform-auto-file-uploads', fileUploads + 1); 66 }); 67 }, 68 detach: function detach(context, settings, trigger) { 69 if (trigger === 'unload') { 70 const removedElements = once.remove('webform-auto-file-upload', 'input[type="file"]', context); 71 $(removedElements).each(function () { 72 if ($(this).data('webform-auto-file-upload')) { 73 // Remove file upload tracking. 74 $(this).removeData('webform-auto-file-upload'); 75 76 // Decrease form file uploads. 77 var $form = $(this.form); 78 var fileUploads = ($form.data('webform-auto-file-uploads') || 0); 79 $form.data('webform-auto-file-uploads', fileUploads - 1); 80 } 81 }); 82 } 83 } 84 }; 85 86 /** 87 * Block form submit. 88 * 89 * @param {jQuery} form 90 * A form. 91 * 92 * @return {boolean} 93 * TRUE if form submit should be blocked. 94 */ 95 Drupal.webformManagedFileBlockSubmit = function (form) { 96 if ($(form).data('webform-auto-file-uploads') < 0) { 97 return false; 98 } 99 100 var message = Drupal.t('File upload in progress. Uploaded file may be lost.') + 101 '\n' + 102 Drupal.t('Click OK to submit the form without finishing the file upload or cancel to return to form.'); 103 var result = !window.confirm(message); 104 105 // If submit once behavior is available, make sure to clear it if the form 106 // can be submitted. 107 if (result && Drupal.behaviors.webformSubmitOnce) { 108 setTimeout(function () {Drupal.behaviors.webformSubmitOnce.clear();}); 109 } 110 111 return result; 112 } 113 114})(jQuery, Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.