//
// /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=', {} );
var revealoptions = {};
var wrapoptions = { class: "", title: "" };
tmplink = $(this).attr('data-dialog-link');
if (tmpclass = $(this).attr('data-dialog-class')) wrapoptions.class = tmpclass;
// console.log("DATASET", this.dataset, this.attributes);
$.each(this.dataset, function(key, value){
regex = /^([a-z]+)(.*?)$/g;
res = regex.exec(key);
if (3>res.length) return;
var keytype = res[1];
var name = res[2];
name = name.charAt(0).toLowerCase() + name.slice(1);
if (value==='true') value = true;
else if (value==='false') value = false;
switch (keytype) {
case 'dialog':
wrapoptions[name] = value;
break;
case 'reveal':
revealoptions[name] = value;
break;
default: break;
}
});
if (tmplink) {
url = tmplink;
} else {
tmp = $(this).attr('href');
url = tmp+'?cat=&id=&idstr='+$(this).data('idstr');
}
// console.log("REEAWL", revealoptions);
// console.log("WRAP", wrapoptions);
cpcommonajaxmodal(url, revealoptions, 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');
}
var cpcommonwrapoptionscache = {};
var cpcommonlastdataforclose = {};
function cpcommonRefreshParent(el) {
cpcommonRefreshContentDo($(el).parents('.cpcommonajaxmodalpayload'));
return false;
}
function cpcommonCloseWaitingBox() {
if ($('.cpawaitingbox').length) {
$('.cpawaitingbox').foundation('close');
$('.cpawaitingbox').remove();
}
}
function cpcommonShowWaitingBox(msg) {
cpcommonCloseWaitingBox();
if (typeof msg === "undefined") msg = '...';
var html = '
';
$('body').append(html);
$('.cpawaitingbox').foundation();
$('.cpawaitingbox').foundation('open');
}
function cpcommonRefreshContentDo(filterOrEl) {
if (typeof filterOrEl === "undefined") filterOrEl = '.cpcommonajaxmodalpayload';
$(filterOrEl).each(function(i, el) {
// console.log("REFRESH", i, el, this);
var uid = $(el).attr('data-uid');
if (!uid) return;
var url = $(el).attr('data-loadedfrom');
if (!url) return;
var wrapoptions = cpcommonwrapoptionscache[uid];
// console.log("WRAPOPT", wrapoptions);
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);
});
});
}
function cpcommonajaxmodalFOUNDATION(url, revealoptionsIN, 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();
});
// https://get.foundation/sites/docs/reveal.html#js-options
// https://get.foundation/sites/docs/reveal.html
revealoptions = { closeOnClick: false, closeOnEsc: true, resetOnClose: true, deepLink: false, multipleOpened: true };
if (typeof revealoptionsIN == 'undefined') {
revealoptionsIN = {};
}
// console.log("OBJ KEYS", Object.keys(revealoptionsIN));
for (var key of Object.keys(revealoptionsIN)) {
// console.log("KEY", key, revealoptionsIN[key]);
revealoptions[key] = revealoptionsIN[key];
}
// console.log("revealoptions",revealoptions);
if (typeof wrapoptions == 'undefined') wrapoptions = { class: "", title: "" };
// console.log("wrapoptions",wrapoptions);
// 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();
//
if (typeof wrapoptions.onclose === "function") {
wrapoptions.onclose(uid, cpcommonlastdataforclose[uid]); // See below in close on submit.
if (typeof cpcommonlastdataforclose[uid] !== 'undefined') delete cpcommonlastdataforclose[uid];
}
});
cpcommonwrapoptionscache[uid] = wrapoptions; // see refresh content.
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") {
cpcommonlastdataforclose[uid] = data;
$('#'+uid).foundation('close');
// wrapoptions.onclose(uid, data); // Moved up to closed.zf.reveal ...
} 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
});
}