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-09-08 09:30:54 +02:00
|
|
|
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) {
|
2022-08-28 14:01:53 +02:00
|
|
|
self.factory = factory
|
2022-08-16 10:39:05 +02:00
|
|
|
self.language = language
|
|
|
|
self.sections = sections
|
|
|
|
self.topBarWebsiteTitle = topBarWebsiteTitle
|
|
|
|
}
|
|
|
|
|
2022-09-04 17:47:13 +02:00
|
|
|
func generate(sectionUrl: String?, languageButton: String?, page: Element) -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
var content = [TopBarTemplate.Key : String]()
|
|
|
|
content[.title] = topBarWebsiteTitle
|
2022-09-04 17:47:13 +02:00
|
|
|
content[.titleLink] = factory.html.topBarWebsiteTitle(language: language, from: page)
|
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
|
|
|
|
}
|
|
|
|
}
|