Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / file / file.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal) {
9   Drupal.behaviors.fileValidateAutoAttach = {
10     attach: function attach(context, settings) {
11       var $context = $(context);
12       var elements = void 0;
13
14       function initFileValidation(selector) {
15         $context.find(selector).once('fileValidate').on('change.fileValidate', { extensions: elements[selector] }, Drupal.file.validateExtension);
16       }
17
18       if (settings.file && settings.file.elements) {
19         elements = settings.file.elements;
20         Object.keys(elements).forEach(initFileValidation);
21       }
22     },
23     detach: function detach(context, settings, trigger) {
24       var $context = $(context);
25       var elements = void 0;
26
27       function removeFileValidation(selector) {
28         $context.find(selector).removeOnce('fileValidate').off('change.fileValidate', Drupal.file.validateExtension);
29       }
30
31       if (trigger === 'unload' && settings.file && settings.file.elements) {
32         elements = settings.file.elements;
33         Object.keys(elements).forEach(removeFileValidation);
34       }
35     }
36   };
37
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);
41     },
42     detach: function detach(context, setting, trigger) {
43       if (trigger === 'unload') {
44         $(context).find('input[type="file"]').removeOnce('auto-file-upload').off('.autoFileUpload');
45       }
46     }
47   };
48
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);
54     },
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);
59     }
60   };
61
62   Drupal.behaviors.filePreviewLinks = {
63     attach: function attach(context) {
64       $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
65     },
66     detach: function detach(context) {
67       $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
68     }
69   };
70
71   Drupal.file = Drupal.file || {
72     validateExtension: function validateExtension(event) {
73       event.preventDefault();
74
75       $('.file-upload-js-error').remove();
76
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, ', ')
84           });
85           $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
86           this.value = '';
87
88           event.stopImmediatePropagation();
89         }
90       }
91     },
92     triggerUploadButton: function triggerUploadButton(event) {
93       $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
94     },
95     disableFields: function disableFields(event) {
96       var $clickedButton = $(this).findOnce('ajax');
97
98       if (!$clickedButton.length) {
99         return;
100       }
101
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');
105       }
106
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);
111       }, 1000);
112     },
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');
118
119         $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
120
121         setTimeout(function () {
122           $progressId.attr('name', originalName);
123         }, 1000);
124       }
125
126       setTimeout(function () {
127         $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown();
128       }, 500);
129     },
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');
134     }
135   };
136 })(jQuery, Drupal);