Last active
February 2, 2021 10:49
-
-
Save sebastian-fahrenkrog/2222b039bcbf8bb33bd79d6b666de590 to your computer and use it in GitHub Desktop.
Multi File Upload Mautic form in a elementor modal
This file contains 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
<script> | |
(function (global) { | |
var formId = 11; | |
var elementorPopupId = 6513; | |
var mauticFormBaseUrl = 'mautic.example.de'; | |
function initFileUpload() { | |
var fileButton = '.mauticform_wrapper button#file_select'; | |
var fileSelector = '.mauticform_wrapper input[type=\'file\']'; | |
var fileName = ''; | |
jQuery(document).off().on('click', fileButton, function (e) { | |
e.preventDefault(); | |
var foundEmpty = false; | |
inputFileFields = jQuery(fileSelector); | |
var i; | |
for (i = 0; i < inputFileFields.length; i++) { | |
if (foundEmpty) { | |
continue; | |
} | |
fileField = jQuery(inputFileFields[i]); | |
if (fileField.length == 0) { | |
continue; | |
} | |
var fileName = fileField.val(); | |
if (!fileName) { | |
fileField.trigger('click'); | |
foundEmpty = true; | |
} | |
} | |
}); | |
jQuery(document).on('change', fileSelector, function (e) { | |
//get filename form input field | |
var fileName = jQuery(this).val().replace(/C:\\fakepath\\/i, ''); | |
//get input file field name | |
var fieldName = jQuery(this).attr('name'); | |
//add new entry to file list | |
jQuery('.files-list').append("<div class='attached-file' data-filefield='" + fieldName + "'><div class='file-name col-md-7 text-right'>" + fileName + "</div><div class='remove col-md-4 text-left'><a href='#'>✖</a></div></div>"); | |
}); | |
jQuery(document).on('click', '.remove a', function (e) { | |
e.preventDefault(); | |
//get file field name | |
var selectedFileInputFieldSel = jQuery(e.target).parent().parent().parent().data('filefield'); | |
//remove file list entry | |
jQuery(e.target).parent().parent().parent().remove(); | |
//reset file field | |
jQuery('input[name="' + selectedFileInputFieldSel + '"]').val(''); | |
}); | |
} | |
function load() { | |
if (!global.jQuery) return setTimeout(load, 50); | |
//your synchronous or asynchronous jQuery-related code | |
global.jQuery(global).on('elementor/frontend/init', function () { | |
elementorFrontend.elements.$document.on('elementor/popup/show', function (event, id) { | |
if (id != elementorPopupId) { | |
return; | |
} | |
// process the show event | |
var script = document.createElement('script'); | |
script.src = '//' + mauticFormBaseUrl + '/form/generate.js?id=' + formId; | |
var arrScripts = document.getElementsByTagName('script'); | |
var s = arrScripts[arrScripts.length - 1]; | |
s.parentNode.insertBefore(script, s); | |
script.onload = function () { | |
if (typeof (MauticSDK) !== 'undefined') { | |
MauticSDK.onLoad(); | |
} | |
initFileUpload(); | |
}; | |
}); | |
}); | |
} | |
load(); | |
})(window); | |
</script> | |
<style> | |
div.file-hidden>input, | |
.file-hidden>label { | |
display: none !important; | |
} | |
.attached-file { | |
margin: 20px 0px; | |
overflow: hidden; | |
height: 30px; | |
} | |
.file-name.col-md-7.text-right { | |
width: 30%; | |
float: left; | |
} | |
</style> |
Author
sebastian-fahrenkrog
commented
Feb 2, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment