-
-
Save Crocoblock/b1e80427627144297c2dcc631bc818ac to your computer and use it in GitHub Desktop.
JetFormbuilder media field text after upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener( 'DOMContentLoaded', function() { | |
const { | |
addAction, | |
} = window.JetPlugins.hooks; | |
const textChoose = 'Choose File'; | |
const textEmpty = 'No file has been chosen'; | |
const textUpload = 'File Uploaded'; | |
function getFileNames( fileList ) { | |
if ( ! fileList.length ) { | |
return ''; | |
} | |
let names = []; | |
for ( const file of fileList ) { | |
if ( file.name ) { | |
names.push( file.name ); | |
} | |
} | |
return names.join( ', ' ); | |
} | |
addAction( | |
'jet.fb.input.makeReactive', | |
'random-index/on-observe', | |
function( input ) { | |
if ( input?.nodes?.length < 1 ) { | |
return; | |
} | |
if ( ! input.nodes[0].classList.contains( 'jet-form-builder-file-upload__input' ) ) { | |
return; | |
} | |
const $ = jQuery; | |
const upload = $( input.nodes[0] ); | |
const fields = $( input.nodes[0].closest( '.jet-form-builder-file-upload__fields' ) ); | |
fields.append(`<input type="button" class="addfile" value="${textChoose}"/>`); | |
fields.append(`<label class="labeladdfile">${textEmpty}</label></div>`); | |
const labelAdd = fields.find('.labeladdfile'); | |
const buttonAdd = fields.find('.addfile'); | |
$( input.nodes[0] ).css('display','none'); | |
buttonAdd.click(function () { | |
upload.trigger('click'); | |
}); | |
input.value.watch( function() { | |
if ( this.current.length ){ | |
buttonAdd.attr( 'value', textUpload ); | |
labelAdd.html( getFileNames( this.current ) || textUpload ); | |
} else { | |
labelAdd.html( textEmpty ); | |
buttonAdd.attr( 'value', textChoose ); | |
} | |
} ) | |
} | |
); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment