$(function () { "use strict"; var $modal = $('.respondent-modal'); var $content = $modal.find('.modal-content'); var $form = $('#respondentForm'); var $inputs = $form.find('.input-target'); const openModal = gsap.timeline({paused: true}); // datastore contains the active data array for use by functions in this context var datastore; openModal.to($modal, { duration: .6, ease: Power4.easeOut, autoAlpha: 1, onStart: function () { $('html').addClass('no-scroll'); }, }); openModal.from($content, { delay: -.3, duration: .6, ease: Power4.easeOut, autoAlpha: 0, y: '0px', }); const confTL = gsap.timeline({paused: true}); confTL.to($('#confirmation-modal'), { duration: .6, ease: Power4.easeOut, autoAlpha: 1, onStart: function () { $('html').addClass('no-scroll'); }, }); confTL.from($('#confirmation-modal .modal-content'), { delay: -.3, duration: .6, ease: Power4.easeOut, autoAlpha: 0, y: '0px', }); window.respondents = {min: 5, max: 99}; var testdata = [ {"firstname": "Willem", "lastname": "van Buuren", "email": "wim@nl.nl"}, {"firstname": "Hans", "lastname": "Kazan", "email": "hannie@abacadabra.nl"} ]; function editRespondent(id) { console.log('edit respondent ' + id, datastore[id]); //modal open and set data to fields :) modalOpen(id); } // fetch values from input fields // update current record // save datastore function updateRecord() { var currentrecordid=$('#recordid').val(); $inputs.each(function (index) { let attid = $(this).data('attr-id'); switch (attid) { case 'id': break; default: console.log('attribute '+attid); if (datastore[currentrecordid]) { datastore[currentrecordid][attid]=$(this).val(); } } }); buildFromData(datastore); $('#respondent-selection').trigger('change'); console.log('updated record ' + currentrecordid, datastore[currentrecordid], datastore); } var $dataholder = $('#respondent-selection'); var $target = $('#build-target'); $target.on('click','.addrow',function(){ if(!$target.hasClass('respondents-submitted')){ datastore = $dataholder.data('json'); let id = $(this).data('id'); if (datastore.length < window.respondents.max) { datastore.push({firstname: '', lastname: '', email: '', role: ''}); editRespondent(datastore.length - 1, datastore); } } }); $('#build-target').on('click', '.respondent img', function () { if(!$target.hasClass('respondents-submitted')){ datastore = $dataholder.data('json'); let id = $(this).data('id'); let i = $(this).parent().data('id'); if(id === 'EDIT') { console.log("IIDD", i, datastore[i]); editRespondent(i, datastore); } else { handleDelete(i,datastore); } } }); function handleDelete(i,datastore){ openConfModal(); $('#confirm-delete-user').text(datastore[i].firstname+' '+datastore[i].lastname); var $confirm = $('#confirmation-modal #confirm-yes'); var $decline = $('#confirmation-modal #confirm-no'); $confirm.off('click'); $confirm.on('click',function () { datastore.splice(i, 1); buildFromData(datastore); $('#respondent-selection').trigger('change'); closeConfModal(); }); $decline.on('click' ,function () { closeConfModal(); }); } // update table to reflect datastore function buildFromData(data) { $target.empty(); $.each(data, function (i, obj) { $target.append('