Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / src / src / sources / CatalogDataSource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import _ from 'lodash'
22 import $ from 'jquery'
23 import alt from '../alt'
24 import utils from '../libraries/utils'
25 import serializers from '../libraries/model/DescriptorModelSerializer'
26 import CatalogDataSourceActions from '../actions/CatalogDataSourceActions'
27 let Utils = require('utils/utils.js');
28 const CatalogDataSource = {
29
30 loadCatalogs: function() {
31 return {
32 remote: function() {
33 return new Promise(function(resolve, reject) {
34 $.ajax({
35 beforeSend: Utils.addAuthorizationStub,
36 url: 'api/catalog?api_server=' + utils.getSearchParams(window.location).api_server,
37 // url: '/composer/big-honking-catalog',
38 // url: '/composer/ping-pong-catalog',
39 // url: '/composer/empty-nsd-catalog',
40 success: function(data) {
41 if (typeof data == 'string') {
42 data = JSON.parse(data);
43 }
44 const context = Object.assign({}, this, {data: data});
45 resolve(context);
46 },
47 error: function(error) {
48 if (typeof error == 'string') {
49 error = JSON.parse(error);
50 }
51 reject(error);
52 }
53 }).fail(function(xhr){
54 //Authentication and the handling of fail states should be wrapped up into a connection class.
55 Utils.checkAuthentication(xhr.status);
56 });
57 });
58 },
59 success: CatalogDataSourceActions.loadCatalogsSuccess,
60 error: CatalogDataSourceActions.loadCatalogsError
61 }
62 },
63
64 deleteCatalogItem: function() {
65 return {
66 remote: function(state, catalogType, itemId) {
67 return new Promise(function(resolve, reject) {
68 $.ajax({
69 url: 'api/catalog/' + catalogType + '/' + itemId + '?api_server=' + utils.getSearchParams(window.location).api_server,
70 type: 'DELETE',
71 beforeSend: utils.addAuthorizationStub,
72 success: function(data) {
73 resolve({
74 data: data,
75 catalogType: catalogType,
76 itemId: itemId
77 });
78 },
79 error: function(error) {
80 reject({
81 error: error,
82 catalogType: catalogType,
83 itemId: itemId
84 });
85 }
86 });
87 });
88 },
89 success: CatalogDataSourceActions.deleteCatalogItemSuccess,
90 error: CatalogDataSourceActions.deleteCatalogItemError
91 }
92 },
93
94 saveCatalogItem: function () {
95 return {
96 remote: function (state, item = {}) {
97 const method = item.uiState.isNew ? 'POST' : 'PUT';
98 const payload = serializers.serialize(item);
99 return new Promise((resolve, reject) => {
100 if (payload === false) {
101 reject({
102 error: 'unable to serialize item: ' + item.id,
103 catalogType: item.uiState.type,
104 itemId: payload.id
105 });
106 }
107 if (method === 'POST') {
108 $.ajax({
109 url: 'api/catalog/' + item.uiState.type + '?api_server=' + utils.getSearchParams(window.location).api_server,
110 type: method,
111 beforeSend: utils.addAuthorizationStub,
112 data: payload,
113 dataType: 'json',
114 success: function(data) {
115 resolve({
116 data: data,
117 catalogType: item.uiState.type,
118 itemId: payload.id
119 });
120 },
121 error: function(error) {
122 reject({
123 error: error,
124 catalogType: item.uiState.type,
125 itemId: payload.id
126 });
127 }
128 });
129 } else {
130 $.ajax({
131 url: 'api/catalog/' + item.uiState.type + '/' + payload.id + '?api_server=' + utils.getSearchParams(window.location).api_server,
132 type: method,
133 beforeSend: utils.addAuthorizationStub,
134 data: payload,
135 dataType: 'json',
136 success: function(data) {
137 resolve({
138 data: data,
139 catalogType: item.uiState.type,
140 itemId: payload.id
141 });
142 },
143 error: function(error) {
144 reject({
145 error: error,
146 catalogType: item.uiState.type,
147 itemId: payload.id
148 });
149 }
150 });
151 }
152 });
153 },
154 success: CatalogDataSourceActions.saveCatalogItemSuccess,
155 error: CatalogDataSourceActions.saveCatalogItemError
156 };
157 }
158 };
159
160 export default CatalogDataSource;