file.js 5.3 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, settings, 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, settings, trigger) {
  49. if (trigger === 'unload') {
  50. var $context = $(context);
  51. $context.find('.js-form-submit').off('mousedown', Drupal.file.disableFields);
  52. $context.find('.js-form-managed-file .js-form-submit').off('mousedown', Drupal.file.progressBar);
  53. }
  54. }
  55. };
  56. Drupal.behaviors.filePreviewLinks = {
  57. attach: function attach(context) {
  58. $(context).find('div.js-form-managed-file .file a').on('click', Drupal.file.openInNewWindow);
  59. },
  60. detach: function detach(context) {
  61. $(context).find('div.js-form-managed-file .file a').off('click', Drupal.file.openInNewWindow);
  62. }
  63. };
  64. Drupal.file = Drupal.file || {
  65. validateExtension: function validateExtension(event) {
  66. event.preventDefault();
  67. $('.file-upload-js-error').remove();
  68. var extensionPattern = event.data.extensions.replace(/,\s*/g, '|');
  69. if (extensionPattern.length > 1 && this.value.length > 0) {
  70. var acceptableMatch = new RegExp('\\.(' + extensionPattern + ')$', 'gi');
  71. if (!acceptableMatch.test(this.value)) {
  72. var error = Drupal.t('The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.', {
  73. '%filename': this.value.replace('C:\\fakepath\\', ''),
  74. '%extensions': extensionPattern.replace(/\|/g, ', ')
  75. });
  76. $(this).closest('div.js-form-managed-file').prepend('<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>');
  77. this.value = '';
  78. event.stopImmediatePropagation();
  79. }
  80. }
  81. },
  82. triggerUploadButton: function triggerUploadButton(event) {
  83. $(event.target).closest('.js-form-managed-file').find('.js-form-submit').trigger('mousedown');
  84. },
  85. disableFields: function disableFields(event) {
  86. var $clickedButton = $(this);
  87. $clickedButton.trigger('formUpdated');
  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. $clickedButton.trigger('fileUpload');
  112. },
  113. openInNewWindow: function openInNewWindow(event) {
  114. event.preventDefault();
  115. $(this).attr('target', '_blank');
  116. window.open(this.href, 'filePreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');
  117. }
  118. };
  119. })(jQuery, Drupal);