Skip to content

Commit

Permalink
Make CurrentTime work on an id basis
Browse files Browse the repository at this point in the history
The JavaScript that updates instances of CurrentTime work on an id
basis, instead of a class basis.
  • Loading branch information
moosichu committed Nov 6, 2017
1 parent 293b6bc commit d2b7d1e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
29 changes: 12 additions & 17 deletions Sources/HaCWebsiteLib/ViewModels/Hackathons/CurrentTime.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import HaCTML
import Foundation

struct CurrentTime : Nodeable {
static let className = "RealTimeClock"
static let element = El.Span[Attr.className => CurrentTime.className].containing("Current Time")
static var ScriptUsed = false
let id = "CurrentTime\(UUID().description)"

var node: Node {
if(!CurrentTime.ScriptUsed) {
CurrentTime.ScriptUsed = true
return Fragment(
CurrentTime.element,
// TODO: get this script to be loaded from CurrentTime.js
//Script(file: "CurrentTime.js", escapes: ["className": CurrentTime.className]).node
El.Script.containing(TextNode(
"function updateClock() { const current = new Date(); Array.from(document.getElementsByClassName(\"\(CurrentTime.className)\")).map(function(x) { x.innerHTML = current.getHours()+\":\"+ (current.getMinutes()<10?\"0\":\"\") + current.getMinutes(); });} updateClock(); setInterval(updateClock,1000);",
escapeLevel: .unsafeRaw
))
)
} else {
return CurrentTime.element
}
return Fragment(
El.Span[Attr.id => id].containing("Current Time"),
// TODO: get this script to be loaded from CurrentTime.js
//Script(file: "CurrentTime.js", escapes: ["className": CurrentTime.className]).node
El.Script.containing(TextNode(
"function updateClock() { const current = new Date(); document.getElementById(\"\(id)\").innerHTML = current.getHours()+\":\"+ (current.getMinutes()<10?\"0\":\"\") + current.getMinutes(); } updateClock(); setInterval(updateClock,1000);",
escapeLevel: .unsafeRaw
))
)
}
}
8 changes: 4 additions & 4 deletions Sources/HaCWebsiteLib/ViewModels/Hackathons/Schedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ struct Schedule : Nodeable {
return El.Ul.containing(
schedule.map {
El.Li.containing(
TextNode($0.key),
TextNode(" "),
TextNode($0.value)
$0.key,
" ",
$0.value
)
}
)
}
}
}

0 comments on commit d2b7d1e

Please sign in to comment.