Rift-15726 compress code in production environment
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeContainerStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18 //This will reach out to the global routes endpoint
19
20 import Alt from './skyquakeAltInstance.js';
21 import SkyquakeContainerSource from './skyquakeContainerSource.js';
22 import SkyquakeContainerActions from './skyquakeContainerActions';
23 import _ from 'lodash';
24 //Temporary, until api server is on same port as webserver
25 var rw = require('utils/rw.js');
26 var API_SERVER = rw.getSearchParams(window.location).api_server;
27 var UPLOAD_SERVER = rw.getSearchParams(window.location).upload_server;
28
29 class SkyquakeContainerStore {
30 constructor() {
31 this.currentPlugin = getCurrentPlugin();
32 this.nav = {};
33 this.notifications = [];
34 this.socket = null;
35 //Notification defaults
36 this.notificationMessage = '';
37 this.displayNotification = false;
38 this.notificationType = 'error';
39 //Screen Loader default
40 this.displayScreenLoader = false;
41 this.bindActions(SkyquakeContainerActions);
42 this.exportAsync(SkyquakeContainerSource);
43
44
45 this.exportPublicMethods({
46 // getNav: this.getNav
47 });
48
49 }
50 getSkyquakeNavSuccess = (data) => {
51 var self = this;
52 this.setState({
53 nav: decorateAndTransformNav(data, self.currentPlugin)
54 })
55 }
56
57 closeSocket = () => {
58 if (this.socket) {
59 window.multiplexer.channel(this.channelId).close();
60 }
61 this.setState({
62 socket: null
63 });
64 }
65 //Remove once logging plugin is implemented
66 getSysLogViewerURLSuccess(data){
67 window.open(data.url);
68 }
69 getSysLogViewerURLError(data){
70 console.log('failed', data)
71 }
72
73 openNotificationsSocketLoading = () => {
74 this.setState({
75 isLoading: true
76 })
77 }
78
79 openNotificationsSocketSuccess = (data) => {
80 var self = this;
81
82 let connection = data.connection;
83 let streamSource = data.streamSource;
84 console.log('Success opening notification socket for stream ', streamSource);
85
86 let ws = window.multiplexer.channel(connection);
87
88 if (!connection) return;
89 self.setState({
90 socket: ws.ws,
91 isLoading: false,
92 channelId: connection
93 });
94
95 ws.onmessage = (socket) => {
96 try {
97 var data = JSON.parse(socket.data);
98 if (!data.notification) {
99 console.warn('No notification in the received payload: ', data);
100 } else {
101 // Temp to test before adding multi-sources
102 data.notification.source = streamSource;
103 if (_.indexOf(self.notifications, data.notification) == -1) {
104 // newly appreared event.
105 // Add to the notifications list and setState
106 self.notifications.unshift(data.notification);
107 self.setState({
108 newNotificationEvent: true,
109 newNotificationMsg: data.notification,
110 notifications: self.notifications,
111 isLoading: false
112 });
113 }
114 }
115 } catch(e) {
116 console.log('Error in parsing data on notification socket');
117 }
118 };
119
120 ws.onclose = () => {
121 self.closeSocket();
122 };
123 }
124
125 openNotificationsSocketError = (data) => {
126 console.log('Error opening notification socket', data);
127 }
128
129 getEventStreamsLoading = () => {
130 this.setState({
131 isLoading: true
132 });
133 }
134
135 getEventStreamsSuccess = (streams) => {
136 console.log('Found streams: ', streams);
137 let self = this;
138
139 streams['ietf-restconf-monitoring:streams'] &&
140 streams['ietf-restconf-monitoring:streams']['stream'] &&
141 streams['ietf-restconf-monitoring:streams']['stream'].map((stream) => {
142 stream['access'] && stream['access'].map((streamLocation) => {
143 if (streamLocation['encoding'] == 'ws_json') {
144 setTimeout(() => {
145 self.getInstance().openNotificationsSocket(streamLocation['location'], stream['name']);
146 }, 0);
147 }
148 })
149 })
150
151 this.setState({
152 isLoading: true,
153 streams: streams
154 })
155 }
156
157 getEventStreamsError = (error) => {
158 console.log('Failed to get streams object');
159 this.setState({
160 isLoading: false
161 })
162 }
163
164 //Notifications
165 showNotification = (data) => {
166 let state = {
167 displayNotification: true,
168 notificationMessage: data,
169 notificationType: 'error',
170 displayScreenLoader: false
171 }
172 if(typeof(data) == 'string') {
173
174 } else {
175 state.notificationMessage = data.msg;
176 if(data.type) {
177 state.notificationType = data.type;
178 }
179 }
180 this.setState(state);
181 }
182 hideNotification = () => {
183 this.setState({
184 displayNotification: false
185 })
186 }
187 //ScreenLoader
188 showScreenLoader = () => {
189 this.setState({
190 displayScreenLoader: true
191 });
192 }
193 hideScreenLoader = () => {
194 this.setState({
195 displayScreenLoader: false
196 })
197 }
198
199 }
200
201 /**
202 * Receives nav data from routes rest endpoint and decorates the data with internal/external linking information
203 * @param {object} nav Nav item from /nav endoingpoint
204 * @param {string} currentPlugin Current plugin name taken from url path.
205 * @return {array} Returns list of constructed nav items.
206 */
207 function decorateAndTransformNav(nav, currentPlugin) {
208 for ( let k in nav) {
209 nav[k].pluginName = k;
210 if (k != currentPlugin) {
211 nav[k].routes.map(function(route, i) {
212 if (API_SERVER) {
213 route.route = '/' + k + '/index.html?api_server=' + API_SERVER + '&upload_server=' + UPLOAD_SERVER + '#' + route.route;
214 } else {
215 route.route = '/' + k + '/#' + route.route;
216 }
217 route.isExternal = true;
218 })
219 }
220 }
221 return nav;
222 }
223
224 function getCurrentPlugin() {
225 var paths = window.location.pathname.split('/');
226 var currentPath = null;
227 if (paths[0] != "") {
228 currentPath = paths[0]
229 } else {
230 currentPath = paths[1];
231 }
232 if (currentPath != null) {
233 return currentPath;
234 } else {
235 console.error('Well, something went horribly wrong with discovering the current plugin name - perhaps you should consider moving this logic to the server?')
236 }
237 }
238
239 export default Alt.createStore(SkyquakeContainerStore, 'SkyquakeContainerStore');