X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Ffile%2Ffile.es6.js;h=dc8a807424e33008fbef9bdd83108ba5e85cb1c4;hb=5b8bb166bfa98770daef9de5c127fc2e6ef02340;hp=9ab5dc2da356683eef570143d4a797d70bb4deda;hpb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;p=yaffs-website diff --git a/web/core/modules/file/file.es6.js b/web/core/modules/file/file.es6.js index 9ab5dc2da..dc8a80742 100644 --- a/web/core/modules/file/file.es6.js +++ b/web/core/modules/file/file.es6.js @@ -7,7 +7,7 @@ * prevents separate file fields from accidentally uploading files). */ -(function ($, Drupal) { +(function($, Drupal) { /** * Attach behaviors to the file fields passed in the settings. * @@ -24,9 +24,14 @@ let elements; function initFileValidation(selector) { - $context.find(selector) + $context + .find(selector) .once('fileValidate') - .on('change.fileValidate', { extensions: elements[selector] }, Drupal.file.validateExtension); + .on( + 'change.fileValidate', + { extensions: elements[selector] }, + Drupal.file.validateExtension, + ); } if (settings.file && settings.file.elements) { @@ -39,7 +44,8 @@ let elements; function removeFileValidation(selector) { - $context.find(selector) + $context + .find(selector) .removeOnce('fileValidate') .off('change.fileValidate', Drupal.file.validateExtension); } @@ -63,11 +69,17 @@ */ Drupal.behaviors.fileAutoUpload = { attach(context) { - $(context).find('input[type="file"]').once('auto-file-upload').on('change.autoFileUpload', Drupal.file.triggerUploadButton); + $(context) + .find('input[type="file"]') + .once('auto-file-upload') + .on('change.autoFileUpload', Drupal.file.triggerUploadButton); }, - detach(context, setting, trigger) { + detach(context, settings, trigger) { if (trigger === 'unload') { - $(context).find('input[type="file"]').removeOnce('auto-file-upload').off('.autoFileUpload'); + $(context) + .find('input[type="file"]') + .removeOnce('auto-file-upload') + .off('.autoFileUpload'); } }, }; @@ -85,13 +97,23 @@ Drupal.behaviors.fileButtons = { attach(context) { const $context = $(context); - $context.find('.js-form-submit').on('mousedown', Drupal.file.disableFields); - $context.find('.js-form-managed-file .js-form-submit').on('mousedown', Drupal.file.progressBar); + $context + .find('.js-form-submit') + .on('mousedown', Drupal.file.disableFields); + $context + .find('.js-form-managed-file .js-form-submit') + .on('mousedown', Drupal.file.progressBar); }, - detach(context) { - const $context = $(context); - $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields); - $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar); + detach(context, settings, trigger) { + if (trigger === 'unload') { + const $context = $(context); + $context + .find('.js-form-submit') + .off('mousedown', Drupal.file.disableFields); + $context + .find('.js-form-managed-file .js-form-submit') + .off('mousedown', Drupal.file.progressBar); + } }, }; @@ -107,10 +129,14 @@ */ Drupal.behaviors.filePreviewLinks = { attach(context) { - $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow); + $(context) + .find('div.js-form-managed-file .file a') + .on('click', Drupal.file.openInNewWindow); }, detach(context) { - $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow); + $(context) + .find('div.js-form-managed-file .file a') + .off('click', Drupal.file.openInNewWindow); }, }; @@ -120,7 +146,6 @@ * @namespace */ Drupal.file = Drupal.file || { - /** * Client-side file input validation of file extensions. * @@ -139,18 +164,25 @@ if (extensionPattern.length > 1 && this.value.length > 0) { const acceptableMatch = new RegExp(`\\.(${extensionPattern})$`, 'gi'); if (!acceptableMatch.test(this.value)) { - const error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', { - // According to the specifications of HTML5, a file upload control - // should not reveal the real local path to the file that a user - // has selected. Some web browsers implement this restriction by - // replacing the local path with "C:\fakepath\", which can cause - // confusion by leaving the user thinking perhaps Drupal could not - // find the file because it messed up the file path. To avoid this - // confusion, therefore, we strip out the bogus fakepath string. - '%filename': this.value.replace('C:\\fakepath\\', ''), - '%extensions': extensionPattern.replace(/\|/g, ', '), - }); - $(this).closest('div.js-form-managed-file').prepend(`
${error}
`); + const error = Drupal.t( + 'The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', + { + // According to the specifications of HTML5, a file upload control + // should not reveal the real local path to the file that a user + // has selected. Some web browsers implement this restriction by + // replacing the local path with "C:\fakepath\", which can cause + // confusion by leaving the user thinking perhaps Drupal could not + // find the file because it messed up the file path. To avoid this + // confusion, therefore, we strip out the bogus fakepath string. + '%filename': this.value.replace('C:\\fakepath\\', ''), + '%extensions': extensionPattern.replace(/\|/g, ', '), + }, + ); + $(this) + .closest('div.js-form-managed-file') + .prepend( + `
${error}
`, + ); this.value = ''; // Cancel all other change event handlers. event.stopImmediatePropagation(); @@ -167,7 +199,10 @@ * The event triggered. For example `change.autoFileUpload`. */ triggerUploadButton(event) { - $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown'); + $(event.target) + .closest('.js-form-managed-file') + .find('.js-form-submit') + .trigger('mousedown'); }, /** @@ -179,17 +214,15 @@ * The event triggered, most likely a `mousedown` event. */ disableFields(event) { - const $clickedButton = $(this).findOnce('ajax'); - - // Only disable upload fields for Ajax buttons. - if (!$clickedButton.length) { - return; - } + const $clickedButton = $(this); + $clickedButton.trigger('formUpdated'); // Check if we're working with an "Upload" button. let $enabledFields = []; if ($clickedButton.closest('div.js-form-managed-file').length > 0) { - $enabledFields = $clickedButton.closest('div.js-form-managed-file').find('input.js-form-file'); + $enabledFields = $clickedButton + .closest('div.js-form-managed-file') + .find('input.js-form-file'); } // Temporarily disable upload fields other than the one we're currently @@ -201,7 +234,11 @@ // functions are called, so we don't have to worry about the fields being // re-enabled too soon. @todo If the previous sentence is true, why not // set the timeout to 0? - const $fieldsToTemporarilyDisable = $('div.js-form-managed-file input.js-form-file').not($enabledFields).not(':disabled'); + const $fieldsToTemporarilyDisable = $( + 'div.js-form-managed-file input.js-form-file', + ) + .not($enabledFields) + .not(':disabled'); $fieldsToTemporarilyDisable.prop('disabled', true); setTimeout(() => { $fieldsToTemporarilyDisable.prop('disabled', false); @@ -218,12 +255,17 @@ */ progressBar(event) { const $clickedButton = $(this); - const $progressId = $clickedButton.closest('div.js-form-managed-file').find('input.file-progress'); + const $progressId = $clickedButton + .closest('div.js-form-managed-file') + .find('input.file-progress'); if ($progressId.length) { const originalName = $progressId.attr('name'); // Replace the name with the required identifier. - $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]); + $progressId.attr( + 'name', + originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0], + ); // Restore the original name after the upload begins. setTimeout(() => { @@ -232,8 +274,12 @@ } // Show the progress bar if the upload takes longer than half a second. setTimeout(() => { - $clickedButton.closest('div.js-form-managed-file').find('div.ajax-progress-bar').slideDown(); + $clickedButton + .closest('div.js-form-managed-file') + .find('div.ajax-progress-bar') + .slideDown(); }, 500); + $clickedButton.trigger('fileUpload'); }, /** @@ -247,7 +293,11 @@ openInNewWindow(event) { event.preventDefault(); $(this).attr('target', '_blank'); - window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550'); + window.open( + this.href, + 'filePreview', + 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550', + ); }, }; -}(jQuery, Drupal)); +})(jQuery, Drupal);