/*
* Copyright 2014 Mirantis, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
**/
import $ from 'jquery';
import _ from 'underscore';
import i18n from 'i18n';
import Backbone from 'backbone';
import React from 'react';
import utils from 'utils';
import models from 'models';
import {backboneMixin, pollingMixin, dispatcherMixin} from 'component_mixins';
import {Popover, Link} from 'views/controls';
import {ChangePasswordDialog, ShowNodeInfoDialog} from 'views/dialogs';
export var Navbar = React.createClass({
mixins: [
dispatcherMixin('updateNodeStats', 'updateNodeStats'),
dispatcherMixin('updateNotifications', 'updateNotifications'),
backboneMixin('user'),
backboneMixin('version'),
backboneMixin('statistics'),
backboneMixin('notifications', 'update change:status'),
pollingMixin(20)
],
togglePopover(popoverName) {
return _.memoize((visible) => {
this.setState((previousState) => {
var nextState = {};
var key = popoverName + 'PopoverVisible';
nextState[key] = _.isBoolean(visible) ? visible : !previousState[key];
return nextState;
});
});
},
setActive(url) {
this.setState({activeElement: url});
},
shouldDataBeFetched() {
return this.props.user.get('authenticated');
},
fetchData() {
return Promise.all([
this.props.statistics.fetch(),
this.props.notifications.fetch({limit: this.props.notificationsDisplayCount})
]);
},
updateNodeStats() {
return this.props.statistics.fetch();
},
updateNotifications() {
return this.props.notifications.fetch({limit: this.props.notificationsDisplayCount});
},
componentDidMount() {
this.props.user.on('change:authenticated', (model, value) => {
if (value) {
this.startPolling();
} else {
this.stopPolling();
this.props.statistics.clear();
this.props.notifications.reset();
}
});
},
getDefaultProps() {
return {
notificationsDisplayCount: 5,
elements: [
{label: 'environments', url: '/clusters'},
{label: 'equipment', url: '/equipment'},
{label: 'releases', url: '/releases'},
{label: 'plugins', url: '/plugins'},
{label: 'support', url: '/support'}
]
};
},
getInitialState() {
return {};
},
scrollToTop() {
$('html, body').animate({scrollTop: 0}, 'fast');
},
render() {
var unreadNotificationsCount = this.props.notifications.filter({status: 'unread'}).length;
var authenticationEnabled = this.props.version.get('auth_required') &&
this.props.user.get('authenticated');
return (