// // /css/cpcommon.css // function addcpcommonajaxmodal(filter) { // $('.afwelementeditlink').each(function(i, el) { $(filter).each(function(i, el) { if ($(el).hasClass('cpcommoncpajaxmodalseen')) return; $(el).addClass('cpcommoncpajaxmodalseen'); $(el).click(function() { // afwelcpajaxmodal('?cat={$GLOBALS['cat']}&id={$GLOBALS['id']}&idstr=', {} ); wrapoptions = { class: "", title: "" }; tmplink = $(this).attr('data-dialog-link'); if (tmpclass = $(this).attr('data-dialog-class')) wrapoptions.class = tmpclass; // console.log("DATASET", this.dataset); $.each(this.attributes, function(i, attrib){ var name = attrib.name; var value = attrib.value; var res = name.split("-"); // data-dialog-class="small" data-dialog-title="HelloTitle" if (res[1]!='dialog') return; name = res[2]; wrapoptions[name] = value; }); if (tmplink) { url = tmplink; } else { tmp = $(this).attr('href'); url = tmp+'?cat=&id=&idstr='+$(this).data('idstr'); } cpcommonajaxmodal(url, {}, wrapoptions); return false; }); }); } function cpcommonajaxmodal(url, options, wrapoptions) { if (typeof Foundation == 'undefined') { // alert("AFW Foundation not loaded. It is required."); cpcommonajaxmodalJQUERY(url, options) } else { cpcommonajaxmodalFOUNDATION(url, options, wrapoptions); } } function cpcommonCloseParent(el) { // input class="button" type="button" value=" Cancel " onclick="return cpcommonCloseParent(this);" $(el).parents('.cpcommonajaxdialog').foundation('close'); } function cpcommonajaxmodalFOUNDATION(url, revealoptions, wrapoptions) { // revealoptions is passed on to FOUNDATION // wrapoptions is used locally in this method. $('div.reveal-overlay').each(function() { if ($(this).is(':empty')) $(this).remove(); }); if (typeof revealoptions == 'undefined') revealoptions = {}; // https://get.foundation/sites/docs/reveal.html#js-options if (Object.keys(revealoptions).length == 0) { // https://get.foundation/sites/docs/reveal.html revealoptions.closeOnEsc = false; revealoptions.resetOnClose = true; revealoptions.deepLink = true; revealoptions.multipleOpened = true; } if (typeof wrapoptions == 'undefined') wrapoptions = { class: "", title: "" }; // console.log("wrapoptions",wrapoptions); var titlebuf = ""; if (wrapoptions.title) { titlebuf = '

'+wrapoptions.title+'

'; } uid = "dialoedl"+Math.floor(Math.random() * 100000) + 1; // var xhtml = $('
' +'' +""+titlebuf +'
...
' +'
').appendTo('body'); var modal = new Foundation.Reveal($('#'+uid), revealoptions ); $('#'+uid).foundation('open').on('closed.zf.reveal', function() { $('#'+uid).parent('.reveal-overlay').remove(); $('#'+uid).remove(); }); var params = {}; if (typeof wrapoptions.params !== 'undefined') params = wrapoptions.params; var ajaxsettings = {}; if (Object.keys(params).length > 0){ ajaxsettings.method = 'POST'; ajaxsettings.data = params; } $.ajax(url, ajaxsettings).done(function(resp) { cpcommoninitModalDialogContent(uid, resp, wrapoptions); }); return false; } function cpcommoninitModalDialogContent(uid, resp, wrapoptions) { // sameish as dialogForm... $('#'+uid+' >div.payload').html(resp); subbtn = $('#'+uid).find('input[type="submit"]'); if (subbtn.length) { // $('#'+uid).find('input[type="submit"]').after(''); var idstr = $('#'+uid).find('#csteditid').val(); if (idstr && wrapoptions.candelete) { $('#'+uid).find('input[type="submit"]').after(''); } $('#'+uid).find('form').not('nosubmit').submit(function(e) { var form = $(this); var url = form.attr('action'); $.ajax({ type: "POST", url: url, data: form.serialize(), success: function(data) { if (data.indexOf('CloseOK')==0) { // ...todo: update parent row. if (typeof wrapoptions.onclose === "function") { $('#'+uid).foundation('close'); wrapoptions.onclose(uid, data); } else { alert('OK'); $('#'+uid).foundation('close'); location.reload(); } } else { cpcommoninitModalDialogContent(uid, data, wrapoptions); // alert(data); // show response from the php script. } } }); // .ajax e.preventDefault(); return false; }); $('#'+uid).find('input[name="xcnl"]').click(function(e) { $('#'+uid).foundation('close'); }); $('#'+uid).find('input[name="xdel"]').click(function(e) { // ... id="'+uid+'delb" ... var form = $(this).parents('form'); var url = form.attr('action'); var idst = $(form).find('#csteditid').val(); $(form).hide('slow'); $(form).after('...'); $.ajax({ type: "POST", url: url, data: { rmel: idst }, success: function(data) { if (data.indexOf('CloseOK')==0) { alert('OK'); $('#'+uid).foundation('close'); location.reload(); } else { alert(data); // show response from the php script. } } }); return false; }); } // subbtn // wrapoptions.onload if (typeof wrapoptions.onload !== 'undefined') { // params = wrapoptions.params; wrapoptions.onload(uid, resp, wrapoptions); } } function cpcommonajaxmodalJQUERY(url, options) { if (typeof options == 'undefined') options = {}; uid = "dialoedl"+Math.floor(Math.random() * 100000) + 1; // $('
').html('
Loading...
').dialog({ modal: true, close: function(ev, ui) { $(this).remove(); }, draggable: true, height: "auto", width: "auto", maxWidth: 680 } ); $.post(url, options.postdata ) .done(function(resp) { $('#'+uid+' >div').html(resp); subbtn = $('#'+uid).find('input[type="submit"]'); if (subbtn.length) { $('#'+uid).find('input[type="submit"]').after(''); var idstr = $('#'+uid).find('#csteditid').val(); if (idstr) { $('#'+uid).find('input[type="submit"]').after(''); } $('#'+uid).find('form').submit(function(e) { var form = $(this); var url = form.attr('action'); $.ajax({ type: "POST", url: url, data: form.serialize(), success: function(data) { if (data.indexOf('CloseOK')==0) { // ...todo: update parent row. alert('OK'); $('#'+uid).dialog('close'); location.reload(); } else { alert(data); // show response from the php script. } }, error: function(xhr, status, errorThrown) { console.log(xhr); console.log(status); console.log(errorThrown); alert('ajax eh, feil...'+url); } }); // .ajax e.preventDefault(); return false; }); $('#'+uid).find('input[name="xcnl"]').click(function(e) { $('#'+uid).foundation('close'); }); $('#'+uid).find('input[name="xdel"]').click(function(e) { var form = $(this).parents('form'); var url = form.attr('action'); var idst = $(form).find('#csteditid').val(); $(form).hide('slow'); $(form).after('...'); $.ajax({ type: "POST", url: url, data: { rmel: idst }, success: function(data) { if (data.indexOf('CloseOK')==0) { alert('OK'); $('#'+uid).foundation('close'); location.reload(); } else { alert(data); // show response from the php script. } } }); return false; }); } // subbtn }); }