imcarousel.js
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
//图片浏览
var IMCarousel = {
//图片池
imgpool: {},
//缓存
cache: {
carWidth: 0,
carHeight: 0,
imgTrans: null,
curangle: 0,
slideLimiter: null,
imToolTip: null,
showTipTimer: null
},
//设定
settings: {
hPadRate: 0.05,
vPadRate: 0.1,
hCarPadRate: 0.1,
vCarPadRate: 0.1,
scaleRadio: 0.2,
imgangle: 90,
languageid: 7
},
//事件队列
handlers: {},
//处理图片加载失败的情况
handleLoadError: function(obj, evt){
obj.src='/social/js/imcarousel/image_deleted.png';
// 防止死循环
obj.onerror = null;
},
//处理url不合法的情况,类似http:www.weaver.com.cn
formatImgUri: function(imguri){
try{
var tempurl = imguri.toLowerCase();
tempurl = $.trim(tempurl);
if(tempurl.indexOf('https:') == 0) {
if(tempurl.indexOf('https://') != 0) {
imguri = imguri.replace(/https:/, 'https://');
}
}else if(tempurl.indexOf('http:') == 0) {
if(tempurl.indexOf('http://') != 0) {
imguri = imguri.replace(/http:/, 'http://');
}
}
}catch(err){
// print error
}
return imguri;
},
//加载轮播图片
loadImgFromPool: function(imgPool, obj, activeImgId, targetId){
var cr = $(obj);
var imgInner = cr.find('.carousel-inner');
imgInner.css({display:'none'});
var imgIndicator = cr.find('.carousel-indicators');
var imgControler = cr.find('.carousel-control');
//清空上一次加载的缓存
imgInner.empty();
imgIndicator.empty();
//client.writeLog(imgPool[targetId], "this is test>>>", imgPool);
var imguri;
var cnt = 0;
var imgMaxW = IMCarousel.cache.carWidth * (1 - IMCarousel.settings.hCarPadRate * 2);
var imgMaxH = IMCarousel.cache.carHeight * (1 - IMCarousel.settings.vCarPadRate * 2);
var imgTop = IMCarousel.cache.carHeight * IMCarousel.settings.vCarPadRate;
var imgLeft = IMCarousel.cache.carWidth * IMCarousel.settings.hCarPadRate;
var imgids = {};
if(targetId != undefined){
imgids = imgPool[targetId];
}else{
imgids = imgPool;
}
for(var imgid in imgids){
imguri = imgids[imgid];
//client.writeLog("imguri", imguri);
if("remove" == imgid.toLowerCase() || "contains" == imgid.toLowerCase()
|| "indexof" == imgid.toLowerCase() || "foreach" == imgid.toLowerCase()
|| "unique" == imgid.toLowerCase() || "removevalues" == imgid.toLowerCase())
continue;
imguri = IMCarousel.formatImgUri(imguri);
var img = "<div _imgid='" + imgid + "' class='item'>" +
"<img src=\"" + imguri + "\" " +
"style='max-width:" + imgMaxW + "px;max-height:" + imgMaxH + "px;' onerror=\"IMCarousel.handleLoadError(this, event)\"/>" +
"</div>";
var imgInd = "<li onclick='IMCarousel.slideTo(this)' data-target='#myCarousel' data-slide-to='" + cnt + "'></li>";
if(activeImgId == imgid){
img = "<div _imgid='" + imgid + "' class='item active'>" +
"<img src=\"" + imguri + "\" " +
"style='max-width:" + imgMaxW + "px;max-height:" + imgMaxH + "px'/>" +
"</div>";
imgInd = "<li onclick='IMCarousel.slideTo(this)' data-target='#myCarousel' data-slide-to='" + cnt + "' class='active'></li>";
}
imgInner.append($(img));
imgIndicator.append(imgInd);
cnt++;
}
//调整图片的显示位置
IMCarousel.resetActImg(imgInner);
//重置缓存
IMCarousel.cache.curangle = 0;
imgInner.data('finished', true);
var indexNo = this.getActiveImgIndexNo(obj);
imgControler.attr('data-index', indexNo);
this.updateControlBar(imgControler, indexNo);
},
//绑定滚轮事件
bindMouseWheel: function(obj) {
$(obj).bind('mousewheel', function(event,delta, deltax, deltay){
var delta = CarouselUtils.getDeltaValue(event);
// console.log(delta);
var dir = delta > 0 ? 'Up' : 'Down',vel=Math.abs(delta);
if(dir == 'Up'){
IMCarousel.scaleImg(obj, 'enlarge');
}else if(dir == 'Down'){
IMCarousel.scaleImg(obj, 'shrink');
}
});
},
//调整图片位置
resetActImg: function(imgInner) {
var active = imgInner.find('.active');
var img = active.find('img');
var imgInnerCord = CarouselUtils.getPageOffset(imgInner);
var handlers = IMCarousel.handlers;
// console.log("imgInnerCord:",imgInnerCord);
//缓存
imgInner.data('cord', imgInnerCord);
var oStyle = {
'left' : '50%',
'top' : '50%',
'margin-left': '-23px',
'margin-top': '-23px'
};
var gif = "/social/images/loading_mb_wev8.gif";
if(!img[0].complete){
CarouselUtils.showLoading(imgInner,oStyle,gif,CarouselUtils.settings.TIMEOUT,function(){
//client.writeLog('图片加载超时');
active.css("top", (imgInnerCord.height - img.height()) / 2 + 'px');
active.data('hascloth', '0');
active.removeClass('hide');
});
active.addClass('hide');
img[0].onload = function(){
// console.log("img==>width:" + img.width() + " img==>height:" + img.height());
img.css({
left: (imgInnerCord.width - img.width()) / 2 + 'px',
top : (imgInnerCord.height - img.height()) / 2 + 'px'
});
CarouselUtils.shutLoading();
active.removeClass('hide');
img.data('origalCord',{left:img.position().left, top:img.position().top});
img.parent().css("top","0");
//为图片添加拖动效果
IMCarousel.allotFocusLayer(imgInner, img);
if(handlers && typeof handlers.onImgLoad == 'function'){
handlers.onImgLoad();
}
};
img[0].onerror = function(e){
CarouselUtils.shutLoading();
active.removeClass('hide');
img.attr('src', '/social/js/imcarousel/image_deleted.png');
img.data('origalCord',{left:img.position().left, top:img.position().top});
if(handlers && typeof handlers.onImgLoad == 'function'){
handlers.onImgLoad();
}
// 防止死循环
img[0].onerror = null;
}
}else{
img.css({
left: (imgInnerCord.width - img.width()) / 2 + 'px',
top : (imgInnerCord.height - img.height()) / 2 + 'px'
});
//img.data('origalCord',{left:img.position().left, top:img.position().top});
IMCarousel.allotFocusLayer(imgInner, img);
if(handlers && typeof handlers.onImgLoad == 'function'){
handlers.onImgLoad();
}
}
},
//图片居中显示
makeImgCenter: function(imgInner){
var finished = imgInner.data('finished');
if(finished){
imgInner.data('finished', false);
var imgInnerCord = CarouselUtils.getPageOffset(imgInner);
var imgs = imgInner.find('img');
imgs.each(function(){
var img = $(this);
var imgpa = img.parent();
if(imgpa.data('hascloth') && imgpa.data('hascloth')=='0'){
imgpa.data('hascloth', '1');
}else{
imgpa.css('top', '0px');
}
img.css({
left: (imgInnerCord.width - img.width()) / 2 + 'px',
top : (imgInnerCord.height - img.height()) / 2 + 'px'
});
});
imgInner.data('finished', true);
}
},
//重新计算图片大小
resizeInnerImgs: function(car){
var imgInner = car.find('.carousel-inner');
var imgMaxW = IMCarousel.cache.carWidth * (1 - IMCarousel.settings.hCarPadRate * 2);
var imgMaxH = IMCarousel.cache.carHeight * (1 - IMCarousel.settings.vCarPadRate * 2);
imgInner.find('img').each(function(){
var img = $(this);
var actH = img.height();
var actW = img.width();
$(this).css('max-width', actW > imgMaxW?actW:imgMaxW);
$(this).css('max-height', actH > imgMaxH?actH:imgMaxH);
});
},
//蒙版伸展
stretchWrap: function(wrapMb, imgWrap, myCarousel, $windoc) {
var cw = $windoc[0].clientWidth;
var ch = $windoc[0].clientHeight;
$(imgWrap).css('width', cw+'px');
$(imgWrap).css('height', ch+'px');
$(wrapMb).css('width', cw+'px');
$(wrapMb).css('height', ch+'px');
var iWidth = cw - cw * IMCarousel.settings.hPadRate * 2;
var iHeight = ch - ch * IMCarousel.settings.vPadRate * 2;
IMCarousel.cache.carWidth = iWidth;
IMCarousel.cache.carHeight = iHeight;
//将宽高绑定到carousel缓存 1106 by wyw
myCarousel.attr("_carWidth", iWidth);
myCarousel.attr("_carHeight", iHeight);
/*myCarousel.data("carSize", {
'carWidth': iWidth,
'carHeight': iHeight
});
*/
myCarousel.css('width', iWidth + 'px');
myCarousel.css('height', iHeight + 'px');
myCarousel.css('top', ((ch - iHeight)/2) + 'px');
myCarousel.css('left', ((cw - iWidth)/2) + 'px');
var carCtrl = myCarousel.find('.carousel-control');
carCtrl.css('top', (iHeight - carCtrl.height()) / 2);
var editpane = myCarousel.find('.carousel-fullpane');
editpane.css({'width': iWidth + 'px'});
},
//数据传入显示Image Crousel
showImgScanner4Pool: function(bShow, imgPool, imgId, chatdiv_id, _win, handlers, wrapMbCss){
var $windoc = $(window.document.body);
if(_win){
$windoc = $(_win.document.body);
//解决ESC无效问题,焦点需跳转到对应窗口 1109 by wyw
_win.focus();
}else{
_win = window;
}
var mychatImgPagWrap = $windoc.find(".chatImgPagWrap");
if(mychatImgPagWrap.length == 1){
IMCarousel.initImgScanner(bShow, imgPool, imgId, chatdiv_id, $windoc, _win, handlers, wrapMbCss);
}else{
// console.log("mychatImgPagWrap length:" + mychatImgPagWrap.length);
//延时加载插件
CarouselUtils.cacheLoad("/social/js/drageasy/drageasy.js", {
success: function(){
//client.log("延迟加载drageasy.js成功");
}
});
CarouselUtils.cacheLoad("/social/js/bootstrap/js/bootstrap.js", {
success: function(){
//client.log("延迟加载bootstrap.js成功");
$.post("/social/im/SocialImgCarousel.jsp?"+(typeof from === 'undefined'?'':'from='+from), function(htmldata){
mychatImgPagWrap.remove();
$windoc.find(".mengbanLayer").remove();
$windoc.append($.trim(htmldata));
IMCarousel.initImgScanner(bShow, imgPool, imgId, chatdiv_id, $windoc, _win, handlers, wrapMbCss);
});
}
})
}
},
//设定浏览参数
initImgScanner: function(bShow, imgPool, imgId, chatdiv_id, $windoc, _win, handlers, wrapMbCss){
var imgWrap = $windoc.find('.chatImgPagWrap');
var wrapMb = $windoc.find('.mengbanLayer');
var selfCtx = this;
imgWrap.css({
"position": "fixed",
"zIndex":"99999",
"left": 0,
"top": 0,
"right": 0,
"bottom": 0,
"display": "none"
});
var wrapMbCssRes = Object.assign({
"position": "fixed",
"zIndex":"99999",
"left": 0,
"top": 0,
"right": 0,
"bottom": 0,
"display": "none",
"opacity": 0.36,
"background": "#000",
}, wrapMbCss);
wrapMb.css(wrapMbCssRes);
var myCarousel = $(imgWrap).find('#myCarousel');
//初始化监听函数
if(typeof handlers == 'object'){
IMCarousel.handlers = handlers;
}
if(bShow){
handlers = handlers || IMCarousel.handlers;
// 设置语言
if(handlers && handlers.languageid) {
IMCarousel.settings.languageid = languageid;
}
// 加载多语言配置文件
CarouselUtils.cacheLoad("/social/js/imcarousel/i18n.js", {
success: function(jsonData){
}
});
if(handlers && typeof handlers.beforeOpen == 'function')
handlers.beforeOpen();
$windoc.addClass('clearSpacing');
var imgInner = myCarousel.find('.carousel-inner');
IMCarousel.stretchWrap(wrapMb, imgWrap, myCarousel,$windoc);
$windoc.bind('resize', function(){
IMCarousel.stretchWrap(wrapMb, imgWrap, myCarousel,$windoc);
IMCarousel.resizeInnerImgs(myCarousel);
IMCarousel.makeImgCenter(imgInner);
});
//如果是一张图片就不显示next 和prev按钮
var carCtrl = myCarousel.find('.carousel-control');
var dis = 'block';
if(chatdiv_id != undefined){ //hash
if(imgPool[chatdiv_id]){
var len = CarouselUtils.getObjectLen(imgPool[chatdiv_id]);
if(len <= 1) dis = 'none';
}
}else{
var len = imgPool.length;
if(len <= 1) dis = 'none';
}
carCtrl.css('display', dis);
//第一次点击没有效果 1109 by wyw
//$(wrapMb).show();
//$(imgWrap).fadeIn();
$(wrapMb).css("display", "block");
$(imgWrap).css("display", "block");
if(handlers && typeof handlers.onContainerLoad == 'function')
handlers.onContainerLoad();
try{
myCarousel.carousel('pause');//停止轮播
// 加载tooltip
// $windoc.find("[data-toggle='tooltip']").tooltip({'delay':10, 'container': imgWrap});
this.getImToolTip(window.top.document.body)
//隐藏tooltip提示
// selfCtx.hideTip(myCarousel.find('.carousel-control'));
}catch(err){
//client.error("bootstrap.js可能没有加载");
CarouselUtils.cacheLoad("/social/js/bootstrap/js/bootstrap.js", {
success: function(){
//client.log("延迟加载bootstrap.js成功");
// 加载tooltip
// $windoc.find("[data-toggle='tooltip']").tooltip({'delay':0});
// selfCtx.hideTip(myCarousel.find('.carousel-control'));
}
})
}
var cr = $(myCarousel);
var imgInner = cr.find('.carousel-inner');
//从图片池加载图片
IMCarousel.loadImgFromPool(imgPool, myCarousel, imgId, chatdiv_id);
//如果是页面已加载完的图片,要延迟调用resize()保证居中显示
_win.setTimeout(function(){
if(!CarouselUtils.cache.loadTimerId){
imgInner.css({display:'block'});
$windoc.resize();
}
}, 200);
if(CarouselUtils.cache.loadTimerId){
imgInner.css({display:'block'});
}
$windoc.unbind('.imgscanner');
//绑定键盘左右键
$windoc.bind('keyup.imgscanner', function(event){
var e = event || _win.event;
var keynum;
if(_win.event != undefined)//FOR IE
keynum=e.keyCode;
else if(e.keyCode)
keynum=e.keyCode;
else if(e.which)
keynum=e.which;
if(keynum == 39){ //右
//myCarousel.carousel('next');
var nextBtn = myCarousel.find('.carousel-control[data-slide="next"]');
if(nextBtn.length > 0){
nextBtn.click();
selfCtx.showTip(nextBtn, 3000);
}
}else if(keynum == 37) { //左
//myCarousel.carousel('prev');
var prevBtn = myCarousel.find('.carousel-control[data-slide="prev"]');
if(prevBtn.length > 0){
prevBtn.click();
selfCtx.showTip(prevBtn, 3000);
}
}else if(38 == keynum){ //上
var enlargBtn = myCarousel.find('.enlarge');
if(enlargBtn.length > 0){
enlargBtn.click();
}
}else if(40 == keynum){ //下
var shrinkBtn = myCarousel.find('.shrink');
if(shrinkBtn.length > 0){
shrinkBtn.click();
}
}else if(keynum == 27){ //esc
$windoc.removeClass('clearSpacing');
$(wrapMb).hide();
$(imgWrap).hide();
$windoc.unbind('.imgscanner');
if(typeof from != 'undefined' && from == 'pc'){
ImageReviewForPc.close();
}
}
});
}else {
$windoc.removeClass('clearSpacing');
$(wrapMb).hide();
$(imgWrap).hide();
$windoc.unbind('.imgscanner');
handlers = handlers || IMCarousel.handlers;
if(handlers && typeof handlers.afterClose == 'function')
handlers.afterClose();
}
},
//显示Image Crousel
showImgScanner: function(e, bShow, imgUrl, handlers) {
e = e || window.event;
var target = $(e.srcElement || e.target);
var chatdiv_id = $(target.parents('.chatdiv')[0]).attr('id');
var chatimgpool = [];
/*下面代码是用来判断是否处于消息记录窗口里 1105 by wyw*/
if(chatdiv_id == undefined){
var dgchatdiv = target.parents('.zDialog_div_content');
//alert(dgchatdiv.length)
if(dgchatdiv.length > 0){
var imgs = dgchatdiv.find(".chatcontentimg img");
for(var i = 0; i < imgs.length; ++i){
chatimgpool.push($(imgs[i]).attr("_imageurl"));
}
}
}else{
chatimgpool = IMCarousel.getchatimgpool(chatdiv_id);
}
//alert("chatdiv_div:"+chatdiv_id+"imglen:"+chatimgpool.length);
var imgIndex = chatimgpool.indexOf(imgUrl);
var _win = window;
//alert(top.location);
//alert(location);
try{
var _location = window.location;
var _toplocation = window.top.location;
//IE 7,8 location undefined
if(!window.location){
_location = document.location;
_toplocation = window.top.document.location;
}
if(_toplocation != _location)
_win = window.top;
} catch(err) {
console.info(err);
}
if(typeof from != 'undefined' && from == 'pc'){
if(bShow){
ImageReviewForPc.show(imgIndex, chatimgpool);
}else {
ImageReviewForPc.close()
}
} else {
IMCarousel.showImgScanner4Pool(bShow, chatimgpool, imgIndex, undefined, _win, handlers);
}
},
//获取聊天窗口的图片集合
getchatimgpool: function(chatdivid){
var pool = [];
var chatimgs = $("#"+chatdivid+" .chatcontentimg img");
for(var i = 0; i < chatimgs.length; ++i){
pool.push($(chatimgs[i]).attr("_imageurl"));
}
return pool;
},
bindImgSize: function(img){
img = img[0] || img;
var w = img.offsetHeight;
var h = img.offsetWidth;
if(w!=0&&h!=0&&!$(img).attr('_width')&&!$(img).attr('_height')){
$(img).attr({
"_width": w,
"_height": h
});
}
},
//为图片添加遮罩层
allotFocusLayer: function(imgInner, img) {
var oStyle = CarouselUtils.getPageOffset(img);
//当直接取页面上图片的src时,图片已经加载完毕,这里img坐标错误
if(img[0].style.left && oStyle.left == 0){
oStyle.left = parseInt(img[0].style.left);
oStyle.top = parseInt(img[0].style.top);
img.data('origalCord',{left:oStyle.left, top:oStyle.top});
}
var img = $(img);
var layer = img.next('.transLayer');
if(layer && layer.length > 0) return;
else {
layer = CarouselUtils.getLayer(oStyle, 'transLayer');
img.after(layer);
img.bind('mouseenter.overflayer', function(e){
IMCarousel.strechToImg(img, layer);
layer.show();
});
layer.bind('mouseleave.outflayer', function(e){
layer.hide();
});
//alert(imgInner.length);
var imgInnerCord = CarouselUtils.getPageOffset(imgInner);
//对于动态加载, 插件可能没有进行预加载
try{
layer.dragDrop({'follower' : img, 'origalCord' : oStyle, "imgInnerCord": imgInnerCord});
}catch(err){
//client.error("drageasy.js可能没有成功加载");
//延时加载插件
CarouselUtils.cacheLoad("/social/js/drageasy/drageasy.js", {
success: function(){
//client.log("延迟加载drageasy.js成功");
layer.dragDrop({'follower' : img, 'origalCord' : oStyle, "imgInnerCord": imgInnerCord});
}
});
}
//绑定滚轮
IMCarousel.bindMouseWheel(layer);
}
},
//遮罩层图片随动
strechToImg: function(img, shadowLayer) {
var oStyle = CarouselUtils.getPageOffset(img);
shadowLayer.css({
left: oStyle.left,
top: oStyle.top,
width: oStyle.width,
height: oStyle.height
});
shadowLayer[0].style.webkitTransform=img[0].style.webkitTransform;
shadowLayer[0].style.MozTransform=img[0].style.MozTransform;
shadowLayer[0].style.msTransform=img[0].style.msTransform;
shadowLayer[0].style.OTransform=img[0].style.OTransform;
shadowLayer[0].style.transform=img[0].style.transform;
},
//显示当前图片序号
showImgIndexNo: function(obj, direction, indexNo) {
// console.log("mouseover...");
var imgCount = IMCarousel.getImgCount(obj);
if(imgCount <= 1){
return;
}
var activeImgIndexNo = typeof indexNo==='undefined'?this.getActiveImgIndexNo(obj):indexNo;
var titleTip = this.getTitleTip(activeImgIndexNo, imgCount, direction);
$(obj).attr('data-original-title', titleTip);
this.updateTip(obj, titleTip)
},
// 获取titleTip
getTitleTip: function(indexNo, imgCount, direction) {
var titleTip = "";
var isFirst = indexNo == 0;
var isLast = indexNo + 1 == imgCount;
// 最后一张
if(isLast && direction == 'next') {
titleTip = CarouselUtils.geti18nString('tip0', IMCarousel.settings.languageid);
}else if (isFirst && direction == 'prev') {
titleTip = CarouselUtils.geti18nString('tip1', IMCarousel.settings.languageid);
}else {
titleTip = (direction == 'next'? indexNo + 1: indexNo - 1) + "/" + imgCount;
}
return titleTip;
},
// 获取imToolTip对象
getImToolTip: function(context, initOptions) {
context = context || window.top.document.body
var container = $('.chatImgPagWrap', context)
return this.cache.imToolTip = this.cache.imToolTip || new IMToolTip(context, initOptions || {'container': container});
},
// 显示tip
showTip: function(obj, timeout){
this.getImToolTip().show(obj)
var content = $(obj).attr('data-original-title');
var selfCtx = this;
selfCtx.getImToolTip().updatePosition(obj)
if(typeof timeout === 'number') {
clearTimeout(this.cache.showTipTimer);
this.cache.showTipTimer = setTimeout(function(){
selfCtx.getImToolTip().hide()
}, timeout);
}
return this;
},
// 隐藏tip
hideTip: function(obj){
this.getImToolTip().hide()
},
// 更新tip
updateTip: function(obj, content) {
this.getImToolTip().setContent(content)
},
// 更新滑动按钮样式
updateControlBar: function(obj, indexNo, imgCount) {
if(typeof indexNo == 'undefined') {
indexNo = this.getActiveImgIndexNo(obj);
}
if(typeof imgCount == 'undefined') {
imgCount = this.getImgCount(obj);
}
var isFirst = indexNo == 0;
var isLast = indexNo + 1 == imgCount;
var CarourselObj = $(obj).parents('#myCarousel');
if(!isFirst) {
CarourselObj.find('a.left').removeClass('disabled').attr('data-original-title', (indexNo + 1) + '/' + imgCount);
}else {
CarourselObj.find('a.left').addClass('disabled').attr('data-original-title', CarouselUtils.geti18nString('tip1', IMCarousel.settings.languageid));
}
if(!isLast) {
CarourselObj.find('a.right').removeClass('disabled').attr('data-original-title', (indexNo + 1) + '/' + imgCount);
}else {
CarourselObj.find('a.right').addClass('disabled').attr('data-original-title', CarouselUtils.geti18nString('tip0', IMCarousel.settings.languageid));
}
},
//手动滑动图片
slideImg: function(obj, direction) {
// 两次滑动间隔控制
if(this.cache.slideLimiter) {
return;
}
var self = this;
self.cache.slideLimiter = setTimeout(function(){
self.cache.slideLimiter = null;
}, 600);
var imgCount = IMCarousel.getImgCount(obj);
var activeImgIndexNo = this.getActiveImgIndexNo(obj);
if(imgCount <= 1){
return;
}
var isFirst = activeImgIndexNo == 0;
var isLast = activeImgIndexNo + 1 == imgCount;
if(direction == 'prev' && isFirst || direction == 'next' && isLast) {
$(obj).addClass('disabled');
return;
}
var newActiveImgIndexNo = activeImgIndexNo;
if(direction == 'prev' && activeImgIndexNo > 0) {
newActiveImgIndexNo--;
$(obj).parents('#myCarousel').find('a.carousel-control').attr('data-index', newActiveImgIndexNo);
} else if(direction == 'next' && activeImgIndexNo < imgCount) {
newActiveImgIndexNo++;
$(obj).parents('#myCarousel').find('a.carousel-control').attr('data-index', newActiveImgIndexNo);
}
IMCarousel.restoreImg(obj);
$(obj).parents('#myCarousel').carousel(direction);
CarouselUtils.stopBubbleEvent(event);
var layerDiv = $(obj).next('.'+direction+'Layer');
this.updateControlBar(obj, newActiveImgIndexNo, imgCount);
//this.showImgIndexNo(obj, direction, activeImgIndexNo);
this.updateTip(obj, $(obj).attr('data-original-title'))
this.hideTip($(obj).siblings('.carousel-control'))
this.showTip(obj);
},
//手动滑动到指定位置
slideTo: function(obj) {
var pos = $(obj).attr('data-slide-to');
IMCarousel.restoreImg(obj);
$(obj).parents('#myCarousel').carousel(parseInt(pos));
CarouselUtils.stopBubbleEvent(event);
},
//获取图片个数
getImgCount: function(obj) {
var o = $(obj).parents('.carousel');
//client.writeLog(o.length);
var items = o.find('.carousel-inner').children('.item');
return items.length;
},
//获取当前图片的序号
getActiveImgIndexNo: function(obj) {
var car = $(obj).parents('.chatImgPagWrap')[0];
var indexNo = parseInt($(obj).attr('data-index'));
// console.log("getActiveImgIndexNo:" + indexNo);
return isNaN(indexNo)?$(car).find('.carousel-inner .active').index() : indexNo ;
},
//找到当前图片
getActiveImg: function(obj) {
var car = $(obj).parents('.chatImgPagWrap')[0];
return $(car).find('.carousel-inner .active img');
},
//旋转图片
rotateImg: function(obj){
var actImg = IMCarousel.getActiveImg(obj);
actImg.addClass('rotatetrans');
var img = actImg[0];
/*if(CarouselUtils.cache.curangle >= 360){
CarouselUtils.cache.curangle -= 360;
}*/
var angle = IMCarousel.settings.imgangle;
var curangle = IMCarousel.cache.curangle;
if(CarouselUtils.supportCss3("Transform")){
img.style.webkitTransform="rotate(" + (curangle + angle) + "deg)"
img.style.MozTransform="rotate(" + (curangle + angle) + "deg)"
img.style.msTransform="rotate(" + (curangle + angle) + "deg)"
img.style.OTransform="rotate(" + (curangle + angle) + "deg)"
img.style.transform="rotate(" + (curangle + angle) + "deg)";
}else if(document.body.filters){ //IE??
var an = (curangle + angle) % 360,rotation = 1;
if(an > 0 && an <= 90) rotation = 1;
else if(an > 90 && an <= 180) rotation = 2;
else if(an > 180 && an < 360) rotation = 3;
else rotation = 0;
img.style.filter = "progid:DXImageTransform.Microsoft.BasicImage(rotation=" + rotation + ")";
}
if(((curangle + angle) / 90) % 2 == 1 ){
actImg.attr("_vShow", "1");
}else{
actImg.attr("_vShow", "0");
}
IMCarousel.cache.curangle += angle;
IMCarousel.strechToImg($(img), $(img).next('.transLayer'));
},
//控制图片大小
scaleImg: function(obj, opt) {
var actImg = IMCarousel.getActiveImg(obj);
var w = actImg.width()
var h = actImg.height();
if('shrink' == opt && w < 100){ //如果超过范围就忽略
return;
}
if('enlarge' == opt && w > 10000){ //如果超过范围就忽略
return;
}
var radio = 1;
/*消息记录窗口模式下有问题
var orgTop = parseInt(actImg.css('top'));
var orgLeft = parseInt(actImg.css('left'));
*/
var orgTop = actImg.position().top;
var orgLeft = actImg.position().left;
var zoom = {x: (orgLeft + w/2), y: (orgTop + h/2)}; //图片中心点的坐标
if(actImg.attr("_vShow") == '1'){
zoom = {x: (orgLeft + h/2), y: (orgTop + w/2)}; //图片中心点的坐标
}
if('enlarge' == opt){
radio = 1 + IMCarousel.settings.scaleRadio;
}else if('shrink'==opt){
radio = 1 - IMCarousel.settings.scaleRadio;
}
var newW = w * radio;
var newH = h * radio;
actImg.css({
'max-width': newW + 'px',
'max-height': newH + 'px'
});
actImg.css({
left: (zoom.x - newW/2)+'px',
top : (zoom.y - newH/2)+'px'
});
actImg.attr('width', newW);
actImg.attr('height', newH);
/*
actImg.css({
"width": newW + "px",
"height": newH + "px"
});
*/
IMCarousel.strechToImg(actImg, actImg.next('.transLayer'));
},
//复制图片
copyImg: function(obj){
var actImg = IMCarousel.getActiveImg(obj);
var url = actImg.attr('src');
copyPcImg(url);
},
//下载图片
downloadImg: function(obj) {
var actImg = IMCarousel.getActiveImg(obj);
var url = actImg.attr('src');
var fileid = CarouselUtils.getFileidInUrl(url);
if(fileid != '') {
CarouselUtils.downloads(url);
} else {
// $("#downloadFrame").attr("src","/social/im/SocialIMOperation.jsp?operation=downrongpic&url=" + url);
}
},
restoreImg: function(obj) {
var car = $(obj).parents('.chatImgPag')[0];
var img = $(car).find('.carousel-inner .active img');
var cacheWidth = IMCarousel.cache.carWidth;
var cacheHeight = IMCarousel.cache.carHeight;
//有时候取到的值为0,原因未知 1106 by wyw
if(cacheWidth == 0 || cacheHeight == 0){
cacheWidth = $(car).attr("_carWidth");
cacheHeight = $(car).attr("_carHeight");
}
var imgMaxW = cacheWidth * (1 - IMCarousel.settings.hCarPadRate * 2);
var imgMaxH = cacheHeight * (1 - IMCarousel.settings.vCarPadRate * 2);
var curangle = IMCarousel.cache.curangle;
img.removeAttr('width');
img.removeAttr('height');
img.css({
'max-width': imgMaxW+'px',
'max-height': imgMaxH+'px'
});
img.removeClass('rotatetrans');
//消除旋转
img[0].style.webkitTransform='';
img[0].style.MozTransform='';
img[0].style.msTransform='';
img[0].style.OTransform='';
img[0].style.transform='';
//重置缓存
IMCarousel.cache.curangle = 0;
//清除拖拽
var oStyle = img.data('origalCord');
// console.log(oStyle);
if(oStyle){
img.css({
left: oStyle.left,
top: oStyle.top
});
}
},
//屏蔽隐藏选择器
SelectorMask : [
'.carousel-indicators', '.carousel-indicators li',
'.carousel-inner .item img', '.carousel-control',
'.miniClose', '.carousel-fullpane .control-pane .ctrlbtn',
'.carousel-fullpane .control-pane', '.transLayer'
],
//处理图片浏览时的自动隐藏
doScannerEscape: function(e, box) {
//pc版屏蔽该动作
e = e || window.event;
if(typeof from != 'undefined' && from == 'pc'){
CarouselUtils.stopBubbleEvent(e);
return;
}
var obj = e.target || e.srcElement;
var selector;
for(var i = 0; i < IMCarousel.SelectorMask.length; ++i){
selector = IMCarousel.SelectorMask[i];
var es = $(box).find(selector);
for(var j = 0; j < es.length; ++j){
if(es[j] == obj){
return;
}
}
}
IMCarousel.showImgScanner(e, 0, '');
CarouselUtils.stopBubbleEvent(e);
}
};
/**
* 工具类
* @type
*/
var CarouselUtils = {
cache: {},
settings: {
TIMEOUT: 2147483647
},
i18nMap: {
'tip0': {
'7': '已经是最后一张',
'8': 'Reach the last',
'9': '已經是最後一張'
},
'tip1': {
'7': '已经是第一张',
'8': 'Reach the first',
'9': '已經是第一張'
}
},
// 阻止冒泡
stopBubbleEvent: function(e) {
e = e || event;
if (e && e.stopPropagation) {
e.stopPropagation();
}else if (window.event) {
// 针对 IE
window.event.cancelBubble = true;
}
},
//加载js到缓存
cacheLoad: function(url, handlers, dataType){
jQuery.ajax({
url: url,
dataType: dataType?dataType:"script",
cache: true,
success: handlers.success
});
},
//获取delta
getDeltaValue : function(event) {
var delta = 0;
if (!event) event = window.event;
if (event.wheelDelta) {
delta = event.wheelDelta/120;
if (window.opera) delta = -delta;
// } else if (!isNaN(event.detail)) {
// 处理jquery版本升级导致的鼠标滚轮问题,暂时注释这一段 zhujing-20200113
// delta = -(event.detail || 0)/3;
}else{
event = event.originalEvent;
if(!isNaN(event.wheelDelta)){
delta = event.wheelDelta/120;
}
//DOMMouseScroll for Firefox
else if(!isNaN(event.detail)){
delta = -(event.detail || 0)/3;
}
}
return delta;
},
//获取元素在网页中的相对位置
getPageOffset: function(oj){
if(oj instanceof jQuery){
oj = oj[0];
}
var pos = oj.getBoundingClientRect();
// console.log("pos:",pos);
return {
'width' : oj.offsetWidth,
'height' : oj.offsetHeight,
'left' : oj.offsetLeft,
'top' : oj.offsetTop
};
},
//显示加载效果
showLoading: function(oBox, oStyle, gif, iDuration, fnCallback){
if(!gif) gif = "/social/images/loading_small_wev8.gif";
var imgwrapdiv = $("<div id='cloading'></div>");
var img = $("<img src='"+gif+"'/>");
imgwrapdiv.css({
"position": "fixed",
"zIndex": "10000",
"display": "block",
"textAlign": "center"
});
imgwrapdiv.css(oStyle);
imgwrapdiv.append(img);
imgwrapdiv.appendTo($(oBox));
var self = this;
self.cache.loadTimerId = window.setTimeout(function(){
self.shutLoading();
self._docallback(fnCallback);
}, iDuration);
},
//处理回调
_docallback: function(fn){
if(fn && fn instanceof Function){
fn();
}
},
//关闭加载效果
shutLoading: function(){
var loadingdiv =$("#cloading");
if(loadingdiv.length == 0){
loadingdiv = $(window.top.document.body).find("#cloading");
}
loadingdiv.css('display','none').remove();
window.clearTimeout(this.cache.loadTimerId);
this.cache.loadTimerId = null;
},
//获取数据对象的长度
getObjectLen: function(obj){
var cnt = 0;
if(typeof obj == 'object'){
for(var i in obj){
if(i != 'remove')
cnt++;
}
}
return cnt;
},
//根据模板获取层
getLayer: function(oStyle, classname){
var left = oStyle.left;
var top = oStyle.top;
var width = oStyle.width;
var height = oStyle.height;
var div = $("<div class=\"" + classname + "\"></div>");
div.css({
'left' : left,
'top' : top,
'width' : width,
'height' : height
});
return div;
},
//判断是否支持css3
supportCss3: function(style){
var prefix = ['webkit', 'Moz', 'ms', 'o'],
i,
humpString = [],
htmlStyle = document.documentElement.style,
_toHumb = function (string) {
return string.replace(/-(\w)/g, function ($0, $1) {
return $1.toUpperCase();
});
};
for (i in prefix)
humpString.push(_toHumb(prefix[i] + '-' + style));
humpString.push(_toHumb(style));
for (i in humpString)
if (humpString[i] in htmlStyle) return true;
return false;
},
//获取fileid
getFileidInUrl: function(url) {
var tag = "fileid=";
var start = url.indexOf(tag);
if(start == -1)
return '';
var len = url.length,pos = start + tag.length,charary=[];
while(pos < len){
if(url.charAt(pos) == '&'){
break;
}
charary.push(url.charAt(pos));
pos++;
}
return charary.join('');
},
// 默认的下载的方法
downloads: function(url) {
if(!url) return;
var downloadFrm = $("#downloadFrame");
if(downloadFrm.length == 0){
downloadFrm = $("#downloadFrame", window.top.document);
}
if(downloadFrm.length == 0){
downloadFrm = $("<iframe id='downloadFrame' style='display: none;'></iframe>");
$(document.body).append(downloadFrm);
}
if(downloadFrm.length > 0){
// downloadFrm.attr("src","/weaver/weaver.file.FileDownload?fileid="+fileid+"&download=1");
if(url.indexOf('&download=') == -1) {
url = url + '&download=1';
}
downloadFrm.attr("src", url);
}
},
// 获取多语言配置
geti18nString: function(key, languageid) {
if(this.i18nMap[key]) {
return this.i18nMap[key][languageid];
}else {
return key;
}
}
};
// 判断顶层页面对象是否存在
if(!window.top.IMCarousel) {
window.top.IMCarousel = IMCarousel;
}
// 判断顶层页面对象是否存在
if(!window.top.CarouselUtils) {
window.top.CarouselUtils = CarouselUtils;
}
/*
* 一个可以代替title显示简易版
*
*/
var IMToolTip = function(context, options) {
var _options = {
'placement': 'top',
'template': '<div id="im-tooltip-div" class="im-tooltip"><div class="im-tooltip-arrow"></div><div class="im-tooltip-inner"></div></div>'
}
this.options = $.extend({}, _options, options)
this.context = context;
this.init();
}
IMToolTip.prototype = {
init: function() {
var $eles = $("[data-toggle='tooltip']", this.context);
var self = this;
var template = this.options.template;
var $tip = $(template)
var $tipdiv = $("#im-tooltip-div", this.context);
if($tipdiv.length == 0) {
this.$tip = this.options.container?$tip.appendTo(this.options.container):$tip.appendTo($(this.context))
}
$eles.each(function(){
this.addEventListener('mouseenter', function(){self.show(this)})
this.addEventListener('mouseleave', function(){self.hide()})
self.fixTitle(this)
});
},
fixTitle: function (ele) {
var $e = $(ele);
if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
$e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
}
},
getTip: function() {
return this.$tip = this.$tip || $("#im-tooltip-div", this.context);
},
updatePosition: function(ele) {
var $e = $(ele)
var $tip = this.getTip();
var pos = $.extend({}, (typeof ele.getBoundingClientRect == 'function') ? ele.getBoundingClientRect() : {
width: ele.offsetWidth
, height: ele.offsetHeight
}, $e.offset())
var actualWidth = $tip[0].offsetWidth
// 不需要多行显示
var actualHeight = $tip[0].offsetHeight > 32 ?32: $tip[0].offsetHeight;
var tp = {};
switch (this.options.placement) {
case 'bottom':
tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
break
case 'top':
tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
break
case 'left':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
break
case 'right':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
break
}
// 边缘检测
if(tp.top < 0) {
tp.top = tp.top + actualHeight + pos.height;
}
if(tp.left < 0) {
tp.left = tp.left + actualWidth + pos.width;
}
var maxWidth = this.context.clientWidth;
var maxHeight = this.context.clientHeight;
if(tp.left + actualWidth > maxWidth) {
tp.left = tp.left - actualWidth - pos.width;
}
if(tp.top + actualHeight > maxHeight) {
tp.top = tp.top - actualHeight - pos.height;
}
this.getTip().css(tp);
},
show: function(ele) {
var $ele = $(ele);
var self = this;
var title = $ele.attr('data-original-title');
this.getTip().find('.im-tooltip-inner').text(title)
this.getTip().show(10, function(){
self.updatePosition(ele)
})
},
hide: function() {
this.getTip().hide()
},
setContent: function(content) {
this.getTip().find('.im-tooltip-inner').text(content)
}
}