update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 import rw from 'utils/rw.js';
22 import Utils from 'utils/utils.js';
23
24 let API_SERVER = rw.getSearchParams(window.location).api_server;
25 let HOST = API_SERVER;
26 let NODE_PORT = rw.getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
27 let DEV_MODE = rw.getSearchParams(window.location).dev_mode || false;
28 let RW_REST_API_PORT = rw.getSearchParams(window.location).rw_rest_api_port || 8008;
29
30 if (DEV_MODE) {
31 HOST = window.location.protocol + '//' + window.location.hostname;
32 }
33
34 export default {
35 getNav() {
36 return {
37 remote: function() {
38 return new Promise(function(resolve, reject) {
39 $.ajax({
40 url: '/nav',
41 type: 'GET',
42 // beforeSend: Utils.addAuthorizationStub,
43 success: function(data) {
44 Utils.detectInactivity();
45 resolve(data);
46 }
47 })
48 })
49 },
50 success: SkyquakeContainerActions.getSkyquakeNavSuccess
51 }
52 },
53
54 getEventStreams() {
55 return {
56 remote: function(state, recordID) {
57 return new Promise(function(resolve, reject) {
58 $.ajax({
59 url: '//' + window.location.hostname + ':' + window.location.port + '/api/operational/restconf-state/streams?api_server=' + API_SERVER,
60 type: 'GET',
61 beforeSend: Utils.addAuthorizationStub,
62 success: function(data) {
63 resolve(data);
64 }
65 }).fail(function(xhr) {
66 //Authentication and the handling of fail states should be wrapped up into a connection class.
67 Utils.checkAuthentication(xhr.status);
68 });;
69 });
70 },
71 loading: SkyquakeContainerActions.getEventStreamsLoading,
72 success: SkyquakeContainerActions.getEventStreamsSuccess,
73 error: SkyquakeContainerActions.getEventStreamsError
74 }
75 },
76
77 openNotificationsSocket() {
78 return {
79 remote: function(state, location, streamSource) {
80 return new Promise((resolve, reject) => {
81 $.ajax({
82 url: '//' + window.location.hostname + ':' + window.location.port + '/socket-polling',
83 type: 'POST',
84 beforeSend: Utils.addAuthorizationStub,
85 data: {
86 url: location
87 },
88 success: (data) => {
89 // var url = Utils.webSocketProtocol() + '//' + window.location.hostname + ':' + data.port + data.socketPath;
90 // var ws = new WebSocket(url);
91 // resolve({
92 // ws: ws,
93 // streamSource: streamSource
94 // });
95 const checker = () => {
96 if (!Utils.isMultiplexerLoaded()) {
97 setTimeout(() => {
98 checker();
99 }, 500);
100 } else {
101 resolve({
102 connection: data.id,
103 streamSource: streamSource
104 });
105 }
106 };
107
108 checker();
109 }
110 });
111 });
112 },
113 loading: SkyquakeContainerActions.openNotificationsSocketLoading,
114 success: SkyquakeContainerActions.openNotificationsSocketSuccess,
115 error: SkyquakeContainerActions.openNotificationsSocketError
116 }
117 },
118 openProjectSocket() {
119 return {
120 remote: function(state) {
121 return new Promise(function(resolve, reject) {
122 //If socket connection already exists, eat the request.
123 if(state.socket) {
124 return resolve(false);
125 }
126 $.ajax({
127 url: '/socket-polling',
128 type: 'POST',
129 beforeSend: Utils.addAuthorizationStub,
130 data: {
131 url: '/project?api_server=' + API_SERVER
132 },
133 success: function(data, textStatus, jqXHR) {
134 Utils.checkAndResolveSocketRequest(data, resolve, reject);
135 }
136 })
137 .fail(function(xhr){
138 //Authentication and the handling of fail states should be wrapped up into a connection class.
139 Utils.checkAuthentication(xhr.status);
140 });;
141 });
142 },
143 success: SkyquakeContainerActions.openProjectSocketSuccess
144 }
145 },
146
147 getUserProfile() {
148 return {
149 remote: function(state, recordID) {
150 return new Promise(function(resolve, reject) {
151 $.ajax({
152 url: '/user-profile?api_server=' + API_SERVER,
153 type: 'GET',
154 beforeSend: Utils.addAuthorizationStub,
155 success: function(data) {
156 resolve(data);
157 }
158 }).fail(function(xhr) {
159 //Authentication and the handling of fail states should be wrapped up into a connection class.
160 Utils.checkAuthentication(xhr.status);
161 });;
162 });
163 },
164 loading: Alt.actions.global.showScreenLoader,
165 success: SkyquakeContainerActions.getUserProfileSuccess
166 }
167 },
168
169 selectActiveProject() {
170 return {
171 remote: function(state, projectId) {
172 const {currentPlugin} = state;
173 const encodedProjectId = encodeURIComponent(projectId);
174 return new Promise(function(resolve, reject) {
175 $.ajax({
176 url: `/session/${encodedProjectId}?api_server=${API_SERVER}&app=${currentPlugin}`,
177 type: 'PUT',
178 beforeSend: Utils.addAuthorizationStub,
179 success: function(data) {
180 resolve(projectId);
181 }
182 }).fail(function(xhr) {
183 //Authentication and the handling of fail states should be wrapped up into a connection class.
184 Utils.checkAuthentication(xhr.status);
185 });;
186 });
187 },
188 success: SkyquakeContainerActions.selectActiveProjectSuccess
189 }
190 }
191 }
192