1 | |
---|
2 | /** |
---|
3 | * Auto-attach standard client side file input validation. |
---|
4 | */ |
---|
5 | Drupal.behaviors.filefieldValidateAutoAttach = function(context) { |
---|
6 | $("input[type=file]", context).bind('change', Drupal.filefield.validateExtensions); |
---|
7 | }; |
---|
8 | |
---|
9 | |
---|
10 | /** |
---|
11 | * Prevent FileField uploads when using buttons not intended to upload. |
---|
12 | */ |
---|
13 | Drupal.behaviors.filefieldButtons = function(context) { |
---|
14 | $('input.form-submit', context).bind('mousedown', Drupal.filefield.disableFields); |
---|
15 | $('div.filefield-element input.form-submit', context).bind('mousedown', Drupal.filefield.progressBar); |
---|
16 | }; |
---|
17 | |
---|
18 | /** |
---|
19 | * Open links to files within the node form in a new window. |
---|
20 | */ |
---|
21 | Drupal.behaviors.filefieldPreviewLinks = function(context) { |
---|
22 | $('div.filefield-element div.widget-preview a', context).click(Drupal.filefield.openInNewWindow).attr('target', '_blank'); |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Admin enhancement: only show the "Files listed by default" when needed. |
---|
27 | */ |
---|
28 | Drupal.behaviors.filefieldAdmin = function(context) { |
---|
29 | var $listField = $('div.filefield-list-field', context); |
---|
30 | if ($listField.size()) { |
---|
31 | $listField.find('input').change(function() { |
---|
32 | if (this.checked) { |
---|
33 | if (this.value == 0) { |
---|
34 | $('#edit-list-default-wrapper').css('display', 'none'); |
---|
35 | } |
---|
36 | else { |
---|
37 | $('#edit-list-default-wrapper').css('display', 'block'); |
---|
38 | } |
---|
39 | } |
---|
40 | }).change(); |
---|
41 | } |
---|
42 | }; |
---|
43 | |
---|
44 | /** |
---|
45 | * Utility functions for use by FileField. |
---|
46 | * @param {Object} event |
---|
47 | */ |
---|
48 | Drupal.filefield = { |
---|
49 | validateExtensions: function(event) { |
---|
50 | // Remove any previous errors. |
---|
51 | $('.file-upload-js-error').remove(); |
---|
52 | |
---|
53 | var fieldName = this.name.replace(/^files\[([a-z0-9_]+)_\d+\]$/, '$1'); |
---|
54 | var extensions = ''; |
---|
55 | if (Drupal.settings.filefield && Drupal.settings.filefield[fieldName]) { |
---|
56 | extensions = Drupal.settings.filefield[fieldName].replace(/[, ]+/g, '|'); |
---|
57 | } |
---|
58 | if (extensions.length > 1 && this.value.length > 0) { |
---|
59 | var extensionPattern = new RegExp('\\.(' + extensions + ')$', 'gi'); |
---|
60 | if (!extensionPattern.test(this.value)) { |
---|
61 | var error = Drupal.t("The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.", |
---|
62 | { '%filename' : this.value, '%extensions' : extensions.replace(/\|/g, ', ') } |
---|
63 | ); |
---|
64 | $(this).parent().before('<div class="messages error file-upload-js-error">' + error + '</div>'); |
---|
65 | this.value = ''; |
---|
66 | return false; |
---|
67 | } |
---|
68 | } |
---|
69 | }, |
---|
70 | disableFields: function(event) { |
---|
71 | var clickedButton = this; |
---|
72 | |
---|
73 | // Only disable upload fields for AHAH buttons. |
---|
74 | if (!$(clickedButton).hasClass('ahah-processed')) { |
---|
75 | return; |
---|
76 | } |
---|
77 | |
---|
78 | // Check if we're working with an "Upload" button. |
---|
79 | var $enabledFields = []; |
---|
80 | if ($(this).parents('div.filefield-element').size() > 0) { |
---|
81 | $enabledFields = $(this).parents('div.filefield-element').find('input.form-file'); |
---|
82 | } |
---|
83 | // Otherwise we're probably dealing with CCK's "Add another item" button. |
---|
84 | else if ($(this).parents('div.content-add-more').size() > 0) { |
---|
85 | $enabledFields = $(this).parent().parent().find('input.form-file'); |
---|
86 | } |
---|
87 | |
---|
88 | var $disabledFields = $('div.filefield-element input.form-file').not($enabledFields); |
---|
89 | |
---|
90 | // Disable upload fields other than the one we're currently working with. |
---|
91 | $disabledFields.attr('disabled', 'disabled'); |
---|
92 | |
---|
93 | // All the other mousedown handlers (like AHAH) are excuted before any |
---|
94 | // timeout functions will be called, so this effectively re-enables |
---|
95 | // the filefields after the AHAH process is complete even though it only |
---|
96 | // has a 1 millisecond timeout. |
---|
97 | setTimeout(function(){ |
---|
98 | $disabledFields.removeAttr('disabled'); |
---|
99 | }, 1000); |
---|
100 | }, |
---|
101 | progressBar: function(event) { |
---|
102 | var clickedButton = this; |
---|
103 | var $progressId = $(clickedButton).parents('div.filefield-element').find('input.filefield-progress'); |
---|
104 | if ($progressId.size()) { |
---|
105 | var originalName = $progressId.attr('name'); |
---|
106 | |
---|
107 | // Replace the name with the required identifier. |
---|
108 | $progressId.attr('name', originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]); |
---|
109 | |
---|
110 | // Restore the original name after the upload begins. |
---|
111 | setTimeout(function() { |
---|
112 | $progressId.attr('name', originalName); |
---|
113 | }, 1000); |
---|
114 | } |
---|
115 | |
---|
116 | // Show the progress bar if the upload takes longer than 3 seconds. |
---|
117 | setTimeout(function() { |
---|
118 | $(clickedButton).parents('div.filefield-element').find('div.ahah-progress-bar').slideDown(); |
---|
119 | }, 500); |
---|
120 | }, |
---|
121 | openInNewWindow: function(event) { |
---|
122 | window.open(this.href, 'filefieldPreview', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550'); |
---|
123 | return false; |
---|
124 | } |
---|
125 | }; |
---|