$( function() {
let $itemsListEl = null;
let $submenuBarEl = null;
let $fade = null;
let counters = null;
let handleItemClick = function( $itemEl, item ) {
if (item.id === 'marathon') {
window.open(item.subitems[0].url);
return;
}
/*
if ( item.id == "notifications_button_small" ) {
return;
}
*/
setSelectedItem( item.id );
if( item.subitems.length == 1 ) {
location.href = item.subitems[0].url;
}
else {
showSubmenu(item);
}
};
let balances = null;
let loadBalances = function( $headerEl ) {
let showBalances = function() {
let $balanceEl = $('
');
for( let balance of balances ) {
let $balanceStr = $("" + balance.label + ": " + balance.value + "
");
$balanceStr.appendTo( $balanceEl );
}
$balanceEl.appendTo( $headerEl );
};
if ( balances === null ) {
ajaxCall("/pl/user/user/get-balance?id=" + window.accountUserId, {}, {}, function (data) {
if ( data.balances && data.balances.length ) {
balances = data.balances;
showBalances();
}
});
}
else {
showBalances();
}
};
updateNotificationsData = function( data ) {
let count = data.count;
let $notificationsContentEl = $submenuBarEl;
$notificationsContentEl.empty();
let groups = data.groups;
if ( groups.length > 0 ) {
let $headerEl = $('').appendTo( $notificationsContentEl );
let $headerLinkWrapper = $('').appendTo( $headerEl );
let $groupsEl = $('').appendTo( $notificationsContentEl );
var notificationsLabel = (typeof Yii != 'undefined') ? Yii.t( "common", "Notifications" ) : 'Уведомления';
let $headerLink = $('').html( count.new > 0 ? notificationsLabel + ' (' + count.new + ')' : notificationsLabel);
$headerLink.appendTo( $headerLinkWrapper );
let markAsReadLabel = (typeof Yii != 'undefined') ? Yii.t( "common", "Mark as read" ) : 'Отметить прочитанными';
let $markViewedAllLink = $( '' );
$markViewedAllLink.html( markAsReadLabel ).appendTo( $headerEl );
$('').appendTo( $headerEl );
let markViewedAllInProcess = false;
$markViewedAllLink.click( function() {
if ( markViewedAllInProcess ) {
return;
}
$("").prependTo( $(this));
markViewedAllInProcess = true;
ajaxCall('/notifications/notifications/viewedAll', {}, {suppressErrors: true}, function (response) {
markViewedAllInProcess = false;
updateNotificationsData(response.groups);
updateCounters();
});
});
for (let group of groups) {
let helper = group.helper ? group.helper : {};
let $el = $("");
$el.addClass('notification-group notification-click-area notification-status-' + group.status);
$el.attr('href', helper.click_url);
$('').html(helper.first_user_thumbnail).appendTo($el);
$('').html(helper.content).appendTo($el);
$('').html(helper.display_date).appendTo($el);
$('').appendTo($el);
let $markAsViewedLink = $('×
');
$markAsViewedLink.appendTo($el);
$markAsViewedLink.data('group-id', group.id);
$markAsViewedLink.click(function (e) {
let groupId = $(this).data('group-id');
if ($(this).data('processing')) {
return;
}
$(this).data('processing', true)
e.stopPropagation();
e.preventDefault();
$markAsViewedLink.html("");
ajaxCall('/notifications/notifications/viewed', {
id: groupId
}, {suppressErrors: true}, function (resp) {
$(this).data('processing', false);
updateNotificationsData(resp.groups);
updateCounters();
});
});
$el.appendTo($groupsEl);
}
var allNotificationsLabel = (typeof Yii != 'undefined') ? Yii.t("common", "All notifications") : 'Все уведомления';
if (data.count.all > 10) {
$('').html(allNotificationsLabel).appendTo($notificationsContentEl);
}
}
else {
var noNotificationMessagesLabel = (typeof Yii != 'undefined') ? Yii.t( "common", "No notification messages" ) : 'Уведомлений для вас еще нет';
$("" + noNotificationMessagesLabel + "
").appendTo($notificationsContentEl);
}
};
loadNotifications = function() {
$submenuBarEl.html("");
ajaxCall( "/notifications/notifications/get", {}, {method: "GET"}, function(response) {
updateNotificationsData( response.data );
});
};
let updateCounters = function() {
$itemsListEl.find('.notify-count').hide();
for ( let key in counters ) {
let value = counters[key];
if (value> 0) {
if ($itemsListEl.find('.menu-item-' + key)) {
let $notifyCount = $itemsListEl.find('.menu-item-' + key + ' .notify-count');
$notifyCount.html( value ).show();
if (key == 'notifications_button_small') {
$itemsListEl.find('.menu-item-' + key + ' .menu-item-icon').hide();
}
}
}
}
};
let setSelectedItem = function( itemId ) {
let $currentSelected = $itemsListEl.find('.menu-item.selected');
if ( $currentSelected.length > 0 ) {
if ($currentSelected.hasClass('menu-item-' + itemId)) {
return;
}
let currentSelectedItem = $currentSelected.data('item');
$currentSelected.find('.menu-item-icon').attr( 'src', currentSelectedItem.iconUrl );
$currentSelected.removeClass('selected');
}
if ( ! itemId ) {
return;
}
let $newSelected = $itemsListEl.find('.menu-item.menu-item-' + itemId );
if ( $newSelected.hasClass( 'active') ) {
return;
}
let newSelectedItem = $newSelected.data('item');
$newSelected.find('.menu-item-icon').attr( 'src', newSelectedItem.activeIconUrl );
$newSelected.addClass('selected');
};
let showSubmenu = function( item ) {
$submenuBarEl.empty();
$submenuBarEl.attr('class', 'gc-account-user-submenu-bar');
$submenuBarEl.addClass( 'gc-account-user-submenu-bar-' + item.id );
if ( item.id == 'notifications_button_small' ) {
loadNotifications();
}
else {
let $header = $("" + item.label + "
");
$header.appendTo( $submenuBarEl );
if ( item.id == 'profile' ) {
loadBalances( $header );
}
let $submenuEl = $("