Check page state for overviews

This commit is contained in:
Christoph Hagen 2022-08-26 22:29:32 +02:00
parent 04272c620e
commit fff62f4b44
4 changed files with 64 additions and 27 deletions

View File

@ -250,11 +250,23 @@ extension Element {
elements.contains { $0.containsElements }
}
var sortedItems: [Element] {
if useManualSorting {
return elements.sorted { $0.sortIndex! < $1.sortIndex! }
func itemsForOverview(_ count: Int? = nil) -> [Element] {
if let shownItemCount = count {
return Array(sortedItems.prefix(shownItemCount))
} else {
return sortedItems
}
return elements.sorted { $0.date! > $1.date! }
}
private var sortedItems: [Element] {
if useManualSorting {
return shownItems.sorted { $0.sortIndex! < $1.sortIndex! }
}
return shownItems.sorted { $0.date! > $1.date! }
}
private var shownItems: [Element] {
elements.filter { $0.state.isShownInOverview }
}
/**
@ -340,9 +352,9 @@ extension Element {
/**
Get the next language to switch to with the language button.
*/
func nextLanguage(for languageIdentifier: String) -> String? {
func nextLanguage(for language: String) -> String? {
let langs = languages.map { $0.language }
guard let index = langs.firstIndex(of: languageIdentifier) else {
guard let index = langs.firstIndex(of: language) else {
return nil
}
for i in 1..<langs.count {
@ -350,7 +362,7 @@ extension Element {
guard hasContent(for: next) else {
continue
}
guard next != languageIdentifier else {
guard next != language else {
return nil
}
@ -393,7 +405,10 @@ extension Element {
}
private func hasContent(for language: String) -> Bool {
existingContentUrl(for: language) != nil
if !elements.isEmpty {
return true
}
return existingContentUrl(for: language) != nil
}
}

View File

@ -16,3 +16,26 @@ enum PageState: String {
*/
case hide
}
extension PageState {
var isShownInOverview: Bool {
switch self {
case .standard, .draft:
return true
case .hide:
return false
}
}
var hasThumbnailLink: Bool {
switch self {
case .standard:
return true
case .draft:
return false
case .hide:
return false
}
}
}

View File

@ -23,7 +23,11 @@ struct OverviewSectionGenerator {
var content = [OverviewSectionTemplate.Key : String]()
content[.url] = relativeUrl
content[.title] = metadata.title
content[.items] = sectionContent(section: section, in: parent, language: language, shownItemCount: sectionItemCount)
content[.items] = generator.generateContent(
items: section.itemsForOverview(sectionItemCount),
parent: parent,
language: language,
style: section.thumbnailStyle)
content[.more] = metadata.moreLinkText
return multipleSectionsTemplate.generate(content)
@ -33,21 +37,11 @@ struct OverviewSectionGenerator {
func generate(section: Element, language: String) -> String {
var content = [OverviewSectionCleanTemplate.Key : String]()
content[.items] = sectionContent(section: section, in: section, language: language, shownItemCount: nil)
return singleSectionsTemplate.generate(content)
}
private func sectionContent(section: Element, in parent: Element, language: String, shownItemCount: Int?) -> String {
let sectionItems: [Element]
if let shownItemCount = shownItemCount {
sectionItems = Array(section.sortedItems.prefix(shownItemCount))
} else {
sectionItems = section.sortedItems
}
return generator.generateContent(
items: sectionItems,
parent: parent,
content[.items] = generator.generateContent(
items: section.itemsForOverview(),
parent: section,
language: language,
style: section.thumbnailStyle)
return singleSectionsTemplate.generate(content)
}
}

View File

@ -14,13 +14,18 @@ struct ThumbnailListGenerator {
}
private func itemContent(_ item: Element, parent: Element, language: String, style: ThumbnailStyle) -> String {
let fullPageUrl = item.fullPageUrl(for: language)
let relativePageUrl = parent.relativePathToFileWithPath(fullPageUrl)
let fullThumbnailPath = item.thumbnailFilePath(for: language)
let relativeImageUrl = parent.relativePathToFileWithPath(fullThumbnailPath)
var content = [ThumbnailKey : String]()
content[.url] = "href=\"\(relativePageUrl)\""
if item.state.hasThumbnailLink {
#warning("If page in language is missing, link to different language")
let fullPageUrl = item.fullPageUrl(for: language)
let relativePageUrl = parent.relativePathToFileWithPath(fullPageUrl)
content[.url] = "href=\"\(relativePageUrl)\""
}
content[.image] = relativeImageUrl
content[.title] = item.title(for: language)
#warning("Generate thumbnail suffix")