forked from samohtred/X-Tree-M
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatajs-1.0.3.js
More file actions
7232 lines (6080 loc) · 308 KB
/
Copy pathdatajs-1.0.3.js
File metadata and controls
7232 lines (6080 loc) · 308 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// datajs.js
(function (window, undefined) {
if (!window.datajs) {
window.datajs = {};
}
if (!window.OData) {
window.OData = {};
}
var datajs = window.datajs;
var odata = window.OData;
// Provides an enumeration of possible kinds of payloads.
var PAYLOADTYPE_BATCH = "b";
var PAYLOADTYPE_COMPLEXTYPE = "c";
var PAYLOADTYPE_ENTRY = "entry"; // This is used when building the payload.
var PAYLOADTYPE_FEEDORLINKS = "f";
var PAYLOADTYPE_PRIMITIVETYPE = "p";
var PAYLOADTYPE_SVCDOC = "s";
var PAYLOADTYPE_UNKNOWN = "u";
var PAYLOADTYPE_NONE = "n";
// Provides an enumeration of possible kinds of properties.
var PROPERTYKIND_COMPLEX = "c";
var PROPERTYKIND_DEFERRED = "d";
var PROPERTYKIND_INLINE = "i";
var PROPERTYKIND_PRIMITIVE = "p";
var PROPERTYKIND_NONE = "n";
var assigned = function (value) {
/// <summary>Checks whether the specified value is different from null and undefined.</summary>
/// <param name="value" mayBeNull="true" optional="true">Value to check.</param>
/// <returns type="Boolean">true if the value is assigned; false otherwise.</returns>
return value !== null && value !== undefined;
};
var contains = function (arr, item) {
/// <summary>Checks whether the specified item is in the array.</summary>
/// <param name="arr" type="Array" optional="false" mayBeNull="false">Array to check in.</param>
/// <param name="item">Item to look for.</param>
/// <returns type="Boolean">true if the item is contained, false otherwise.</returns>
var i, len;
for (i = 0, len = arr.length; i < len; i++) {
if (arr[i] === item) {
return true;
}
}
return false;
};
var defined = function (a, b) {
/// <summary>Given two values, picks the first one that is not undefined.</summary>
/// <param name="a">First value.</param>
/// <param name="b">Second value.</param>
/// <returns>a if it's a defined value; else b.</returns>
return (a !== undefined) ? a : b;
};
var delay = function (callback) {
/// <summary>Delays the invocation of the specified function until execution unwinds.</summary>
/// <param name="callback" type="Function">Callback function.</param>
if (arguments.length === 1) {
window.setTimeout(callback, 0);
return;
}
var args = Array.prototype.slice.call(arguments, 1);
window.setTimeout(function () {
callback.apply(this, args);
}, 0);
};
var forEachSchema = function (metadata, callback) {
/// <summary>Invokes a function once per schema in metadata.</summary>
/// <param name="metadata">Metadata store; one of edmx, schema, or an array of any of them.</param>
/// <param name="callback" type="Function">Callback function to invoke once per schema.</param>
/// <returns>
/// The first truthy value to be returned from the callback; null or the last falsy value otherwise.
/// </returns>
if (!metadata) {
return null;
}
if (isArray(metadata)) {
var i, len, result;
for (i = 0, len = metadata.length; i < len; i++) {
result = forEachSchema(metadata[i], callback);
if (result) {
return result;
}
}
return null;
} else {
if (metadata.dataServices) {
return forEachSchema(metadata.dataServices.schema, callback);
}
return callback(metadata);
}
};
var isDateTimeOffset = function (value) {
/// <summary>Checks whether a Date object is DateTimeOffset value</summary>
/// <param name="value" type="Date" mayBeNull="false">Value to check.</param>
/// <returns type="Boolean">true if the value is a DateTimeOffset, false otherwise.</returns>
return (value.__edmType === "Edm.DateTimeOffset" || (!value.__edmType && value.__offset));
};
var formatMilliseconds = function (ms, ns) {
/// <summary>Formats a millisecond and a nanosecond value into a single string.</summary>
/// <param name="ms" type="Number" mayBeNull="false">Number of milliseconds to format.</param>
/// <param name="ns" type="Number" mayBeNull="false">Number of nanoseconds to format.</param>
/// <returns type="String">Formatted text.</returns>
/// <remarks>If the value is already as string it's returned as-is.</remarks>
// Avoid generating milliseconds if not necessary.
if (ms === 0) {
ms = "";
} else {
ms = "." + formatNumberWidth(ms.toString(), 3);
}
if (ns > 0) {
if (ms === "") {
ms = ".000";
}
ms += formatNumberWidth(ns.toString(), 4);
}
return ms;
};
var formatDateTimeOffset = function (value) {
/// <summary>Formats a DateTime or DateTimeOffset value a string.</summary>
/// <param name="value" type="Date" mayBeNull="false">Value to format.</param>
/// <returns type="String">Formatted text.</returns>
/// <remarks>If the value is already as string it's returned as-is.</remarks>
if (typeof value === "string") {
return value;
}
var hasOffset = isDateTimeOffset(value);
var offset = getCanonicalTimezone(value.__offset);
if (hasOffset && offset !== "Z") {
// We're about to change the value, so make a copy.
value = new Date(value.valueOf());
var timezone = parseTimezone(offset);
var hours = value.getUTCHours() + (timezone.d * timezone.h);
var minutes = value.getMinutes() + (timezone.d * timezone.m);
value.setUTCHours(hours, minutes);
} else if (!hasOffset) {
// Don't suffix a 'Z' for Edm.DateTime values.
offset = "";
}
var year = value.getUTCFullYear();
var month = value.getUTCMonth() + 1;
var sign = "";
if (year <= 0) {
year = -(year - 1);
sign = "-";
}
var ms = formatMilliseconds(value.getUTCMilliseconds(), value.__ns);
return sign +
formatNumberWidth(year, 4) + "-" +
formatNumberWidth(month, 2) + "-" +
formatNumberWidth(value.getUTCDate(), 2) + "T" +
formatNumberWidth(value.getUTCHours(), 2) + ":" +
formatNumberWidth(value.getUTCMinutes(), 2) + ":" +
formatNumberWidth(value.getUTCSeconds(), 2) +
ms + offset;
};
var formatDuration = function (value) {
/// <summary>Converts a duration to a string in xsd:duration format.</summary>
/// <param name="value" type="Object">Object with ms and __edmType properties.</param>
/// <returns type="String">String representation of the time object in xsd:duration format.</returns>
var ms = value.ms;
var sign = "";
if (ms < 0) {
sign = "-";
ms = -ms;
}
var days = Math.floor(ms / 86400000);
ms -= 86400000 * days;
var hours = Math.floor(ms / 3600000);
ms -= 3600000 * hours;
var minutes = Math.floor(ms / 60000);
ms -= 60000 * minutes;
var seconds = Math.floor(ms / 1000);
ms -= seconds * 1000;
return sign + "P" +
formatNumberWidth(days, 2) + "DT" +
formatNumberWidth(hours, 2) + "H" +
formatNumberWidth(minutes, 2) + "M" +
formatNumberWidth(seconds, 2) +
formatMilliseconds(ms, value.ns) + "S";
};
var formatNumberWidth = function (value, width, append) {
/// <summary>Formats the specified value to the given width.</summary>
/// <param name="value" type="Number">Number to format (non-negative).</param>
/// <param name="width" type="Number">Minimum width for number.</param>
/// <param name="append" type="Boolean">Flag indicating if the value is padded at the beginning (false) or at the end (true).</param>
/// <returns type="String">Text representation.</returns>
var result = value.toString(10);
while (result.length < width) {
if (append) {
result += "0";
} else {
result = "0" + result;
}
}
return result;
};
var getCanonicalTimezone = function (timezone) {
/// <summary>Gets the canonical timezone representation.</summary>
/// <param name="timezone" type="String">Timezone representation.</param>
/// <returns type="String">An 'Z' string if the timezone is absent or 0; the timezone otherwise.</returns>
return (!timezone || timezone === "Z" || timezone === "+00:00" || timezone === "-00:00") ? "Z" : timezone;
};
var invokeRequest = function (request, success, error, handler, httpClient, context) {
/// <summary>Sends a request containing OData payload to a server.</summary>
/// <param name="request">Object that represents the request to be sent..</param>
/// <param name="success">Callback for a successful read operation.</param>
/// <param name="error">Callback for handling errors.</param>
/// <param name="handler">Handler for data serialization.</param>
/// <param name="httpClient">HTTP client layer.</param>
/// <param name="context">Context used for processing the request</param>
return httpClient.request(request, function (response) {
try {
if (response.headers) {
normalizeHeaders(response.headers);
}
if (response.data === undefined) {
handler.read(response, context);
}
} catch (err) {
if (err.request === undefined) {
err.request = request;
}
if (err.response === undefined) {
err.response = response;
}
error(err);
return;
}
success(response.data, response);
}, error);
};
var isArray = function (value) {
/// <summary>Checks whether the specified value is an array object.</summary>
/// <param name="value">Value to check.</param>
/// <returns type="Boolean">true if the value is an array object; false otherwise.</returns>
return Object.prototype.toString.call(value) === "[object Array]";
};
var isDate = function (value) {
/// <summary>Checks whether the specified value is a Date object.</summary>
/// <param name="value">Value to check.</param>
/// <returns type="Boolean">true if the value is a Date object; false otherwise.</returns>
return Object.prototype.toString.call(value) === "[object Date]";
};
var lookupProperty = function (properties, name) {
/// <summary>Looks up a property by name.</summary>
/// <param name="properties" type="Array" mayBeNull="true">Array of property objects as per EDM metadata.</param>
/// <param name="name" type="String">Name to look for.</param>
/// <returns type="Object">The property object; null if not found.</returns>
if (properties) {
var i, len;
for (i = 0, len = properties.length; i < len; i++) {
if (properties[i].name === name) {
return properties[i];
}
}
}
return null;
};
var lookupTypeInMetadata = function (name, metadata, kind) {
/// <summary>Looks up a type object by name.</summary>
/// <param name="name" type="String">Name, possibly null or empty.</param>
/// <param name="metadata">Metadata store; one of edmx, schema, or an array of any of them.</param>
/// <param name="kind" type="String">Kind of type to look for; one of 'entityType' or 'complexType'.</param>
/// <returns>An type description if the name is found; null otherwise.</returns>
return (name) ? forEachSchema(metadata, function (schema) {
return lookupTypeInSchema(name, schema, kind);
}) : null;
};
var lookupComplexType = function (name, metadata) {
/// <summary>Looks up a complex type object by name.</summary>
/// <param name="name" type="String">Name, possibly null or empty.</param>
/// <param name="metadata">Metadata store; one of edmx, schema, or an array of any of them.</param>
/// <returns>A complex type description if the name is found; null otherwise.</returns>
return lookupTypeInMetadata(name, metadata, "complexType");
};
var lookupEntityType = function (name, metadata) {
/// <summary>Looks up an entity type object by name.</summary>
/// <param name="name" type="String">Name, possibly null or empty.</param>
/// <param name="metadata">Metadata store; one of edmx, schema, or an array of any of them.</param>
/// <returns>An entity type description if the name is found; null otherwise.</returns>
return lookupTypeInMetadata(name, metadata, "entityType");
};
//// Commented out - metadata is largely optional and we don't rely on it to this extent.
//// var lookupEntityTypeForNavigation = function (navigationProperty, metadata) {
//// /// <summary>Looks up the target entity type for a navigation property.</summary>
//// /// <param name="navigationProperty" type="Object"></param>
//// /// <param name="metadata" type="Object"></param>
//// /// <returns type="Object">The entity type metadata for the specified property, null if not found.</returns>
//// var rel = navigationProperty.relationship;
//// var association = forEachSchema(metadata, function (schema) {
//// // The name should be the namespace qualified name in 'ns'.'type' format.
//// var nameOnly = removeNamespace(schema["namespace"], rel);
//// var associations = schema.association;
//// if (nameOnly && associations) {
//// var i, len;
//// for (i = 0, len = associations.length; i < len; i++) {
//// if (associations[i].name === nameOnly) {
//// return associations[i];
//// }
//// }
//// }
//// });
//// var result = null;
//// if (association) {
//// var end = association.end[0];
//// if (end.role !== navigationProperty.toRole) {
//// end = association.end[1];
//// // For metadata to be valid, end.role === navigationProperty.toRole now.
//// }
//// result = lookupEntityType(end.type, metadata);
//// }
//// return result;
//// };
var removeNamespace = function (ns, fullName) {
/// <summary>Given an expected namespace prefix, removes it from a full name.</summary>
/// <param name="ns" type="String">Expected namespace.</param>
/// <param name="fullName" type="String">Full name in 'ns'.'name' form.</param>
/// <returns type="String">The local name, null if it isn't found in the expected namespace.</returns>
if (fullName.indexOf(ns) === 0 && fullName.charAt(ns.length) === ".") {
return fullName.substr(ns.length + 1);
}
return null;
};
var lookupTypeInSchema = function (name, metadata, kind) {
/// <summary>Looks up an entity type object by name.</summary>
/// <param name="name" type="String">Name (assigned).</param>
/// <param name="metadata">Metadata store; one of edmx, schema.</param>
/// <param name="kind" type="String">Kind of type to look for; one of 'entityType' or 'complexType'.</param>
/// <returns>An entity type description if the name is found; null otherwise.</returns>
/// <remarks>
/// metadata is considered an edmx object if it contains a dataServices object.
/// </remarks>
if (metadata) {
// The name should be the namespace qualified name in 'ns'.'type' format.
var nameOnly = removeNamespace(metadata["namespace"], name);
var types = metadata[kind];
if (nameOnly && types) {
var i, len;
for (i = 0, len = types.length; i < len; i++) {
if (types[i].name === nameOnly) {
return types[i];
}
}
}
}
return null;
};
var normalHeaders = {
"accept": "Accept",
"content-type": "Content-Type",
"dataserviceversion": "DataServiceVersion",
"maxdataserviceversion": "MaxDataServiceVersion"
};
var normalizeHeaders = function (headers) {
/// <summary>Normalizes headers so they can be found with consistent casing.</summary>
/// <param name="headers" type="Object">Dictionary of name/value pairs.</param>
for (var name in headers) {
var lowerName = name.toLowerCase();
var normalName = normalHeaders[lowerName];
if (normalName && name !== normalName) {
var val = headers[name];
delete headers[name];
headers[normalName] = val;
}
}
};
var undefinedDefault = function (value, defaultValue) {
/// <summary>Returns a default value in place of undefined.</summary>
/// <param name="value" mayBeNull="true" optional="true">Value to check.</param>
/// <param name="defaultValue">Value to return if value is undefined.</param>
/// <returns>value if it's defined; defaultValue otherwise.</returns>
/// <remarks>
/// This should only be used for cases where falsy values are valid;
/// otherwise the pattern should be 'x = (value) ? value : defaultValue;'.
/// </remarks>
return (value !== undefined) ? value : defaultValue;
};
var parseInt10 = function (value) {
/// <summary>Parses a value in base 10.</summary>
/// <param name="value" type="String">String value to parse.</param>
/// <returns type="Number">The parsed value, NaN if not a valid value.</returns>
return parseInt(value, 10);
};
// The captured indices for this expression are:
// 0 - complete input
// 1 - direction
// 2,3,4 - years, months, days
// 5,6,7,8 - hours, minutes, seconds, miliseconds
var parseTimeRE = /^([+-])?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?)?/;
var parseDuration = function (duration) {
/// <summary>Parses a string in xsd:duration format.</summary>
/// <param name="duration" type="String">Duration value.</param>
/// <remarks>
/// This method will throw an exception if the input string has a year or a month component.
/// </remarks>
/// <returns type="Object">Object representing the time</returns>
var parts = parseTimeRE.exec(duration);
if (parts === null) {
throw { message: "Invalid duration value." };
}
var years = parts[2] || "0";
var months = parts[3] || "0";
var days = parseInt10(parts[4] || 0);
var hours = parseInt10(parts[5] || 0);
var minutes = parseInt10(parts[6] || 0);
var seconds = parseFloat(parts[7] || 0);
if (years !== "0" || months !== "0") {
throw { message: "Unsupported duration value." };
}
var ms = parts[8];
var ns = 0;
if (!ms) {
ms = 0;
} else {
if (ms.length > 7) {
throw { message: "Cannot parse duration value to given precision." };
}
ns = formatNumberWidth(ms.substring(3), 4, true);
ms = formatNumberWidth(ms.substring(0, 3), 3, true);
ms = parseInt10(ms);
ns = parseInt10(ns);
}
ms += seconds * 1000 + minutes * 60000 + hours * 3600000 + days * 86400000;
if (parts[1] === "-") {
ms = -ms;
}
var result = { ms: ms, __edmType: "Edm.Time" };
if (ns) {
result.ns = ns;
}
return result;
};
var parseTimezone = function (timezone) {
/// <summary>Parses a timezone description in (+|-)nn:nn format.</summary>
/// <param name="timezone" type="String">Timezone offset.</param>
/// <returns type="Object">
/// An object with a (d)irection property of 1 for + and -1 for -,
/// offset (h)ours and offset (m)inutes.
/// </returns>
var direction = timezone.substring(0, 1);
direction = (direction === "+") ? 1 : -1;
var offsetHours = parseInt10(timezone.substring(1));
var offsetMinutes = parseInt10(timezone.substring(timezone.indexOf(":") + 1));
return { d: direction, h: offsetHours, m: offsetMinutes };
};
var payloadTypeOf = function (data) {
/// <summary>Determines the kind of payload applicable for the specified value.</summary>
/// <param name="data">Value to check.</param>
/// <returns type="String">One of the values declared on the payloadType object.</returns>
switch (typeof (data)) {
case "object":
if (!data) {
return PAYLOADTYPE_NONE;
}
if (isArray(data) || isArray(data.results)) {
return PAYLOADTYPE_FEEDORLINKS;
}
if (data.__metadata && data.__metadata.uri !== undefined) {
return PAYLOADTYPE_ENTRY;
}
if (isArray(data.EntitySets)) {
return PAYLOADTYPE_SVCDOC;
}
if (isArray(data.__batchRequests)) {
return PAYLOADTYPE_BATCH;
}
if (isDate(data)) {
return PAYLOADTYPE_PRIMITIVETYPE;
}
return PAYLOADTYPE_COMPLEXTYPE;
case "string":
case "number":
case "boolean":
return PAYLOADTYPE_PRIMITIVETYPE;
}
return PAYLOADTYPE_UNKNOWN;
};
var prepareRequest = function (request, handler, context) {
/// <summary>Prepares a request object so that it can be sent through the network.</summary>
/// <param name="request">Object that represents the request to be sent.</param>
/// <param name="handler">Handler for data serialization</param>
/// <param name="context">Context used for preparing the request</param>
// Default to GET if no method has been specified.
if (!request.method) {
request.method = "GET";
}
if (!request.headers) {
request.headers = {};
} else {
normalizeHeaders(request.headers);
}
if (request.headers.Accept === undefined) {
request.headers.Accept = handler.accept;
}
if (assigned(request.data) && request.body === undefined) {
handler.write(request, context);
}
if (!assigned(request.headers.MaxDataServiceVersion)) {
request.headers.MaxDataServiceVersion = handler.maxDataServiceVersion || "1.0";
}
};
var propertyKindOf = function (value) {
/// <summary>Determines the kind of property for the specified value.</summary>
/// <param name="value">Value to check.</param>
/// <returns type="String">One of the values declared on the propertyKind object.</returns>
switch (payloadTypeOf(value)) {
case PAYLOADTYPE_COMPLEXTYPE:
if (value.__deferred && value.__deferred.uri) {
return PROPERTYKIND_DEFERRED;
}
return PROPERTYKIND_COMPLEX;
case PAYLOADTYPE_FEEDORLINKS:
case PAYLOADTYPE_ENTRY:
return PROPERTYKIND_INLINE;
case PAYLOADTYPE_PRIMITIVETYPE:
return PROPERTYKIND_PRIMITIVE;
}
return PROPERTYKIND_NONE;
};
var throwErrorCallback = function (error) {
/// <summary>Default error handler.</summary>
/// <param name="error" type="Object">Error to handle.</param>
throw error;
};
var trimString = function (str) {
/// <summary>Removes leading and trailing whitespaces from a string.</summary>
/// <param name="str" type="String" optional="false" mayBeNull="false">String to trim</param>
/// <returns type="String">The string with no leading or trailing whitespace.</returns>
if (str.trim) {
return str.trim();
}
return str.replace(/^\s+|\s+$/g, '');
};
// Regular expression that splits a uri into its components:
// 0 - is the matched string.
// 1 - is the scheme.
// 2 - is the authority.
// 3 - is the path.
// 4 - is the query.
// 5 - is the fragment.
var uriRegEx = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#:]+)?(\?[^#]*)?(#.*)?/;
var uriPartNames = ["scheme", "authority", "path", "query", "fragment"];
var getURIInfo = function (uri) {
/// <summary>Gets information about the components of the specified URI.</summary>
/// <param name="uri" type="String">URI to get information from.</param>
/// <returns type="Object">
/// An object with an isAbsolute flag and part names (scheme, authority, etc.) if available.
/// </returns>
var result = { isAbsolute: false };
if (uri) {
var matches = uriRegEx.exec(uri);
if (matches) {
var i, len;
for (i = 0, len = uriPartNames.length; i < len; i++) {
if (matches[i + 1]) {
result[uriPartNames[i]] = matches[i + 1];
}
}
}
if (result.scheme) {
result.isAbsolute = true;
}
}
return result;
};
var getURIFromInfo = function (uriInfo) {
/// <summary>Builds a URI string from its components.</summary>
/// <param name="uriInfo" type="Object"> An object with uri parts (scheme, authority, etc.).</param>
/// <returns type="String">URI string.</returns>
return "".concat(
uriInfo.scheme || "",
uriInfo.authority || "",
uriInfo.path || "",
uriInfo.query || "",
uriInfo.fragment || "");
};
// Regular expression that splits a uri authority into its subcomponents:
// 0 - is the matched string.
// 1 - is the userinfo subcomponent.
// 2 - is the host subcomponent.
// 3 - is the port component.
var uriAuthorityRegEx = /^\/{0,2}(?:([^@]*)@)?([^:]+)(?::{1}(\d+))?/;
// Regular expression that matches percentage enconded octects (i.e %20 or %3A);
var pctEncodingRegEx = /%[0-9A-F]{2}/ig;
var normalizeURICase = function (uri) {
/// <summary>Normalizes the casing of a URI.</summary>
/// <param name="uri" type="String">URI to normalize, absolute or relative.</param>
/// <returns type="String">The URI normalized to lower case.</returns>
var uriInfo = getURIInfo(uri);
var scheme = uriInfo.scheme;
var authority = uriInfo.authority;
if (scheme) {
uriInfo.scheme = scheme.toLowerCase();
if (authority) {
var matches = uriAuthorityRegEx.exec(authority);
if (matches) {
uriInfo.authority = "//" +
(matches[1] ? matches[1] + "@" : "") +
(matches[2].toLowerCase()) +
(matches[3] ? ":" + matches[3] : "");
}
}
}
uri = getURIFromInfo(uriInfo);
return uri.replace(pctEncodingRegEx, function (str) {
return str.toLowerCase();
});
};
var normalizeURI = function (uri, base) {
/// <summary>Normalizes a possibly relative URI with a base URI.</summary>
/// <param name="uri" type="String">URI to normalize, absolute or relative.</param>
/// <param name="base" type="String" mayBeNull="true">Base URI to compose with.</param>
/// <returns type="String">The composed URI if relative; the original one if absolute.</returns>
if (!base) {
return uri;
}
var uriInfo = getURIInfo(uri);
if (uriInfo.isAbsolute) {
return uri;
}
var baseInfo = getURIInfo(base);
var normInfo = {};
var path;
if (uriInfo.authority) {
normInfo.authority = uriInfo.authority;
path = uriInfo.path;
normInfo.query = uriInfo.query;
} else {
if (!uriInfo.path) {
path = baseInfo.path;
normInfo.query = uriInfo.query || baseInfo.query;
} else {
if (uriInfo.path.charAt(0) === '/') {
path = uriInfo.path;
} else {
path = mergeUriPathWithBase(uriInfo.path, baseInfo.path);
}
normInfo.query = uriInfo.query;
}
normInfo.authority = baseInfo.authority;
}
normInfo.path = removeDotsFromPath(path);
normInfo.scheme = baseInfo.scheme;
normInfo.fragment = uriInfo.fragment;
return getURIFromInfo(normInfo);
};
var mergeUriPathWithBase = function (uriPath, basePath) {
/// <summary>Merges the path of a relative URI and a base URI.</summary>
/// <param name="uriPath" type="String>Relative URI path.</param>
/// <param name="basePath" type="String">Base URI path.</param>
/// <returns type="String">A string with the merged path.</returns>
var path = "/";
var end;
if (basePath) {
end = basePath.lastIndexOf("/");
path = basePath.substring(0, end);
if (path.charAt(path.length - 1) !== "/") {
path = path + "/";
}
}
return path + uriPath;
};
var removeDotsFromPath = function (path) {
/// <summary>Removes the special folders . and .. from a URI's path.</summary>
/// <param name="path" type="string">URI path component.</param>
/// <returns type="String">Path without any . and .. folders.</returns>
var result = "";
var segment = "";
var end;
while (path) {
if (path.indexOf("..") === 0 || path.indexOf(".") === 0) {
path = path.replace(/^\.\.?\/?/g, "");
} else if (path.indexOf("/..") === 0) {
path = path.replace(/^\/\..\/?/g, "/");
end = result.lastIndexOf("/");
if (end === -1) {
result = "";
} else {
result = result.substring(0, end);
}
} else if (path.indexOf("/.") === 0) {
path = path.replace(/^\/\.\/?/g, "/");
} else {
segment = path;
end = path.indexOf("/", 1);
if (end !== -1) {
segment = path.substring(0, end);
}
result = result + segment;
path = path.replace(segment, "");
}
}
return result;
};
var ticks = 0;
var canUseJSONP = function (request) {
/// <summary>
/// Checks whether the specified request can be satisfied with a JSONP request.
/// </summary>
/// <param name="request">Request object to check.</param>
/// <returns type="Boolean">true if the request can be satisfied; false otherwise.</returns>
// Requests that 'degrade' without changing their meaning by going through JSONP
// are considered usable.
//
// We allow data to come in a different format, as the servers SHOULD honor the Accept
// request but may in practice return content with a different MIME type.
if (request.method && request.method !== "GET") {
return false;
}
return true;
};
var createIFrame = function (url) {
/// <summary>Creates an IFRAME tag for loading the JSONP script</summary>
/// <param name="url" type="String">The source URL of the script</param>
/// <returns type="HTMLElement">The IFRAME tag</returns>
var iframe = window.document.createElement("IFRAME");
iframe.style.display = "none";
var attributeEncodedUrl = url.replace(/&/g, "&").replace(/"/g, """).replace(/\</g, "<");
var html = "<html><head><script type=\"text/javascript\" src=\"" + attributeEncodedUrl + "\"><\/script><\/head><body><\/body><\/html>";
var body = window.document.getElementsByTagName("BODY")[0];
body.appendChild(iframe);
writeHtmlToIFrame(iframe, html);
return iframe;
};
var createXmlHttpRequest = function () {
/// <summary>Creates a XmlHttpRequest object.</summary>
/// <returns type="XmlHttpRequest">XmlHttpRequest object.</returns>
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest();
}
var exception;
if (window.ActiveXObject) {
try {
return new window.ActiveXObject("Msxml2.XMLHTTP.6.0");
} catch (_) {
try {
return new window.ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch (e) {
exception = e;
}
}
} else {
exception = { message: "XMLHttpRequest not supported" };
}
throw exception;
};
var isAbsoluteUrl = function (url) {
/// <summary>Checks whether the specified URL is an absolute URL.</summary>
/// <param name="url" type="String">URL to check.</param>
/// <returns type="Boolean">true if the url is an absolute URL; false otherwise.</returns>
return url.indexOf("http://") === 0 ||
url.indexOf("https://") === 0 ||
url.indexOf("file://") === 0;
};
var isLocalUrl = function (url) {
/// <summary>Checks whether the specified URL is local to the current context.</summary>
/// <param name="url" type="String">URL to check.</param>
/// <returns type="Boolean">true if the url is a local URL; false otherwise.</returns>
if (!isAbsoluteUrl(url)) {
return true;
}
// URL-embedded username and password will not be recognized as same-origin URLs.
var location = window.location;
var locationDomain = location.protocol + "//" + location.host + "/";
return (url.indexOf(locationDomain) === 0);
};
var removeCallback = function (name, tick) {
/// <summary>Removes a callback used for a JSONP request.</summary>
/// <param name="name" type="String">Function name to remove.</param>
/// <param name="tick" type="Number">Tick count used on the callback.</param>
try {
delete window[name];
} catch (err) {
window[name] = undefined;
if (tick === ticks - 1) {
ticks -= 1;
}
}
};
var removeIFrame = function (iframe) {
/// <summary>Removes an iframe.</summary>
/// <param name="iframe" type="Object">The iframe to remove.</param>
/// <returns type="Object">Null value to be assigned to iframe reference.</returns>
if (iframe) {
writeHtmlToIFrame(iframe, "");
iframe.parentNode.removeChild(iframe);
}
return null;
};
var readResponseHeaders = function (xhr, headers) {
/// <summary>Reads response headers into array.</summary>
/// <param name="xhr" type="XMLHttpRequest">HTTP request with response available.</param>
/// <param name="headers" type="Array">Target array to fill with name/value pairs.</param>
var responseHeaders = xhr.getAllResponseHeaders().split(/\r?\n/);
var i, len;
for (i = 0, len = responseHeaders.length; i < len; i++) {
if (responseHeaders[i]) {
var header = responseHeaders[i].split(": ");
headers[header[0]] = header[1];
}
}
};
var writeHtmlToIFrame = function (iframe, html) {
/// <summary>Writes HTML to an IFRAME document.</summary>
/// <param name="iframe" type="HTMLElement">The IFRAME element to write to.</param>
/// <param name="html" type="String">The HTML to write.</param>
var frameDocument = (iframe.contentWindow) ? iframe.contentWindow.document : iframe.contentDocument.document;
frameDocument.open();
frameDocument.write(html);
frameDocument.close();
};
odata.defaultHttpClient = {
callbackParameterName: "$callback",
formatQueryString: "$format=json",
enableJsonpCallback: false,
request: function (request, success, error) {
/// <summary>Performs a network request.</summary>
/// <param name="request" type="Object">Request description.</request>
/// <param name="success" type="Function">Success callback with the response object.</param>
/// <param name="error" type="Function">Error callback with an error object.</param>
/// <returns type="Object">Object with an 'abort' method for the operation.</returns>
var result = {};
var xhr = null;
var done = false;
var iframe;
result.abort = function () {