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 _
from 'lodash';
25 //Temporary, until api server is on same port as webserver
26 var rw
= require('utils/rw.js');
27 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
28 var UPLOAD_SERVER
= rw
.getSearchParams(window
.location
).upload_server
;
30 class SkyquakeContainerStore
{
32 this.currentPlugin
= getCurrentPlugin();
34 this.notifications
= [];
38 //Notification defaults
39 this.notificationMessage
= '';
40 this.displayNotification
= false;
41 this.notificationType
= 'error';
42 //Screen Loader default
43 this.displayScreenLoader
= false;
44 this.bindActions(SkyquakeContainerActions
);
45 this.exportAsync(SkyquakeContainerSource
);
48 this.exportPublicMethods({
49 // getNav: this.getNav
53 getSkyquakeNavSuccess
= (data
) => {
56 nav
: decorateAndTransformNav(data
, self
.currentPlugin
)
62 window
.multiplexer
.channel(this.channelId
).close();
68 //Remove once logging plugin is implemented
69 getSysLogViewerURLSuccess(data
){
70 window
.open(data
.url
);
72 getSysLogViewerURLError(data
){
73 console
.log('failed', data
)
76 openNotificationsSocketLoading
= () => {
82 openNotificationsSocketSuccess
= (data
) => {
85 let connection
= data
.connection
;
86 let streamSource
= data
.streamSource
;
87 console
.log('Success opening notification socket for stream ', streamSource
);
89 let ws
= window
.multiplexer
.channel(connection
);
91 if (!connection
) return;
98 ws
.onmessage
= (socket
) => {
100 var data
= JSON
.parse(socket
.data
);
101 if (!data
.notification
) {
102 console
.warn('No notification in the received payload: ', data
);
104 // Temp to test before adding multi-sources
105 data
.notification
.source
= streamSource
;
106 if (_
.indexOf(self
.notifications
, data
.notification
) == -1) {
107 // newly appreared event.
108 // Add to the notifications list and setState
109 self
.notifications
.unshift(data
.notification
);
111 newNotificationEvent
: true,
112 newNotificationMsg
: data
.notification
,
113 notifications
: self
.notifications
,
119 console
.log('Error in parsing data on notification socket');
128 openNotificationsSocketError
= (data
) => {
129 console
.log('Error opening notification socket', data
);
132 getEventStreamsLoading
= () => {
138 getEventStreamsSuccess
= (streams
) => {
139 console
.log('Found streams: ', streams
);
142 streams
['ietf-restconf-monitoring:streams'] &&
143 streams
['ietf-restconf-monitoring:streams']['stream'] &&
144 streams
['ietf-restconf-monitoring:streams']['stream'].map((stream
) => {
145 stream
['access'] && stream
['access'].map((streamLocation
) => {
146 if (streamLocation
['encoding'] == 'ws_json') {
148 self
.getInstance().openNotificationsSocket(streamLocation
['location'], stream
['name']);
160 getEventStreamsError
= (error
) => {
161 console
.log('Failed to get streams object');
167 openProjectSocketSuccess
= (connection
) => {
169 var ws
= window
.multiplexer
.channel(connection
);
170 if (!connection
) return;
173 channelId
: connection
175 ws
.onmessage = function(socket
) {
177 var data
= JSON
.parse(socket
.data
);
178 Utils
.checkAuthentication(data
.statusCode
, function() {
181 if (!_
.isEqual(data
.project
, self
.projects
)) {
182 let user
= self
.user
;
183 user
.projects
= data
.project
;
186 projects
: data
.project
190 console
.log('HIT an exception in openProjectSocketSuccess', e
);
194 getUserProfileSuccess
= (user
) => {
195 this.alt
.actions
.global
.hideScreenLoader
.defer();
196 this.setState({user
})
198 selectActiveProjectSuccess
= (projectId
) => {
199 let user
= this.user
;
200 user
.projectId
= projectId
;
201 this.setState({user
});
202 window
.location
.reload(true);
205 showNotification
= (data
) => {
207 displayNotification
: true,
208 notificationMessage
: data
,
209 notificationType
: 'error',
210 displayScreenLoader
: false
212 if(typeof(data
) == 'string') {
216 state
.notificationMessage
= data
.msg
|| 'Something wrong occurred. Check the network tab and console logs for more information.';
218 state
.notificationType
= data
.type
;
221 this.setState(state
);
223 hideNotification
= () => {
225 displayNotification
: false
229 showScreenLoader
= () => {
231 displayScreenLoader
: true
234 hideScreenLoader
= () => {
236 displayScreenLoader
: false
243 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
244 * @param {object} nav Nav item from /nav endoingpoint
245 * @param {string} currentPlugin Current plugin name taken from url path.
246 * @return {array} Returns list of constructed nav items.
248 function decorateAndTransformNav(nav
, currentPlugin
) {
249 for ( let k
in nav
) {
250 nav
[k
].pluginName
= k
;
251 if (k
!= currentPlugin
) {
252 nav
[k
].routes
.map(function(route
, i
) {
254 route
.route
= '/' + k
+ '/index.html?api_server=' + API_SERVER
+ '&upload_server=' + UPLOAD_SERVER
+ '#' + route
.route
;
256 route
.route
= '/' + k
+ '/#' + route
.route
;
258 route
.isExternal
= true;
265 function getCurrentPlugin() {
266 var paths
= window
.location
.pathname
.split('/');
267 var currentPath
= null;
268 if (paths
[0] != "") {
269 currentPath
= paths
[0]
271 currentPath
= paths
[1];
273 if (currentPath
!= null) {
276 console
.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
280 export default Alt
.createStore(SkyquakeContainerStore
);