annotate core/modules/file/file.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal) {
Chris@0 9 Drupal.behaviors.fileValidateAutoAttach = {
Chris@0 10 attach: function attach(context, settings) {
Chris@0 11 var $context = $(context);
Chris@0 12 var elements = void 0;
Chris@0 13
Chris@0 14 function initFileValidation(selector) {
Chris@0 15 $context.find(selector).once('fileValidate').on('change.fileValidate', { extensions: elements[selector] }, Drupal.file.validateExtension);
Chris@0 16 }
Chris@0 17
Chris@0 18 if (settings.file && settings.file.elements) {
Chris@0 19 elements = settings.file.elements;
Chris@0 20 Object.keys(elements).forEach(initFileValidation);
Chris@0 21 }
Chris@0 22 },
Chris@0 23 detach: function detach(context, settings, trigger) {
Chris@0 24 var $context = $(context);
Chris@0 25 var elements = void 0;
Chris@0 26
Chris@0 27 function removeFileValidation(selector) {
Chris@0 28 $context.find(selector).removeOnce('fileValidate').off('change.fileValidate', Drupal.file.validateExtension);
Chris@0 29 }
Chris@0 30
Chris@0 31 if (trigger === 'unload' && settings.file && settings.file.elements) {
Chris@0 32 elements = settings.file.elements;
Chris@0 33 Object.keys(elements).forEach(removeFileValidation);
Chris@0 34 }
Chris@0 35 }
Chris@0 36 };
Chris@0 37
Chris@0 38 Drupal.behaviors.fileAutoUpload = {
Chris@0 39 attach: function attach(context) {
Chris@0 40 $(context).find('input[type="file"]').once('auto-file-upload').on('change.autoFileUpload', Drupal.file.triggerUploadButton);
Chris@0 41 },
Chris@17 42 detach: function detach(context, settings, trigger) {
Chris@0 43 if (trigger === 'unload') {
Chris@0 44 $(context).find('input[type="file"]').removeOnce('auto-file-upload').off('.autoFileUpload');
Chris@0 45 }
Chris@0 46 }
Chris@0 47 };
Chris@0 48
Chris@0 49 Drupal.behaviors.fileButtons = {
Chris@0 50 attach: function attach(context) {
Chris@0 51 var $context = $(context);
Chris@0 52 $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields);
Chris@0 53 $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar);
Chris@0 54 },
Chris@17 55 detach: function detach(context, settings, trigger) {
Chris@17 56 if (trigger === 'unload') {
Chris@17 57 var $context = $(context);
Chris@17 58 $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields);
Chris@17 59 $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar);
Chris@17 60 }
Chris@0 61 }
Chris@0 62 };
Chris@0 63
Chris@0 64 Drupal.behaviors.filePreviewLinks = {
Chris@0 65 attach: function attach(context) {
Chris@0 66 $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
Chris@0 67 },
Chris@0 68 detach: function detach(context) {
Chris@0 69 $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
Chris@0 70 }
Chris@0 71 };
Chris@0 72
Chris@0 73 Drupal.file = Drupal.file || {
Chris@0 74 validateExtension: function validateExtension(event) {
Chris@0 75 event.preventDefault();
Chris@0 76
Chris@0 77 $('.file-upload-js-error').remove();
Chris@0 78
Chris@0 79 var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
Chris@0 80 if (extensionPattern.length > 1 && this.value.length > 0) {
Chris@0 81 var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
Chris@0 82 if (!acceptableMatch.test(this.value)) {
Chris@0 83 var error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', {
Chris@0 84 '%filename': this.value.replace('C:\\fakepath\\', ''),
Chris@0 85 '%extensions': extensionPattern.replace(/\|/g, ', ')
Chris@0 86 });
Chris@0 87 $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
Chris@0 88 this.value = '';
Chris@0 89
Chris@0 90 event.stopImmediatePropagation();
Chris@0 91 }
Chris@0 92 }
Chris@0 93 },
Chris@0 94 triggerUploadButton: function triggerUploadButton(event) {
Chris@0 95 $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
Chris@0 96 },
Chris@0 97 disableFields: function disableFields(event) {
Chris@16 98 var $clickedButton = $(this);
Chris@17 99 $clickedButton.trigger('formUpdated');
Chris@0 100
Chris@0 101 var $enabledFields = [];
Chris@0 102 if ($clickedButton.closest('div.js-form-managed-file').length > 0) {
Chris@0 103 $enabledFields = $clickedButton.closest('div.js-form-managed-file').find('input.js-form-file');
Chris@0 104 }
Chris@0 105
Chris@0 106 var $fieldsToTemporarilyDisable = $('div.js-form-managed-file input.js-form-file').not($enabledFields).not(':disabled');
Chris@0 107 $fieldsToTemporarilyDisable.prop('disabled', true);
Chris@0 108 setTimeout(function () {
Chris@0 109 $fieldsToTemporarilyDisable.prop('disabled', false);
Chris@0 110 }, 1000);
Chris@0 111 },
Chris@0 112 progressBar: function progressBar(event) {
Chris@0 113 var $clickedButton = $(this);
Chris@0 114 var $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress');
Chris@0 115 if ($progressId.length) {
Chris@0 116 var originalName = $progressId.attr('name');
Chris@0 117
Chris@0 118 $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);
Chris@0 119
Chris@0 120 setTimeout(function () {
Chris@0 121 $progressId.attr('name', originalName);
Chris@0 122 }, 1000);
Chris@0 123 }
Chris@0 124
Chris@0 125 setTimeout(function () {
Chris@0 126 $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown();
Chris@0 127 }, 500);
Chris@17 128 $clickedButton.trigger('fileUpload');
Chris@0 129 },
Chris@0 130 openInNewWindow: function openInNewWindow(event) {
Chris@0 131 event.preventDefault();
Chris@0 132 $(this).attr('target', '_blank');
Chris@0 133 window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');
Chris@0 134 }
Chris@0 135 };
Chris@0 136 })(jQuery, Drupal);