2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 import React, {Component} from 'react';
25 import {connect} from 'react-redux';
26 import FontAwesome from 'react-fontawesome';
27 import {clearFilters} from 'filter-bar-utils';
28 import Button from 'react-bootstrap/lib/Button.js';
29 import Modal from 'react-bootstrap/lib/Modal.js';
30 import GlobalAutoCompleteSearchBar from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBar.jsx';
31 import {postAnalyticsData} from 'app/analytics/AnalyticsActions.js';
32 import GlobalInlineMessageBar from 'app/globalInlineMessageBar/GlobalInlineMessageBar.jsx';
33 import {getClearGlobalMessageEvent} from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
34 import {externalUrlRequest, externalMessageRequest, getSubscriptionPayload} from 'app/contextHandler/ContextHandlerActions.js';
38 } from 'utils/GlobalConstants.js';
43 } from 'react-router-dom';
47 MENU_ITEM_TIER_SUPPORT,
49 } from './MainScreenWrapperConstants.js';
53 clearExtensibleViewData,
55 } from './MainScreenWrapperActionHelper.js';
57 import {clearSuggestionsTextField} from 'app/globalAutoCompleteSearchBar/GlobalAutoCompleteSearchBarActions.js';
58 import {changeUrlAddress} from 'utils/Routes.js';
59 import extensibleViews from 'resources/views/extensibleViews.json';
62 const mapStateToProps = ({mainWrapper}) => {
65 toggleButtonActive = false,
66 externalRequestFound = {},
68 subscriptionPayload = {},
69 subscriptionEnabled = false
83 const mapActionsToProps = (dispatch) => {
85 onShowMenu: () => dispatch(showMainMenu(true)),
87 dispatch(showMainMenu(false));
89 dispatchAnalyticsData: () => dispatch(
90 postAnalyticsData(document.documentElement.outerHTML.replace('\s+', ''))),
91 onRouteChange: () => {
92 dispatch(getClearGlobalMessageEvent());
93 dispatch(clearSuggestionsTextField());
94 dispatch(clearExtensibleViewData());
95 dispatch(clearFilters(filterBarActionTypes.CLEAR_FILTERS));
96 dispatch(setSecondaryTitle(undefined));
98 onExternalUrlRequest: (urlParamString) => {
99 dispatch(externalUrlRequest(urlParamString));
101 onExternalMessageRecieved: (messageJson) => {
102 dispatch(externalMessageRequest(messageJson));
104 onGetSubscriptionPayload: () => {
105 dispatch(getSubscriptionPayload());
110 class MainScreenHeader extends Component {
112 showMenu: React.PropTypes.bool,
113 toggleButtonActive: React.PropTypes.bool,
114 externalRequestFound: React.PropTypes.object,
115 secondaryTitle: React.PropTypes.string,
116 subscriptionPayload: React.PropTypes.object
119 navigationLinkAndCurrentPathMatch(location, to) {
120 let linkPathElements = to.split('/');
121 let locationElements = location.pathname.split('/');
123 // the element arrays above will have the route at index 1 ... need to
124 // verify if the routes match
125 return locationElements[1] === linkPathElements[1];
128 hasRouteChanged(currentPath, nextPath) {
129 let currentPathParts = currentPath.split('/');
130 let nextPathParts = nextPath.split('/');
132 if (currentPathParts[1] !== nextPathParts[1]) {
139 isValidExternalURL(url) {
140 if(decodeURIComponent(url).indexOf('&') > 0 ) {
147 componentWillMount() {
148 this.props.onGetSubscriptionPayload();
149 if(this.props.match.params.externalUrl !== undefined &&
150 this.isValidExternalURL(this.props.match.params.externalUrl)) {
151 this.props.onExternalUrlRequest(this.props.match.params.externalUrl);
155 componentWillReceiveProps(nextProps) {
156 if (this.props.location &&
157 this.props.location.pathname !==
158 nextProps.location.pathname) {
160 this.props.dispatchAnalyticsData();
162 if (this.hasRouteChanged(this.props.location.pathname,
163 nextProps.location.pathname)) {
164 this.props.onRouteChange();
168 if(nextProps.match.params.externalUrl !== undefined &&
169 nextProps.match.params.externalUrl !== this.props.match.params.externalUrl &&
170 this.isValidExternalURL(nextProps.match.params.externalUrl)) {
171 this.props.onExternalUrlRequest(nextProps.match.params.externalUrl);
173 /* if the externalURL is not valid, we do not add any message as other proper
174 views will get that messages since the route will be this parameter.*/
176 if(this.props.externalRequestFound !== nextProps.externalRequestFound &&
177 nextProps.externalRequestFound !== undefined && nextProps.externalRequestFound.suggestion !== undefined) {
178 changeUrlAddress(nextProps.externalRequestFound.suggestion, nextProps.history);
181 if (nextProps.subscriptionEnabled) {
182 if (nextProps.subscriptionPayload !== this.props.subscriptionPayload &&
183 Object.keys(nextProps.subscriptionPayload).length > 0) {
184 var getWindowUrl = function (url) {
185 var split = url.split('/');
186 return split[0] + '//' + split[2];
188 window.parent.postMessage(
189 JSON.stringify(nextProps.subscriptionPayload),
190 getWindowUrl(document.referrer));
195 receiveMessage(event, $this) {
196 function isJson(str) {
204 if(isJson(event.data)) {
205 let messageData = JSON.parse(event.data);
206 if(isJson(messageData.message)) {
207 $this.props.onExternalMessageRecieved(messageData.message);
212 componentDidMount() {
213 //TODO Move this logic to the component will receive props.
214 //Check if the event lister is available and if the subscription is
215 // enabled before registering for it
216 if(document.referrer) {
218 window.addEventListener('message', function (e) {
219 $this.receiveMessage(e, $this);
223 componentWillUnmount() {
224 if(this.props.subscriptionEnabled) {
226 window.removeEventListener('message', function (e) {
227 $this.receiveMessage(e, $this);
242 let menuOptions = [];
244 const MenuItem = ({label, iconClass, to}) => (
245 <Route path={to} children={({location}) => (
246 <NavLink to={to} onClick={onHideMenu}>
247 <div className={this.navigationLinkAndCurrentPathMatch(location, to) ? 'main-menu-button-active' : 'main-menu-button'}>
248 <div className={iconClass}/>
249 <div className='button-icon'>{label}</div>
255 // add Tier Support view
257 <MenuItem key='schemaMenu' to='/schema' label={MENU_ITEM_TIER_SUPPORT}
258 iconClass='button-icon view-inspect-button-icon'/>
263 <MenuItem key='vnfSearchMenu'
265 label={MENU_ITEM_VNF_SEARCH}
266 iconClass='button-icon vnf-search-button-icon'/>
269 // add all custom view menu options
270 for (let view in extensibleViews) {
272 <MenuItem key={extensibleViews[view]['viewName'] + 'Menu'} to={'/' + extensibleViews[view]['viewName']}
273 label={extensibleViews[view]['displayName']}
274 iconClass={'button-icon ' + extensibleViews[view]['iconClass']}/>
278 let secondaryTitleClass = 'secondary-header';
279 if (secondaryTitle === undefined || secondaryTitle === '') {
280 secondaryTitleClass = secondaryTitleClass + ' hidden';
284 <div className='header'>
287 bsClass={(toggleButtonActive)
288 ? 'toggle-view-button-active'
289 : 'toggle-view-button'}
290 onClick={onShowMenu}>
291 <FontAwesome name='bars'/>
293 <Modal show={showMenu} onHide={onHideMenu}
294 dialogClassName='modal-main-menu'>
299 <span className='application-title'>{AAI_TITLE}</span>
300 <GlobalAutoCompleteSearchBar history={this.props.history}/>
302 <GlobalInlineMessageBar />
303 <div className={secondaryTitleClass}>
304 <span className='secondary-title'>{secondaryTitle}</span>
311 export default connect(mapStateToProps, mapActionsToProps)(MainScreenHeader);