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 var UPLOAD_SERVER
= rw
.getSearchParams(window
.location
).upload_server
;
32 class SkyquakeContainerStore
{
34 this.currentPlugin
= getCurrentPlugin();
36 this.notifications
= [];
40 //Notification defaults
41 this.notificationMessage
= '';
42 this.displayNotification
= false;
43 this.notificationType
= 'error';
44 //Screen Loader default
45 this.displayScreenLoader
= false;
46 this.bindActions(SkyquakeContainerActions
);
47 this.exportAsync(SkyquakeContainerSource
);
50 this.exportPublicMethods({
51 // getNav: this.getNav
55 getSkyquakeNavSuccess
= (data
) => {
58 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
64 window
.multiplexer
.channel(this.channelId
).close();
70 //Remove once logging plugin is implemented
71 getSysLogViewerURLSuccess(data
){
72 window
.open(data
.url
);
74 getSysLogViewerURLError(data
){
75 console
.log('failed', data
)
78 openNotificationsSocketLoading
= () => {
84 openNotificationsSocketSuccess
= (data
) => {
87 let connection
= data
.connection
;
88 let streamSource
= data
.streamSource
;
89 console
.log('Success opening notification socket for stream ', streamSource
);
91 let ws
= window
.multiplexer
.channel(connection
);
93 if (!connection
) return;
100 ws
.onmessage
= (socket
) => {
102 var data
= JSON
.parse(socket
.data
);
103 if (!data
.notification
) {
104 console
.warn('No notification in the received payload: ', data
);
106 // Temp to test before adding multi-sources
107 data
.notification
.source
= streamSource
;
108 if (_indexOf(self
.notifications
, data
.notification
) == -1) {
109 // newly appreared event.
110 // Add to the notifications list and setState
111 self
.notifications
.unshift(data
.notification
);
113 newNotificationEvent
: true,
114 newNotificationMsg
: data
.notification
,
115 notifications
: self
.notifications
,
121 console
.log('Error in parsing data on notification socket');
130 openNotificationsSocketError
= (data
) => {
131 console
.log('Error opening notification socket', data
);
134 getEventStreamsLoading
= () => {
140 getEventStreamsSuccess
= (streams
) => {
141 console
.log('Found streams: ', streams
);
144 streams
['ietf-restconf-monitoring:streams'] &&
145 streams
['ietf-restconf-monitoring:streams']['stream'] &&
146 streams
['ietf-restconf-monitoring:streams']['stream'].map((stream
) => {
147 stream
['access'] && stream
['access'].map((streamLocation
) => {
148 if (streamLocation
['encoding'] == 'ws_json') {
150 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
162 getEventStreamsError
= (error
) => {
163 console
.log('Failed to get streams object');
169 openProjectSocketSuccess
= (connection
) => {
171 var ws
= window
.multiplexer
.channel(connection
);
172 if (!connection
) return;
175 channelId
: connection
177 ws
.onmessage = function(socket
) {
179 var data
= JSON
.parse(socket
.data
);
180 Utils
.checkAuthentication(data
.statusCode
, function() {
183 if (!_isEqual(data
.project
, self
.projects
)) {
184 let user
= self
.user
;
185 user
.projects
= data
.project
;
188 projects
: data
.project
192 console
.log('HIT an exception in openProjectSocketSuccess', e
);
196 getUserProfileSuccess
= (user
) => {
197 this.alt
.actions
.global
.hideScreenLoader
.defer();
198 this.setState({user
})
200 selectActiveProjectSuccess
= (projectId
) => {
201 let user
= this.user
;
202 user
.projectId
= projectId
;
203 this.setState({user
});
204 window
.location
.reload(true);
207 showNotification
= (data
) => {
209 displayNotification
: true,
210 notificationMessage
: data
,
211 notificationType
: 'error',
212 displayScreenLoader
: false
214 if(typeof(data
) == 'string') {
218 state
.notificationMessage
= data
.msg
|| 'Something wrong occurred. Check the network tab and console logs for more information.';
220 state
.notificationType
= data
.type
;
223 this.setState(state
);
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
+ '&upload_server=' + UPLOAD_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');