file.js 5.2 KB

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