Hugo go go go
Made a hugo setup from scratch to generate a static blog and portfolio webpage. The old stuff may still be found in static/old.
This commit is contained in:
81
static/old/it1/gjestebok/gjestebok.js
Normal file
81
static/old/it1/gjestebok/gjestebok.js
Normal file
@@ -0,0 +1,81 @@
|
||||
|
||||
var db = firebase.firestore()
|
||||
|
||||
class Comment {
|
||||
constructor(name, message, id=0) {
|
||||
this.name = name,
|
||||
this.message = message,
|
||||
this.cfid = id
|
||||
}
|
||||
toString() {
|
||||
return this.name+': '+this.message
|
||||
}
|
||||
toHtml(elm) {
|
||||
elm.append('<p class="kommentar"></p>')
|
||||
$($('.kommentar')[$('.kommentar').length-1]).text(this.toString())
|
||||
firebase.auth().currentUser?$($('.kommentar')[$('.kommentar').length-1]).append(' <a class="btn btn-primary" href="javascript: remove(\''+this.cfid+'\')">Slett</a>'):undefined
|
||||
}
|
||||
}
|
||||
|
||||
commentConverter = {
|
||||
toFirestore: function(comment) {
|
||||
return {
|
||||
navn: comment.name,
|
||||
melding: comment.message
|
||||
}
|
||||
},
|
||||
fromFirestore: function(snapshot, options) {
|
||||
const data = snapshot.data(options)
|
||||
return new Comment(
|
||||
data.navn,
|
||||
data.melding,
|
||||
snapshot.id
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var list = function() {
|
||||
db.collection('gjestebok')
|
||||
.withConverter(commentConverter)
|
||||
.get()
|
||||
.then(function(query) {
|
||||
$('#kommentarer').html('<br/>')
|
||||
query.forEach(function(doc) {
|
||||
book = doc.data()
|
||||
book.toHtml($('#kommentarer'))
|
||||
})
|
||||
}).catch(function(error) {
|
||||
$('#status').html('<h3>Kunne ikke hente kommentarer!</h3><br/><p>'+error.message+'</p>')
|
||||
$('#status').css('color', 'red')
|
||||
})
|
||||
}
|
||||
|
||||
var add = function() {
|
||||
db.collection("gjestebok")
|
||||
.withConverter(commentConverter)
|
||||
.add(new Comment(
|
||||
$('#navn')[0].value,
|
||||
$('#melding')[0].value
|
||||
)).then(function() {
|
||||
$('#status').html('<p>La til melding med navn '+$('#navn')[0].value+'!</p>')
|
||||
$('#status').css('color', 'green')
|
||||
$('#kommentarer').html('')
|
||||
list()
|
||||
}).catch(function() {
|
||||
$('#status').html('<p>Problem med å legge til melding med navn '+$('#navn')[0].value+'!</p>')
|
||||
$('#status').css('color', 'red')
|
||||
})
|
||||
}
|
||||
|
||||
var remove = function(id) {
|
||||
db.collection("gjestebok").doc(id)
|
||||
.delete().then(function() {
|
||||
$('#kommentarer').html('')
|
||||
list()
|
||||
}).catch(function(error) {
|
||||
alert("Error removing document: ", error)
|
||||
})
|
||||
}
|
||||
|
||||
list()
|
||||
|
38
static/old/it1/gjestebok/index.html
Normal file
38
static/old/it1/gjestebok/index.html
Normal file
@@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Firebase basert gjestebok</title>
|
||||
<meta charset="utf-8">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="../main.css">
|
||||
<link rel="stylesheet" href="../css/form.css">
|
||||
<link rel="stylesheet" href="../css/gjestebok.css">
|
||||
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-app.js"></script>
|
||||
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-auth.js"></script>
|
||||
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-firestore.js"></script>
|
||||
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
||||
<script type="text/javascript" src="../js/init-firebase.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/javascript" id="navbar" src="../js/navbar.js"></script>
|
||||
<div id="content">
|
||||
<form action="#" method="POST" id="form" class="top">
|
||||
<div class="form-group">
|
||||
<div class="row">
|
||||
<div class="col col-sm-3"><input type="name" class="form-control" id="navn" placeholder="Navn"></div>
|
||||
<div class="col col-sm-9"><input type="name" class="form-control" id="melding" placeholder="Melding"></div>
|
||||
</div>
|
||||
</div>
|
||||
<a type="submit" class="btn btn-primary" href="javascript: add()">Legg til</a>
|
||||
</form>
|
||||
<div id="status">
|
||||
<p> </p>
|
||||
</div>
|
||||
<!-- <a type="submit" class="btn btn-primary" href="javascript: list()">Hent kommentarer</a> -->
|
||||
<div id="kommentarer">
|
||||
</div>
|
||||
<p id="status"></p>
|
||||
</div>
|
||||
<script type="text/javascript" src="gjestebok.js"></script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user