-
Notifications
You must be signed in to change notification settings - Fork 50
/
ZmMailMsgView.js
2671 lines (2331 loc) · 85.6 KB
/
ZmMailMsgView.js
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
/*
* ***** BEGIN LICENSE BLOCK *****
* Zimbra Collaboration Suite Web Client
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 VMware, Inc.
*
* The contents of this file are subject to the Zimbra Public License
* Version 1.3 ("License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.zimbra.com/license.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* ***** END LICENSE BLOCK *****
*/
ZmMailMsgView = function(params) {
if (arguments.length == 0) { return; }
params.className = params.className || "ZmMailMsgView";
ZmMailItemView.call(this, params);
this._mode = params.mode;
this._controller = params.controller;
this._viewId = this._getViewId(params.sessionId);
this._displayImagesId = ZmId.getViewId(this._viewId, ZmId.MV_DISPLAY_IMAGES, this._mode);
this._msgTruncatedId = ZmId.getViewId(this._viewId, ZmId.MV_MSG_TRUNC, this._mode);
this._infoBarId = ZmId.getViewId(this._viewId, ZmId.MV_INFO_BAR, this._mode);
this._tagRowId = ZmId.getViewId(this._viewId, ZmId.MV_TAG_ROW, this._mode);
this._tagCellId = ZmId.getViewId(this._viewId, ZmId.MV_TAG_CELL, this._mode);
this._attLinksId = ZmId.getViewId(this._viewId, ZmId.MV_ATT_LINKS, this._mode);
this._scrollWithIframe = params.scrollWithIframe;
this._limitAttachments = this._scrollWithIframe ? 3 : 0; //making it local
this._attcMaxSize = this._limitAttachments * 16 + 8;
this.setScrollStyle(this._scrollWithIframe ? DwtControl.CLIP : DwtControl.SCROLL);
ZmTagsHelper.setupListeners(this); //setup tags related listeners.
this._setMouseEventHdlrs(); // needed by object manager
this._objectManager = true;
this._changeListener = this._msgChangeListener.bind(this);
this.addListener(DwtEvent.ONSELECTSTART, this._selectStartListener.bind(this));
this.addListener(DwtEvent.CONTROL, this._controlEventListener.bind(this));
// bug fix #25724 - disable right click selection for offline
if (!appCtxt.isOffline) {
this._setAllowSelection();
}
this.noTab = true;
this._attachmentLinkIdToFileNameMap = null;
};
ZmMailMsgView.prototype = new ZmMailItemView;
ZmMailMsgView.prototype.constructor = ZmMailMsgView;
ZmMailMsgView.prototype.isZmMailMsgView = true;
ZmMailMsgView.prototype.toString = function() { return "ZmMailMsgView"; };
// displays any additional headers in messageView
//pass ZmMailMsgView.displayAdditionalHdrsInMsgView[<actualHeaderName>] = <DisplayName>
//pass ZmMailMsgView.displayAdditionalHdrsInMsgView["X-Mailer"] = "Sent Using:"
ZmMailMsgView.displayAdditionalHdrsInMsgView = {};
// Consts
ZmMailMsgView.SCROLL_WITH_IFRAME = true;
ZmMailMsgView.LIMIT_ATTACHMENTS = ZmMailMsgView.SCROLL_WITH_IFRAME ? 3 : 0;
ZmMailMsgView.ATTC_COLUMNS = 2;
ZmMailMsgView.ATTC_MAX_SIZE = ZmMailMsgView.LIMIT_ATTACHMENTS * 16 + 8;
ZmMailMsgView.QUOTE_DEPTH_MOD = 3;
ZmMailMsgView.MAX_SIG_LINES = 8;
ZmMailMsgView.SIG_LINE = /^(- ?-+)|(__+)\r?$/;
ZmMailMsgView._inited = false;
ZmMailMsgView.SHARE_EVENT = "share";
ZmMailMsgView.SUBSCRIBE_EVENT = "subscribe";
ZmMailMsgView.IMG_FIX_RE = new RegExp("(<img\\s+.*dfsrc\\s*=\\s*)[\"']http[^'\"]+part=([\\d\\.]+)[\"']([^>]*>)", "gi");
ZmMailMsgView.FILENAME_INV_CHARS_RE = /[\./?*:;{}'\\]/g; // Chars we do not allow in a filename
ZmMailMsgView.SETHEIGHT_MAX_TRIES = 3;
ZmMailMsgView._URL_RE = /^((https?|ftps?):\x2f\x2f.+)$/;
ZmMailMsgView._MAILTO_RE = /^mailto:[\x27\x22]?([^@?&\x22\x27]+@[^@?&]+\.[^@?&\x22\x27]+)[\x27\x22]?/;
ZmMailMsgView.MAX_ADDRESSES_IN_FIELD = 10;
// tags that are trusted in HTML content that is not displayed in an iframe
ZmMailMsgView.TRUSTED_TAGS = ["#text", "a", "abbr", "acronym", "address", "article", "b", "basefont", "bdo", "big",
"blockquote", "body", "br", "caption", "center", "cite", "code", "col", "colgroup", "dd", "del", "dfn", "dir",
"div", "dl", "dt", "em", "font", "footer", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hr", "html", "i", "img",
"ins", "kbd", "li", "mark", "menu", "meter", "nav", "ol", "p", "pre", "q", "s", "samp", "section", "small",
"span", "strike", "strong", "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "time", "tr", "tt",
"u", "ul", "var", "wbr"];
// attributes that we don't want to appear in HTML displayed in a div
ZmMailMsgView.UNTRUSTED_ATTRS = ["id", "class", "name", "profile"];
// styles that we don't want to appear in HTML displayed in a div
// for example, some clown may try to mess with us using absolute positioning
ZmMailMsgView.BAD_STYLES = ["position:absolute", "font-"];
// Public methods
ZmMailMsgView.prototype.getController =
function() {
return this._controller;
};
ZmMailMsgView.prototype.reset =
function() {
// Bug 23692: cancel any pending actions
if (this._resizeAction) {
AjxTimedAction.cancelAction(this._resizeAction);
this._resizeAction = null;
}
if (this._objectsAction) {
AjxTimedAction.cancelAction(this._objectsAction);
this._objectsAction = null;
}
this._msg = this._item = null;
this._htmlBody = null;
this._containerEl = null;
// TODO: reuse all these controls that are being disposed here.....
if (this._ifw) {
this._ifw.dispose();
this._ifw = null;
}
if (this._inviteMsgView) {
this._inviteMsgView.reset();
}
var el = this.getHtmlElement();
if (el) {
el.innerHTML = "";
}
if (this._objectManager && this._objectManager.reset) {
this._objectManager.reset();
}
this.setScrollWithIframe(this._scrollWithIframe);
};
ZmMailMsgView.prototype.dispose =
function() {
ZmTagsHelper.disposeListeners(this);
ZmMailItemView.prototype.dispose.apply(this, arguments);
};
ZmMailMsgView.prototype.preventSelection =
function() {
return false;
};
ZmMailMsgView.prototype.set =
function(msg, force, dayViewCallback) {
if (!force && this._msg && msg && !msg.force && (this._msg == msg)) { return; }
var oldMsg = this._msg;
this.reset();
var contentDiv = this._getContainer();
this._msg = this._item = msg;
if (!msg) {
if (this._inviteMsgView) {
this._inviteMsgView.resize(true); //make sure the msg preview pane takes the entire area, in case we were viewing an invite. (since then it was resized to allow for day view) - bug 53098
}
contentDiv.innerHTML = (this._controller.getList().size()) ? AjxTemplate.expand("mail.Message#viewMessage") : "";
this.noTab = true;
return;
}
msg.force = false;
var respCallback = this._handleResponseSet.bind(this, msg, oldMsg, dayViewCallback);
this._renderMessage(msg, contentDiv, respCallback);
this.noTab = AjxEnv.isIE;
};
ZmMailMsgView.prototype._getContainer =
function() {
return this.getHtmlElement();
};
ZmMailMsgView.prototype.__hasMountpoint =
function(share) {
var tree = appCtxt.getFolderTree();
return tree
? this.__hasMountpoint2(tree.root, share.grantor.id, share.link.id)
: false;
};
ZmMailMsgView.prototype.__hasMountpoint2 =
function(organizer, zid, rid) {
if (organizer.zid == zid && organizer.rid == rid)
return true;
if (organizer.children) {
var children = organizer.children.getArray();
for (var i = 0; i < children.length; i++) {
var found = this.__hasMountpoint2(children[i], zid, rid);
if (found) {
return true;
}
}
}
return false;
};
ZmMailMsgView.prototype.highlightObjects =
function(origText) {
if (origText != null) {
// we get here only for text messages; it's a lot
// faster to call findObjects on the whole text rather
// than parsing the DOM.
DBG.timePt("START - highlight objects on-demand, text msg.");
this._lazyCreateObjectManager();
var html = this._objectManager.findObjects(origText, true, null, true);
html = html.replace(/^ /mg, " ")
.replace(/\t/g, "<pre style='display:inline;'>\t</pre>")
.replace(/\n/g, "<br>");
var container = this.getContentContainer();
container.innerHTML = html;
DBG.timePt("END - highlight objects on-demand, text msg.");
} else {
this._processHtmlDoc();
}
};
ZmMailMsgView.prototype.resetMsg =
function(newMsg) {
// Remove listener for current msg if it exists
if (this._msg) {
this._msg.removeChangeListener(this._changeListener);
}
};
ZmMailMsgView.prototype.getMsg =
function() {
return this._msg;
};
ZmMailMsgView.prototype.getItem = ZmMailMsgView.prototype.getMsg;
// Following two overrides are a hack to allow this view to pretend it's a list view
ZmMailMsgView.prototype.getSelection =
function() {
return [this._msg];
};
ZmMailMsgView.prototype.getSelectionCount =
function() {
return 1;
};
ZmMailMsgView.prototype.getMinHeight =
function() {
if (!this._headerHeight) {
var headerObj = document.getElementById(this._hdrTableId);
this._headerHeight = headerObj ? Dwt.getSize(headerObj).y : 0;
}
return this._headerHeight;
};
// returns true if the current message was rendered in HTML
ZmMailMsgView.prototype.hasHtmlBody =
function() {
return this._htmlBody != null;
};
// returns the IFRAME's document if we are using one, or the window document
ZmMailMsgView.prototype.getDocument =
function() {
return this._usingIframe ? Dwt.getIframeDoc(this.getIframe()) : document;
};
// returns the IFRAME element if we are using one
ZmMailMsgView.prototype.getIframe =
function() {
if (!this._usingIframe) { return null; }
var iframe = this._iframeId && document.getElementById(this._iframeId);
iframe = iframe || (this._ifw && this._ifw.getIframe());
return iframe;
};
ZmMailMsgView.prototype.getIframeElement = ZmMailMsgView.prototype.getIframe;
// Returns a BODY element if we are using an IFRAME, the container DIV if we are not.
ZmMailMsgView.prototype.getContentContainer =
function() {
if (this._usingIframe) {
var idoc = this.getDocument();
return idoc && idoc.body;
}
else {
return this._containerEl;
}
};
ZmMailMsgView.prototype.getContent =
function() {
var container = this.getContentContainer();
return container ? container.innerHTML : "";
};
ZmMailMsgView.prototype.addInviteReplyListener =
function(listener) {
this.addListener(ZmInviteMsgView.REPLY_INVITE_EVENT, listener);
};
ZmMailMsgView.prototype.addShareListener =
function(listener) {
this.addListener(ZmMailMsgView.SHARE_EVENT, listener);
};
ZmMailMsgView.prototype.addSubscribeListener =
function(listener) {
this.addListener(ZmMailMsgView.SUBSCRIBE_EVENT, listener);
};
ZmMailMsgView.prototype.setVisible =
function(visible, readingPaneOnRight,msg) {
DwtComposite.prototype.setVisible.apply(this, arguments);
var inviteMsgView = this._inviteMsgView;
if (!inviteMsgView) {
return;
}
if (visible && this._msg) {
if (this._msg != msg) {
var dayView = inviteMsgView.getDayView();
if (dayView) {
dayView.setIsRight(readingPaneOnRight);
}
inviteMsgView.set(this._msg);
inviteMsgView.showMoreInfo(null, null, readingPaneOnRight);
}
}
else {
inviteMsgView.reset();
}
};
// Private / protected methods
ZmMailMsgView.prototype._getSubscribeToolbar =
function(req) {
if (this._subscribeToolbar) {
if (AjxEnv.isIE) {
//reparenting on IE does not work. So recreating in this case. (similar to bug 52412 for the invite toolbar)
this._subscribeToolbar.dispose();
this._subscribeToolbar = null;
}
else {
return this._subscribeToolbar;
}
}
this._subscribeToolbar = this._getButtonToolbar([ZmOperation.SUBSCRIBE_APPROVE, ZmOperation.SUBSCRIBE_REJECT],
ZmId.TB_SUBSCRIBE,
this._subscribeToolBarListener.bind(this, req));
return this._subscribeToolbar;
};
ZmMailMsgView.prototype._getShareToolbar =
function() {
if (this._shareToolbar) {
if (AjxEnv.isIE) {
//reparenting on IE does not work. So recreating in this case. (similar to bug 52412 for the invite toolbar)
this._shareToolbar.dispose();
this._shareToolbar = null;
}
else {
return this._shareToolbar;
}
}
this._shareToolbar = this._getButtonToolbar([ZmOperation.SHARE_ACCEPT, ZmOperation.SHARE_DECLINE],
ZmId.TB_SHARE,
this._shareToolBarListener.bind(this));
return this._shareToolbar;
};
ZmMailMsgView.prototype._getButtonToolbar =
function(buttonIds, toolbarType, listener) {
var params = {
parent: this,
buttons: buttonIds,
posStyle: DwtControl.STATIC_STYLE,
className: "ZmShareToolBar",
buttonClassName: "DwtToolbarButton",
context: this._mode,
toolbarType: toolbarType
};
var toolbar = new ZmButtonToolBar(params);
for (var i = 0; i < buttonIds.length; i++) {
var id = buttonIds[i];
// HACK: IE doesn't support multiple class names.
var b = toolbar.getButton(id);
b._hoverClassName = b._className + "-" + DwtCssStyle.HOVER;
b._activeClassName = b._className + "-" + DwtCssStyle.ACTIVE;
toolbar.addSelectionListener(id, listener);
}
return toolbar;
};
ZmMailMsgView.prototype._notifyZimletsNewMsg =
function(msg, oldMsg) {
// notify zimlets that a new message has been opened
appCtxt.notifyZimlets("onMsgView", [msg, oldMsg, this]);
};
ZmMailMsgView.prototype._handleResponseSet =
function(msg, oldMsg, dayViewCallback) {
var notifyZimletsInShowMore = false;
if (this._inviteMsgView) {
// always show F/B view if in stand-alone message view otherwise, check
// if reading pane is on
if (this._inviteMsgView.isActive() &&
(this._controller.isReadingPaneOn() || (this._controller instanceof ZmMsgController)))
{
notifyZimletsInShowMore = true;
this._inviteMsgView.showMoreInfo(this._notifyZimletsNewMsg.bind(this, msg, oldMsg), dayViewCallback);
}
else {
// resize the message view without F/B view
this._inviteMsgView.resize(true);
}
}
if (!appCtxt.isChildWindow) {
this._setTags(msg);
// Remove listener for current msg if it exists
if (oldMsg) {
oldMsg.removeChangeListener(this._changeListener);
}
msg.addChangeListener(this._changeListener);
}
// reset scroll view to top most
var htmlElement = this.getHtmlElement();
htmlElement.scrollTop = 0;
if (htmlElement.scrollTop != 0 && this._usingIframe) {
/* situation that happens only on Chrome, without repro steps - bug 55775/57090 */
AjxDebug.println(AjxDebug.SCROLL, "scrollTop not set to 0. scrollTop=" + htmlElement.scrollTop + " offsetHeight=" + htmlElement.offsetHeight + " scrollHeight=" + htmlElement.scrollHeight + " browser=" + navigator.userAgent);
AjxDebug.dumpObj(AjxDebug.SCROLL, htmlElement.outerHTML);
/*
trying this hack for solution -
explanation: The scroll bar does not appear if the scrollHeight of the div is bigger than the total height of the iframe and header together (i.e. if htmlElement.scrollHeight >= htmlElement.offsetHeight)
If the scrollbar does not appear it's set to, and stays 0 when the scrollbar reappears due to resizing the iframe in _resetIframeHeight (which is later, I think always on timer).
So what I do here is set the height of the iframe to very small (since the default is 150px), so the scroll bar disappears.
it will reappear when we reset the size in _resetIframeHeight. I hope this will solve the issue.
*/
var iframe = this.getIframe();
if (iframe) {
iframe.style.height = "1px";
AjxDebug.println(AjxDebug.SCROLL, "scrollTop after resetting it with the hack =" + htmlElement.scrollTop);
}
}
if (!notifyZimletsInShowMore) {
this._notifyZimletsNewMsg(msg, oldMsg);
}
if (!msg.isDraft && msg.readReceiptRequested) {
this._controller.sendReadReceipt(msg);
}
};
// This is needed for Gecko only: for some reason, clicking on a local link will
// open the full Zimbra chrome in the iframe :-( so we fake a scroll to the link
// target here. (bug 7927)
ZmMailMsgView.__localLinkClicked =
function(msgView, ev) {
// note that this function is called in the context of the link ('this' is an A element)
var id = this.getAttribute("href");
var el = null;
var doc = this.ownerDocument;
if (id.substr(0, 1) == "#") {
id = id.substr(1);
el = doc.getElementById(id);
if (!el) {
try {
el = doc.getElementsByName(id)[0];
} catch(ex) {}
}
if (!el) {
id = decodeURIComponent(id);
el = doc.getElementById(id);
if (!el) {
try {
el = doc.getElementsByName(id)[0];
} catch(ex) {}
}
}
}
// attempt #1: doesn't work at all -- we're not scrolling with the IFRAME :-(
// if (el) {
// var pos = Dwt.getLocation(el);
// doc.contentWindow.scrollTo(pos.x, pos.y);
// }
// attempt #2: works pretty well, but the target node will showup at the bottom of the frame
// var foo = doc.createElement("a");
// foo.href = "#";
// foo.innerHTML = "foo";
// el.parentNode.insertBefore(foo, el);
// foo.focus();
// the final monstrosity: scroll the containing DIV
// (that is the whole msgView). Note we have to take
// into account the headers, "display images", etc --
// so we add iframe.offsetTop/Left.
if (el) {
var div = msgView.getHtmlElement();
var iframe = msgView.getIframe();
var pos = Dwt.getLocation(el);
div.scrollTop = pos.y + iframe.offsetTop - 20; // fuzz factor necessary for unknown reason :-(
div.scrollLeft = pos.x + iframe.offsetLeft;
}
if (ev) {
ev.stopPropagation();
ev.preventDefault();
}
return false;
};
ZmMailMsgView.prototype.hasValidHref =
function (node) {
// Bug 22958: IE can throw when you try and get the href if it doesn't like
// the value, so we wrap the test in a try/catch.
// hrefs formatted like http://www.name@domain.com can cause this to happen.
try {
var href = node.href;
return ZmMailMsgView._URL_RE.test(href) || ZmMailMsgView._MAILTO_RE.test(href);
} catch (e) {
return false;
}
};
// Dives recursively into the given DOM node. Creates ObjectHandlers in text
// nodes and cleans the mess in element nodes. Discards by default "script",
// "link", "object", "style", "applet" and "iframe" (most of them shouldn't
// even be here since (1) they belong in the <head> and (2) are discarded on
// the server-side, but we check, just in case..).
ZmMailMsgView.prototype._processHtmlDoc =
function() {
var parent = this._usingIframe ? this.getDocument() : this._containerEl;
if (!parent) { return; }
DBG.timePt("Starting ZmMailMsgView.prototype._processHtmlDoc");
// bug 8632
var images = parent.getElementsByTagName("img");
if (images.length > 0) {
var length = images.length;
for (var i = 0; i < images.length; i++) {
this._checkImgInAttachments(images[i]);
}
}
//Find Zimlet Objects lazly
this.lazyFindMailMsgObjects(500);
DBG.timePt("-- END _processHtmlDoc");
};
ZmMailMsgView.prototype.lazyFindMailMsgObjects =
function(interval) {
if (this._objectManager && !this._disposed) {
this._lazyCreateObjectManager();
this._objectsAction = new AjxTimedAction(this, this._findMailMsgObjects);
AjxTimedAction.scheduleAction(this._objectsAction, ( interval || 500 ));
}
};
ZmMailMsgView.prototype._findMailMsgObjects =
function() {
var doc = this.getDocument();
if (doc) {
this._objectManager.processObjectsInNode(doc, this.getContentContainer());
}
};
ZmMailMsgView.prototype._checkImgInAttachments =
function(img) {
if (!this._msg || img.getAttribute("zmforced")) { return; }
var attachments = this._msg.attachments;
var csfeMsgFetch = appCtxt.get(ZmSetting.CSFE_MSG_FETCHER_URI);
try {
var src = img.getAttribute("src") || img.getAttribute("dfsrc");
}
catch(e) {
AjxDebug.println(AjxDebug.DATA_URI, "_checkImgInAttachments: couldn't access attribute src or dfsrc");
}
var cid;
if (/^cid:(.*)/.test(src)) {
cid = "<" + RegExp.$1 + ">";
}
for (var i = 0; i < attachments.length; i++) {
var att = attachments[i];
if (att.foundInMsgBody) { continue; }
if (cid && att.contentId == cid) {
att.foundInMsgBody = true;
break;
} else if (src && src.indexOf(csfeMsgFetch) == 0) {
var mpId = src.substring(src.lastIndexOf("=") + 1);
if (mpId == att.part) {
att.foundInMsgBody = true;
break;
}
} else if (att.contentLocation && src) {
var filename = src.substring(src.lastIndexOf("/") + 1);
if (filename == att.fileName) {
att.foundInMsgBody = true;
break;
}
}
}
};
ZmMailMsgView.prototype._fixMultipartRelatedImages =
function(msg, parent) {
// fix <img> tags
var images = parent.getElementsByTagName("img");
var hasExternalImages = false;
if (this._usingIframe) {
var self = this;
var onload = function() {
//resize iframe onload of image
ZmMailMsgView._resetIframeHeight(self);
this.onload = null; // *this* is reference to <img> el.
};
}
for (var i = 0; i < images.length; i++) {
var img = images[i];
var external = ZmMailMsgView._isExternalImage(img); // has "dfsrc" attr
if (!external) { //Inline image
ZmMailMsgView.__unfangInternalImage(msg, img, "src", false);
if (onload) {
img.onload = onload;
}
}
else {
img.src = "/img/zimbra/1x1-trans.png";
}
hasExternalImages = external || hasExternalImages;
}
// fix all elems with "background" attribute
hasExternalImages = this._fixMultipartRelatedImagesRecurse(msg, this._usingIframe ? parent.body : parent) || hasExternalImages;
// did we get them all?
return !hasExternalImages;
};
ZmMailMsgView.prototype._fixMultipartRelatedImagesRecurse =
function(msg, node) {
var hasExternalImages = false;
function recurse(node){
var child = node.firstChild;
while (child) {
if (child.nodeType == AjxUtil.ELEMENT_NODE) {
hasExternalImages = ZmMailMsgView.__unfangInternalImage(msg, child, "background", true) || hasExternalImages;
recurse(child);
}
child = child.nextSibling;
}
}
if (node.innerHTML.indexOf("dfbackground") != -1) {
recurse(node);
}
else if (node.attributes && node.getAttribute("dfbackground") != -1) {
hasExternalImages = ZmMailMsgView.__unfangInternalImage(msg, node, "background", true);
}
if (!hasExternalImages && $(node).find("table[dfbackground], td[dfbackground]").length) {
hasExternalImages = true;
}
return hasExternalImages;
};
/**
* Determines if an img element references an external image
* @param elem {HTMLelement}
* @return {Boolean} true if image is external
*/
ZmMailMsgView._isExternalImage =
function(elem) {
if (!elem) {
return false;
}
return Boolean(elem.getAttribute("dfsrc"));
}
/**
* Reverses the work of the (server-side) defanger, so that images are displayed.
*
* @param {ZmMailMsg} msg mail message
* @param {Element} elem element to be checked (img)
* @param {string} aname attribute name
* @param {boolean} external if true, look only for external images
*
* @return true if the image is external
*/
ZmMailMsgView.__unfangInternalImage =
function(msg, elem, aname, external) {
var avalue, pnsrc;
try {
if (external) {
avalue = elem.getAttribute("df" + aname);
}
else {
pnsrc = avalue = elem.getAttribute("pn" + aname);
avalue = avalue || elem.getAttribute(aname);
}
}
catch(e) {
AjxDebug.println(AjxDebug.DATA_URI, "__unfangInternalImage: couldn't access attribute " + aname);
}
if (avalue) {
if (avalue.substr(0,4) == "cid:") {
var cid = "<" + AjxStringUtil.urlComponentDecode(avalue.substr(4)) + ">"; // Parse percent-escaping per bug #52085 (especially %40 to @)
avalue = msg.getContentPartAttachUrl(ZmMailMsg.CONTENT_PART_ID, cid);
if (avalue) {
elem.setAttribute(aname, avalue);
}
return false;
} else if (avalue.substring(0,4) == "doc:") {
avalue = [appCtxt.get(ZmSetting.REST_URL), ZmFolder.SEP, avalue.substring(4)].join('');
if (avalue) {
elem.setAttribute(aname, avalue);
return false;
}
} else if (pnsrc) { // check for content-location verison
avalue = msg.getContentPartAttachUrl(ZmMailMsg.CONTENT_PART_LOCATION, avalue);
if (avalue) {
elem.setAttribute(aname, avalue);
return false;
}
} else if (avalue.substring(0,5) == "data:") {
return false;
}
return true; // not recognized as inline img
}
return false;
};
ZmMailMsgView.prototype._createDisplayImageClickClosure =
function(msg, parent, id) {
var self = this;
return function(ev) {
var target = DwtUiEvent.getTarget(ev),
targetId = target ? target.id : null,
addrToAdd = "";
var diEl = document.getElementById(id);
//This is required in case of the address is marked as trusted, the function is called without any target being set
var force = (msg && msg.showImages) || appCtxt.get(ZmSetting.DISPLAY_EXTERNAL_IMAGES);
if (!force) {
if (!targetId) { return; }
if (targetId.indexOf("domain") != -1) {
//clicked on domain
addrToAdd = msg.sentByDomain;
}
else if (targetId.indexOf("email") != -1) {
//clicked on email
addrToAdd = msg.sentByAddr;
}
else if (targetId.indexOf("dispImgs") != -1) {
//do nothing here - just load the images
}
else if (targetId.indexOf("close") != -1) {
Dwt.setVisible(diEl, false);
return;
}
else {
//clicked elsewhere in the info bar - DO NOTHING AND RETURN
return;
}
}
//Create a modifyprefs req and add the addr to modify
if (addrToAdd) {
var trustedList = self.getTrustedSendersList();
trustedList.add(addrToAdd, null, true);
var callback = self._addTrustedAddrCallback.bind(self, addrToAdd);
var errorCallback = self._addTrustedAddrErrorCallback.bind(self, addrToAdd);
self._controller.addTrustedAddr(trustedList.getArray(), callback, errorCallback);
}
var images = parent.getElementsByTagName("img");
var onload = null;
if (self._usingIframe) {
onload = function() {
ZmMailMsgView._resetIframeHeight(self);
this.onload = null; // *this* is reference to <img> el.
DBG.println(AjxDebug.DBG3, "external image onload called for " + this.src);
};
}
for (var i = 0; i < images.length; i++) {
var dfsrc = images[i].getAttribute("dfsrc");
if (dfsrc && dfsrc.match(/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\_\.]*(\?\S+)?)?)?/)) {
images[i].onload = onload;
// Fix for IE: Over HTTPS, http src urls for images might cause an issue.
try {
DBG.println(AjxDebug.DBG3, "displaying external images. src = " + images[i].src);
images[i].src = ''; //unload it first
images[i].src = images[i].getAttribute("dfsrc");
DBG.println(AjxDebug.DBG3, "displaying external images. src is now = " + images[i].src);
} catch (ex) {
// do nothing
}
}
}
//determine if any tables or table cells have an external background image
var tableCells = $(parent).find("table[dfbackground], td[dfbackground]");
for (var i=0; i<tableCells.length; i++) {
var dfbackground = $(tableCells[i]).attr("dfbackground");
if (ZmMailMsgView._URL_RE.test(dfbackground)) {
$(tableCells[i]).attr("background", dfbackground);
}
}
Dwt.setVisible(diEl, false);
self._htmlBody = self.getContentContainer().innerHTML;
if (msg) {
msg.setHtmlContent(self._htmlBody);
msg.showImages = true;
}
//Make sure the link is not followed
return false;
};
};
ZmMailMsgView.prototype._resetIframeHeightOnTimer =
function(attempt) {
if (!this._usingIframe) { return; }
DBG.println(AjxDebug.DBG1, "_resetIframeHeightOnTimer attempt: " + (attempt != null ? attempt : "null"));
// Because sometimes our view contains images that are slow to download, wait a
// little while before resizing the iframe.
var act = this._resizeAction = new AjxTimedAction(this, ZmMailMsgView._resetIframeHeight, [this, attempt]);
AjxTimedAction.scheduleAction(act, 200);
};
ZmMailMsgView.prototype._makeHighlightObjectsDiv =
function(origText) {
var self = this;
function func() {
var div = document.getElementById(self._highlightObjectsId);
div.innerHTML = ZmMsg.pleaseWaitHilitingObjects;
setTimeout(function() {
self.highlightObjects(origText);
Dwt.setVisible(div, false);
ZmMailMsgView._resetIframeHeight(self);
}, 3);
return false;
}
// avoid closure memory leaks
(function() {
var infoBarDiv = document.getElementById(self._infoBarId);
if (infoBarDiv) {
self._highlightObjectsId = ZmId.getViewId(self._viewId, ZmId.MV_HIGHLIGHT_OBJ, self._mode);
var subs = {
id: self._highlightObjectsId,
text: ZmMsg.objectsNotDisplayed,
link: ZmMsg.hiliteObjects
};
var html = AjxTemplate.expand("mail.Message#InformationBar", subs);
infoBarDiv.appendChild(Dwt.parseHtmlFragment(html));
var div = document.getElementById(subs.id+"_link");
Dwt.setHandler(div, DwtEvent.ONCLICK, func);
}
})();
};
ZmMailMsgView.prototype._stripHtmlComments =
function(html) {
// bug: 38273 - Remove HTML Comments <!-- -->
// But make sure not to remove inside style|script tags.
var regex = /<(?:!(?:--[\s\S]*?--\s*)?(>)\s*|(?:script|style|SCRIPT|STYLE)[\s\S]*?<\/(?:script|style|SCRIPT|STYLE)>)/g;
html = html.replace(regex,function(m, $1) {
return $1 ? '':m;
});
return html;
};
// Returns true (the default) if we should display content in an IFRAME as opposed to a DIV.
ZmMailMsgView.prototype._useIframe =
function(isTextMsg, html, isTruncated) {
return true;
};
// Displays the given content in an IFRAME or a DIV.
ZmMailMsgView.prototype._displayContent =
function(params) {
var html = params.html || "";
if (!params.isTextMsg) {
//Microsoft silly smilies
html = html.replace(/<span style="font-family:Wingdings">J<\/span>/g, "\u263a"); // :)
html = html.replace(/<span style="font-family:Wingdings">L<\/span>/g, "\u2639"); // :(
}
// The info bar allows the user to load external images. We show it if:
// - msg is HTML
// - user pref says not to show images up front, or this is Spam folder
// - we're not already showing images
// - there are <img> tags
var isSpam = (this._msg && this._msg.folderId == ZmOrganizer.ID_SPAM);
var imagesNotShown = (!this._msg || !this._msg.showImages);
this._needToShowInfoBar = (!params.isTextMsg &&
(!appCtxt.get(ZmSetting.DISPLAY_EXTERNAL_IMAGES) || isSpam) &&
imagesNotShown && /<img/i.test(html));
var displayImages;
if (this._needToShowInfoBar) {
displayImages = this._showInfoBar(this._infoBarId);
}
var callback;
var msgSize = (html.length / 1024);
var maxHighlightSize = appCtxt.get(ZmSetting.HIGHLIGHT_OBJECTS);
if (params.isTextMsg) {
if (this._objectManager) {
if (msgSize <= maxHighlightSize) {
callback = this.lazyFindMailMsgObjects.bind(this, 500);
} else {
this._makeHighlightObjectsDiv(params.origText);
}
}
if (AjxEnv.isSafari) {
html = "<html><head></head><body>" + html + "</body></html>";
}
} else {
html = this._stripHtmlComments(html);
if (this._objectManager) {
var images = html.match(/<img[^>]+>/ig);
msgSize = (images) ? msgSize - (images.join().length / 1024) : msgSize; // Excluding images in the message
if (msgSize <= maxHighlightSize) {
callback = this._processHtmlDoc.bind(this);
} else {
this._makeHighlightObjectsDiv();
}
}
}
var msgTruncated;
this._isMsgTruncated = false;
if (params.isTruncated) {
this._isMsgTruncated = true;
var msgTruncatedDiv = document.getElementById(this._msgTruncatedId);
if (!msgTruncatedDiv) {
var infoBarDiv = document.getElementById(this._infoBarId);
if (infoBarDiv) {
var subs = {
id: this._msgTruncatedId,
text: ZmMsg.messageTooLarge,
link: ZmMsg.viewEntireMessage
};
var msgTruncatedHtml = AjxTemplate.expand("mail.Message#InformationBar", subs);
msgTruncated = Dwt.parseHtmlFragment(msgTruncatedHtml);
infoBarDiv.appendChild(msgTruncated);
Dwt.setHandler(msgTruncated, DwtEvent.ONCLICK, this._handleMsgTruncated.bind(this));
}
}
}