2021-12-29 11:01:34 -05:00
"use strict" ;
2021-01-13 21:56:14 -05:00
Vue . component ( 'tf-message' , {
2021-01-19 21:20:40 -05:00
props : [ 'message' , 'messages' , 'votes' ] ,
2021-01-13 21:56:14 -05:00
data : function ( ) { return { showRaw : false } } ,
computed : {
content _json : function ( ) {
try {
return JSON . parse ( this . message . content ) ;
} catch {
return undefined ;
}
} ,
2022-01-09 16:31:05 -05:00
content _raw : function ( ) {
try {
return JSON . stringify ( JSON . parse ( this . message . content ) , null , 2 ) ;
} catch {
return this . message . content ;
}
2022-01-30 16:44:27 -05:00
} ,
timestamp _relative : function ( ) {
var units = [
{ value : 1 , name : 'milliseconds' } ,
{ value : 1000 , name : 'seconds' } ,
{ value : 1000 * 60 , name : 'minutes' } ,
{ value : 1000 * 60 * 60 , name : 'hours' } ,
{ value : 1000 * 60 * 60 * 24 , name : 'days' } ,
{ value : 1000 * 60 * 60 * 24 * 7 , name : 'weeks' } ,
{ value : 1000 * 60 * 60 * 24 * 30 , name : 'months' } ,
{ value : 1000 * 60 * 60 * 24 * 365 , name : 'years' } ,
] ;
var v = new Date ( ) . valueOf ( ) - this . message . timestamp ;
var result = null ;
for ( let unit of units ) {
if ( v >= unit . value ) {
result = Math . round ( v / unit . value ) + ' ' + unit . name + ' ago' ;
}
}
return result ;
} ,
2021-01-13 21:56:14 -05:00
} ,
methods : {
2022-01-18 21:37:39 -05:00
markdown : markdown ,
2021-12-31 14:01:53 -05:00
set _reply : function ( ) {
g _data . reply _root = this . content _json . root || this . message . id ;
g _data . reply _branch = this . message . id ;
2022-01-09 12:44:46 -05:00
} ,
vote : function ( event ) {
var reaction = event . srcElement . innerText ;
var message = this . message . id ;
if ( confirm ( 'Are you sure you want to react with ' + reaction + ' to ' + message + '?' ) ) {
window . parent . postMessage (
{
appendMessage : {
type : 'vote' ,
vote : {
link : message ,
value : 1 ,
expression : reaction ,
} ,
} ,
} ,
'*' ) ;
}
} ,
2022-01-11 21:55:29 -05:00
show _message : function ( ) {
window . parent . postMessage ( { action : 'setHash' , hash : this . message . id } , '*' ) ;
} ,
2022-01-29 16:14:16 -05:00
expand _image : function ( event ) {
var div = document . createElement ( 'div' ) ;
div . style . left = 0 ;
div . style . top = 0 ;
div . style . width = '100%' ;
div . style . height = '100%' ;
div . style . position = 'fixed' ;
div . style . background = '#000' ;
div . style . zIndex = 100 ;
div . style . display = 'grid' ;
var img = document . createElement ( 'img' ) ;
img . src = event . srcElement . src ;
img . style . maxWidth = '100%' ;
img . style . maxHeight = '100%' ;
img . style . display = 'block' ;
img . style . margin = 'auto' ;
img . style . objectFit = 'contain' ;
img . style . width = '100%' ;
div . appendChild ( img ) ;
div . onclick = function ( ) { document . body . removeChild ( div ) ; } ;
document . body . appendChild ( div ) ;
console . log ( document . body . children ) ;
} ,
2021-01-13 21:56:14 -05:00
} ,
template : ` <md-app class="md-elevation-8" style="margin: 1em" v-if="!content_json || ['pub', 'vote'].indexOf(content_json.type) == -1">
< md - app - toolbar >
< h3 >
2022-01-12 19:16:27 -05:00
< md - button class = "md-icon-button md-dense" @ click = "show_message" >
2022-01-11 21:55:29 -05:00
< md - icon > percent < / m d - i c o n >
< / m d - b u t t o n >
2022-01-14 19:29:51 -05:00
< tf - user : id = "message.author" v - if = "message.author" > < / t f - u s e r >
2021-01-13 21:56:14 -05:00
< / h 3 >
2022-01-14 19:29:51 -05:00
< template v - if = "message.author" >
< div style = "font-size: x-small" >
2022-01-30 16:44:27 -05:00
{ { timestamp _relative } }
< md - tooltip style = "height: auto" >
< div > { { new Date ( message . timestamp ) } } < / d i v >
< div > { { message . id } } < / d i v >
< / m d - t o o l t i p >
2022-01-14 19:29:51 -05:00
< / d i v >
< div class = "md-toolbar-section-end" >
< md - menu >
< md - switch v - model = "showRaw" > < / m d - s w i t c h >
< md - tooltip > Show Raw Message < / m d - t o o l t i p >
< / m d - m e n u >
< / d i v >
< / t e m p l a t e >
< template v - else >
< h3 > Missing < / h 3 >
< / t e m p l a t e >
2021-01-13 21:56:14 -05:00
< / m d - a p p - t o o l b a r >
< md - app - content >
2022-01-14 19:29:51 -05:00
< template v - if = "message.author" >
< div v - if = "showRaw" >
< h1 > { { message . id } } < / h 1 >
< pre style = "word-wrap: break-all; white-space: pre-wrap" > { { content _raw } } < / p r e >
2021-01-13 21:56:14 -05:00
< / d i v >
2022-01-14 19:29:51 -05:00
< div v - else >
< div v - if = "content_json && content_json.type == 'post'" >
< div v - html = "this.markdown(content_json.text)" > < / d i v >
2022-01-29 16:14:16 -05:00
< span v - for = "mention in content_json.mentions" v - if = "mention.link && typeof(mention.link) == 'string' && mention.link.startsWith('&')" >
2022-01-14 19:29:51 -05:00
< a v - if = "mention.type == 'application/tildefriends'" : href = "'/' + mention.link + '/'" target = "_top" > { { mention . name } } < / a >
2022-01-29 16:14:16 -05:00
< img v - else class = "md-elevation-4" style = "margin: 4px; max-width: 320px; max-height: 240px" : src = "'/' + mention.link + '/view'" v - on : click = "expand_image" > < / i m g >
< / s p a n >
2022-01-14 19:29:51 -05:00
< / d i v >
< div v - else - if = "content_json && content_json.type == 'tildefriends-app'" >
< div v - html = "this.markdown(content_json.text)" > < / d i v >
< md - button target = "_top" : href = "'/' + message.id + '/'" > { { content _json . name || 'tildefriends-app' } } < / m d - b u t t o n >
< / d i v >
< div v - else - if = "content_json && content_json.type == 'contact'" > < tf - user : id = "message.author" > < /tf-user> {{content_json.following ? '==>' : '=/ = & gt ; ' } } < tf - user : id = "content_json.contact" > < / t f - u s e r > < / d i v >
< div v - else > { { message . content } } < / d i v >
2021-01-13 21:56:14 -05:00
< / d i v >
2022-01-14 19:29:51 -05:00
< / t e m p l a t e >
< template v - else >
{ { message . id } }
< / t e m p l a t e >
2021-12-29 11:01:34 -05:00
< tf - message v - for = "sub_message in (message.children || [])" v - bind : message = "sub_message" v - bind : messages = "messages" v - bind : votes = "votes" v - bind : key = "sub_message.id" > < / t f - m e s s a g e >
< md - chip v - for = "vote in Object.keys(votes[message.id] || {})" v - bind : key = "vote" >
{ { vote + ( votes [ message . id ] [ vote ] . length > 1 ? ' (' + votes [ message . id ] [ vote ] . length + ')' : '' ) } }
2021-01-13 21:56:14 -05:00
< / m d - c h i p >
2022-01-14 19:29:51 -05:00
< md - card - actions v - if = "message.author" >
2022-01-09 16:31:05 -05:00
< md - button class = "md-icon-button" @ click = "set_reply" >
< md - icon > reply < / m d - i c o n >
< / m d - b u t t o n >
2022-01-09 12:44:46 -05:00
< md - menu >
< md - menu - content >
< md - menu - item @ click = "vote" > Like < / m d - m e n u - i t e m >
< / m d - m e n u - c o n t e n t >
< md - button class = "md-icon-button" md - menu - trigger >
< md - icon > thumb _up < / m d - i c o n >
< / m d - b u t t o n >
< / m d - m e n u >
< / m d - c a r d - a c t i o n s >
2021-01-13 21:56:14 -05:00
< / m d - a p p - c o n t e n t >
< / m d - a p p > ` ,
} ) ;