// Client-side renderer. Contains local helpers etc and calls common render
// method internally.


'use strict';


var _      = require('lodash');
var date   = require('nodeca.core/lib/system/date');
var render = require('nodeca.core/lib/system/render/common');

// Expose jade runtime to `window.jade`
require('./jade_runtime.js');

////////////////////////////////////////////////////////////////////////////////


var tzOffset = (new Date).getTimezoneOffset();


////////////////////////////////////////////////////////////////////////////////


/**
 *  render(apiPath[, locals[, helpers]]) -> String
 *  - apiPath (String): Template to render, e.g. `forum.index`
 *  - locals (Object): Locals data to pass to the renderer function
 *  - helpers (Object): Helper functions and constants
 *
 *  Renders a template passing `locals` and `helpers` as `self` object within
 *  it. The difference between these is that `locals` is only for the specified
 *  template, but `helpers` passes forward to partials.
 **/
function renderWrapper(apiPath, locals, helpers) {
  var _helpers = {};

  _helpers.runtime = N.runtime;

  _helpers.t = N.runtime.t;

  _helpers.date = function dateWrapper(value, format) {
    return date(value, format, N.runtime.locale, tzOffset);
  };

  _helpers.asset_include = function assetsIncludeWrapper() {
    N.logger.error('asset_include() is a server-side only helper, ' +
                   'thus can be used in base layouts only.');
    return '';
  };

  _helpers.link_to = function linkToWrapper(name, params) {
    return N.runtime.router.linkTo(name, params) || '#';
  };

  _.extend(_helpers, helpers);

  return render(N, apiPath, locals, _helpers);
}


module.exports = renderWrapper;

@@ -328,16 +328,17 @@
 .js');%0A%0A
+%0A
 ////////
@@ -465,647 +465,42 @@
 ;%0A%0A%0A
-////////////////////////////////////////////////////////////////////////////////%0A%0A%0A/**%0A *  render(apiPath%5B, locals%5B, helpers%5D%5D) -%3E String%0A *  - apiPath (String): Template to render, e.g. %60forum.index%60%0A *  - locals (Object): Locals data to pass to the renderer function%0A *  - helpers (Object): Helper functions and constants%0A *%0A *  Renders a template passing %60locals%60 and %60helpers%60 as %60self%60 object within%0A *  it. The difference between these is that %60locals%60 is only for the specified%0A *  template, but %60helpers%60 passes forward to partials.%0A **/%0Afunction renderWrapper(apiPath, locals, helpers) %7B%0A  var _helpers = %7B%7D;%0A%0A  _helpers.
+var DEFAULT_HELPERS = %7B%0A  
 runtime
- =
+:
  N.r
@@ -509,25 +509,14 @@
 time
-;
 %0A%0A
-  _helpers.t =
+, t:
  N.r
@@ -527,28 +527,17 @@
 me.t
-;
 %0A%0A
+,
  
- _helpers.
 date
- =
+:
  fun
@@ -546,20 +546,20 @@
 ion date
-Wrap
+_hel
 per(valu
@@ -634,30 +634,20 @@
 et);%0A  %7D
-;
 %0A%0A
+,
  
- _helpers.
 asset_in
@@ -651,18 +651,17 @@
 _include
- =
+:
  functio
@@ -672,19 +672,20 @@
 sets
-I
+_i
 nclude
-Wrap
+_hel
 per(
@@ -842,31 +842,20 @@
 %0A  %7D
-;
 %0A%0A
+,
  
- _helpers.
 link_to
- =
+:
  fun
@@ -868,14 +868,15 @@
 link
-ToWrap
+_to_hel
 per(
@@ -954,42 +954,288 @@
 ;%0A  
+%7D%0A
 %7D;%0A%0A
-  _.extend(_helpers, helpers);
+%0A////////////////////////////////////////////////////////////////////////////////%0A%0A%0Amodule.exports = function clientRenderWrapper(apiPath, locals, helpers) %7B%0A  if (helpers) %7B%0A    helpers = _.extend(%7B%7D, DEFAULT_HELPERS, helpers);%0A  %7D else %7B%0A    helpers = DEFAULT_HELPERS;%0A  %7D
 %0A%0A  
@@ -1268,17 +1268,16 @@
 locals, 
-_
 helpers)
@@ -1283,39 +1283,6 @@
 );%0A%7D
-%0A%0A%0Amodule.exports = renderWrapper
 ;%0A

Refactor client-side `render` wrapper.