// Member page
//

'use strict';

var _   = require('lodash');
var Bag = require('bag.js');


// Store/restore blocks collapse state
//
N.wire.on('navigate.done:' + module.apiPath, function store_blocks_state() {
  var key = 'member_blocks_collapsed';
  var collapsedBlocks;
  var bag = new Bag();

  // Handle show/hide events
  $('.member-block__inner')
    .on('shown.bs.collapse', function (event) {
      collapsedBlocks = _.without(collapsedBlocks, $(event.target).attr('id'));

      bag.set(key, collapsedBlocks);
    })
    .on('hidden.bs.collapse', function (event) {
      collapsedBlocks.push($(event.target).attr('id'));

      bag.set(key, collapsedBlocks);
    });

  // Restore previous state
  bag.get(key, function (__, data) {
    collapsedBlocks = data || [];

    collapsedBlocks.forEach(function (blockID) {
      $('#' + blockID)
        .removeClass('in')
        .parent()
        .find('.member-block__header-collapser')
        .addClass('collapsed');
    });
  });
});


N.wire.once('navigate.done:' + module.apiPath, function page_once() {

  // Click to avatar
  //
  N.wire.on('users.member:change_avatar', function change_avatar() {
    var data = {};

    N.wire.emit('users.avatar.change', data, function () {

      $('.member-avatar__image').attr('src', N.router.linkTo('core.gridfs', { bucket: data.avatar_id }));
      $('.member-layout').addClass('member-layout__m-avatar-exists');
    });
  });


  // Show delete avatar confirm
  //
  N.wire.before('users.member:detete_avatar', function confirm_delete_avatar(event, callback) {
    N.wire.emit('common.blocks.confirm', t('delete_avatar_confirm'), callback);
  });


  // Click to delete avatar button
  //
  N.wire.on('users.member:detete_avatar', function delete_avatar() {
    N.io.rpc('users.avatar.delete').done(function (result) {

      $('.member-avatar__image').attr('src', N.router.linkTo('core.gridfs', { bucket: result.avatar_id }));
      $('.member-layout').removeClass('member-layout__m-avatar-exists');
    });
  });


  // Page exit
  //
  N.wire.on('navigate.exit:' + module.apiPath, function page_exit() {
    $('.member-block__inner')
      .off('shown.bs.collapse')
      .off('hidden.bs.collapse');
  });
});

@@ -202,16 +202,20 @@
 s_state(
+data
 ) %7B%0A  va
@@ -226,35 +226,197 @@
 y = 
-'member_blocks_collapsed'
+%5B%0A    'blocks_collapsed',%0A    N.runtime.user_hid,%0A    // Different blocks state for owner's page and for another user's page%0A    data.params.user_hid === N.runtime.user_hid%0A  %5D.join('_')
 ;%0A
+%0A
   va
@@ -434,16 +434,17 @@
 Blocks;%0A
+%0A
   var ba
@@ -455,16 +455,87 @@
 new Bag(
+%7B%0A    prefix: 'nodeca_settings',%0A    expire: 12 * 30 * 24 // 1 year%0A  %7D
 );%0A%0A  //

fix bag.js usage