-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved templating into Mustache.js templates
- Loading branch information
Showing
20 changed files
with
920 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,107 @@ | ||
import * as Mustache from 'mustache' | ||
|
||
import I18n from '../../javascripts/lib/i18n' | ||
import { resizeContainer, render } from '../../javascripts/lib/helpers' | ||
import getPreloadTemplate from '../../templates/preload' | ||
import getDefaultTemplate from '../../templates/default' | ||
import { preloadTemplate } from '../../templates/preload' | ||
import { | ||
defaultTemplate, | ||
errorTemplate, | ||
} from '../../templates/default' | ||
import { | ||
emptyWarning, | ||
errorDetails, | ||
linkedWarning, | ||
plainWarning, | ||
ruler, | ||
} from '../../templates/partials' | ||
import { extractTags } from './tags' | ||
const MAX_HEIGHT = 1000 | ||
|
||
const tinApp = async (client, initial = false) => { | ||
if (initial) { | ||
await init(client).catch(e => handleError(e)) | ||
} | ||
await fullRender(client) | ||
if (initial) { | ||
await init(client) | ||
.catch(e => handleError(e, 'Could not initialize the application')) | ||
} | ||
await appRender(client) | ||
} | ||
|
||
const init = async (client) => { | ||
const currentUser = (await client.get('currentUser')).currentUser | ||
I18n.loadTranslations(currentUser.locale) | ||
render('.loader', getPreloadTemplate()) | ||
await fullRender(client).catch(e => handleError(e)) | ||
const currentUser = (await client.get('currentUser')).currentUser | ||
I18n.loadTranslations(currentUser.locale) | ||
render('.loader', preloadTemplate) | ||
await appRender(client) | ||
} | ||
|
||
const fullRender = async (client) => { | ||
const tags = await extractTags(client).catch(e => handleError(e)) | ||
const settings = await client.metadata().catch(e => handleError(e)) | ||
if (Boolean(tags) && Boolean(settings)) { | ||
render('.app', getDefaultTemplate({ "tags": tags, "settings": settings.settings })) | ||
const appRender = async (client) => { | ||
const tags = await extractTags(client) | ||
.catch(e => handleError(e, I18n.t('runtime.errors.tagsLoading'))) | ||
const settings = await client | ||
.metadata() | ||
.catch(e => handleError(e, I18n.t('runtime.errors.settingsLoading'))) | ||
const view = await buildView(tags, settings.settings) | ||
.catch(e => handleError(e, I18n.t('runtime.errors.viewLoading'))) | ||
|
||
if (view) { | ||
render('.app', view) | ||
} | ||
return resizeContainer(client, MAX_HEIGHT) | ||
} | ||
} | ||
|
||
const extractTags = async (client) => { | ||
let currentLocation = (await client.context().catch(e => { throw new Error(e) })).location | ||
currentLocation = currentLocation.replace('_sidebar', '.tags') | ||
let tags = await client.get(currentLocation).catch(e => { throw new Error(e) }) | ||
if (Object.keys(tags['errors']).length > 0) { | ||
let errorString = Object.keys(tags['errors']).reduce(key => `${key}: ${tags['errors'][key]}`, '') | ||
throw new Error(`API returned errors: ${errorString}`) | ||
} else { | ||
return tags[currentLocation] | ||
} | ||
const buildView = async (tags, settings) => { | ||
let config = {} | ||
let content = '' | ||
try { | ||
config = { | ||
tags: JSON.parse(settings.tags), | ||
styles: JSON.parse(settings.styles), | ||
defaults: JSON.parse(settings.defaults), | ||
} | ||
} catch (e) { | ||
handleError(e, I18n.t('runtime.errors.configParsing')) | ||
console.log(e) | ||
return | ||
} | ||
console.log(config) | ||
|
||
const objectTags = config.tags.filter(tag => tags.includes(tag.name)) | ||
if (!objectTags.length) { | ||
content = Mustache.render(emptyWarning, { | ||
description: config.defaults.desc || 'Coast is clear', | ||
}) | ||
console.log(content) | ||
} else { | ||
content = objectTags.map(tag => { | ||
let style = config.styles.find(style => style.name === tag.style) | ||
style = style ? style.style : (config.defaults.style || '') | ||
if (tag.link) { | ||
return Mustache.render(linkedWarning, { | ||
style: style, | ||
description: tag.desc, | ||
link: tag.link, | ||
linkText: I18n.t('runtime.doclink'), | ||
}) | ||
} | ||
return Mustache.render(plainWarning, { | ||
style: style, | ||
description: tag.desc, | ||
}) | ||
}).join(ruler) | ||
console.log(content) | ||
} | ||
|
||
return Mustache.render(defaultTemplate, { | ||
content | ||
}) | ||
} | ||
|
||
const handleError = (error) => { | ||
console.warn('An error happened: ', error.message) | ||
const handleError = (error, errorDescription) => { | ||
console.log(error) | ||
render('.app', Mustache.render(errorTemplate, { | ||
errorDescription, | ||
errorDetails: error.message, | ||
}, { | ||
errorDetails, | ||
})) | ||
} | ||
|
||
export default tinApp |
Oops, something went wrong.