// Register avatar helper, and display avatars on page load
//

'use strict';

var _           = require('lodash');
var identicon   = require('nodeca.users/lib/identicon');
var avatarSizes = _.reduce('$$ JSON.stringify(N.config.users.avatars.resize) $$', function (result, val, key) {
  result[key] = val.width;
  return result;
}, {});


// Returns a list of attributes for avatar img tag.
//
// Usage:
//  - avatar_helper(user [, size_name])
//  - avatar_helper(user_id [, size_name])
//  - avatar_helper(user_id, user [, size_name])
//
function avatar_helper(user_id, user, size_name) {
  var avatar_id, src;

  if (typeof user_id !== 'string') {
    // avatar_helper(user, size_name)
    size_name = user;
    user = user_id;
    user_id = user._id;
  }

  if (typeof user !== 'object') {
    // avatar_helper(user_id, size_name)
    size_name = user;
    user = null;
  }

  if (user) {
    avatar_id = user.avatar_id;
  } else if (N && N.runtime && N.runtime.user_id === user_id) {
    avatar_id = N.runtime.user_avatar;
  }

  if (size_name && !avatarSizes[size_name]) {
    throw new Error('Invalid avatar size: ' + size_name);
  }

  if (avatar_id) {
    src = N.router.linkTo('core.gridfs', { bucket: avatar_id + (size_name ? '_' + size_name : '') });
  } else {
    src = identicon(user_id, avatarSizes[size_name ? size_name : 'orig']);
  }

  return {
    src: src
  };
}


N.wire.once('init:assets', function avatar_helper_register() {
  N.runtime.render.helpers.avatar = avatar_helper;
});

N.wire.once('navigate.done', function identicon_replace() {
  $('._identicon').each(function (n, img) {
    var $img = $(img),
        user_id = $img.data('user-id'),
        avatar_id = $img.data('avatar-id'),
        size_name = $img.data('avatar-size');

    if (size_name && !avatarSizes.hasOwnProperty(size_name)) {
      throw new Error('invalid avatar size: ' + size_name);
    }

    $img.removeClass('_identicon');
    if (avatar_id) {
      $img.attr('src', N.router.linkTo('core.gridfs', { bucket: avatar_id + (size_name ? '_' + size_name : '') }));
    } else {
      $img.attr('src', identicon(user_id, avatarSizes[size_name ? size_name : 'orig']));
    }
  });
});

@@ -52,16 +52,28 @@
 age load
+/page update
 %0A//%0A%0A'us
@@ -1395,190 +1395,145 @@
 %7D%0A%0A%0A
-N.wire.once('init:assets', function avatar_helper_register() %7B%0A  N.runtime.render.helpers.avatar = avatar_helper;%0A%7D);%0A%0AN.wire.once('navigate.done', function identicon_replace() %7B%0A  $
+// Replace placeholders (images with %22_identicon%22 class) with avatars%0A//%0Afunction replace_placeholders(selector, users) %7B%0A  selector.find
 ('._
@@ -1861,288 +1861,1253 @@
     
-$img.removeClass('_identicon');%0A    if (avatar_id) %7B%0A      $img.attr('src', N.router.linkTo('core.gridfs', %7B bucket: avatar_id + (size_name ? '_' + size_name : '') %7D));%0A    %7D else %7B%0A      $img.attr('src', identicon(user_id, avatarSizes%5Bsize_name ? size_name : 'orig'%5D));%0A    %7D%0A  %7D
+// If current user is missing avatar info - try to restore%0A    if (user_id === N.runtime.user_id && N.runtime.user_avatar) %7B%0A      avatar_id = avatar_id %7C%7C N.runtime.user_avatar;%0A    %7D%0A%0A    // Avatar id is not specified, but we can retrieve it from locals%0A    if (users && users%5Buser_id%5D) %7B%0A      avatar_id = avatar_id %7C%7C users%5Buser_id%5D.avatar_id;%0A    %7D%0A%0A    $img.removeClass('_identicon');%0A    if (avatar_id) %7B%0A      $img.attr('src', N.router.linkTo('core.gridfs', %7B bucket: avatar_id + (size_name ? '_' + size_name : '') %7D));%0A    %7D else %7B%0A      $img.attr('src', identicon(user_id, avatarSizes%5Bsize_name ? size_name : 'orig'%5D));%0A    %7D%0A  %7D);%0A%7D%0A%0A%0AN.wire.once('init:assets', function avatar_helper_register() %7B%0A  N.runtime.render.helpers.avatar = avatar_helper;%0A%7D);%0A%0A%0AN.wire.on('navigate.done', function identicon_replace(data) %7B%0A  if (data.locals) %7B%0A    // page generated on client-side, so we have all the locals%0A    replace_placeholders($(document), data.locals.users);%0A  %7D else %7B%0A    // page generated on server-side with users provided through page_data%0A    replace_placeholders($(document), N.runtime.page_data.users);%0A  %7D%0A%7D);%0A%0A%0AN.wire.on('navigate.update', function identicon_replace(data) %7B%0A  replace_placeholders(data.$, data.locals.users
 );%0A%7D

Copy identicon replacement from common to admin/core