From 511ae5ac0b445527e7b375a5dde56b6d71623359 Mon Sep 17 00:00:00 2001 From: Aleksei Voronov Date: Fri, 25 Mar 2016 13:56:49 +0300 Subject: [PATCH] Implement a basic version of Event JSON-LD transform Refs #27 --- client/src/js/models/event.js | 41 +++++++++++++++++++++++++++++++++- client/src/templates/event.ejs | 24 ++++---------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/client/src/js/models/event.js b/client/src/js/models/event.js index a4ccfd4..b28fa21 100644 --- a/client/src/js/models/event.js +++ b/client/src/js/models/event.js @@ -1,7 +1,7 @@ import moment from 'moment'; import Model from '../base/model'; -import {capfirst, range} from '../utils'; +import {capfirst, range, makeAbsoluteURL} from '../utils'; import Place from './place'; @@ -51,6 +51,16 @@ export default class Event extends Model { isExhibition() { return this.data.categories.indexOf('exhibition') !== -1; } + + getItemType() { + if (this.isFestival()) { + return 'Festival'; + } else if (this.isExhibition()) { + return 'ExhibitionEvent'; + } else { + return 'TheaterEvent'; + } + } } @@ -92,9 +102,38 @@ export class Date { return moment.duration((this.endTs - this.startTs) * 1000); } + hasKnownEnd() { + return this.endTs != this.startTs; + } + isActual() { return this.endTs > moment().unix(); } + + toJSONLD(app) { + const url = app.resolver.reverse('event', {id: this.event.data.id}); + const result = { + '@context': 'http://schema.org', + '@type': this.event.getItemType(), + name: this.event.getShortTitle(), + url: makeAbsoluteURL(url), + } + + const place = this.event.getPlace(); + if (place) { + result.location = place.toJSONLD(app); + } + + if (this.isDateBased) { + result.startDate = this.start.format('YYYY-MM-DD'); + result.endDate = this.end.format('YYYY-MM-DD'); + } else { + result.startDate = this.start.format('YYYY-MM-DD[T]HH:mm'); + if (this.hasKnownEnd()) result.endDate = this.end.format('YYYY-MM-DD[T]HH:mm'); + } + + return result; + } } diff --git a/client/src/templates/event.ejs b/client/src/templates/event.ejs index 27db144..4447b43 100644 --- a/client/src/templates/event.ejs +++ b/client/src/templates/event.ejs @@ -98,10 +98,10 @@
<%- date.start.format('dddd') %>
- <% if (date.start.isSame(date.end, 'minute')) { %> - <%- date.start.format('LT') %> - <% } else { %> + <% if (date.hasKnownEnd()) { %> <%- date.start.format('LT') %>–<%- date.end.format('LT') %> + <% } else { %> + <%- date.start.format('LT') %> <% } %>
<% } %> @@ -151,23 +151,7 @@ <% event.getDates().forEach(function(date) { %> - + <% }) %>