import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
const likesWeight = 0.2
const viewsWeight = 0.8
const trendingPostsToShow = 3
const trendingCategoriesToShow = 5
$w.onReady(function () {
// TODO: write your page related code here...
//let info =
wixLocation.onChange((res) => {
freshState()
})
freshState()
});
function freshState(parameter) {
$w("#post1").getPost().then(async (res) => {
if (res) {
loadInfo(res)
categories()
$w("#post1").expand()
$w("#statebox9").expand()
} else {
$w("#noshowStrip").expand()
$w("#statebox9, #columnStrip1, #post1").collapse()
}
})
}
function timeAgo(parameter, dateboolean) {
let timeAgoInfo = '';
let num
let now = new Date() //current timeAgoInfo
let diff = now - parameter // diff between current and provided timeAgoInfo
var ms_Min = 60 * 1000;
var ms_Hour = ms_Min * 60;
var ms_Day = ms_Hour * 24;
var ms_Mon = ms_Day * 30;
var ms_Yr = ms_Mon * 365
if (dateboolean) {
const monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let month = parameter.getMonth() + 1
let day = parameter.getDate()
let year = parameter.getFullYear()
let hour = (parameter.getHours() > 12) ? parameter.getHours() - 12 : (parameter.getHours() === 0) ? '12' : parameter.getHours()
let minute = (parameter.getMinutes() > 10) ? parameter.getMinutes() : "0" + parameter.getMinutes()
let ampm = (parameter.getHours() > 12) ? "pm" : "am"
timeAgoInfo = monthsShort[month] + ". " + day + ", " + year // + " " + hour + ":" + minute + " " + ampm
//console.log(out)
return timeAgoInfo
} else {
if (diff < ms_Min) {
timeAgoInfo = 'Just Now'
//console.log(timeAgoInfo)
return timeAgoInfo
} else if (diff < ms_Hour) {
num = Math.floor(diff / ms_Min)
timeAgoInfo = (num === 1) ? num + " minute ago" : num + " minutes ago";
//console.log(timeAgoInfo)
return timeAgoInfo
} else if (diff < ms_Day) {
num = Math.floor(diff / ms_Hour)
timeAgoInfo = (num === 1) ? num + " hour ago" : num + " hours ago";
//console.log(timeAgoInfo)
return timeAgoInfo
} else if (diff < ms_Mon) {
num = Math.floor(diff / ms_Day)
timeAgoInfo = (num === 1) ? num + " day ago" : num + " days ago";
//console.log(timeAgoInfo)
return timeAgoInfo
} else if (diff < ms_Yr) {
num = Math.floor(diff / ms_Mon)
timeAgoInfo = (num === 1) ? num + " month ago" : num + " months ago";
//console.log(timeAgoInfo)
return timeAgoInfo
} else if (diff > ms_Yr) {
num = Math.floor(diff / ms_Yr)
timeAgoInfo = (num === 1) ? num + " year ago" : num + " years ago";
////console.log(timeAgoInfo)
return timeAgoInfo
} else {
let month = parameter.getMonth() + 1
let day = parameter.getDate()
let year = parameter.getFullYear()
let hour = (parameter.getHours() > 12) ? parameter.getHours() - 12 : (parameter.getHours() === 0) ? '12' : parameter.getHours()
let minute = (parameter.getMinutes() > 10) ? parameter.getMinutes() : "0" + parameter.getMinutes()
let ampm = (parameter.getHours() > 12) ? "pm" : "am"
timeAgoInfo = month + "/" + day + "/" + year + " " + hour + ":" + minute + " " + ampm
//console.log(timeAgoInfo)
return timeAgoInfo
}
}
}
async function loadInfo(postRes) {
let info = await postRes;
let defaultImage = $w("#columnStrip1").background.src;
$w("#columnStrip1").background.src = await (info.coverImage) ? info.coverImage : defaultImage;
$w("#dateOriginal").text = await (info.publishedDate) ? "Published: " + timeAgo(info.publishedDate, true).toString() : "";
$w("#dateLast").text = await (info.lastPublishedDate) ? "Updated: " + timeAgo(info.lastPublishedDate).toString() : "";
(info.publishedDate !== info.lastPublishedDate) ? $w("#box21").show(): $w("#box21").hide();
$w("#views").text = await (info.viewCount) ? info.viewCount.toString() : "1";
$w("#likes").text = await (info.likeCount) ? info.likeCount.toString() : "0";
$w("#title").text = await info.title;
//w("#excerpt").text = await (info.excerpt.length > 150) ? info.excerpt.slice(0, 147) + "..." : info.excerpt;
if (info.categories) {
let categoryList = []
for (var i = 0; i < info.categories.length; i++) {
let infoCat = await loadPostCategories(info.categories[i])
await categoryList.push({ "label": infoCat.label, "value": infoCat.value })
}
if (categoryList.length > 0) {
$w("#categories").options = await categoryList
$w("#categoryBox").show()
$w("#categories").onClick((event) => {
wixLocation.to($w("#categories").value.toString())
})
} else {
$w("#categoryBox").hide()
}
} else {
$w("#categoryBox").hide()
$w("#categories").options = []
}
}
async function categories(parameter) {
await wixData.query("Blog/Categories").find().then((res) => {
let catList = res.items
if (catList.length > 0) {
if (catList.length > 5) {
$w("#categoryRepeater").data = catList.slice(0, 5)
$w("#viewcategory").show()
} else {
$w("#categoryRepeater").data = catList
$w("#viewcategory").hide()
}
} else {
$w("#categoryRepeater, #viewcategory, #categoryLabel").collapse()
}
})
}
Have fun!
Comments