@ -1,5 +1,151 @@
const MONTHNAME = [ "gennaio" , "febbraio" , "marzo" , "aprile" , "maggio" , "giugno" , "luglio" , "agosto" , "settembre" , "ottobre" , "novembre" , "dicembre" ] ;
function fetch _calendar ( ) {
const class _prefix = 'zerocalcare-'
let ul = document . getElementById ( class _prefix + "latest-events" ) ;
fetch ( "https://golem.linux.it/cgi/zerocalcare-test/main.py?interval=4weeks" )
. then ( response => response . json ( ) )
. then ( json => {
for ( let i = 0 ; i < Math . min ( 3 , json . length ) ; ++ i ) {
// Do not display private events
if ( typeof json [ i ] [ 'CLASS' ] === 'string' && json [ i ] [ 'CLASS' ] == 'PRIVATE' ) {
continue ;
}
// Future improvements needed for a better backend output date in ISO format
// Now we have to parse the string :( very very ugly
let date = new Date ( json [ i ] [ 'DATETIME' ] ) ;
let m = new moment ( date ) ;
m . locale ( 'it' ) ;
let eventElement = document . createElement ( 'li' ) ;
let titleElement = document . createElement ( 'div' ) ;
titleElement . appendChild ( document . createTextNode ( decodeURI ( json [ i ] [ 'SUMMARY' ] ) ) ) ;
titleElement . classList . add ( class _prefix + 'title' ) ;
eventElement . appendChild ( titleElement ) ;
let contentEventElement = document . createElement ( 'div' ) ;
contentEventElement . classList . add ( class _prefix + 'details' )
eventElement . appendChild ( contentEventElement ) ;
// Check if event is not confirmed
if ( typeof json [ i ] [ 'STATUS' ] === 'string' ) {
if ( json [ i ] [ 'STATUS' ] == 'TENTATIVE' ) {
// Make the text a bit lighter and italic
contentEventElement . style . fontStyle = 'italic' ;
contentEventElement . style . color = 'gray' ;
// Add note
let unconfirmedElement = document . createElement ( 'div' ) ;
unconfirmedElement . style . fontWeight = 'bold' ;
unconfirmedElement . appendChild ( document . createTextNode ( '⚠️ Non confermato!' ) ) ;
contentEventElement . appendChild ( unconfirmedElement ) ;
}
else if ( json [ i ] [ 'STATUS' ] == 'CANCELLED' ) {
// Strike time, date and location to remark this concept
dateText . style . textDecoration = 'line-through' ;
timeText . style . textDecoration = 'line-through' ;
locationText . style . textDecoration = 'line-through' ;
// Add note
let unconfirmedElement = document . createElement ( 'div' ) ;
unconfirmedElement . style . fontWeight = 'bold' ;
unconfirmedElement . appendChild ( document . createTextNode ( '⚠️ Cancellato!' ) ) ;
contentEventElement . appendChild ( unconfirmedElement ) ;
}
}
let dateElement = document . createElement ( 'div' ) ;
let dateText = document . createElement ( 'time' ) ;
dateText . appendChild ( document . createTextNode ( m . format ( 'dddd D MMMM YYYY' ) ) ) ;
dateElement . appendChild ( document . createTextNode ( '📅 ' ) ) ;
dateElement . appendChild ( dateText ) ;
contentEventElement . appendChild ( dateElement ) ;
let timeElement = document . createElement ( 'div' ) ;
let timeText = document . createElement ( 'time' ) ;
let timeString = ( json [ i ] [ 'ALLDAY' ] == true ) ? 'Tutto il giorno' : ( 'ore ' + m . format ( 'HH:mm' ) ) ;
timeText . appendChild ( document . createTextNode ( timeString ) ) ;
timeElement . appendChild ( document . createTextNode ( '⏰ ' ) ) ;
timeElement . appendChild ( timeText ) ;
contentEventElement . appendChild ( timeElement ) ;
// add if location is not empty -- default location should be selected by backend
let locationElement = document . createElement ( 'div' ) ;
let locationText = document . createElement ( 'span' ) ;
if ( json [ i ] [ 'LOCATION' ] !== undefined && json [ i ] [ 'LOCATION' ] != '' ) {
let text = decodeURI ( json [ i ] [ 'LOCATION' ] ) ;
let textNode = document . createTextNode ( text ) ;
if ( text . match ( "^(http|https):\\/\\/.*$" ) ) {
let anchorElement = document . createElement ( 'a' ) ;
anchorElement . href = text ;
anchorElement . appendChild ( textNode ) ;
locationText . appendChild ( anchorElement ) ;
} else {
locationText . appendChild ( textNode ) ;
}
}
else {
locationText . appendChild ( document . createTextNode ( 'Officina Informatica' ) ) ;
}
locationElement . appendChild ( document . createTextNode ( '📍 ' ) ) ;
locationElement . appendChild ( locationText ) ;
contentEventElement . appendChild ( locationElement ) ;
if ( json [ i ] [ 'DESCRIPTION' ] !== undefined && json [ i ] [ 'DESCRIPTION' ] != '' ) {
let descriptionElement = document . createElement ( 'div' ) ;
let descriptionText = document . createElement ( 'span' ) ;
descriptionText . appendChild ( document . createTextNode ( decodeURI ( json [ i ] [ 'DESCRIPTION' ] ) ) ) ;
descriptionElement . appendChild ( document . createTextNode ( '📝 ' ) ) ;
descriptionElement . appendChild ( descriptionText ) ;
descriptionElement . style . display = "none" ;
// Display a clickable "..." button.
let moreElement = document . createElement ( 'div' ) ;
let moreShowText = document . createElement ( 'a' ) ;
let moreHideText = document . createElement ( 'a' ) ;
moreShowText . appendChild ( document . createTextNode ( '⬇️ Più informazioni...' ) ) ;
moreElement . appendChild ( moreShowText ) ;
moreHideText . appendChild ( document . createTextNode ( '⬆️ Nascondi informazioni...' ) ) ;
moreHideText . style . display = "none" ;
moreElement . appendChild ( moreHideText ) ;
moreElement . onclick = ( ) => {
if ( descriptionElement . style . display == "none" ) {
descriptionElement . style . display =
moreHideText . style . display = "block" ;
moreShowText . style . display = "none" ;
} else {
descriptionElement . style . display =
moreHideText . style . display = "none" ;
moreShowText . style . display = "block" ;
}
} ;
contentEventElement . appendChild ( moreElement ) ;
contentEventElement . appendChild ( descriptionElement ) ;
}
ul . appendChild ( eventElement ) ;
atLeastOne = true ;
}
if ( atLeastOne == false ) {
let warn = document . createElement ( 'span' ) ;
warn . textContent = 'Nessun appuntamento in programma' ;
ul . appendChild ( warn ) ;
}
} )
. catch ( err => {
let warn = document . createElement ( 'span' ) ;
warn . textContent = 'Errore nella connessione alle API zerocalcare.' ;
console . log ( class _prefix + "error: " + err ) ;
ul . appendChild ( warn ) ;
} ) ; ;
}
function counters ( ) {
fetch _calendar ( ) ;
fetch ( "https://golem.linux.it/wp/wp-json/wp/v2/posts" )
. then ( response => response . json ( ) )