jQuery(document).ready(function() {
const jdoc = jQuery(document);
const jwin = jQuery(window)
let xhr;
//følg blog
jdoc.on("click tap", ".js-follow-blog", function() {
var blog_id = jQuery(this).attr("data-blog-id");
var user_id = jQuery(this).attr("data-user-id");
var action = jQuery(this).attr("data-action");
if (action == 'add') {
jQuery.post("/wp-content/themes/bloggersdelight/follow/follow_create.php", { "blog_id": blog_id, "user_id": user_id, "action": "add" });
jQuery(this).html("Følger");
var action = jQuery(this).attr("data-action", "delete");
} else if (action == 'delete') {
jQuery.post("/wp-content/themes/bloggersdelight/follow/follow_create.php", { "blog_id": blog_id, "user_id": user_id, "action": "delete" });
jQuery(this).html('');
jQuery(this).attr("data-action", "add");
}
return false;
});
//global email notification for user
jdoc.on("click tap", ".js-email-notification", function() {
var userid = jQuery(this).attr("data-userid");
var action = jQuery(this).attr("data-action");
if (action == 'add') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=user_email_notification",
data: { "userid": userid, "do": "add" },
success: function(data) {}
});
jQuery(this).html('');
jQuery(this).attr("data-action", "delete");
} else if (action == 'delete') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=user_email_notification",
data: { "userid": userid, "do": "delete" },
success: function(data) {}
});
jQuery(this).html('');
jQuery(this).attr("data-action", "add");
}
return false;
});
jdoc.on("click tap", ".js-email-type", function() {
var userid = jQuery(this).attr("data-userid");
var action = jQuery(this).attr("data-action");
// set digest unchecked
if (action == 'email-single') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=user_email_type",
data: { "userid": userid, "do": "single" },
success: function(data) {}
});
jQuery(".js-email-type-digest").html('');
jQuery(".js-email-type-single").html('');
} else if (action == 'email-digest') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=user_email_type",
data: { "userid": userid, "do": "digest" },
success: function(data) {}
// set single unchecked
// set digest checked
});
jQuery('.js-email-type-single').html('');
jQuery('.js-email-type-digest').html('');
}
return false;
});
//follow blog email notification
jdoc.on("click tap", ".js-follow-blog-email", function() {
var id = jQuery(this).attr("data-id");
var user_id = jQuery(this).attr("data-user-id");
var blog_id = jQuery(this).attr("data-blog-id");
var action = jQuery(this).attr("data-action");
if (action == 'add') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=follow_blog_email_notification",
data: { "id": id, "do": "add", "user_id": user_id, "blog_id": blog_id },
success: function(data) {}
});
jQuery(this).html('');
jQuery(this).attr("data-action", "delete");
} else if (action == 'delete') {
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php?preview=true&action=follow_blog_email_notification",
data: { "id": id, "do": "delete", "user_id": user_id, "blog_id": blog_id },
success: function(data) {}
});
jQuery(this).html('');
jQuery(this).attr("data-action", "add");
}
return false;
});
//toogle email options
jdoc.on("click tap", ".js-email-notification", function() {
jQuery('.email_options').toggle()
})
// make filter bar opacity
jwin.scroll(function() {
if (jwin.scrollTop() > 100) {
jQuery("#filter-container").addClass('filter-fade-out');
}
});
//show filter bar when scrolling stops
(function() {
var timer;
jwin.bind('scroll', function() {
clearTimeout(timer);
timer = setTimeout(refresh, 550);
});
var refresh = function() {
// do stuff
jQuery("#filter-container").removeClass('filter-fade-out');
};
})();
//Menu search animation. It's verbose and ugly because bootstrap 2.ancient
jdoc.on("click tap touchstart", "#mobile-search-btn", function() {
//calculate the maximum width we are allowed to set.
var searchBarMaxSize = Math.abs(
jQuery('#nav-container').width() -
jQuery('#top-menu-btn').outerWidth(true) -
jQuery('.logo').width() -
parseInt(jQuery(".logo").css("marginRight"))-
(jQuery('.mobile-search').outerWidth(true) * 2)
);
jQuery('.mobile-top-search').css({ 'width': searchBarMaxSize });
jQuery('.mobile-top-search').animate({ 'width': 'toggle' });
jQuery('#search-icon-wrapper').toggleClass('hide-wrapper');
});
// scroll to top
jdoc.on("click tap", "#scrollup", function() {
jQuery("html, body").animate({ scrollTop: 0 }, 600);
return false;
});
//scroll to bottom
jdoc.on("click tap", "#scrolldown", function() {
jwin.unbind('scroll');
jQuery("#filter-container").hide();
jQuery("html, body").animate({ scrollTop: jdoc.height() }, 1000);
return false;
});
//check if captcha is ok
jQuery('#captcha').on('blur', function() {
var captcha = jQuery('#captcha').val();
var encrypt = jQuery('#encrypt').val();
jQuery.ajax({
url: '/wp-content/themes/bloggersdelight/lib/check.php',
cache: false,
data: ({ code: captcha, encrypt_code: encrypt }),
success: function(data) {
if (data == 1) {
jQuery('#captcha').css('border-bottom', '3px solid #ddd');
jQuery('#captcha-true').val("good")
jQuery("#check-captcha-msg").html();
jQuery('#check-captcha-msg').addClass('hide');
return 1;
} else {
jQuery('#captcha').css('border-bottom', '3px solid #CC0000');
jQuery('#check-captcha-msg').removeClass('hide');
jQuery("#check-captcha-msg").html('De indtastede bogstaver er ikke korrekte');
}
}
});
});
//check if email has spelling mistake in form
var domains = ['hotmail.com', 'gmail.com', 'aol.com'];
var topLevelDomains = ["com", "net", "org", "dk"];
jQuery('.email-creation').on('blur', function() {
// check if email is valid
const el = jQuery(this)
var email = el.val();
jQuery('#email-suggestion').removeClass('hide');
jQuery('#email-suggestion').html(' Checker om emailen er gyldig');
jQuery.ajax({
url: '/wp-admin/admin-ajax.php?preview=true',
cache: false,
data: ({ action: 'email_validate', email: email }),
success: function(data) {
if (data == 1) {
jQuery('#email-suggestion').html(' Emailen er gyldig');
} else if (data == 2) {
jQuery('#email-suggestion').html(' Der er allerede oprettet en bruger med den e-mail');
} else {
jQuery('#email-suggestion').removeClass('hide');
jQuery('#email-suggestion').html(' Emailen er ikke gyldig');
//check if the system can detect the error
el.mailcheck({
domains: domains, // optional
topLevelDomains: topLevelDomains, // optional
suggested: function(element, suggestion) {
jQuery('#email-suggestion').append('. Mente du ');
jQuery('#email-suggestion').removeClass('hide');
jQuery('#email-suggestion').append('' + suggestion.address + '@' + suggestion.domain + '?');
},
empty: function(element) {}
});
}
}
});
});
jdoc.on("click tap", "#email-suggestion .suggested-email", function() {
var email = jQuery('.suggested-email').text();
jQuery(this).parents("form").find('[name=email]').val(email);
jQuery('#email-suggestion').addClass('hide');
});
const bdValidateDomain = bdDebounce(function() {
domain = jQuery('#domain').val();
if (domain.length > 0) {
if (xhr && xhr.readyState != 4) { xhr.abort(); }
jQuery('#check-domain-msg').removeClass('hide');
jQuery('#check-domain-msg').html(' Checker om domænet er ledigt');
xhr = jQuery.ajax({
url: '/wp-admin/admin-ajax.php?preview=true',
cache: false,
data: ({ action: 'check_username', domain: domain }),
success: function(data) {
if (data == 1) {
jQuery('#check-domain-msg').html(' Domænet er ledigt');
jQuery('#valid').val('1');
} else if (data == 2) {
jQuery('#check-domain-msg').html(' Domænet er ugyldigt
Kun a-z samt 0-9 og - tilladt.');
jQuery('#valid').val('1');
} else {
jQuery('#check-domain-msg').html(' Domænet er optaget');
jQuery('#valid').val('no');
}
}
});
}
}, 750)
// check if domain exists
jQuery('#domain').on('keyup', bdValidateDomain);
jdoc.on("click tap", ".modal a", function() {
jQuery(this).attr("target", "_blank");
});
// clean modal
jQuery('body').on('hidden', '.modal', function() {
jQuery(this).removeData('modal');
});
});
function bdDebounce(func, timeout = 300) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(function() {
func.apply(this, args);
}, timeout);
}
}