Skip to content

Commit

Permalink
feat(helpers): handlebars link helper is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
gerard2perez committed Nov 3, 2017
1 parent ab9d961 commit b94988b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 31 deletions.
15 changes: 8 additions & 7 deletions src/support/KoatonRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ export default class KoatonRouter {
let content = null;
try {
content = require(ProyPath(router.loc, 'controllers', controller));
/* istanbul ignore if */
if (content && !content.default) {
if (!content.default) {
console.warn(`${controller} controller does not export any default. This will not be supported`);
} else if (content && content.default) {
} else {
content = content.default;
}
} catch (err) {
debug(err);
}
console.warn(content, actions);
let action = DeepGet(content, ...actions) || DefaultView(controller);
return action;
}
Expand Down Expand Up @@ -242,10 +242,11 @@ export default class KoatonRouter {
url = undefined;
}
let controller;
try {
controller = require(ProyPath(this.loc, 'controllers', model)).default;
} catch (ex) {
controller = {};
controller = require(ProyPath(this.loc, 'controllers', model));
if (!controller.default) {
console.warn('controller must export a default variable; not exporting a default won\'t be supporter in v3');
} else {
controller = controller.default;
}
controller = Object.assign({
Name: model,
Expand Down
2 changes: 1 addition & 1 deletion src/views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ for (const engine of avaliableEngines) {
* @param {String} file - relative to views/
* @return {Object}
*/
function template (file, locals = {}) {
function template (file, locals) {
let fullpath = resolve('views', file);
const [engine, target] = ex2engine(fullpath);
if (engine !== 'html') {
Expand Down
58 changes: 35 additions & 23 deletions src/views/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ import * as fs from 'fs';
import KoatonRouter from '../support/KoatonRouter';
import deprecate from '../support/deprecate';

/**
* Translate the given work to the current locale of to a one specified in the arguments.
* @param {object} ctx The current context in wich the helper is executing.
* @param {String} url The url to genereate the link.
* @return {String} The helper content.
*/
function makeLink (ctx, url, render) {
let route = KoatonRouter.AllRoutes(ctx.subdomain).map(exp => {
let variables = (exp[1].match(/\$/g) || []).length;
let matches = (url.match(exp[0]) || []).length - 1;
if (matches === variables && url.replace(...exp).indexOf('.') === -1) {
return url.replace(...exp);
}
}).filter(r => !!r);
route = url === 'home' ? '/' : route[0];
let content;
let active = ctx.route === url || ctx.path === route;
if (ctx.path === route) {
content = `<a class="active">${render(this)}</a>`;
} else {
content = `<a href="${route}" class="${active ? 'active' : ''}">${render()}</a>`;
}
return content;
}
/**
* Translate the given work to the current locale of to a one specified in the arguments.
* @param {String} key The key to use in the translation sheet.
* @param {String} locale Custom locale to translate the key.
* @return {String} localized key.
*/
function translate (key, locale) {
if (locale) {
let loc = i18n.getLocale();
Expand All @@ -25,6 +55,9 @@ function handlebars () {
const Handlebars = require(ProyPath('node_modules', 'handlebars'));
const layouts = require(ProyPath('node_modules', 'handlebars-layouts'));
Handlebars.registerHelper(layouts(Handlebars));
Handlebars.registerHelper('link', function (url, helper) {
return makeLink(helper.data.root, url, helper.fn);
});
// Bundles
Handlebars.registerHelper('bundle', function (bundle) {
if (Kmetadata.bundles[bundle] === undefined) {
Expand Down Expand Up @@ -64,7 +97,7 @@ function nunjucks () {
// parse the body and possibly the error block, which is optional
let body = parser.parseUntilBlocks('error', 'endlink');
let errorBody = null;

/* istanbul ignore next */
if (parser.skipSymbol('error')) {
parser.skip(lexer.TOKEN_BLOCK_END);
errorBody = parser.parseUntilBlocks('endlink');
Expand All @@ -76,23 +109,7 @@ function nunjucks () {
return new nodes.CallExtension(this, 'run', args, [body, errorBody]);
}
run (context, url, body, errorBody) {
let route = KoatonRouter.AllRoutes(context.ctx.subdomain).map(exp => {
let variables = (exp[1].match(/\$/g) || []).length;
let matches = (url.match(exp[0]) || []).length - 1;
if (matches === variables && url.replace(...exp).indexOf('.') === -1) {
return url.replace(...exp);
}
}).filter(r => !!r);
route = url === 'home' ? '/' : route[0];
let content;
console.log(context.ctx.path, context.ctx.route, url, route);
let active = context.ctx.route === url || context.ctx.path === route;
if (context.ctx.path === route) {
content = `<a class="active">${body()}</a>`;
} else {
content = `<a href="${route}" class="${active ? 'active' : ''}">${body()}</a>`;
}
return new nunjucks.runtime.SafeString(content);
return new nunjucks.runtime.SafeString(makeLink(context.ctx, url, body));
}
}
env.addFilter('bundle', function (bundle) {
Expand All @@ -103,11 +120,6 @@ function nunjucks () {
});
env.addFilter('i18n', _i18n);
env.addFilter('t', translate);
// env.addFilter('link', function(name, callback) {
// console.log(name);
// console.log(callback);
// callback();
// }, true);
env.addExtension('Link', new Link());
return env;
}
Expand Down
28 changes: 28 additions & 0 deletions test/integration/html_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ describe('Simple Router Responses', function () {
done(null, null);
}, done).catch(done);
});
it('gets a compiled noengine response', function (done) {
server.get('/helpers/njk').then(function (body) {
assert.ok(body.html().indexOf('a href="/"') > -1, body.html());
assert.ok(body.html().indexOf('a href="/helpers"') > -1, body.html());
assert.ok(body.html().indexOf('a href="/helpers/handlebars"') > -1, body.html());
assert.ok(body.html().indexOf('a class="active"') > -1, body.html());
assert.ok(body.html().indexOf('Traducido') > -1, body.html());
done(null, null);
}, done).catch(done);
});
it('gets a compiled noengine response', function (done) {
server.get('/helpers/handlebars').then(function (body) {
assert.ok(body.html().indexOf('a href="/"') > -1, body.html());
assert.ok(body.html().indexOf('a href="/helpers/njk"') > -1, body.html());
assert.ok(body.html().indexOf('a href="/helpers"') > -1, body.html());
assert.ok(body.html().indexOf('a class="active"') > -1, body.html());
assert.ok(body.html().indexOf('Traducido') > -1, body.html());
done(null, null);
}, done).catch(done);
});
it('gets a compiled response directly inserted in router', function (done) {
server.get('/helpers').then(function (body) {
assert.ok(body.html().indexOf('<title>Nunjucks title</title>') > -1);
assert.ok(body.html().indexOf('Translated') > -1);
assert.ok(body.html().indexOf('Traducido') > -1);
done(null, null);
}, done).catch(done);
});
it('translates handlebars page', function (done) {
server.get('/handlebars').then(function (body) {
assert.ok(body.html().indexOf('<title>Handlebars Title</title>') > -1);
Expand Down

0 comments on commit b94988b

Please sign in to comment.