2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct PrefilledTopBarTemplate {
|
|
|
|
|
|
|
|
let language: String
|
|
|
|
|
|
|
|
let sections: [SectionInfo]
|
|
|
|
|
|
|
|
let topBarWebsiteTitle: String
|
|
|
|
|
2022-08-28 14:01:53 +02:00
|
|
|
private let factory: TemplateFactory
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-28 14:01:53 +02:00
|
|
|
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws {
|
|
|
|
self.factory = factory
|
2022-08-16 10:39:05 +02:00
|
|
|
self.language = language
|
|
|
|
self.sections = sections
|
|
|
|
self.topBarWebsiteTitle = topBarWebsiteTitle
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func generate(sectionUrl: String?, languageButton: String?) -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
var content = [TopBarTemplate.Key : String]()
|
|
|
|
content[.title] = topBarWebsiteTitle
|
2022-08-28 14:01:53 +02:00
|
|
|
content[.titleLink] = factory.html.topBarWebsiteTitle(language: language)
|
2022-08-26 17:40:51 +02:00
|
|
|
content[.elements] = elements(activeSectionUrl: sectionUrl)
|
2022-08-28 14:01:53 +02:00
|
|
|
content[.languageButton] = languageButton.unwrapped(factory.html.topBarLanguageButton) ?? ""
|
|
|
|
return factory.topBar.generate(content)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func elements(activeSectionUrl: String?) -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
sections
|
2022-08-28 14:01:53 +02:00
|
|
|
.map { factory.html.topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url) }
|
2022-08-16 10:39:05 +02:00
|
|
|
.joined(separator: "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SectionInfo {
|
|
|
|
|
|
|
|
let name: String
|
|
|
|
|
|
|
|
let url: String
|
|
|
|
}
|
|
|
|
}
|