/***@文件*为托管文件字段类型提供JavaScript添加。**此文件提供进度条支持(如果可用),弹出窗口用于*文件预览,以及在Ajax上传期间禁用其他文件字段(这*防止单独的文件字段意外上传文件)。*/(函数($){/***将行为附加到托管文件元素上载字段。*/Drupal.behaviors.fileValidateAutoAttach={附加:函数(上下文,设置){if(settings.file&&settings.file.elements){$.each(settings.file.elements,函数(选择器){var扩展名=settings.file.elements[选择器];$(选择器,上下文).bind('change',{extensions:extensions},Drupal.file.validateExtension);});}},分离:函数(上下文,设置){if(settings.file和settings.file.elements){$.each(settings.file.elements,函数(选择器){$(选择器,上下文).unbind('change',Drupal.file.validateExtension);});}}};/***将行为附加到文件上载和删除按钮。*/Drupal.behaviors.fileButtons=药物.行为.文件按钮={附加:函数(上下文){$('input.form-submit',context).bind('mousedown',Drupal.file.disableFields);$('div.form-managed-file-input.form-submit',context).bind('mousedown',Drupal.file.progressBar);},分离:函数(上下文){$('input.form-submit',context).unbind('mousedown',Drupal.file.disableFields);$('div.form-managed-file-input.form-submit',context).unbind('mousedown',Drupal.file.progressBar);}};/***将行为附加到托管文件元素中的链接。*/Drupal.behaviors.filePreviewLinks={附加:函数(上下文){$('div.form-managed-file.file a,.file-widget.file a',context).bind('click',Drupal.file.openInNewWindow);},分离:函数(上下文){$('div.form-managed-file.file a,.file-widget.file a',context).unbind('click',Drupal.file.openInNewWindow);}};/***文件上传实用程序功能。*/Drupal.file=Drupal.文件||{/***文件扩展名的客户端文件输入验证。*/validateExtension:函数(事件){//删除以前的所有错误。$('.file-upload-js-error').remove();//为输入[type=file]添加客户端验证。var extensionPattern=事件.data.extensions.replace(/,\s*/g,'|');if(extensionPattern.length>1&&this.value.length>0){var acceptableMatch=新RegExp('\\.('+extensionPattern+')$','gi');if(!可接受Match.test(this.value)){var error=Drupal.t(“无法上载所选文件%filename。只允许具有以下扩展名的文件:%extensions。”{//根据HTML5的规范,一个文件上传控件//不应显示用户//已选择。一些web浏览器通过以下方式实现此限制//将本地路径替换为“C:\fakepath”,这可能会导致//让用户认为Drupal可能无法//找到该文件,因为它弄乱了文件路径。为了避免这种情况//因此,我们去掉了伪fakepath字符串。“%filename”:this.value.replace(“C:\\fakepath\\”,“”),“%extensions”:extensionPattern.replace(/\|/g,',')});$(this).closest('div.form-managed-file').prepend('
“+错误+”
');this.value=“”;返回false;}}},/***使用不打算上传的按钮时,防止文件上传。*/disableFields:函数(事件){var clickedButton=此;//仅禁用Ajax按钮的上传字段。if(!$(clickedButton).hasClass('jax-processed')){回报;}//检查我们是否正在使用“上传”按钮。var$enabledFields=[];if($(this).closest('div.form-managed-file').length>0){$enabledFields=$(this).closest('div.form-managed-file').find('input.form-file');}//暂时禁用当前以外的上传字段//与一起工作。筛选出已禁用的字段,以便//当我们在行为结束时重新启用这些字段时,请不要启用//处理。将setTimeout设置为相对较短的量时可重新启用//时间(1秒)。所有其他mousedown处理程序(如Drupal的Ajax//行为)在调用任何超时函数之前执行,因此我们//不用担心这些领域很快就会重新启用。//@todo如果前一句是真的,为什么不将超时设置为0?var$fieldsToTemporarilyDisable=$('div.form-managed-file input.form-file').not($enabledFields).not'(':disabled');$fieldsToTemporarilyDisable.attr(“禁用”,“禁用”);setTimeout(函数(){$fieldsToTemporarilyDisable.attr('disabled',false);}, 1000);},/***如果可能,添加进度条支持。*/progressBar:函数(事件){var clickedButton=此;var$progressId=$(clickedButton).closest('div.form-managed-file').find('input.file-progress');if($progressId.length){var originalName=$progressId.attr('name');//用所需的标识符替换名称。$progressId.attr('name',originalName.match(/APC_UPLOAD_PROGRESS|UPLOAD_IDENTIFIER/)[0]);//上传开始后恢复原始名称。setTimeout(函数(){$progressId.attr('name',originalName);}, 1000);}//如果上传时间超过半秒,则显示进度条。setTimeout(函数(){$(clickedButton).closest('div.form-managed-file').find('div-ajax-progress-bar').slideDown();}, 500);},/***在新窗口中打开表单中文件的链接。*/openInNewWindow:函数(事件){$(this).attr('target','_blank');window.open(this.href,'filePreview','toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=500,height=550');返回false;}};})(jQuery);