root / drupal7 / sites / all / themes / bootstrap / js / modules / file / file.js @ 9525582e
1 | caf16a48 | Assos Assos | (function ($) { |
---|---|---|---|
2 | // Override core JS so it works with "button" tags.
|
||
3 | /**
|
||
4 | * Attach behaviors to the file upload and remove buttons.
|
||
5 | */
|
||
6 | Drupal.behaviors.fileButtons = { |
||
7 | attach: function (context) { |
||
8 | $(':input.form-submit', context).bind('mousedown', Drupal.file.disableFields); |
||
9 | $('div.form-managed-file :input.form-submit', context).bind('mousedown', Drupal.file.progressBar); |
||
10 | }, |
||
11 | detach: function (context) { |
||
12 | $(':input.form-submit', context).unbind('mousedown', Drupal.file.disableFields); |
||
13 | $('div.form-managed-file :input.form-submit', context).unbind('mousedown', Drupal.file.progressBar); |
||
14 | } |
||
15 | }; |
||
16 | if (Drupal.file) {
|
||
17 | /**
|
||
18 | * Prevent file uploads when using buttons not intended to upload.
|
||
19 | */
|
||
20 | Drupal.file.disableFields = function (event){ |
||
21 | var clickedButton = this; |
||
22 | |||
23 | // Only disable upload fields for Ajax buttons.
|
||
24 | if (!$(clickedButton).hasClass('ajax-processed')) { |
||
25 | return;
|
||
26 | } |
||
27 | |||
28 | // Check if we're working with an "Upload" button.
|
||
29 | var $enabledFields = []; |
||
30 | if ($(this).closest('div.form-managed-file').length > 0) { |
||
31 | $enabledFields = $(this).closest('div.form-managed-file').find(':input.form-file'); |
||
32 | } |
||
33 | |||
34 | // Temporarily disable upload fields other than the one we're currently
|
||
35 | // working with. Filter out fields that are already disabled so that they
|
||
36 | // do not get enabled when we re-enable these fields at the end of behavior
|
||
37 | // processing. Re-enable in a setTimeout set to a relatively short amount
|
||
38 | // of time (1 second). All the other mousedown handlers (like Drupal's Ajax
|
||
39 | // behaviors) are excuted before any timeout functions are called, so we
|
||
40 | // don't have to worry about the fields being re-enabled too soon.
|
||
41 | // @todo If the previous sentence is true, why not set the timeout to 0?
|
||
42 | var $fieldsToTemporarilyDisable = $('div.form-managed-file :input.form-file').not($enabledFields).not(':disabled'); |
||
43 | $fieldsToTemporarilyDisable.attr('disabled', 'disabled'); |
||
44 | setTimeout(function (){
|
||
45 | $fieldsToTemporarilyDisable.attr('disabled', false); |
||
46 | }, 1000);
|
||
47 | }; |
||
48 | /**
|
||
49 | * Add progress bar support if possible.
|
||
50 | */
|
||
51 | Drupal.file.progressBar = function (event) { |
||
52 | var clickedButton = this; |
||
53 | var $progressId = $(clickedButton).closest('div.form-managed-file').find(':input.file-progress'); |
||
54 | if ($progressId.length) { |
||
55 | var originalName = $progressId.attr('name'); |
||
56 | |||
57 | // Replace the name with the required identifier.
|
||
58 | $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]); |
||
59 | |||
60 | // Restore the original name after the upload begins.
|
||
61 | setTimeout(function () {
|
||
62 | $progressId.attr('name', originalName); |
||
63 | }, 1000);
|
||
64 | } |
||
65 | // Show the progress bar if the upload takes longer than half a second.
|
||
66 | setTimeout(function () {
|
||
67 | $(clickedButton).closest('div.form-managed-file').find('div.ajax-progress-bar').slideDown(); |
||
68 | }, 500);
|
||
69 | }; |
||
70 | |||
71 | /**
|
||
72 | * Styling invalid file extension error message (Issue #2331595 by NetTantra).
|
||
73 | */
|
||
74 | Drupal.file.validateExtension = function (event) { |
||
75 | // Remove any previous errors.
|
||
76 | $('.file-upload-js-error').remove(); |
||
77 | |||
78 | // Add client side validation for the input[type=file].
|
||
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 | // According to the specifications of HTML5, a file upload control
|
||
85 | // should not reveal the real local path to the file that a user
|
||
86 | // has selected. Some web browsers implement this restriction by
|
||
87 | // replacing the local path with "C:\fakepath\", which can cause
|
||
88 | // confusion by leaving the user thinking perhaps Drupal could not
|
||
89 | // find the file because it messed up the file path. To avoid this
|
||
90 | // confusion, therefore, we strip out the bogus fakepath string.
|
||
91 | '%filename': this.value.replace('C:\\fakepath\\', ''), |
||
92 | '%extensions': extensionPattern.replace(/\|/g, ', ') |
||
93 | }); |
||
94 | $(this).closest('div.form-managed-file').parents('.form-item').first().prepend('<div class="alert alert-danger alert-dismissible messages error file-upload-js-error" aria-live="polite" role="alert">\ |
||
95 | <button type="button" class="close" data-dismiss="alert">\
|
||
96 | <span aria-hidden="true">×</span>\
|
||
97 | <span class="sr-only">Close</span>\
|
||
98 | </button>' + error + '</div>'); |
||
99 | this.value = ''; |
||
100 | return false; |
||
101 | } |
||
102 | } |
||
103 | }; |
||
104 | } |
||
105 | })(jQuery); |