2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal) {
9 Drupal.behaviors.fileValidateAutoAttach = {
10 attach: function attach(context, settings) {
11 var $context = $(context);
12 var elements = void 0;
14 function initFileValidation(selector) {
15 $context.find(selector).once('fileValidate').on('change.fileValidate', { extensions: elements[selector] }, Drupal.file.validateExtension);
18 if (settings.file && settings.file.elements) {
19 elements = settings.file.elements;
20 Object.keys(elements).forEach(initFileValidation);
23 detach: function detach(context, settings, trigger) {
24 var $context = $(context);
25 var elements = void 0;
27 function removeFileValidation(selector) {
28 $context.find(selector).removeOnce('fileValidate').off('change.fileValidate', Drupal.file.validateExtension);
31 if (trigger === 'unload' && settings.file && settings.file.elements) {
32 elements = settings.file.elements;
33 Object.keys(elements).forEach(removeFileValidation);
38 Drupal.behaviors.fileAutoUpload = {
39 attach: function attach(context) {
40 $(context).find('input[type="file"]').once('auto-file-upload').on('change.autoFileUpload', Drupal.file.triggerUploadButton);
42 detach: function detach(context, setting, trigger) {
43 if (trigger === 'unload') {
44 $(context).find('input[type="file"]').removeOnce('auto-file-upload').off('.autoFileUpload');
49 Drupal.behaviors.fileButtons = {
50 attach: function attach(context) {
51 var $context = $(context);
52 $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields);
53 $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar);
55 detach: function detach(context) {
56 var $context = $(context);
57 $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields);
58 $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar);
62 Drupal.behaviors.filePreviewLinks = {
63 attach: function attach(context) {
64 $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
66 detach: function detach(context) {
67 $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
71 Drupal.file = Drupal.file || {
72 validateExtension: function validateExtension(event) {
73 event.preventDefault();
75 $('.file-upload-js-error').remove();
77 var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
78 if (extensionPattern.length > 1 && this.value.length > 0) {
79 var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
80 if (!acceptableMatch.test(this.value)) {
81 var error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', {
82 '%filename': this.value.replace('C:\\fakepath\\', ''),
83 '%extensions': extensionPattern.replace(/\|/g, ', ')
85 $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
88 event.stopImmediatePropagation();
92 triggerUploadButton: function triggerUploadButton(event) {
93 $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
95 disableFields: function disableFields(event) {
96 var $clickedButton = $(this).findOnce('ajax');
98 if (!$clickedButton.length) {
102 var $enabledFields = [];
103 if ($clickedButton.closest('div.js-form-managed-file').length > 0) {
104 $enabledFields = $clickedButton.closest('div.js-form-managed-file').find('input.js-form-file');
107 var $fieldsToTemporarilyDisable = $('div.js-form-managed-file input.js-form-file').not($enabledFields).not(':disabled');
108 $fieldsToTemporarilyDisable.prop('disabled', true);
109 setTimeout(function () {
110 $fieldsToTemporarilyDisable.prop('disabled', false);
113 progressBar: function progressBar(event) {
114 var $clickedButton = $(this);
115 var $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
116 if ($progressId.length) {
117 var originalName = $progressId.attr('name');
119 $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
121 setTimeout(function () {
122 $progressId.attr('name', originalName);
126 setTimeout(function () {
127 $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown();
130 openInNewWindow: function openInNewWindow(event) {
131 event.preventDefault();
132 $(this).attr('target', '_blank');
133 window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');