'use strict';


/**
 *  shared
 **/

/**
 *  shared.common
 **/


/*global nodeca, _*/


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


// see [[shared.common.render.getLayoutStack]]
function get_layout_stack(layout) {
  var stack = layout.split('.'), i, l;

  for (i = 1, l = stack.length; i < l; i++) {
    stack[i] = stack[i - 1] + '.' + stack[i];
  }

  return stack;
}


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


/**
 *  shared.common.render(views, path, layout, data) -> String
 *  - views (Object): Views tree (without locale and/or theme subpaths).
 *  - path (String): View name to render, e.g. `forums.index`
 *  - layout (String): Layout to render, e.g. `default.blogs`
 *  - data (Object): Locals data to pass to the renderer function
 *
 *  Renders view registered as `path` with given `layout` and returns result.
 *
 *      render(views, 'blogs.post.show', 'default.blogs');
 *
 *  In the example above, it will render `blogs.post.show` view with given
 *  `data`, then will render `default.blogs` layout with `data` where `content`
 *  property will be rendered view, then `default` layout with `data` where
 *  `content` property will be previously rendered layout.
 **/
module.exports = function render(views, path, layout, data) {
  var view = nodeca.shared.common.getByPath(views, path),
      html = view && view(data) || '';

  if (!view) {
    // Here we just notify that view not found.
    // This should never happen - one must check path existance before render()
    nodeca.logger.warn("View " + path + " not found");
  }

  if (layout) {
    layout = (_.isArray(layout) ? layout.slice() : get_layout_stack(layout));
    _.each(layout.reverse(), function (path) {
      var fn = nodeca.shared.common.getByPath(views.layouts, path);

      if (!_.isFunction(fn)) {
        nodeca.logger.warn("Layout " + path + " not found");
        return;
      }

      data.content = html;
      html = fn(data);
    });
  }

  return html;
};


/**
 *  shared.common.render.getLayoutStack(layout) -> Array
 *  - layout (string): Full layout path
 *
 *  Returns stack of layouts.
 *
 *      getLayoutStack('foo.bar.baz') // => ['foo', 'foo.bar', 'foo.bar.baz']
 **/
module.exports.getLayoutStack = get_layout_stack;

@@ -211,16 +211,22 @@
 tStack%5D%5D
+ below
 %0Afunctio
@@ -571,16 +571,20 @@
  - views
+Tree
  (Object
@@ -1298,32 +1298,36 @@
 ion render(views
+Tree
 , path, layout, 
@@ -1339,16 +1339,22 @@
  %7B%0A  var
+ html,
  view = 
@@ -1393,19 +1393,38 @@
 iews
+Tree
 , path)
-,
+;%0A
 %0A  
+if (!!view) %7B%0A
     
@@ -1438,43 +1438,24 @@
 view
- && view(data) %7C%7C '';%0A%0A  if (!view)
+(data);%0A  %7D else
  %7B%0A 
@@ -1636,21 +1636,37 @@
 ound%22);%0A
+    html = '';%0A
   %7D%0A%0A
+%0A
   if (la
@@ -1849,16 +1849,20 @@
 th(views
+Tree
 .layouts

Refactor shared.common.render()