github.io/it1/js/converters/bok.js

60 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-03-27 14:48:13 +00:00
class Book {
2020-03-27 21:24:01 +00:00
constructor(title, author, publisher, rating, published, id=0) {
this.cfid = id
2020-03-27 14:48:13 +00:00
this.title = title
this.author = author
this.publisher = publisher
this.rating = rating
2020-03-27 21:24:01 +00:00
this.published = published
2020-03-27 14:48:13 +00:00
}
toString() {
2020-03-27 21:24:01 +00:00
return this.title+' av '+this.author+', terningkast '+this.rating+', utgitt '+this.published.toString().slice(0,15)+', forlag; '+this.publisher
2020-03-27 14:48:13 +00:00
}
toHtml(elm) {
let span = function(selector, string, color='red') {
$($(selector)[$(selector).length-1]).append('<span style="color: '+color+'"></span>')
//$($(selector+' > span')[$(selector+' > span').length-1])
2020-03-27 14:48:13 +00:00
}
let user = firebase.auth().currentUser
elm.append('<p class="book"></p>', this.title)
span('.book')
//.text(this.toString())
if (user) {
$($('.kommentar')[$('.kommentar').length-1]).append(' &nbsp; <a class="btn btn-primary" href="javascript: remove(\''+this.cfid+'\')">Slett</a>')
}
} /*
let span = function(string, color='red') {
return '<span style="color: '+color+'">'+string+'</span>'
}
2020-03-27 21:24:01 +00:00
return '<p>'+span(this.title)+' av '+span(this.author)+', terningkast '+span(this.rating)+', utgitt '
+span(this.published.toString().slice(0,15), 'green')+', forlag; '+span(this.publisher, 'yellow')
+(user?' &nbsp; <a class="btn btn-primary" href="javascript: remove(\''+this.cfid+'\')">Slett</a>':'')+'</p>'
}*/
2020-03-27 14:48:13 +00:00
}
bookConverter = {
toFirestore: function(book) {
2020-03-27 21:24:01 +00:00
let millis = Date.parse(book.published)
2020-03-27 14:48:13 +00:00
return {
2020-03-27 21:24:01 +00:00
tittel: book.title,
forfatter: book.author,
forlag: book.publisher,
terningkast: book.rating,
utgitt: new firebase.firestore.Timestamp(millis/1000, 0)
2020-03-27 14:48:13 +00:00
}
},
fromFirestore: function(snapshot, options) {
const data = snapshot.data(options)
2020-03-27 21:24:01 +00:00
return new Book(
data.tittel,
data.forfatter,
data.forlag,
data.terningkast,
data.utgitt.toDate(),
snapshot.id
2020-03-27 22:29:16 +00:00
)
2020-03-27 14:48:13 +00:00
}
}