2015-08-17 17:00:26 -07:00
/ * *
2018-11-23 12:29:20 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
* https : //www.drupal.org/node/2815083
* @ preserve
* * /
2015-08-17 17:00:26 -07:00
( function ( $ , Drupal ) {
Drupal . behaviors . fileValidateAutoAttach = {
2018-11-23 12:29:20 +00:00
attach : function attach ( context , settings ) {
2015-08-17 17:00:26 -07:00
var $context = $ ( context ) ;
2018-11-23 12:29:20 +00:00
var elements = void 0 ;
2015-08-17 17:00:26 -07:00
function initFileValidation ( selector ) {
2018-11-23 12:29:20 +00:00
$context . find ( selector ) . once ( 'fileValidate' ) . on ( 'change.fileValidate' , { extensions : elements [ selector ] } , Drupal . file . validateExtension ) ;
2015-08-17 17:00:26 -07:00
}
if ( settings . file && settings . file . elements ) {
elements = settings . file . elements ;
Object . keys ( elements ) . forEach ( initFileValidation ) ;
}
} ,
2018-11-23 12:29:20 +00:00
detach : function detach ( context , settings , trigger ) {
2015-08-17 17:00:26 -07:00
var $context = $ ( context ) ;
2018-11-23 12:29:20 +00:00
var elements = void 0 ;
2015-08-17 17:00:26 -07:00
function removeFileValidation ( selector ) {
2018-11-23 12:29:20 +00:00
$context . find ( selector ) . removeOnce ( 'fileValidate' ) . off ( 'change.fileValidate' , Drupal . file . validateExtension ) ;
2015-08-17 17:00:26 -07:00
}
if ( trigger === 'unload' && settings . file && settings . file . elements ) {
elements = settings . file . elements ;
Object . keys ( elements ) . forEach ( removeFileValidation ) ;
}
}
} ;
Drupal . behaviors . fileAutoUpload = {
2018-11-23 12:29:20 +00:00
attach : function attach ( context ) {
2015-08-17 17:00:26 -07:00
$ ( context ) . find ( 'input[type="file"]' ) . once ( 'auto-file-upload' ) . on ( 'change.autoFileUpload' , Drupal . file . triggerUploadButton ) ;
} ,
2018-11-23 12:29:20 +00:00
detach : function detach ( context , settings , trigger ) {
2015-08-17 17:00:26 -07:00
if ( trigger === 'unload' ) {
$ ( context ) . find ( 'input[type="file"]' ) . removeOnce ( 'auto-file-upload' ) . off ( '.autoFileUpload' ) ;
}
}
} ;
Drupal . behaviors . fileButtons = {
2018-11-23 12:29:20 +00:00
attach : function attach ( context ) {
2015-08-17 17:00:26 -07:00
var $context = $ ( context ) ;
$context . find ( '.js-form-submit' ) . on ( 'mousedown' , Drupal . file . disableFields ) ;
$context . find ( '.js-form-managed-file .js-form-submit' ) . on ( 'mousedown' , Drupal . file . progressBar ) ;
} ,
2018-11-23 12:29:20 +00:00
detach : function detach ( context , settings , trigger ) {
if ( trigger === 'unload' ) {
var $context = $ ( context ) ;
$context . find ( '.js-form-submit' ) . off ( 'mousedown' , Drupal . file . disableFields ) ;
$context . find ( '.js-form-managed-file .js-form-submit' ) . off ( 'mousedown' , Drupal . file . progressBar ) ;
}
2015-08-17 17:00:26 -07:00
}
} ;
Drupal . behaviors . filePreviewLinks = {
2018-11-23 12:29:20 +00:00
attach : function attach ( context ) {
2015-11-17 13:42:33 -08:00
$ ( context ) . find ( 'div.js-form-managed-file .file a' ) . on ( 'click' , Drupal . file . openInNewWindow ) ;
2015-08-17 17:00:26 -07:00
} ,
2018-11-23 12:29:20 +00:00
detach : function detach ( context ) {
2015-11-17 13:42:33 -08:00
$ ( context ) . find ( 'div.js-form-managed-file .file a' ) . off ( 'click' , Drupal . file . openInNewWindow ) ;
2015-08-17 17:00:26 -07:00
}
} ;
Drupal . file = Drupal . file || {
2018-11-23 12:29:20 +00:00
validateExtension : function validateExtension ( event ) {
2015-08-17 17:00:26 -07:00
event . preventDefault ( ) ;
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
$ ( '.file-upload-js-error' ) . remove ( ) ;
var extensionPattern = event . data . extensions . replace ( /,\s*/g , '|' ) ;
if ( extensionPattern . length > 1 && this . value . length > 0 ) {
var acceptableMatch = new RegExp ( '\\.(' + extensionPattern + ')$' , 'gi' ) ;
if ( ! acceptableMatch . test ( this . value ) ) {
2015-10-21 21:44:50 -07:00
var error = Drupal . t ( 'The selected file %filename cannot be uploaded. Only files with the following extensions are allowed: %extensions.' , {
2015-08-17 17:00:26 -07:00
'%filename' : this . value . replace ( 'C:\\fakepath\\' , '' ) ,
'%extensions' : extensionPattern . replace ( /\|/g , ', ' )
} ) ;
$ ( this ) . closest ( 'div.js-form-managed-file' ) . prepend ( '<div class="messages messages--error file-upload-js-error" aria-live="polite">' + error + '</div>' ) ;
this . value = '' ;
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
event . stopImmediatePropagation ( ) ;
}
}
} ,
2018-11-23 12:29:20 +00:00
triggerUploadButton : function triggerUploadButton ( event ) {
2015-08-17 17:00:26 -07:00
$ ( event . target ) . closest ( '.js-form-managed-file' ) . find ( '.js-form-submit' ) . trigger ( 'mousedown' ) ;
} ,
2018-11-23 12:29:20 +00:00
disableFields : function disableFields ( event ) {
var $clickedButton = $ ( this ) ;
$clickedButton . trigger ( 'formUpdated' ) ;
2015-08-17 17:00:26 -07:00
var $enabledFields = [ ] ;
if ( $clickedButton . closest ( 'div.js-form-managed-file' ) . length > 0 ) {
$enabledFields = $clickedButton . closest ( 'div.js-form-managed-file' ) . find ( 'input.js-form-file' ) ;
}
var $fieldsToTemporarilyDisable = $ ( 'div.js-form-managed-file input.js-form-file' ) . not ( $enabledFields ) . not ( ':disabled' ) ;
$fieldsToTemporarilyDisable . prop ( 'disabled' , true ) ;
setTimeout ( function ( ) {
$fieldsToTemporarilyDisable . prop ( 'disabled' , false ) ;
} , 1000 ) ;
} ,
2018-11-23 12:29:20 +00:00
progressBar : function progressBar ( event ) {
2015-08-17 17:00:26 -07:00
var $clickedButton = $ ( this ) ;
var $progressId = $clickedButton . closest ( 'div.js-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 ( function ( ) {
$progressId . attr ( 'name' , originalName ) ;
} , 1000 ) ;
}
2018-11-23 12:29:20 +00:00
2015-08-17 17:00:26 -07:00
setTimeout ( function ( ) {
$clickedButton . closest ( 'div.js-form-managed-file' ) . find ( 'div.ajax-progress-bar' ) . slideDown ( ) ;
} , 500 ) ;
2018-11-23 12:29:20 +00:00
$clickedButton . trigger ( 'fileUpload' ) ;
2015-08-17 17:00:26 -07:00
} ,
2018-11-23 12:29:20 +00:00
openInNewWindow : function openInNewWindow ( event ) {
2015-08-17 17:00:26 -07:00
event . preventDefault ( ) ;
$ ( 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' ) ;
}
} ;
2018-11-23 12:29:20 +00:00
} ) ( jQuery , Drupal ) ;