forked from Saleslogix/SDataJavaScriptClientLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDataUri.js
More file actions
362 lines (324 loc) · 12.5 KB
/
Copy pathSDataUri.js
File metadata and controls
362 lines (324 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright (c) 2010, Sage Software, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function(){
"use strict";
var Sage = window.Sage,
S = Sage,
C = Sage.namespace('Sage.SData.Client'),
trueRE = /^true$/i;
Sage.SData.Client.SDataUri = Sage.Class.define({
scheme: 'http',
host: '',
server: '',
port: -1,
version: null,
queryArgs: null,
pathSegments: null,
constructor: function(uri) {
/// <field name="scheme" type="String"></field>
this.base.apply(this, arguments);
S.apply(this, uri);
/* create copies; segments only needs a shallow copy, as elements are replaced, not modified. */
this.queryArgs = S.apply({}, uri && uri.queryArgs);
this.pathSegments = (uri && uri.pathSegments && uri.pathSegments.slice(0)) || [];
this.version = (uri && uri.version && S.apply({}, uri.version)) || { major: 1, minor: 0 };
},
clone: function() {
return new Sage.SData.Client.SDataUri(this);
},
getVersion: function() {
return this.version;
},
setVersion: function(value) {
this.version = S.apply({
major: 0,
minor: 0
}, value);
return this;
},
getScheme: function() {
/// <returns type="String">The scheme component of the URI.</returns>
return this.scheme;
},
setScheme: function(value) {
/// <param name="val" type="String">The new scheme for the URI</param>
this.scheme = value;
return this;
},
getHost: function() {
/// <returns type="String">The host component of the URI.</returns>
return this.host;
},
setHost: function(value) {
/// <param name="val" type="String">The new host for the URI</param>
this.host = value;
return this;
},
getPort: function() {
/// <returns type="Number">The port component of the URI.</returns>
return this.port;
},
setPort: function(value) {
/// <param name="val" type="String">The new port for the URI</param>
this.port = value;
return this;
},
getServer: function() {
/// <summary>
/// Access the SData "server" component of the URI. This is the first path segment in the URI.
///
/// i.e. [scheme]://[host]/[server]
/// </summary>
/// <returns type="String">The SData "server" component of URI.</returns>
return this.server;
},
setServer: function(value) {
/// <param name="val" type="String">The new SData "server" for the URI</param>
this.server = value;
return this;
},
getQueryArgs: function() {
/// <returns type="Object">The query arguments of the URI.</returns>
return this.queryArgs;
},
setQueryArgs: function(value, replace) {
/// <param name="val" type="Object">
/// The query arguments that will either be merged with the existing values, or replace
/// them entirely.
/// <param>
/// <param name="replace" type="Boolean" optional="true">True if you want to replace the existing query arguments.</param>
this.queryArgs = replace ? value : S.apply(this.queryArgs, value);
return this;
},
getQueryArg: function(key) {
/// <summary>Returns the requested query argument.</summary>
/// <param name="key" type="String">The name of the query argument to be returned.</param>
/// <returns type="String">The value of the requested query argument.</returns>
return this.queryArgs[key];
},
setQueryArg: function(key, value) {
/// <summary>Sets a requested query argument.</summary>
/// <param name="key" type="String">The name of the query argument to be set.</param>
/// <param name="val" type="String">The new value for the query argument.</param>
this.queryArgs[key] = value;
return this;
},
getPathSegments: function() {
/// <returns elementType="String">The path segments of the URI.</returns>
return this.pathSegments;
},
setPathSegments: function(value) {
this.pathSegments = value;
return this;
},
getPathSegment: function(i) {
return this.pathSegments.length > i
? this.pathSegments[i]
: false;
},
setPathSegment: function(i, value, predicate) {
/* can clear the segment */
if (!value && !predicate)
{
this.pathSegments[i] = null;
}
/* merge object onto segment */
else if (typeof value === 'object')
{
this.pathSegments[i] = S.apply({}, value, this.pathSegments[i]);
}
/* merge values onto segment */
else
{
var segment = {};
if (value) segment['text'] = value;
if (predicate) segment['predicate'] = predicate;
this.pathSegments[i] = S.apply({}, segment, this.pathSegments[i]);
}
return this;
},
getStartIndex: function() {
return this.queryArgs[C.SDataUri.QueryArgNames.StartIndex]
? parseInt(this.queryArgs[C.SDataUri.QueryArgNames.StartIndex])
: -1;
},
setStartIndex: function(value) {
this.queryArgs[C.SDataUri.QueryArgNames.StartIndex] = value;
return this;
},
getCount: function() {
return this.queryArgs[C.SDataUri.QueryArgNames.Count]
? parseInt(this.queryArgs[C.SDataUri.QueryArgNames.Count])
: -1;
},
setCount: function(value) {
this.queryArgs[C.SDataUri.QueryArgNames.Count] = value;
return this;
},
getIncludeContent: function() {
var name, value;
name = this.version.major >= 1
? C.SDataUri.QueryArgNames.IncludeContent
: C.SDataUri.QueryArgNames.LegacyIncludeContent;
value = this.queryArgs[name];
if (typeof value !== 'undefined') {
return trueRE.test(value);
} else {
return value;
}
},
setCompact: function(value) {
this.queryArgs[C.SDataUri.QueryArgNames.Compact] = value;
return this;
},
getCompact: function() {
return this.queryArgs[C.SDataUri.QueryArgNames.Compact];
},
setIncludeContent: function(value) {
var name = this.version.major >= 1
? C.SDataUri.QueryArgNames.IncludeContent
: C.SDataUri.QueryArgNames.LegacyIncludeContent;
this.queryArgs[name] = "" + value;
return this;
},
appendPath: function(value) {
var segment = typeof value === 'string' ? {text: value} : value;
this.pathSegments.push(segment);
return this;
},
toString: function(excludeQuery) {
return this.build(excludeQuery);
},
build: function(excludeQuery) {
var url = [];
url.push(this.getScheme() || C.SDataUri.Http);
url.push(C.SDataUri.SchemeSuffix);
url.push(C.SDataUri.PathSegmentPrefix);
url.push(C.SDataUri.PathSegmentPrefix);
url.push(this.getHost());
if (this.getPort() > 0) url.push(C.SDataUri.PortPrefix, this.getPort());
url.push(C.SDataUri.PathSegmentPrefix);
var segments = this.getPathSegments();
var path = [];
var server = this.getServer();
if (server && server.length > 0)
path = path.concat(server.split('/'));
for (var i = 0; i < segments.length; i++)
{
var segment = segments[i];
if (segment && segment['text'])
{
if (segment['predicate'])
path.push(encodeURIComponent(segment['text'] + '(' + segment['predicate'] + ')'));
else
path.push(encodeURIComponent(segment['text']));
}
}
url.push(path.join(C.SDataUri.PathSegmentPrefix));
if (excludeQuery) return url.join('');
var queryArgs = this.getQueryArgs();
var query = [];
for (var key in queryArgs)
{
query.push(
encodeURIComponent(key) +
C.SDataUri.QueryArgValuePrefix +
encodeURIComponent(queryArgs[key])
);
}
if (query.length > 0)
{
url.push(C.SDataUri.QueryPrefix);
url.push(query.join(C.SDataUri.QueryArgPrefix));
}
return url.join('');
},
getProduct: function() {
return this.getPathSegment(C.SDataUri.ProductPathIndex);
},
setProduct: function(val) {
return this.setPathSegment(C.SDataUri.ProductPathIndex, val);
},
getContract: function() {
return this.getPathSegment(C.SDataUri.ContractTypePathIndex);
},
setContract: function(val) {
return this.setPathSegment(C.SDataUri.ContractTypePathIndex, val);
},
getCompanyDataset: function() {
return this.getPathSegment(C.SDataUri.CompanyDatasetPathIndex);
},
setCompanyDataset: function(val) {
return this.setPathSegment(C.SDataUri.CompanyDatasetPathIndex, val);
},
getCollectionType: function() {
return this.getPathSegment(C.SDataUri.CollectionTypePathIndex);
},
setCollectionType: function(val) {
return this.setPathSegment(C.SDataUri.CollectionTypePathIndex, val);
},
getCollectionPredicate: function() {
var segment = this.getPathSegment(C.SDataUri.CollectionTypePathIndex);
return (segment && segment['predicate']) || false;
},
setCollectionPredicate: function(value) {
return this.setPathSegment(C.SDataUri.CollectionTypePathIndex, {
predicate: value
});
}
});
Sage.apply(Sage.SData.Client.SDataUri, {
Http: 'http',
Https: 'https',
PathSegmentPrefix: '/',
PortPrefix: ':',
QueryArgPrefix: '&',
QueryArgValuePrefix: '=',
QueryPrefix: '?',
SchemeSuffix: ':',
UnspecifiedPort: -1,
UriName: 'uri',
QueryArgNames: {
Compact: '_compact',
Count: 'count',
Exclude: 'exclude',
Format: 'format',
Include: 'include',
IncludeContent: '_includeContent',
LegacyIncludeContent: 'includeContent',
IncludeSchema: 'includeSchema',
Language: 'language',
OrderBy: 'orderby',
Precedence: 'precedence',
ReturnDelta: 'returnDelta',
Search: 'search',
Select: 'select',
StartIndex: 'startIndex',
Thumbnail: 'thumbnail',
TrackingID: 'trackingID',
Where: 'where'
},
ProductPathIndex: 0,
ContractTypePathIndex: 1,
CompanyDatasetPathIndex: 2,
CollectionTypePathIndex: 3,
ResourcePropertyIndex: 4,
ServiceMethodSegment: '$service',
TemplateSegment: '$template',
SystemSegment: '$system',
NamedQuerySegment: '$queries',
BatchSegment: '$batch'
});
})();