inside out project STEP-105 to STEP-106 app.js Code Changes

Created Diff never expires
6 removals
84 lines
27 additions
104 lines
// inside out project STEP-105
// inside out project STEP-106


window.onload = init();
window.onload = init();


function init() {
function init() {


window.addEventListener('scroll', function(e) {
window.addEventListener('scroll', function(e) {
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
var distanceY = window.pageYOffset || document.documentElement.scrollTop,
shrinkOn = 300,
shrinkOn = 300,
header = document.querySelector("header");
header = document.querySelector("header");
if (distanceY > shrinkOn) {
if (distanceY > shrinkOn) {
classie.add(header, "smaller");
classie.add(header, "smaller");
} else {
} else {
if (classie.has(header, "smaller")) {
if (classie.has(header, "smaller")) {
classie.remove(header, "smaller");
classie.remove(header, "smaller");
}
}
}
}
});
});




$.ajax({
$.ajax({
method: 'GET',
method: 'GET',
url: 'https://me.inside-out-project.com/wp-json/wp-api-menus/v2/menus/3',
url: 'https://me.inside-out-project.com/wp-json/wp-api-menus/v2/menus/3',
dataType: 'json',
dataType: 'json',
success: function(data) {
success: function(data) {
$('nav').hide();
$('nav').hide();
var menu = menuBuilder(data.items);
var menu = menuBuilder(data.items);
$('nav').html(menu).slideDown();
$('nav').html(menu).slideDown();
$('nav li a').click(function() {
$('nav li a').click(function() {
getPage($(this).data("pgid"));
getPage($(this).data("pgid"));
});
});
getPage(314);
getPage(314);
$("#loaderDiv").fadeOut("slow");
$("#loaderDiv").fadeOut("slow");
},
},
error: function() {
error: function() {
console.log('all is not good');
console.log('all is not good');
}
}
});
});

$.ajax({
method: 'GET',
url: 'https://me.inside-out-project.com/wp-json/wp-api-menus/v2/menus/12',
dataType: 'json',
success: function (data) {
var menu = menuBuilder(data.items, 'genLinks', 'footer-ul');
$('#genLinks').replaceWith(menu);
$('#genLinks li a').click(function () {
getPage($(this).data("pgid"));
});
},
error: function () {
console.log('all is not good');
}
});
}
}




function menuBuilder(obj) {
function menuBuilder(obj, targetEl, classInfo) {
var theMenu = '';
var theMenu = '';
if (obj.length > 0) {
if (obj.length > 0) {
theMenu = theMenu + '<ul>';
let target = (targetEl)?' id="'+targetEl+'"':'';
obj.forEach(function(item) {
let elClass = (classInfo)?' class="'+classInfo+'"':'';
theMenu = theMenu + '<ul'+target+''+elClass+'>';
console.log(theMenu+' '+target);
obj.forEach(function (item) {
theMenu = theMenu + '<li><a href="#" data-pgid="' + item.object_id + '">' + item.title + '</a>';
theMenu = theMenu + '<li><a href="#" data-pgid="' + item.object_id + '">' + item.title + '</a>';
if (item.children) {
if (item.children) {
theMenu = theMenu + menuBuilder(item.children);
theMenu = theMenu + menuBuilder(item.children);
}
}
theMenu = theMenu + '</li>';
theMenu = theMenu + '</li>';
});
});
theMenu = theMenu + '</ul>';
theMenu = theMenu + '</ul>';
} else {
} else {
console.log('no data');
console.log('no data');
}
}
return theMenu;
return theMenu;
}
}

function getPage(obj) {
function getPage(obj) {
$("#loaderDiv").fadeIn("slow");
$("#loaderDiv").fadeIn("slow");
$.ajax({
$.ajax({
method: 'GET',
method: 'GET',
url: 'https://me.inside-out-project.com/wp-json/wp/v2/pages/' + obj,
url: 'https://me.inside-out-project.com/wp-json/wp/v2/pages/' + obj,
dataType: 'json',
dataType: 'json',
success: function(data) {
success: function(data) {
var pgbuild = '';
var pgbuild = '';
pgbuild = '<section><div class="container">' + data.content.rendered + '</div></section>';
pgbuild = '<section><div class="container">' + data.content.rendered + '</div></section>';
$("#content").fadeOut(function() {
$("#content").fadeOut(function() {
$('html').animate({
$('html').animate({
scrollTop: 0
scrollTop: 0
}, 'slow'); //IE, FF
}, 'slow'); //IE, FF
$('body').animate({
$('body').animate({
scrollTop: 0
scrollTop: 0
}, 'slow'); //chrome, don't know if Safari works
}, 'slow'); //chrome, don't know if Safari works
$(this).html(pgbuild).fadeIn();
$(this).html(pgbuild).fadeIn();
$("#loaderDiv").fadeOut("slow");
$("#loaderDiv").fadeOut("slow");
});
});
},
},
error: function() {
error: function() {
console.log('bad');
console.log('bad');
}
}
});
});
}
}