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