forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
1235 lines (1141 loc) · 36.9 KB
/
Util.js
File metadata and controls
1235 lines (1141 loc) · 36.9 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
import {
Zondy
} from './Base';
var extend = function (destination, source) {
destination = destination || {};
if (source) {
for (var property in source) {
var value = source[property];
if (value !== undefined) {
destination[property] = value;
}
}
/**
* IE doesn't include the toString property when iterating over an object's
* properties with the for(property in object) syntax. Explicitly check if
* the source has its own toString property.
*/
/*
* FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
* prototype object" when calling hawOwnProperty if the source object
* is an instance of window.Event.
*/
var sourceIsEvt = typeof window.Event === "function" &&
source instanceof window.Event;
if (!sourceIsEvt &&
source.hasOwnProperty && source.hasOwnProperty("toString")) {
destination.toString = source.toString;
}
}
return destination;
};
/*
* @description 判断一个对象是否是数组。
* @param a - {Object} 对象。
* @return {boolean} 是否是数组。
*/
var isArray = function (a) {
return (Object.prototype.toString.call(a) === '[object Array]');
};
var extendDeep = function (destination, source) {
var i, toStr = Object.prototype.toString,
astr = "[object Array]";
destination = destination || {};
for (i in source) {
if (source.hasOwnProperty(i)) {
if (typeof source[i] === "object") {
if (toStr.call(destination[i]) === '[object Null]' || toStr.call(destination[i]) === '[object Undefined]') {
destination[i] = (toStr.call(source[i]) === astr) ? [] : {};
}
extendDeep(destination[i], source[i]);
} else {
destination[i] = source[i];
}
if(source[i]===''||source[i]===null){
destination[i] = source[i];
}
}
}
return destination;
};
/*
* @description 对象拷贝。
* @param des - {Object} 目标对象。
* @param soc - {Object} 源对象
*/
var copy = function (des, soc) {
des = des || {};
var v;
if (soc) {
for (var p in des) {
v = soc[p];
if (typeof v !== 'undefined') {
des[p] = v;
}
}
}
};
/*
* @description 对象拷贝。
* @param des - {Object} 目标对象。
* @param soc - {Object} 源对象
*/
var copyExcluce = function (des, soc, exclude) {
des = des || {};
var v;
if (soc) {
for (var p in soc) {
v = soc[p];
if (typeof v !== 'undefined' && typeof v !== 'function') {
if (exclude.indexOf(p) < 0) {
des[p] = v;
}
}
}
}
};
/*
* @description 销毁对象,将其属性置空
* @param obj - {Object} 目标对象。
*/
var reset = function (obj) {
obj = obj || {};
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
if (typeof obj[p] === "object" && obj[p] instanceof Array) {
for (var i in obj[p]) {
if (obj[p][i].destroy) {
obj[p][i].destroy();
}
}
obj[p].length = 0;
} else if (typeof obj[p] === "object" && obj[p] instanceof Object) {
if (obj[p].destroy) {
obj[p].destroy();
}
}
obj[p] = null;
}
}
};
/*
* @description 获取HTML元素数组。
* @param argument - {String | HTMLElement | Window}
* @return {Array<HTMLElement>} HTML元素数组。
*/
var getElement = function () {
var elements = [];
for (var i = 0, len = arguments.length; i < len; i++) {
var element = arguments[i];
if (typeof element === 'string') {
element = document.getElementById(element);
}
if (arguments.length === 1) {
return element;
}
elements.push(element);
}
return elements;
};
/*
* @description instance of的跨浏览器实现。
* @param o - {Object} 对象。
* @return {boolean} 是否是页面元素
*/
var isElement = function (o) {
return !!(o && o.nodeType === 1);
};
/*
* @description 从数组中删除某一项。
* @param array - {Array} 数组。
* @param item - {Object} 数组中要删除的一项。
* @return {Array} 执行删除操作后的数组。
*/
var removeItem = function (array, item) {
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] === item) {
array.splice(i, 1);
//break;more than once??
}
}
return array;
};
/*
* @description 获取某对象再数组中的索引值。
* @param array - {Array} 数组。
* @param obj - {Object} 对象。
* @return {number} 某对象再数组中的索引值。
*/
var indexOf = function (array, obj) {
if (array == null) {
return -1;
} else {
// use the build-in function if available.
if (typeof array.indexOf === "function") {
return array.indexOf(obj);
} else {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] === obj) {
return i;
}
}
return -1;
}
}
};
/*
* @description 修改某DOM元素的许多属性。
* @param element - {HTMLElement} 待修改的DOM元素。
* @param id - {string} DOM元素的id。
* @param px - {Pixel} 包含DOM元素的style属性的left和top属性。
* @param sz - {Size} 包含DOM元素的width和height属性。
* @param position - {string} DOM元素的position属性。
* @param border - {string} DOM元素的style属性的border属性。
* @param overflow - {string} DOM元素的style属性的overflow属性。
* @param opacity - {number} 不透明度值。取值范围为 (0.0 - 1.0)。
*/
var modifyDOMElement = function (element, id, px, sz, position,
border, overflow, opacity) {
if (id) {
element.id = id;
}
if (px) {
element.style.left = px.x + "px";
element.style.top = px.y + "px";
}
if (sz) {
element.style.width = sz.w + "px";
element.style.height = sz.h + "px";
}
if (position) {
element.style.position = position;
}
if (border) {
element.style.border = border;
}
if (overflow) {
element.style.overflow = overflow;
}
if (parseFloat(opacity) >= 0.0 && parseFloat(opacity) < 1.0) {
element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
element.style.opacity = opacity;
} else if (parseFloat(opacity) === 1.0) {
element.style.filter = '';
element.style.opacity = '';
}
};
/*
* @description Takes an object and copies any properties that don't exist from
* another properties
*
* @param to -{Object} The destination object.
* @param from -{Object} The source object. Any properties of this object that
* are undefined in the to object will be set on the to object.
*
* @return {Object} A reference to the to object. Note that the to argument is modified
* in place and returned by this function.
*/
var applyDefaults = function (to, from) {
to = to || {};
/*
* FF/Windows < 2.0.0.13 reports "Illegal operation on WrappedNative
* prototype object" when calling hawOwnProperty if the source object is an
* instance of window.Event.
*/
var fromIsEvt = typeof window.Event === "function" &&
from instanceof window.Event;
for (var key in from) {
if (to[key] === undefined ||
(!fromIsEvt && from.hasOwnProperty &&
from.hasOwnProperty(key) && !to.hasOwnProperty(key))) {
to[key] = from[key];
}
}
/**
* IE doesn't include the toString property when iterating over an object's
* properties with the for(property in object) syntax. Explicitly check if
* the source has its own toString property.
*/
if (!fromIsEvt && from && from.hasOwnProperty &&
from.hasOwnProperty('toString') && !to.hasOwnProperty('toString')) {
to.toString = from.toString;
}
return to;
};
/*
* @param params - {Object} 参数对象。
* @return {string} HTTP的GEI请求中的参数字符串。
* @description 将参数对象转换为HTTP的GEI请求中的参数字符串。例如:"key1=value1&key2=value2&key3=value3"。
*/
var getParameterString = function (params) {
var paramsArray = [];
for (var key in params) {
var value = params[key];
if ((value != null) && (typeof value !== 'function')) {
var encodedValue;
if (typeof value === 'object' && value.constructor === Array) {
/* value is an array; encode items and separate with "," */
var encodedItemArray = [];
var item;
for (var itemIndex = 0, len = value.length; itemIndex < len; itemIndex++) {
item = value[itemIndex];
encodedItemArray.push(encodeURIComponent(
(item === null || item === undefined) ? "" : item));
}
encodedValue = encodedItemArray.join(",");
} else {
/* value is a string; simply encode */
encodedValue = encodeURIComponent(value);
}
paramsArray.push(encodeURIComponent(key) + "=" + encodedValue);
}
}
return paramsArray.join("&");
};
/*
*获取工作流参数字符串
*/
var getWFParameterString = function (params) {
var paramsArray = [];
for (var key in params) {
var value = params[key];
if ((value != null) && (typeof value !== 'function')) {
var encodedValue;
if (typeof value === 'object' && value.constructor === Array) {
/* value is an array; encode items and separate with "," */
var encodedItemArray = [];
var item;
for (var itemIndex = 0, len = value.length; itemIndex < len; itemIndex++) {
item = value[itemIndex];
encodedItemArray.push(encodeURIComponent(
(item === null || item === undefined) ? "" : item));
}
encodedValue = encodedItemArray.join(",");
} else {
/* value is a string; simply encode */
encodedValue = encodeURIComponent(value);
}
paramsArray.push(encodeURIComponent(key) + ":" + encodedValue);
}
}
return paramsArray.join(";");
};
/*
* @description 给url追加参数。
* @param url - {string} 待追加参数的url字符串。
* @param paramStr - {string} 待追加的参数。
* @return {string} The new url
*/
var urlAppend = function (url, paramStr) {
var newUrl = url;
if (paramStr) {
var parts = (url + " ").split(/[?&]/);
newUrl += (parts.pop() === " " ?
paramStr :
parts.length ? "&" + paramStr : "?" + paramStr);
}
return newUrl;
};
/*
* @description 从URL字符串中解析出参数对象。
* @param url - {string} url。
* @return {Object} 解析出的参数对象。
*/
var getParameters = function (url) {
// if no url specified, take it from the location bar
url = (url === null || url === undefined) ? window.location.href : url;
//parse out parameters portion of url string
var paramsString = "";
if (url.indexOf('?') > -1) {
var start = url.indexOf('?') + 1;
url.indexOf("#") > -1
var end = url.indexOf("#") > -1 ?
url.indexOf('#') : url.length;
paramsString = url.substring(start, end);
}
var parameters = {};
var pairs = paramsString.split(/[&;]/);
for (var i = 0, len = pairs.length; i < len; ++i) {
var keyValue = pairs[i].split('=');
if (keyValue[0]) {
var key = keyValue[0];
try {
key = decodeURIComponent(key);
} catch (err) {
key = unescape(key);
}
// being liberal by replacing "+" with " "
var value = (keyValue[1] || '').replace(/\+/g, " ");
try {
value = decodeURIComponent(value);
} catch (err) {
value = unescape(value);
}
// follow OGC convention of comma delimited values
value = value.split(",");
//if there's only one value, do not return as array
if (value.length == 1) {
value = value[0];
}
parameters[key] = value;
}
}
return parameters;
};
/*
* @description 如果userAgent捕获到浏览器使用的是Gecko引擎则返回true。
* @constant
*/
var IS_GECKO = (function () {
var ua = navigator.userAgent.toLowerCase();
return ua.indexOf("webkit") === -1 && ua.indexOf("gecko") !== -1;
})();
/*
* @description 浏览器名称,依赖于userAgent属性,BROWSER_NAME可以是空,或者以下浏览器:
* * "opera" -- Opera
* * "msie" -- Internet Explorer
* * "safari" -- Safari
* * "firefox" -- Firefox
* * "mozilla" -- Mozilla
* @constant
*/
var Browser = (function () {
var name = '',
version = '',
device = 'pc',
uaMatch;
//以下进行测试
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("msie") > -1 || (ua.indexOf("trident") > -1 && ua.indexOf("rv") > -1)) {
name = 'msie';
uaMatch = ua.match(/msie ([\d.]+)/) || ua.match(/rv:([\d.]+)/);
} else if (ua.indexOf("chrome") > -1) {
name = 'chrome';
uaMatch = ua.match(/chrome\/([\d.]+)/);
} else if (ua.indexOf("firefox") > -1) {
name = 'firefox';
uaMatch = ua.match(/firefox\/([\d.]+)/);
} else if (ua.indexOf("opera") > -1) {
name = 'opera';
uaMatch = ua.match(/version\/([\d.]+)/);
} else if (ua.indexOf("safari") > -1) {
name = 'safari';
uaMatch = ua.match(/version\/([\d.]+)/);
}
version = uaMatch ? uaMatch[1] : '';
if (ua.indexOf("ipad") > -1 || ua.indexOf("ipod") > -1 || ua.indexOf("iphone") > -1) {
device = 'apple';
} else if (ua.indexOf("android") > -1) {
uaMatch = ua.match(/version\/([\d.]+)/);
version = uaMatch ? uaMatch[1] : '';
device = 'android';
}
return {
name: name,
version: version,
device: device
};
})();
/*
* @description 获取浏览器相关信息。支持的浏览器包括:Opera,Internet Explorer,Safari,Firefox。
* @return {Object} 获取浏览器名称、版本、设备名称。对应的属性分别为 name, version, device。
*/
var getBrowser = function () {
return Browser;
};
/*
* @description 浏览器是否支持Canvas。
* @return {boolean} 获取当前浏览器是否支持 HTML5 Canvas 。
*/
var isSupportCanvas = (function () {
var checkRes = true,
broz = getBrowser();
if (document.createElement("canvas").getContext) {
if (broz.name === 'firefox' && parseFloat(broz.version) < 5) {
checkRes = false;
}
if (broz.name === 'safari' && parseFloat(broz.version) < 4) {
checkRes = false;
}
if (broz.name === 'opera' && parseFloat(broz.version) < 10) {
checkRes = false;
}
if (broz.name === 'msie' && parseFloat(broz.version) < 9) {
checkRes = false;
}
} else {
checkRes = false;
}
return checkRes;
})();
/*
* @description 判断;浏览器是否支持Canvas。
* @return {boolean} 获取当前浏览器是否支持 HTML5 Canvas 。
*/
var supportCanvas = function () {
return isSupportCanvas;
};
/*
* @description 判断一个 URL 请求是否在当前域中。
* @param url - {string} URL 请求字符串。
* @return {boolean} URL请求是否在当前域中。
*/
var isInTheSameDomain = function (url) {
if (!url) {
return true;
}
var index = url.indexOf("//");
var documentUrl = document.location.toString();
var documentIndex = documentUrl.indexOf("//");
if (index === -1) {
return true;
} else {
var protocol;
var substring = protocol = url.substring(0, index);
var documentSubString = documentUrl.substring(documentIndex + 2);
documentIndex = documentSubString.indexOf("/");
var documentPortIndex = documentSubString.indexOf(":");
var documentDomainWithPort = documentSubString.substring(0, documentIndex);
//var documentPort;
var documentprotocol = document.location.protocol;
if (documentPortIndex !== -1) {
// documentPort = +documentSubString.substring(documentPortIndex, documentIndex);
} else {
documentDomainWithPort += ':' + (documentprotocol.toLowerCase() === 'http:' ? 80 : 443);
}
if (documentprotocol.toLowerCase() !== substring.toLowerCase()) {
return false;
}
substring = url.substring(index + 2);
var portIndex = substring.indexOf(":");
index = substring.indexOf("/");
var domainWithPort = substring.substring(0, index);
var domain;
if (portIndex !== -1) {
domain = substring.substring(0, portIndex);
} else {
domain = substring.substring(0, index);
domainWithPort += ':' + (protocol.toLowerCase() === 'http:' ? 80 : 443);
}
var documentDomain = document.domain;
if (domain === documentDomain && domainWithPort === documentDomainWithPort) {
return true;
}
}
return false;
};
/*
* @description 将对象转换成 JSON 字符串。
* @param obj - {Object} 要转换成 JSON 的 Object 对象。
* @return {string} 返回转换后的 JSON 对象。
*/
var toJSON = function (obj) {
var objInn = obj;
if (objInn == null) {
return null;
}
switch (objInn.constructor) {
case String:
//s = "'" + str.replace(/(["\\])/g, "\\$1") + "'"; string含有单引号出错
objInn = '"' + objInn.replace(/(["\\])/g, '\\$1') + '"';
objInn = objInn.replace(/\n/g, "\\n");
objInn = objInn.replace(/\r/g, "\\r");
objInn = objInn.replace("<", "<");
objInn = objInn.replace(">", ">");
objInn = objInn.replace(/%/g, "%25");
objInn = objInn.replace(/&/g, "%26");
return objInn;
case Array:
var arr = [];
for (var i = 0, len = objInn.length; i < len; i++) {
arr.push(toJSON(objInn[i]));
}
return "[" + arr.join(",") + "]";
case Number:
return isFinite(objInn) ? String(objInn) : null;
case Boolean:
return String(objInn);
case Date:
var dateStr = "{" + "'__type':\"System.DateTime\"," +
"'Year':" + objInn.getFullYear() + "," +
"'Month':" + (objInn.getMonth() + 1) + "," +
"'Day':" + objInn.getDate() + "," +
"'Hour':" + objInn.getHours() + "," +
"'Minute':" + objInn.getMinutes() + "," +
"'Second':" + objInn.getSeconds() + "," +
"'Millisecond':" + objInn.getMilliseconds() + "," +
"'TimezoneOffset':" + objInn.getTimezoneOffset() + "}";
return dateStr;
default:
//if (objInn["toJSON"] != null && typeof objInn["toJSON"] === "function") {
// return objInn.toJSON();
//}
if (typeof objInn === "object") {
if (objInn.length) {
var arr = [];
for (var i = 0, len = objInn.length; i < len; i++) {
arr.push(toJSON(objInn[i]));
}
return "[" + arr.join(",") + "]";
}
var arr1 = [];
for (var attr in objInn) {
if (typeof objInn[attr] !== "function" && attr !== "CLASS_NAME") {
arr1.push("'" + attr + "':" + toJSON(objInn[attr]));
}
}
if (arr1.length > 0) {
return "{" + arr1.join(",") + "}";
} else {
return "{}";
}
}
return objInn.toString();
}
};
/*
* @description 转换查询结果。
* @param result - {Object} 查询结果。
* @return {Object} 转换后的查询结果。
*/
var transformResult = function (result) {
if (result.responseText && typeof result.responseText === "string") {
result = JSON.parse(result.responseText);
}
return result;
};
/*
* @description 属性拷贝,不拷贝方法类名(CLASS_NAME)等。
* @param destination - {Object} 拷贝目标。
* @param source - {Object} 源对象。
*
*/
var copyAttributes = function (destination, source) {
destination = destination || {};
if (source) {
for (var property in source) {
var value = source[property];
if (value !== undefined && property !== "CLASS_NAME" && typeof value !== "function") {
destination[property] = value;
}
}
}
return destination;
};
/*
* @description 将源对象上的属性拷贝到目标对象上。(不拷贝 CLASS_NAME 和方法)
* @param destination - {Object} 目标对象。
* @param source - {Object} 源对象。
* @param clip - {Array<string>} 源对象中禁止拷贝到目标对象的属性,目的是防止目标对象上不可修改的属性被篡改。
*
*/
var copyAttributesWithClip = function (destination, source, clip) {
destination = destination || {};
if (source) {
for (var property in source) {
//去掉禁止拷贝的属性
var isInClip = false;
if (clip && clip.length) {
for (var i = 0, len = clip.length; i < len; i++) {
if (property === clip[i]) {
isInClip = true;
break;
}
}
}
if (isInClip === true) {
continue;
}
var value = source[property];
if (value !== undefined && property !== "CLASS_NAME" && typeof value !== "function") {
destination[property] = value;
}
}
}
return destination;
};
/*
* @description 克隆一份Object对象
* @param obj - {Object} 需要克隆的对象。
* @return {Object} 返回对象的拷贝对象,注意是新的对象,不是指向。
*/
var cloneObject = function (obj) {
// Handle the 3 simple types, and null or undefined
if (null === obj || "object" !== typeof obj) {
return obj;
}
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = obj.slice(0);
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = cloneObject(obj[attr]);
}
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
};
var newGuid = function () {
/// <summary>生成一个guid</summary>
var guid = "";
for (var i = 1; i <= 32; i++) {
var n = Math.floor(Math.random() * 16.0).toString(16);
guid += n;
if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
guid += "-";
}
return guid;
};
var bind = function (func, object) {
// create a reference to all arguments past the second one
var args = Array.prototype.slice.apply(arguments, [2]);
return function () {
// Push on any additional arguments from the actual function call.
// These will come after those sent to the bind call.
var newArgs = args.concat(
Array.prototype.slice.apply(arguments, [0])
);
return func.apply(object, newArgs);
};
};
/*
* @description 绑定函数到对象,在调用该函数时配置并使用事件对象作为第一个参数.
* @param func - {function} 用于监听事件的函数.
* @param object - {Object} this 对象的引用.
* @returns {function}
*/
var bindAsEventListener = function (func, object) {
return function (event) {
return func.call(object, event || window.event);
};
};
var getTopAnalysisResult = function (data) {
/// <summary>解析拓扑分析的服务器REST返回结果,以更友好的形式返回给客户端</summary>
var enumNum = data.value;
switch (enumNum) {
case 0:
return "Intersect";
case 1:
return "Disjoin";
case 2:
return "Include";
case 3:
return "Adjacent";
default:
return "Unknown";
}
};
var ChineseToUtf8 = function (s) {
return s.replace(/([\u4E00-\u9FA5]|[\uFE30-\uFFA0])/g, function (newStr) {
return EncodeUtf8(newStr);
});
};
var EncodeUtf8 = function (s1) {
var s = escape(s1);
var sa = s.split("%");
var retV = "";
if (sa[0] != "") {
retV = sa[0];
}
for (var i = 1; i < sa.length; i++) {
if (sa[i].substring(0, 1) == "u") {
retV += Hex2Utf8(Str2Hex(sa[i].substring(1, 5)));
} else retV += "%" + sa[i];
}
return retV;
};
var Str2Hex = function (s) {
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digS = "";
for (var i = 0; i < s.length; i++) {
c = s.charAt(i);
n = ss.indexOf(c);
digS += Dec2Dig(n);
}
//return value;
return digS;
};
var Hex2Utf8 = function (s) {
var retS = "";
var tempS = "";
var ss = "";
if (s.length == 16) {
tempS = "1110" + s.substring(0, 4);
tempS += "10" + s.substring(4, 10);
tempS += "10" + s.substring(10, 16);
var sss = "0123456789ABCDEF";
for (var i = 0; i < 3; i++) {
retS += "%";
ss = tempS.substring(i * 8, (i + 1) * 8);
retS += sss.charAt(Dig2Dec(ss.substring(0, 4)));
retS += sss.charAt(Dig2Dec(ss.substring(4, 8)));
}
return retS;
}
return "";
};
var Dig2Dec = function (s) {
var retV = 0;
if (s.length == 4) {
for (var i = 0; i < 4; i++) {
retV += s.charAt(i) * Math.pow(2, 3 - i);
}
return retV;
}
return -1;
};
var Dec2Dig = function (n1) {
var s = "";
var n2 = 0;
for (var i = 0; i < 4; i++) {
n2 = Math.pow(2, 3 - i);
if (n1 >= n2) {
s += '1';
n1 = n1 - n2;
} else
s += '0';
}
return s;
};
var DeepMerge = function (obj1, obj2) {
if (Object.prototype.toString.call(obj1) === '[object Object]' && Object.prototype.toString.call(obj2) === '[object Object]') {
for (var prop2 in obj2) { //obj1无值,都有取obj2
if (!obj1[prop2]) {
obj1[prop2] = obj2[prop2];
} else { //递归赋值
obj1[prop2] = DeepMerge(obj1[prop2], obj2[prop2]);
}
}
} else if (Object.prototype.toString.call(obj1) === '[object Array]' && Object.prototype.toString.call(obj2) === '[object Array]') {
// 两个都是数组,进行合并
obj1 = obj1.concat(obj2);
} else { //其他情况,取obj2的值
obj1 = obj2;
}
return obj1;
};
/*
* Method: mergeItem
* 合并源对象的单个属性到目标对象。
*
* Parameters:
* target - {Object} 目标对象。
* source - {Object} 源对象。
* key - {String} 键。
* overwrite - {Boolean} 是否覆盖。
*
* Returns:
* {Object} 目标对象。
*/
var mergeItem = function (target, source, key, overwrite) {
var BUILTIN_OBJECT = {
'[object Function]': 1,
'[object RegExp]': 1,
'[object Date]': 1,
'[object Error]': 1,
'[object CanvasGradient]': 1
};
if (source.hasOwnProperty(key)) {
if (typeof target[key] == 'object' &&
!BUILTIN_OBJECT[Object.prototype.toString.call(target[key])]
) {
// 如果需要递归覆盖,就递归调用merge
merge(
target[key],
source[key],
overwrite
);
} else if (overwrite || !(key in target)) {
// 否则只处理overwrite为true,或者在目标对象中没有此属性的情况
target[key] = source[key];
}
}
};
/*
* APIMethod: merge
* 合并源对象的属性到目标对象。
*
* Parameters:
* target - {Object} 目标对象。
* source - {Object} 源对象。
* overwrite - {Boolean} 是否覆盖。
*
* Returns:
* {Object} 目标对象。
*/
var merge = function (target, source, overwrite) {
for (var i in source) {
mergeItem(target, source, i, overwrite);
}
return target;
};
/*
* @description 实现多重继承
* @param ...mixins {Class|Object}继承的类
*/
var mixin = function (...mixins) {
class Mix {
constructor(options) {
for (var index = 0; index < mixins.length; index++) {
copyProperties(this, new mixins[index](options));
}
}
}
for (var index = 0; index < mixins.length; index++) {
var mixin = mixins[index];
copyProperties(Mix, mixin);
copyProperties(Mix.prototype, mixin.prototype);
copyProperties(Mix.prototype, new mixin());
}
return Mix;
function copyProperties(target, source) {
var ownKeys = Object.getOwnPropertyNames(source);
if (Object.getOwnPropertySymbols) {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source));
}
for (var index = 0; index < ownKeys.length; index++) {
var key = ownKeys[index];
if (key !== "constructor" &&
key !== "prototype" &&
key !== "name" && key !== "length") {