import QtQuick 2.2 import Sailfish.Silica 1.0 import "../components/Markdown.js" as Markdown import "../components" // Every tag in the vault, with how many notes carry it. Built from a scan (the // tags live inside the notes, so the only way to know them is to read them): // front-matter tags and inline #tags both count, case-insensitively. Page { id: page property var dav property string rootPath: "" // "" = whole vault property bool scanning: false property int total: 0 property int read: 0 property int errors: 0 // tag (lower case) -> { name: original case, count: n } property var found: ({}) property int tagCount: 0 property string coverText: qsTr("Tags") ListModel { id: tagsModel } Component.onCompleted: start() function start() { if (!dav) return tagsModel.clear() found = ({}) tagCount = 0 total = 0 read = 0 errors = 0 scanning = true dav.scanVault(rootPath) } function addTags(content) { var d = Markdown.tagDetails(content || "") var all = d.frontMatter.concat(d.inline) for (var i = 0; i < all.length; i++) { var name = all[i] var key = name.toLowerCase() var known = found[key] if (known) { known.count += 1 } else { found[key] = { name: name, count: 1 } tagCount += 1 } } } // Most used first, then alphabetical: the list is a map of the vault. function rebuild() { tagsModel.clear() var list = [] for (var key in found) { if (found.hasOwnProperty(key)) list.push(found[key]) } list.sort(function (a, b) { if (a.count !== b.count) return b.count - a.count var an = a.name.toLowerCase(), bn = b.name.toLowerCase() return an < bn ? -1 : (an > bn ? 1 : 0) }) for (var i = 0; i < list.length; i++) { tagsModel.append({ tag: list[i].name, uses: list[i].count }) } } Connections { target: page.dav onScanStart: { page.total = count page.scanning = count > 0 } onScanProgress: { page.read = done page.total = all } onScanFile: page.addTags(content) onScanFinished: { page.read = done page.errors = errs page.scanning = false page.rebuild() } onFailed: banner.show(message, true) } SilicaListView { id: listView anchors.fill: parent model: tagsModel header: Column { width: parent.width PageHeader { title: qsTr("Tags") } Label { visible: page.scanning x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge text: page.total > 0 ? qsTr("Reading notes: %1 of %2").arg(page.read).arg(page.total) : qsTr("Looking for notes...") color: Theme.secondaryColor font.pixelSize: Theme.fontSizeSmall } Label { visible: !page.scanning x: Theme.paddingLarge width: parent.width - 2 * Theme.paddingLarge text: page.tagCount === 1 ? qsTr("1 tag in the notes") : qsTr("%1 tags in the notes").arg(page.tagCount) color: Theme.secondaryColor font.pixelSize: Theme.fontSizeSmall } } ViewPlaceholder { enabled: !page.scanning && tagsModel.count === 0 text: qsTr("No tags in the notes. Write #tag in a note or add a tags: field at the top.") } delegate: ListItem { contentHeight: Theme.itemSizeSmall onClicked: pageStack.push(Qt.resolvedUrl("SearchPage.qml"), { dav: page.dav, tagFilter: model.tag, rootPath: "" }) Label { x: Theme.paddingLarge anchors.verticalCenter: parent.verticalCenter width: parent.width - 2 * Theme.paddingLarge - usesLabel.width - Theme.paddingLarge text: "#" + model.tag color: Theme.highlightColor truncationMode: TruncationMode.Fade } Label { id: usesLabel anchors.right: parent.right anchors.rightMargin: Theme.paddingLarge anchors.verticalCenter: parent.verticalCenter text: model.uses color: Theme.secondaryColor font.pixelSize: Theme.fontSizeSmall } } VerticalScrollDecorator { id: decorator } PullDownMenu { MenuItem { text: qsTr("Scan again") onClicked: page.start() } MenuItem { visible: page.dav && page.dav.scanning text: qsTr("Stop") onClicked: page.dav.cancelScan() } } BusyIndicator { anchors.centerIn: parent size: BusyIndicatorSize.Large running: page.scanning } } onStatusChanged: { if (status === PageStatus.Inactive && page.dav && page.dav.scanning) page.dav.cancelScan() } Banner { id: banner } }