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