From 20ebd784fc806593018ad84b19e45a01cd8cdb7b Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 8 Apr 2015 12:28:30 +0200 Subject: [PATCH] Add link-to helper with locale support --- app/assets/javascripts/cmtool/html_edit.js.coffee | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/assets/javascripts/cmtool/html_edit.js.coffee b/app/assets/javascripts/cmtool/html_edit.js.coffee index 17fa1b2..18834fa 100644 --- a/app/assets/javascripts/cmtool/html_edit.js.coffee +++ b/app/assets/javascripts/cmtool/html_edit.js.coffee @@ -43,3 +43,17 @@ class HtmlEdit text_field.hide() template_field.hide() @html_edit = new HtmlEdit() +Handlebars.registerHelper "link-to", (url, args..., options = {})-> + unless not url or url.match(/^http|^\/\w{2}\//) + # Starts with http or /:locale/ explicitly + if options.hash and options.hash.hasOwnProperty 'locale' + if locale = options.hash.locale + url = "/#{locale}/#{url}".replace('//', '/') + else if locale = $('#page_locale').val() + url = "/#{locale}/#{url}".replace('//', '/') + if options.fn + text = options.fn(this) + else + text = args[0] + link = "#{text}" + new Handlebars.SafeString(link)