Pull merge.
[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, settings, 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, settings, trigger) {
56       if (trigger === 'unload') {
57         var $context = $(context);
58         $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields);
59         $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar);
60       }
61     }
62   };
63
64   Drupal.behaviors.filePreviewLinks = {
65     attach: function attach(context) {
66       $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
67     },
68     detach: function detach(context) {
69       $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
70     }
71   };
72
73   Drupal.file = Drupal.file || {
74     validateExtension: function validateExtension(event) {
75       event.preventDefault();
76
77       $('.file-upload-js-error').remove();
78
79       var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
80       if (extensionPattern.length > 1 && this.value.length > 0) {
81         var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
82         if (!acceptableMatch.test(this.value)) {
83           var error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', {
84             '%filename': this.value.replace('C:\\fakepath\\', ''),
85             '%extensions': extensionPattern.replace(/\|/g, ', ')
86           });
87           $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
88           this.value = '';
89
90           event.stopImmediatePropagation();
91         }
92       }
93     },
94     triggerUploadButton: function triggerUploadButton(event) {
95       $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
96     },
97     disableFields: function disableFields(event) {
98       var $clickedButton = $(this);
99       $clickedButton.trigger('formUpdated');
100
101       var $enabledFields = [];
102       if ($clickedButton.closest('div.js-form-managed-file').length > 0) {
103         $enabledFields = $clickedButton.closest('div.js-form-managed-file').find('input.js-form-file');
104       }
105
106       var $fieldsToTemporarilyDisable = $('div.js-form-managed-file input.js-form-file').not($enabledFields).not(':disabled');
107       $fieldsToTemporarilyDisable.prop('disabled', true);
108       setTimeout(function () {
109         $fieldsToTemporarilyDisable.prop('disabled', false);
110       }, 1000);
111     },
112     progressBar: function progressBar(event) {
113       var $clickedButton = $(this);
114       var $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
115       if ($progressId.length) {
116         var originalName = $progressId.attr('name');
117
118         $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
119
120         setTimeout(function () {
121           $progressId.attr('name', originalName);
122         }, 1000);
123       }
124
125       setTimeout(function () {
126         $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown();
127       }, 500);
128       $clickedButton.trigger('fileUpload');
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);