3 * Provides JavaScript additions to the managed file field type.
5 * This file provides progress bar support (if available), popup windows for
6 * file previews, and disabling of other file fields during Ajax uploads (which
7 * prevents separate file fields from accidentally uploading files).
10 (function ($, Drupal) {
15 * Attach behaviors to the file fields passed in the settings.
17 * @type {Drupal~behavior}
19 * @prop {Drupal~behaviorAttach} attach
20 * Attaches validation for file extensions.
21 * @prop {Drupal~behaviorDetach} detach
22 * Detaches validation for file extensions.
24 Drupal.behaviors.fileValidateAutoAttach = {
25 attach: function (context, settings) {
26 var $context = $(context);
29 function initFileValidation(selector) {
30 $context.find(selector)
32 .on('change.fileValidate', {extensions: elements[selector]}, Drupal.file.validateExtension);
35 if (settings.file && settings.file.elements) {
36 elements = settings.file.elements;
37 Object.keys(elements).forEach(initFileValidation);
40 detach: function (context, settings, trigger) {
41 var $context = $(context);
44 function removeFileValidation(selector) {
45 $context.find(selector)
46 .removeOnce('fileValidate')
47 .off('change.fileValidate', Drupal.file.validateExtension);
50 if (trigger === 'unload' && settings.file && settings.file.elements) {
51 elements = settings.file.elements;
52 Object.keys(elements).forEach(removeFileValidation);
58 * Attach behaviors to file element auto upload.
60 * @type {Drupal~behavior}
62 * @prop {Drupal~behaviorAttach} attach
63 * Attaches triggers for the upload button.
64 * @prop {Drupal~behaviorDetach} detach
65 * Detaches auto file upload trigger.
67 Drupal.behaviors.fileAutoUpload = {
68 attach: function (context) {
69 $(context).find('input[type="file"]').once('auto-file-upload').on('change.autoFileUpload', Drupal.file.triggerUploadButton);
71 detach: function (context, setting, trigger) {
72 if (trigger === 'unload') {
73 $(context).find('input[type="file"]').removeOnce('auto-file-upload').off('.autoFileUpload');
79 * Attach behaviors to the file upload and remove buttons.
81 * @type {Drupal~behavior}
83 * @prop {Drupal~behaviorAttach} attach
84 * Attaches form submit events.
85 * @prop {Drupal~behaviorDetach} detach
86 * Detaches form submit events.
88 Drupal.behaviors.fileButtons = {
89 attach: function (context) {
90 var $context = $(context);
91 $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields);
92 $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar);
94 detach: function (context) {
95 var $context = $(context);
96 $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields);
97 $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar);
102 * Attach behaviors to links within managed file elements for preview windows.
104 * @type {Drupal~behavior}
106 * @prop {Drupal~behaviorAttach} attach
108 * @prop {Drupal~behaviorDetach} detach
111 Drupal.behaviors.filePreviewLinks = {
112 attach: function (context) {
113 $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
115 detach: function (context) {
116 $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
121 * File upload utility functions.
125 Drupal.file = Drupal.file || {
128 * Client-side file input validation of file extensions.
130 * @name Drupal.file.validateExtension
132 * @param {jQuery.Event} event
133 * The event triggered. For example `change.fileValidate`.
135 validateExtension: function (event) {
136 event.preventDefault();
137 // Remove any previous errors.
138 $('.file-upload-js-error').remove();
140 // Add client side validation for the input[type=file].
141 var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
142 if (extensionPattern.length > 1 && this.value.length > 0) {
143 var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
144 if (!acceptableMatch.test(this.value)) {
145 var error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', {
146 // According to the specifications of HTML5, a file upload control
147 // should not reveal the real local path to the file that a user
148 // has selected. Some web browsers implement this restriction by
149 // replacing the local path with "C:\fakepath\", which can cause
150 // confusion by leaving the user thinking perhaps Drupal could not
151 // find the file because it messed up the file path. To avoid this
152 // confusion, therefore, we strip out the bogus fakepath string.
153 '%filename': this.value.replace('C:\\fakepath\\', ''),
154 '%extensions': extensionPattern.replace(/\|/g, ', ')
156 $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
158 // Cancel all other change event handlers.
159 event.stopImmediatePropagation();
165 * Trigger the upload_button mouse event to auto-upload as a managed file.
167 * @name Drupal.file.triggerUploadButton
169 * @param {jQuery.Event} event
170 * The event triggered. For example `change.autoFileUpload`.
172 triggerUploadButton: function (event) {
173 $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
177 * Prevent file uploads when using buttons not intended to upload.
179 * @name Drupal.file.disableFields
181 * @param {jQuery.Event} event
182 * The event triggered, most likely a `mousedown` event.
184 disableFields: function (event) {
185 var $clickedButton = $(this).findOnce('ajax');
187 // Only disable upload fields for Ajax buttons.
188 if (!$clickedButton.length) {
192 // Check if we're working with an "Upload" button.
193 var $enabledFields = [];
194 if ($clickedButton.closest('div.js-form-managed-file').length > 0) {
195 $enabledFields = $clickedButton.closest('div.js-form-managed-file').find('input.js-form-file');
198 // Temporarily disable upload fields other than the one we're currently
199 // working with. Filter out fields that are already disabled so that they
200 // do not get enabled when we re-enable these fields at the end of
201 // behavior processing. Re-enable in a setTimeout set to a relatively
202 // short amount of time (1 second). All the other mousedown handlers
203 // (like Drupal's Ajax behaviors) are executed before any timeout
204 // functions are called, so we don't have to worry about the fields being
205 // re-enabled too soon. @todo If the previous sentence is true, why not
206 // set the timeout to 0?
207 var $fieldsToTemporarilyDisable = $('div.js-form-managed-file input.js-form-file').not($enabledFields).not(':disabled');
208 $fieldsToTemporarilyDisable.prop('disabled', true);
209 setTimeout(function () {
210 $fieldsToTemporarilyDisable.prop('disabled', false);
215 * Add progress bar support if possible.
217 * @name Drupal.file.progressBar
219 * @param {jQuery.Event} event
220 * The event triggered, most likely a `mousedown` event.
222 progressBar: function (event) {
223 var $clickedButton = $(this);
224 var $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
225 if ($progressId.length) {
226 var originalName = $progressId.attr('name');
228 // Replace the name with the required identifier.
229 $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
231 // Restore the original name after the upload begins.
232 setTimeout(function () {
233 $progressId.attr('name', originalName);
236 // Show the progress bar if the upload takes longer than half a second.
237 setTimeout(function () {
238 $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown();
243 * Open links to files within forms in a new window.
245 * @name Drupal.file.openInNewWindow
247 * @param {jQuery.Event} event
248 * The event triggered, most likely a `click` event.
250 openInNewWindow: function (event) {
251 event.preventDefault();
252 $(this).attr('target', '_blank');
253 window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');