2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 import React, {Component} from 'react';
22 import { PropTypes } from 'prop-types';
23 import {connect} from 'react-redux';
24 import FontAwesome from 'react-fontawesome';
25 import {clearFilters} from 'filter-bar-utils';
26 import Button from 'react-bootstrap/lib/Button.js';
27 import Modal from 'react-bootstrap/lib/Modal.js';
28 import GlobalAutoCompleteSearchBar from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBar.jsx';
29 import {postAnalyticsData} from 'app/analytics/AnalyticsActions.js';
30 import GlobalInlineMessageBar from 'app/globalInlineMessageBar/GlobalInlineMessageBar.jsx';
31 import {getClearGlobalMessageEvent} from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
32 import {externalUrlRequest, externalMessageRequest, getSubscriptionPayload} from 'app/contextHandler/ContextHandlerActions.js';
33 import { getConfigurableViewConfigs } from 'app/configurableViews/ConfigurableViewActions.js';
36 } from 'utils/GlobalConstants.js';
41 } from 'react-router-dom';
45 MENU_ITEM_TIER_SUPPORT,
47 } from './MainScreenWrapperConstants.js';
51 clearExtensibleViewData,
53 } from './MainScreenWrapperActionHelper.js';
55 import {clearSuggestionsTextField} from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
56 import {changeUrlAddress} from 'utils/Routes.js';
57 import extensibleViews from 'resources/views/extensibleViews.json';
60 const mapStateToProps = ({mainWrapper, configurableViews}) => {
63 toggleButtonActive = false,
64 externalRequestFound = {},
66 subscriptionPayload = {},
67 subscriptionEnabled = false
71 configurableViewsConfig
72 } = configurableViews;
81 configurableViewsConfig
86 const mapActionsToProps = (dispatch) => {
88 onShowMenu: () => dispatch(showMainMenu(true)),
90 dispatch(showMainMenu(false));
92 dispatchAnalyticsData: () => dispatch(
93 postAnalyticsData(document.documentElement.outerHTML.replace('\s+', ''))),
94 onRouteChange: () => {
95 dispatch(getClearGlobalMessageEvent());
96 dispatch(clearSuggestionsTextField());
97 dispatch(clearExtensibleViewData());
98 dispatch(clearFilters(filterBarActionTypes.CLEAR_FILTERS));
99 dispatch(setSecondaryTitle(undefined));
101 onExternalUrlRequest: (urlParamString) => {
102 dispatch(externalUrlRequest(urlParamString));
104 onExternalMessageRecieved: (messageJson) => {
105 dispatch(externalMessageRequest(messageJson));
107 onGetSubscriptionPayload: () => {
108 dispatch(getSubscriptionPayload());
110 onFetchCustomViews: () => {
111 dispatch(getConfigurableViewConfigs());
116 class MainScreenHeader extends Component {
118 showMenu: PropTypes.bool,
119 toggleButtonActive: PropTypes.bool,
120 externalRequestFound: PropTypes.object,
121 secondaryTitle: PropTypes.string,
122 subscriptionPayload: PropTypes.object
125 navigationLinkAndCurrentPathMatch(location, to) {
126 let linkPathElements = to.split('/');
127 let locationElements = location.pathname.split('/');
129 // the element arrays above will have the route at index 1 ... need to
130 // verify if the routes match
131 return locationElements[1] === linkPathElements[1];
134 hasRouteChanged(currentPath, nextPath) {
135 let currentPathParts = currentPath.split('/');
136 let nextPathParts = nextPath.split('/');
138 if (currentPathParts[1] !== nextPathParts[1]) {
145 isValidExternalURL(url) {
146 if(decodeURIComponent(url).indexOf('&') > 0 ) {
153 componentWillMount() {
154 this.props.onGetSubscriptionPayload();
155 if(this.props.match.params.externalUrl !== undefined &&
156 this.isValidExternalURL(this.props.match.params.externalUrl)) {
157 this.props.onExternalUrlRequest(this.props.match.params.externalUrl);
161 componentWillReceiveProps(nextProps) {
162 if (this.props.location &&
163 this.props.location.pathname !==
164 nextProps.location.pathname) {
166 this.props.dispatchAnalyticsData();
168 if (this.hasRouteChanged(this.props.location.pathname,
169 nextProps.location.pathname)) {
170 this.props.onRouteChange();
174 if(nextProps.match.params.externalUrl !== undefined &&
175 nextProps.match.params.externalUrl !== this.props.match.params.externalUrl &&
176 this.isValidExternalURL(nextProps.match.params.externalUrl)) {
177 this.props.onExternalUrlRequest(nextProps.match.params.externalUrl);
179 /* if the externalURL is not valid, we do not add any message as other proper
180 views will get that messages since the route will be this parameter.*/
182 if(this.props.externalRequestFound !== nextProps.externalRequestFound &&
183 nextProps.externalRequestFound !== undefined && nextProps.externalRequestFound.suggestion !== undefined) {
184 changeUrlAddress(nextProps.externalRequestFound.suggestion, nextProps.history);
187 if (nextProps.subscriptionEnabled) {
188 if (nextProps.subscriptionPayload !== this.props.subscriptionPayload &&
189 Object.keys(nextProps.subscriptionPayload).length > 0) {
190 var getWindowUrl = function (url) {
191 var split = url.split('/');
192 return split[0] + '//' + split[2];
194 window.parent.postMessage(
195 JSON.stringify(nextProps.subscriptionPayload),
196 getWindowUrl(document.referrer));
201 receiveMessage(event, $this) {
202 function isJson(str) {
210 if(isJson(event.data)) {
211 let messageData = JSON.parse(event.data);
212 if(isJson(messageData.message)) {
213 $this.props.onExternalMessageRecieved(messageData.message);
218 componentDidMount() {
219 //TODO Move this logic to the component will receive props.
220 //Check if the event lister is available and if the subscription is
221 // enabled before registering for it
222 if(document.referrer) {
224 window.addEventListener('message', function (e) {
225 $this.receiveMessage(e, $this);
229 // fetch custom views
230 this.props.onFetchCustomViews();
233 componentWillUnmount() {
234 if(this.props.subscriptionEnabled) {
236 window.removeEventListener('message', function (e) {
237 $this.receiveMessage(e, $this);
249 configurableViewsConfig
252 let menuOptions = [];
254 const MenuItem = ({label, iconClass, to}) => (
255 <Route path={to} children={({location}) => (
256 <NavLink to={to} onClick={onHideMenu}>
257 <div className={this.navigationLinkAndCurrentPathMatch(location, to) ? 'main-menu-button-active' : 'main-menu-button'}>
258 <div className={iconClass}/>
259 <div className='button-icon'>{label}</div>
265 const ConfigurableMenuItem = ({label, to}) => (
266 <Route path={to} children={({location}) => (
267 <NavLink to={to} onClick={onHideMenu}>
268 <div className={this.navigationLinkAndCurrentPathMatch(location, to) ?
269 'main-menu-button-active' : 'main-menu-button'}>
270 <div className='button-icon configurable-view-button-icon'/>
271 <div className='button-icon'>{label}</div>
277 // add Tier Support view
279 <MenuItem key='schemaMenu' to='/schema' label={MENU_ITEM_TIER_SUPPORT}
280 iconClass='button-icon view-inspect-button-icon'/>
285 <MenuItem key='vnfSearchMenu'
287 label={MENU_ITEM_VNF_SEARCH}
288 iconClass='button-icon vnf-search-button-icon'/>
291 // add all custom view menu options
292 for (let view in extensibleViews) {
293 let shouldDisplayIcon = false;
294 if(extensibleViews[view]['onlyRoute'] === undefined){
295 shouldDisplayIcon = true;
296 } else if(extensibleViews[view]['onlyRoute'] === false){
297 shouldDisplayIcon = true;
299 if(shouldDisplayIcon === true){
301 <MenuItem key={extensibleViews[view]['viewName'] + 'Menu'} to={'/' + extensibleViews[view]['viewName']}
302 label={extensibleViews[view]['displayName']}
303 iconClass={'button-icon ' + extensibleViews[view]['iconClass']}/>
308 if (configurableViewsConfig && configurableViewsConfig.layouts) {
309 for (let configurableView in configurableViewsConfig.layouts) {
311 <ConfigurableMenuItem key={configurableViewsConfig.layouts[configurableView]['id'] + 'Menu'} to={'/' + configurableViewsConfig.layouts[configurableView]['id']}
312 label={configurableViewsConfig.layouts[configurableView]['title']}/>
317 let secondaryTitleClass = 'secondary-header';
318 if (secondaryTitle === undefined || secondaryTitle === '') {
319 secondaryTitleClass = secondaryTitleClass + ' hidden';
323 <div className='header'>
326 bsClass={(toggleButtonActive)
327 ? 'toggle-view-button-active'
328 : 'toggle-view-button'}
329 onClick={onShowMenu}>
330 <FontAwesome name='bars'/>
332 <Modal show={showMenu} onHide={onHideMenu}
333 dialogClassName='modal-main-menu'>
338 <span className='application-title'>{AAI_TITLE}</span>
339 <GlobalAutoCompleteSearchBar history={this.props.history}/>
341 <GlobalInlineMessageBar />
342 <div className={secondaryTitleClass}>
343 <span className='secondary-title'>{secondaryTitle}</span>
350 export default connect(mapStateToProps, mapActionsToProps)(MainScreenHeader);