'use strict'; var CheatSheets = { render: function(params) { var el = document.getElementById('content'); if (params[0]) { this.renderSingle(el, params[0]); return; } el.textContent = 'Lade Cheat Sheets...'; var self = this; API.get('/cheatsheets').then(function(data) { self.renderList(el, data.cheatsheets); }) .catch(function(e) { el.textContent = e.message; }); }, renderList: function(el, sheets) { var groups = {}; sheets.forEach(function(s) { var cat = s.category || 'Sonstige'; if (!groups[cat]) groups[cat] = []; groups[cat].push(s); }); var h = ''; h += ''; h += '
'; Object.keys(groups).forEach(function(cat) { h += '

' + Markdown.esc(cat) + '

'; groups[cat].forEach(function(s) { h += '
' + Markdown.esc(s.title) + '
'; h += '
' + Markdown.esc(s.category) + '
'; }); h += '
'; }); h += '
'; el.innerHTML = h; el.querySelectorAll('.sheet-card').forEach(function(card) { card.addEventListener('click', function() { location.hash = '#/cheatsheets/' + card.dataset.slug; }); }); var self = this; var searchTimer; document.getElementById('sheet-search').addEventListener('input', function() { clearTimeout(searchTimer); var q = this.value; searchTimer = setTimeout(function() { if (q.length < 2) { API.get('/cheatsheets').then(function(data) { self.renderList(el, data.cheatsheets); }); } else { API.get('/cheatsheets/search?q=' + encodeURIComponent(q)).then(function(data) { var list = document.getElementById('sheet-list'); if (!list) return; var lh = '
'; data.cheatsheets.forEach(function(s) { lh += '
' + Markdown.esc(s.title) + '
' + Markdown.esc(s.category) + '
'; }); lh += '
'; if (data.cheatsheets.length === 0) lh = '
Keine Treffer
'; list.innerHTML = lh; list.querySelectorAll('.sheet-card').forEach(function(card) { card.addEventListener('click', function() { location.hash = '#/cheatsheets/' + card.dataset.slug; }); }); }).catch(function() {}); } }, 300); }); }, renderSingle: function(el, slug) { el.textContent = 'Lade...'; API.get('/cheatsheets/' + slug).then(function(data) { var s = data.cheatsheet; var h = '← Zurueck zu Cheat Sheets'; h += '
' + Markdown.render(s.content_md) + '
'; el.innerHTML = h; }).catch(function(e) { el.textContent = e.message; }); } };