3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 //This will reach out to the global routes endpoint
20 import Alt
from './skyquakeAltInstance.js';
21 import SkyquakeContainerSource
from './skyquakeContainerSource.js';
22 import SkyquakeContainerActions
from './skyquakeContainerActions';
23 let Utils
= require('utils/utils.js');
24 import _indexOf
from 'lodash/indexOf';
25 import _isEqual
from 'lodash/isEqual';
26 //Temporary, until api server is on same port as webserver
27 import rw
from 'utils/rw.js';
29 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
30 const MAX_STORED_EVENTS
= 20;
32 class SkyquakeContainerStore
{
34 this.currentPlugin
= getCurrentPlugin();
36 let notificationList
= null;
37 try {notificationList
= JSON
.parse(sessionStorage
.getItem('notifications'));} catch (e
) {}
38 this.notifications
= notificationList
|| [];
42 //Notification defaults
43 this.notificationMessage
= '';
44 this.displayNotification
= false;
45 this.notificationType
= 'error';
46 //Screen Loader default
47 this.displayScreenLoader
= false;
48 this.bindActions(SkyquakeContainerActions
);
49 this.exportAsync(SkyquakeContainerSource
);
52 this.exportPublicMethods({
53 // getNav: this.getNav
57 getSkyquakeNavSuccess
= (data
) => {
60 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
66 window
.multiplexer
.channel(this.channelId
).close();
72 //Remove once logging plugin is implemented
73 getSysLogViewerURLSuccess(data
){
74 window
.open(data
.url
);
76 getSysLogViewerURLError(data
){
77 console
.log('failed', data
)
80 openNotificationsSocketLoading
= () => {
86 openNotificationsSocketSuccess
= (data
) => {
89 let connection
= data
.connection
;
90 let streamSource
= data
.streamSource
;
91 console
.log('Success opening notification socket for stream ', streamSource
);
93 let ws
= window
.multiplexer
.channel(connection
);
95 if (!connection
) return;
102 ws
.onmessage
= (socket
) => {
104 var data
= JSON
.parse(socket
.data
);
105 if(data
.hasOwnProperty('map')) {
108 if (!data
.notification
) {
109 console
.warn('No notification in the received payload: ', data
);
111 // Temp to test before adding multi-sources
112 data
.notification
.source
= streamSource
;
113 if (_indexOf(self
.notifications
, data
.notification
) == -1) {
114 // newly appreared event.
115 // Add to the notifications list and setState
116 self
.notifications
.unshift(data
.notification
);
117 (self
.notifications
.length
> MAX_STORED_EVENTS
) && self
.notifications
.pop();
119 newNotificationEvent
: true,
120 newNotificationMsg
: data
.notification
,
121 notifications
: self
.notifications
,
124 sessionStorage
.setItem('notifications', JSON
.stringify(self
.notifications
));
128 console
.log('Error in parsing data on notification socket');
137 openNotificationsSocketError
= (data
) => {
138 console
.log('Error opening notification socket', data
);
141 getEventStreamsLoading
= () => {
147 getEventStreamsSuccess
= (streams
) => {
148 console
.log('Found streams: ', streams
);
152 streams
['ietf-restconf-monitoring:streams'] &&
153 streams
['ietf-restconf-monitoring:streams']['stream'] &&
154 streams
['ietf-restconf-monitoring:streams']['stream'].map((stream
) => {
155 stream
['access'] && stream
['access'].map((streamLocation
) => {
156 if (streamLocation
['encoding'] == 'ws_json') {
158 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
170 getEventStreamsError
= (error
) => {
171 console
.log('Failed to get streams object');
177 openProjectSocketSuccess
= (connection
) => {
179 var ws
= window
.multiplexer
.channel(connection
);
180 if (!connection
) return;
183 channelId
: connection
185 ws
.onmessage = function(socket
) {
187 var data
= JSON
.parse(socket
.data
);
188 Utils
.checkAuthentication(data
.statusCode
, function() {
191 if (!data
.project
|| !_isEqual(data
.project
, self
.projects
)) {
192 let user
= self
.user
;
193 user
.projects
= data
.project
;
196 projects
: data
.project
|| {}
200 console
.log('HIT an exception in openProjectSocketSuccess', e
);
204 getUserProfileSuccess
= (user
) => {
205 this.alt
.actions
.global
.hideScreenLoader
.defer();
206 this.setState({user
})
208 selectActiveProjectSuccess
= (projectId
) => {
209 let user
= this.user
;
210 user
.projectId
= projectId
;
211 this.setState({user
});
212 window
.location
.href
= window
.location
.origin
;
215 handleServerReportedError
= (result
) => {
216 this.hideScreenLoader();
217 this.alt
.actions
.global
.showNotification
.defer(result
);
219 showNotification
= (notificationData
) => {
222 displayNotification
: true
225 hideNotification
= () => {
227 displayNotification
: false
231 showScreenLoader
= () => {
233 displayScreenLoader
: true
236 hideScreenLoader
= () => {
238 displayScreenLoader
: false
245 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
246 * @param {object} nav Nav item from /nav endoingpoint
247 * @param {string} currentPlugin Current plugin name taken from url path.
248 * @return {array} Returns list of constructed nav items.
250 function decorateAndTransformNav(nav
, currentPlugin
) {
251 for ( let k
in nav
) {
252 nav
[k
].pluginName
= k
;
253 if (k
!= currentPlugin
) {
254 nav
[k
].routes
.map(function(route
, i
) {
256 route
.route
= '/' + k
+ '/index.html?api_server=' + API_SERVER
+ '#' + route
.route
;
258 route
.route
= '/' + k
+ '/#' + route
.route
;
260 route
.isExternal
= true;
267 function getCurrentPlugin() {
268 var paths
= window
.location
.pathname
.split('/');
269 var currentPath
= null;
270 if (paths
[0] != "") {
271 currentPath
= paths
[0]
273 currentPath
= paths
[1];
275 if (currentPath
!= null) {
278 console
.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
282 export default Alt
.createStore(SkyquakeContainerStore
, 'SkyquakeContainerStore');