Bug 209
[osm/UI.git] / skyquake / framework / widgets / skyquake_container / skyquakeContainerSource.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 import Alt from './skyquakeAltInstance.js';
19 import $ from 'jquery';
20 import SkyquakeContainerActions from './skyquakeContainerActions'
21
22 let Utils = require('utils/utils.js');
23 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
24 let HOST = API_SERVER;
25 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
26 let DEV_MODE = require('utils/rw.js').getSearchParams(window.location).dev_mode || false;
27 let RW_REST_API_PORT = require('utils/rw.js').getSearchParams(window.location).rw_rest_api_port || 8008;
28
29 if (DEV_MODE) {
30 HOST = window.location.protocol + '//' + window.location.hostname;
31 }
32
33 export default {
34 getNav() {
35 return {
36 remote: function() {
37 return new Promise(function(resolve, reject) {
38 $.ajax({
39 url: '/nav',
40 type: 'GET',
41 // beforeSend: Utils.addAuthorizationStub,
42 success: function(data) {
43 Utils.detectInactivity();
44 resolve(data);
45 }
46 })
47 })
48 },
49 success: SkyquakeContainerActions.getSkyquakeNavSuccess
50 }
51 },
52
53 getEventStreams() {
54 return {
55 remote: function(state, recordID) {
56 return new Promise(function(resolve, reject) {
57 $.ajax({
58 url: '//' + window.location.hostname + ':' + window.location.port + '/api/operational/restconf-state/streams?api_server=' + API_SERVER,
59 type: 'GET',
60 beforeSend: Utils.addAuthorizationStub,
61 success: function(data) {
62 resolve(data);
63 }
64 }).fail(function(xhr) {
65 //Authentication and the handling of fail states should be wrapped up into a connection class.
66 Utils.checkAuthentication(xhr.status);
67 });;
68 });
69 },
70 loading: SkyquakeContainerActions.getEventStreamsLoading,
71 success: SkyquakeContainerActions.getEventStreamsSuccess,
72 error: SkyquakeContainerActions.getEventStreamsError
73 }
74 },
75
76 openNotificationsSocket() {
77 return {
78 remote: function(state, location, streamSource) {
79 return new Promise((resolve, reject) => {
80 $.ajax({
81 url: '//' + window.location.hostname + ':' + window.location.port + '/socket-polling?api_server=' + API_SERVER,
82 type: 'POST',
83 beforeSend: Utils.addAuthorizationStub,
84 data: {
85 url: location
86 },
87 success: (data) => {
88 // var url = Utils.webSocketProtocol() + '//' + window.location.hostname + ':' + data.port + data.socketPath;
89 // var ws = new WebSocket(url);
90 // resolve({
91 // ws: ws,
92 // streamSource: streamSource
93 // });
94 const checker = () => {
95 if (!Utils.isMultiplexerLoaded()) {
96 setTimeout(() => {
97 checker();
98 }, 500);
99 } else {
100 resolve({
101 connection: data.id,
102 streamSource: streamSource
103 });
104 }
105 };
106
107 checker();
108 }
109 });
110 });
111 },
112 loading: SkyquakeContainerActions.openNotificationsSocketLoading,
113 success: SkyquakeContainerActions.openNotificationsSocketSuccess,
114 error: SkyquakeContainerActions.openNotificationsSocketError
115 }
116 }
117 }
118