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 {connect} from 'react-redux';
23 import FontAwesome from 'react-fontawesome';
24 import {clearFilters} from 'filter-bar-utils';
25 import Button from 'react-bootstrap/lib/Button.js';
26 import Modal from 'react-bootstrap/lib/Modal.js';
27 import GlobalAutoCompleteSearchBar from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBar.jsx';
28 import {postAnalyticsData} from 'app/analytics/AnalyticsActions.js';
29 import GlobalInlineMessageBar from 'app/globalInlineMessageBar/GlobalInlineMessageBar.jsx';
30 import {getClearGlobalMessageEvent} from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
31 import {externalUrlRequest, externalMessageRequest, getSubscriptionPayload} from 'app/contextHandler/ContextHandlerActions.js';
35 } from 'utils/GlobalConstants.js';
40 } from 'react-router-dom';
44 MENU_ITEM_TIER_SUPPORT,
46 } from './MainScreenWrapperConstants.js';
50 clearExtensibleViewData,
52 } from './MainScreenWrapperActionHelper.js';
54 import {clearSuggestionsTextField} from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
55 import {changeUrlAddress} from 'utils/Routes.js';
56 import extensibleViews from 'resources/views/extensibleViews.json';
59 const mapStateToProps = ({mainWrapper}) => {
62 toggleButtonActive = false,
63 externalRequestFound = {},
65 subscriptionPayload = {},
66 subscriptionEnabled = false
80 const mapActionsToProps = (dispatch) => {
82 onShowMenu: () => dispatch(showMainMenu(true)),
84 dispatch(showMainMenu(false));
86 dispatchAnalyticsData: () => dispatch(
87 postAnalyticsData(document.documentElement.outerHTML.replace('\s+', ''))),
88 onRouteChange: () => {
89 dispatch(getClearGlobalMessageEvent());
90 dispatch(clearSuggestionsTextField());
91 dispatch(clearExtensibleViewData());
92 dispatch(clearFilters(filterBarActionTypes.CLEAR_FILTERS));
93 dispatch(setSecondaryTitle(undefined));
95 onExternalUrlRequest: (urlParamString) => {
96 dispatch(externalUrlRequest(urlParamString));
98 onExternalMessageRecieved: (messageJson) => {
99 dispatch(externalMessageRequest(messageJson));
101 onGetSubscriptionPayload: () => {
102 dispatch(getSubscriptionPayload());
107 class MainScreenHeader extends Component {
109 showMenu: React.PropTypes.bool,
110 toggleButtonActive: React.PropTypes.bool,
111 externalRequestFound: React.PropTypes.object,
112 secondaryTitle: React.PropTypes.string,
113 subscriptionPayload: React.PropTypes.object
116 navigationLinkAndCurrentPathMatch(location, to) {
117 let linkPathElements = to.split('/');
118 let locationElements = location.pathname.split('/');
120 // the element arrays above will have the route at index 1 ... need to
121 // verify if the routes match
122 return locationElements[1] === linkPathElements[1];
125 hasRouteChanged(currentPath, nextPath) {
126 let currentPathParts = currentPath.split('/');
127 let nextPathParts = nextPath.split('/');
129 if (currentPathParts[1] !== nextPathParts[1]) {
136 isValidExternalURL(url) {
137 if(decodeURIComponent(url).indexOf('&') > 0 ) {
144 componentWillMount() {
145 this.props.onGetSubscriptionPayload();
146 if(this.props.match.params.externalUrl !== undefined &&
147 this.isValidExternalURL(this.props.match.params.externalUrl)) {
148 this.props.onExternalUrlRequest(this.props.match.params.externalUrl);
152 componentWillReceiveProps(nextProps) {
153 if (this.props.location &&
154 this.props.location.pathname !==
155 nextProps.location.pathname) {
157 this.props.dispatchAnalyticsData();
159 if (this.hasRouteChanged(this.props.location.pathname,
160 nextProps.location.pathname)) {
161 this.props.onRouteChange();
165 if(nextProps.match.params.externalUrl !== undefined &&
166 nextProps.match.params.externalUrl !== this.props.match.params.externalUrl &&
167 this.isValidExternalURL(nextProps.match.params.externalUrl)) {
168 this.props.onExternalUrlRequest(nextProps.match.params.externalUrl);
170 /* if the externalURL is not valid, we do not add any message as other proper
171 views will get that messages since the route will be this parameter.*/
173 if(this.props.externalRequestFound !== nextProps.externalRequestFound &&
174 nextProps.externalRequestFound !== undefined && nextProps.externalRequestFound.suggestion !== undefined) {
175 changeUrlAddress(nextProps.externalRequestFound.suggestion, nextProps.history);
178 if (nextProps.subscriptionEnabled) {
179 if (nextProps.subscriptionPayload !== this.props.subscriptionPayload &&
180 Object.keys(nextProps.subscriptionPayload).length > 0) {
181 var getWindowUrl = function (url) {
182 var split = url.split('/');
183 return split[0] + '//' + split[2];
185 window.parent.postMessage(
186 JSON.stringify(nextProps.subscriptionPayload),
187 getWindowUrl(document.referrer));
192 receiveMessage(event, $this) {
193 function isJson(str) {
201 if(isJson(event.data)) {
202 let messageData = JSON.parse(event.data);
203 if(isJson(messageData.message)) {
204 $this.props.onExternalMessageRecieved(messageData.message);
209 componentDidMount() {
210 //TODO Move this logic to the component will receive props.
211 //Check if the event lister is available and if the subscription is
212 // enabled before registering for it
213 if(document.referrer) {
215 window.addEventListener('message', function (e) {
216 $this.receiveMessage(e, $this);
220 componentWillUnmount() {
221 if(this.props.subscriptionEnabled) {
223 window.removeEventListener('message', function (e) {
224 $this.receiveMessage(e, $this);
239 let menuOptions = [];
241 const MenuItem = ({label, iconClass, to}) => (
242 <Route path={to} children={({location}) => (
243 <NavLink to={to} onClick={onHideMenu}>
244 <div className={this.navigationLinkAndCurrentPathMatch(location, to) ? 'main-menu-button-active' : 'main-menu-button'}>
245 <div className={iconClass}/>
246 <div className='button-icon'>{label}</div>
252 // add Tier Support view
254 <MenuItem key='schemaMenu' to='/schema' label={MENU_ITEM_TIER_SUPPORT}
255 iconClass='button-icon view-inspect-button-icon'/>
260 <MenuItem key='vnfSearchMenu'
262 label={MENU_ITEM_VNF_SEARCH}
263 iconClass='button-icon vnf-search-button-icon'/>
266 // add all custom view menu options
267 for (let view in extensibleViews) {
269 <MenuItem key={extensibleViews[view]['viewName'] + 'Menu'} to={'/' + extensibleViews[view]['viewName']}
270 label={extensibleViews[view]['displayName']}
271 iconClass={'button-icon ' + extensibleViews[view]['iconClass']}/>
275 let secondaryTitleClass = 'secondary-header';
276 if (secondaryTitle === undefined || secondaryTitle === '') {
277 secondaryTitleClass = secondaryTitleClass + ' hidden';
281 <div className='header'>
284 bsClass={(toggleButtonActive)
285 ? 'toggle-view-button-active'
286 : 'toggle-view-button'}
287 onClick={onShowMenu}>
288 <FontAwesome name='bars'/>
290 <Modal show={showMenu} onHide={onHideMenu}
291 dialogClassName='modal-main-menu'>
296 <span className='application-title'>{AAI_TITLE}</span>
297 <GlobalAutoCompleteSearchBar history={this.props.history}/>
299 <GlobalInlineMessageBar />
300 <div className={secondaryTitleClass}>
301 <span className='secondary-title'>{secondaryTitle}</span>
308 export default connect(mapStateToProps, mapActionsToProps)(MainScreenHeader);