Skip to content

Commit

Permalink
Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Feb 4, 2024
1 parent 66829f1 commit 0e74df4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,77 +17,92 @@ let PreviewActivity = {
mounted() {
this.el.addEventListener("click", e => {
console.log("PreviewActivity clicked")
console.log("where clicked")
let trigger = this.el.querySelector('.open_preview_link')
let anchor = e.target.closest('a')
let anchor = e.target.closest('a')
console.log(e.target)

// this was used to expand long posts by clicking on them, now replaced with a 'Read more' button
// let previewable_activity = e.target.closest('.previewable_activity')
// anchor == trigger || (!anchor && previewable_activity && ( previewable_activity.classList.contains('previewable_expanded') || this.isTruncated(previewable_activity.querySelector('.previewable_truncate')) == false)

if ((trigger || !window.liveSocket) && ((!anchor || anchor.classList.contains('preview_activity_link')) && !e.ctrlKey && !e.metaKey && (!window.getSelection().toString() || window.getSelection().toString() == "") && !e.target.closest('button') && !e.target.closest('figure') && !e.target.closest('.dropdown') && !e.target.closest('[data-id=activity_actions]')
)) {
let uri = this.el.dataset.href || (trigger !== undefined && trigger.getAttribute('href'))
if (window.liveSocket) {
// const feed = document.querySelector(".feed")
const main = document.getElementById("inner")
const layout = document.getElementById("root")
const preview_content = document.getElementById("preview_content")
let previous_scroll = null

console.log("push event to load up the PreviewContent")
this.pushEventTo(trigger, "open", {})

// this.pushEvent("Bonfire.Social.Feeds:open_activity", { id: this.el.dataset.id, permalink: uri })

if (layout) {
previous_scroll = layout.scrollTop
}

if (preview_content) {
preview_content.classList.remove("hidden")
}
if (main) {
main.classList.add("hidden")
}
if (uri) {
// console.log(uri)

history.pushState(
{
'previous_url': document.location.href,
'previous_scroll': previous_scroll
},
'',
uri)
}

// this was used to expand long posts by clicking on them, now replaced with a 'Read more' button
// let previewable_activity = e.target.closest('.previewable_activity')
// anchor == trigger || (!anchor && previewable_activity && ( previewable_activity.classList.contains('previewable_expanded') || this.isTruncated(previewable_activity.querySelector('.previewable_truncate')) == false)

if (trigger && ((!anchor || anchor.classList.contains('preview_activity_link')) && !e.ctrlKey && !e.metaKey && !window.getSelection().toString() && !e.target.closest('button') && !e.target.closest('figure') && !e.target.closest('.dropdown') && !e.target.closest('[data-id=activity_actions]')
)) {
let uri = this.el.dataset.href || (trigger !== undefined && trigger.getAttribute('href')) //this.el.dataset.permalink
if (window.liveSocket) {
// const feed = document.querySelector(".feed")
const main = document.getElementById("inner")
const layout = document.getElementById("root")
const preview_content = document.getElementById("preview_content")
let previous_scroll = null

console.log("push event to load up the PreviewContent")
this.pushEventTo(trigger, "open", {})
e.preventDefault();

// this.pushEvent("Bonfire.Social.Feeds:open_activity", { id: this.el.dataset.id, permalink: uri })
} else {

if (layout) {
previous_scroll = layout.scrollTop
}
// fallback if not connected with live socket

if (preview_content) {
preview_content.classList.remove("hidden")
if (uri) {
console.log(uri)
window.location = uri;
e.preventDefault();
} else {
console.log("No URL")
}
}
if (main) {
main.classList.add("hidden")
}
if (uri) {
// console.log(uri)

history.pushState(
{
'previous_url': document.location.href,
'previous_scroll': previous_scroll
},
'',
uri)
}

e.preventDefault();

} else {

// fallback if not connected with live socket

if (uri) {
console.log(uri)
window.location = uri;
e.preventDefault();
} else {
console.log("No URL")
}
}

} else {

// e.preventDefault();

console.log("PreviewActivity: do not trigger preview in favour of another link or button's action (or opening in new tab)")

// if (previewable_activity) { previewable_activity.classList.add("previewable_expanded") }
console.log(trigger)
console.log(window.liveSocket)
console.log(anchor)
console.log(anchor.classList)
console.log(e.ctrlKey)
console.log(e.metaKey)
console.log(window.getSelection().toString())
console.log(e.target.closest('button'))
console.log(e.target.closest('figure'))
console.log(e.target.closest('.dropdown'))
console.log(e.target.closest('[data-id=activity_actions]'))

// if (previewable_activity) { previewable_activity.classList.add("previewable_expanded") }

return;
return;

}
}




})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file was generated by the Surface compiler.
*/


export default {
timeAgo(input) {
const locale = (navigator.languages && navigator.languages.length) ? navigator.languages[0] : navigator.language; // TODO: use app local instead of browser locale
const date = (input instanceof Date) ? input : new Date(input);
const formatter = new Intl.RelativeTimeFormat(locale);
const ranges = {
years: 3600 * 24 * 365,
months: 3600 * 24 * 30,
weeks: 3600 * 24 * 7,
days: 3600 * 24,
hours: 3600,
minutes: 60,
seconds: 1
};
const secondsElapsed = (date.getTime() - Date.now()) / 1000;
for (let key in ranges) {
if (ranges[key] < Math.abs(secondsElapsed)) {
const delta = secondsElapsed / ranges[key];
return formatter.format(Math.round(delta), key);
}
}
},
setTimeAgo(el, the_date) {
var date_ago = this.timeAgo(the_date)
// console.log(date_ago)

if (date_ago) {
this.el.innerHTML = date_ago

setTimeout(() => {
this.setTimeAgo(el, the_date)
}, 60_000);
}
},
mounted() {
var the_date = this.el.getAttribute("data-date")
// console.log(the_date)
if (the_date) {
setTimeout(() => {
this.setTimeAgo(this.el, the_date)
}, 60_000);
}
}
}
4 changes: 3 additions & 1 deletion flavours/classic/config/flavour_assets/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as c5 from "./Bonfire.UI.Common.LoadMoreLive.hooks"
import * as c6 from "./Bonfire.UI.Common.ChangeLocaleLive.hooks"
import * as c7 from "./Bonfire.UI.Common.ChangeThemesLive.hooks"
import * as c8 from "./Bonfire.UI.Common.ComposerLive.hooks"
import * as c9 from "./Bonfire.UI.Social.Activity.DateAgoLive.hooks"

let hooks = Object.assign(
ns(c1, "Bonfire.Editor.Milkdown"),
Expand All @@ -27,7 +28,8 @@ let hooks = Object.assign(
ns(c5, "Bonfire.UI.Common.LoadMoreLive"),
ns(c6, "Bonfire.UI.Common.ChangeLocaleLive"),
ns(c7, "Bonfire.UI.Common.ChangeThemesLive"),
ns(c8, "Bonfire.UI.Common.ComposerLive")
ns(c8, "Bonfire.UI.Common.ComposerLive"),
ns(c9, "Bonfire.UI.Social.Activity.DateAgoLive")
)

export default hooks

0 comments on commit 0e74df4

Please sign in to comment.