This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.EmlViewer.js
352 lines (333 loc) · 13.2 KB
/
class.EmlViewer.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
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
Class.create("EmlViewer", AbstractEditor, {
initialize: function($super, oFormObject, options)
{
$super(oFormObject, options);
this.actions.get("downloadFileButton").observe('click', function(){
if(!this.currentFile) return;
ajaxplorer.triggerDownload(ajxpBootstrap.parameters.get('ajxpServerAccess')+'&action=download&file='+this.currentFile);
return false;
}.bind(this));
},
open : function($super, node){
// Move hidden download form in body, if not already there
var original = $("emlDownloadAttachmentForm");
if($("emlDownloadForm")){
original.remove();
}else{
$$("body")[0].insert(original);
original.id="emlDownloadForm";
original.setAttribute("id", "emlDownloadForm");
$("emlDownloadForm").insert(new Element("input", {"type":"hidden", "name":"get_action", "value":"eml_dl_attachment"}));
$("emlDownloadForm").insert(new Element("input", {"type":"hidden", "name":"file", "value":""}));
$("emlDownloadForm").insert(new Element("input", {"type":"hidden", "name":"secure_token", "value":Connexion.SECURE_TOKEN}));
$("emlDownloadForm").insert(new Element("input", {"type":"hidden", "name":"attachment_id", "value":""}));
}
$super(node);
var fileName = node.getPath();
this.textareaContainer = new Element('div');
this.contentMainContainer = this.textareaContainer;
this.textareaContainer.setStyle({width:'100%', overflow:'auto'});
this.element.insert(this.textareaContainer);
fitHeightToBottom($(this.textareaContainer), $(modal.elementName));
// LOAD FILE NOW
this.loadFileContent(fileName);
if(window.ajxpMobile){
this.setFullScreen();
attachMobileScroll(this.textareaContainer, "vertical");
}
},
loadFileContent : function(fileName){
this.currentFile = fileName;
var connexion = new Connexion();
connexion.addParameter('get_action', 'eml_get_xml_structure');
connexion.addParameter('file', fileName);
connexion.onComplete = function(transp){
this.parseXmlStructure(transp);
this.updateTitle(getBaseName(fileName));
}.bind(this);
var connexion2 = new Connexion();
connexion2.addParameter('get_action', 'eml_get_bodies');
connexion2.addParameter('file', fileName);
connexion2.onComplete = function(transp){
this.parseBodies(transp);
}.bind(this);
this.setModified(false);
this.setOnLoad(this.textareaContainer);
connexion.sendAsync();
connexion2.sendAsync();
},
dlAttachment : function(event){
var form = $("emlDownloadForm");
form.elements["secure_token"].value = Connexion.SECURE_TOKEN;
form.elements["file"].value = this.currentFile;
form.elements["attachment_id"].value = event.target.up("div").__ATTACHMENT_ID;
var agent = navigator.userAgent;
if(agent && (agent.indexOf('iPhone')!=-1||agent.indexOf('iPod')!=-1||agent.indexOf('iPad')!=-1||agent.indexOf('iOs')!=-1||agent.indexOf('Safari')!=-1)){
document.location.href = window.ajxpServerAccessPath + "&" + form.serialize();
}else{
form.submit();
}
},
cpAttachment : function(event){
var container = this.element.down('#treeSelectorCpContainer');
this.treeSelector = new TreeSelector(container);
var user = ajaxplorer.user;
if(user) var activeRepository = user.getActiveRepository();
if(user && user.canCrossRepositoryCopy() && user.hasCrossRepositories()){
var firstKey ;
var reposList = new Hash();
user.getCrossRepositories().each(function(pair){
if(!firstKey) firstKey = pair.key;
reposList.set(pair.key, pair.value.getLabel());
}.bind(this));
if(!user.canWrite()){
var nodeProvider = new RemoteNodeProvider();
nodeProvider.initProvider({tmp_repository_id:firstKey});
var rootNode = new AjxpNode("/", false, MessageHash[373], "folder.png", nodeProvider);
this.treeSelector.load(rootNode);
}else{
this.treeSelector.load();
}
this.treeSelector.setFilterShow(true);
reposList.each(function(pair){
this.treeSelector.appendFilterValue(pair.key, pair.value);
}.bind(this));
if(user.canWrite()) this.treeSelector.appendFilterValue(activeRepository, "<"+MessageHash[372]+">", 'top');
this.treeSelector.setFilterSelectedIndex(0);
this.treeSelector.setFilterChangeCallback(function(e){
var externalRepo = this.filterSelector.getValue();
var nodeProvider = new RemoteNodeProvider();
nodeProvider.initProvider({tmp_repository_id:externalRepo});
this.resetAjxpRootNode(new AjxpNode("/", false, MessageHash[373], "folder.png", nodeProvider));
});
}else{
this.treeSelector.load();
}
//this.treeSelector.load();
var currentAtt = event.target.up("div");
var attachmentId = currentAtt.__ATTACHMENT_ID;
if(this.fullScreenMode){
container.setStyle({right: 4, top: ($('emlHeaderContainer').getHeight() + 128) + "px"});
}else{
container.setStyle({right: 11, top: ($('emlHeaderContainer').getHeight() + 75) + "px"});
}
var hideSelector = function(){
this.treeSelector.unload();
currentAtt.removeClassName("active");
currentAtt.select("a.emlAttachmentAction").each(Element.hide);
container.hide();
}.bind(this);
container.down("#eml_cp_ok").observeOnce("click", function(e){
Event.stop(e);
var selectedNode = this.treeSelector.getSelectedNode();
var actionValue = "eml_cp_attachment";
var crossCopy = false;
var crtRepoType = ajaxplorer.user.repositories.get(ajaxplorer.user.activeRepository).accessType;
if(activeRepository && this.treeSelector.getFilterActive(activeRepository)){
crossCopy = true;
}
var connexion = new Connexion();
if(crtRepoType == "imap"){
connexion.setParameters({
file: this.currentFile+"#attachments/"+attachmentId,
get_action:crossCopy?"cross_copy":"copy",
dest:selectedNode,
dest_repository_id:crossCopy?this.treeSelector.filterSelector.getValue():""
});
}else{
connexion.setParameters({
file: this.currentFile,
get_action:"eml_cp_attachment",
attachment_id:attachmentId,
destination:selectedNode,
dest_repository_id:this.treeSelector.filterSelector.getValue()
});
console.log(connexion._parameters);
}
connexion.onComplete = function(transport){
ajaxplorer.actionBar.parseXmlMessage(transport.responseXML);
};
connexion.sendAsync();
hideSelector();
}.bind(this));
container.down("#eml_cp_can").observeOnce("click", function(e){
Event.stop(e);
hideSelector();
}.bind(this));
currentAtt.addClassName("active");
container.show();
},
parseBodies : function (transport){
var xmlDoc = transport.responseXML;
var html = XPathSelectSingleNode(xmlDoc, 'email_body/mimepart[@type="html"]');
if(html){
this.iFrame = new Element('iframe');
this.textareaContainer.insert(this.iFrame);
this.textareaContainer.setStyle({overflowY:'hidden'});
this.iFrameContent = html.firstChild.nodeValue;
this.iFrame.contentWindow.document.write(this.iFrameContent);
this.iFrame.setStyle({width: '100%', height: '100%', border: '0px'});
if(Prototype.Browser.IE){
this.element.observeOnce("editor:exitFSend", function(){
// Fix IE disappearing elements in Quirks Mode
$('emlHeaderContainer').setStyle({position:"absolute"});
this.textareaContainer.setStyle({marginTop:$('emlHeaderContainer').getHeight()});
}.bind(this) );
}else{
var reloader = function(){
this.iFrame.contentWindow.document.write(this.iFrameContent);
}.bind(this);
this.element.observe("editor:enterFSend", reloader);
this.element.observe("editor:exitFSend", reloader);
}
}else{
var pre = new Element("pre");
pre.insert(XPathSelectSingleNode(xmlDoc, 'email_body/mimepart[@type="plain"]').firstChild.nodeValue);
this.textareaContainer.insert(pre);
this.textareaContainer.setStyle({overflowY:'auto'});
pre.setStyle({display:'block',height: '100%',margin:0});
}
},
parseXmlStructure : function(transport){
var xmlDoc = transport.responseXML;
var hContainer = this.element.down("#emlHeaderContainer");
// PARSE HEADERS
var headers = XPathSelectNodes(xmlDoc, "email/header");
var labels = {"From":"editor.eml.1", "To":"editor.eml.2", "Cc":"editor.eml.12", "Date":"editor.eml.4", "Subject":"editor.eml.3"};;
var searchedHeaders = {"From":[], "To":[], "Cc":[], "Date":[], "Subject":[]};
headers.each(function(el){
var hName = XPathGetSingleNodeText(el, "headername");
var hValue = XPathGetSingleNodeText(el, "headervalue");
if(searchedHeaders[hName]){
if(hValue.strip() != ''){
searchedHeaders[hName].push(hValue.strip().escapeHTML());
}
}
});
//console.log(searchedHeaders);
$H(searchedHeaders).each(function(pair){
if(pair.value.length){
var value = pair.value.join(", ");
var label = MessageHash[labels[pair.key]];
hContainer.insert('\
<div class="emlHeader">\
<div class="emlHeaderLabel">'+label+'</div>\
<div class="emlHeaderContent">'+value+'</div>\
</div>');
}
});
// PARSE ATTACHEMENTS
// Go throught headers and find Content-Disposition: attachment ones
var allHeaders = XPathSelectNodes(xmlDoc, "//header");
var attachments = {};
var id = 0;
allHeaders.each(function(el){
var hName = XPathGetSingleNodeText(el, "headername");
var hValue = XPathGetSingleNodeText(el, "headervalue");
if(hName != "Content-Disposition" || hValue != "attachment") return;
var mimepart = el.parentNode;
var filename = "";
// Find filename
var params = XPathSelectNodes(el, "parameter");
params.each(function(c){
if(XPathGetSingleNodeText(c, "paramname") == "filename"){
filename = XPathGetSingleNodeText(c, "paramvalue");
}
});
// Find attachment ID - not always
var foundId = false;
allHeaders.each(function(h){
if(h.parentNode != mimepart) return;
var siblingName = XPathGetSingleNodeText(h, "headername");
var siblingValue = XPathGetSingleNodeText(h, "headervalue");
if(siblingName == "X-Attachment-Id"){
id = XPathGetSingleNodeText(h, "headervalue");
foundId = true;
}
});
attachments[id] = filename;
if(!foundId){
id = id+1;
}
});
if(Object.keys(attachments).length){
var attachCont = new Element('div', {id:"attachments_container", className:"emlAttachCont", style:"height:"+($('emlHeaderContainer').getHeight()-14)+"px"});
hContainer.insert({top:attachCont});
for(var key in attachments){
var att = new Element("div", {className:"emlAttachment"});
att.__ATTACHMENT_ID = key;
att.insert(attachments[key]);
attachCont.insert(att);
var dlBut = new Element("a", {
className:"emlAttachmentAction",
title:MessageHash["editor.eml.10"]+attachments[key]});
dlBut.update(new Element("img", {
src:window.ajxpResourcesFolder+'/images/actions/16/download_manager.png',
height: 16,
width: 16}));
var cpBut = new Element("a", {className:"emlAttachmentAction", title:MessageHash["editor.eml.11"]});
cpBut.update(new Element("img", {
src:window.ajxpResourcesFolder+'/images/actions/16/editcopy.png',
height: 16,
width: 16}));
att.insert({top: dlBut});
att.insert({top: cpBut});
dlBut.observe("click", this.dlAttachment.bind(this));
cpBut.observe("click", this.cpAttachment.bind(this));
dlBut.hide();cpBut.hide();
att.observe("mouseenter", function(e){
e.target.select("a.emlAttachmentAction").each(Element.show);
});
att.observe("mouseleave", function(e){
if(e.target.hasClassName("active")) return;
e.target.select("a.emlAttachmentAction").each(Element.hide);
});
if(Prototype.Browser.IE){
att.observe("mouseenter", function(e){
e.target.addClassName("ieHover");
});
att.observe("mouseleave", function(e){
e.target.removeClassName("ieHover");
});
}
}
}
fitHeightToBottom($(this.textareaContainer), $(modal.elementName));
this.removeOnLoad(this.textareaContainer);
},
attachmentCellRenderer : function(element, ajxpNode, type){
if(!element) return;
if(ajxpNode.getMetadata().get("eml_attachments") == "0") {
if(type == "row") element.update('<span class="text_label"> </span>');
return;
}
element.setStyle({
backgroundImage:'url("plugins/editor.eml/attach.png")',
backgroundRepeat: 'no-repeat',
backgroundPosition: (type=="thumb" ? '2px 2px': '5px 4px')
});
if(type == "row"){
element.update('<span class="text_label"> </span>');
}
element.setAttribute("title", ajxpNode.getMetadata().get("eml_attachments")+" attachments");
}
});