<%
var proj = theme.wiki.tree[page.wiki]

function layoutSectionHeader(title) {
  if (!title || title.length === 0) {
    return ''
  }
  return '<div class="widget-header dis-select"><span class="name">' + escape_html(title) + '</span></div>'
}

function layoutDocTree(pages) {
  var entries = []
  for (var docPage of pages) {
    if (docPage.title == null || docPage.title.length === 0 || proj.pages.length <= 1) {
      continue
    }
    entries.push({
      href: pretty_url(docPage.path),
      title: docPage.title,
      active: docPage.path === page.path,
      gradientRef: 'leftbar-active-icon-grad'
    })
  }
  return partial('_partial/components/collection', {options: {
    layout: 'list',
    variant: 'nav',
    density: 'compact',
    items: entries
  }})
}

function layoutDiv() {
  if (proj == null || proj.pages == null || proj.pages.length === 0 || proj.sections == null || proj.sections.length === 0) {
    return ''
  }
  var body = ''
  for (var section of proj.sections) {
    if (section.pages.length === 0) {
      continue
    }
    var collection = (layoutDocTree(section.pages) || '').trim()
    if (collection.length > 0) {
      body += '<section class="widget-section">'
      body += layoutSectionHeader(section.title)
      body += collection
      body += '</section>'
    }
  }
  return partial('_partial/components/widget-frame', {
    className: 'doc-tree-widget',
    body: body,
    attrs: {'data-wiki': page.wiki},
    reveal: false
  })
}
%>

<%- layoutDiv() %>
