/
dimpcore.js
377 lines (314 loc) · 10.8 KB
/
dimpcore.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
/**
* dimpcore.js - Dimp UI application logic.
*
* Copyright 2005-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*/
/* DimpCore object. */
var DimpCore = {
// Vars used and defaulting to null/false:
// DMenu
conf: {},
context: {},
text: {},
// Wrapper methods around HordeCore functions.
// IMP specific 'opts': uids
doAction: function(action, params, opts)
{
if (opts && opts.uids) {
params = $H(params).clone();
opts = opts || {};
if (opts.uids.viewport_selection) {
opts.uids = opts.uids.get('uid');
}
params.set('buid', opts.uids.toViewportUidString());
}
return HordeCore.doAction(action, params, opts);
},
compose: function(type, params)
{
params = params || {};
params.type = type;
HordeCore.popupWindow(DimpCore.conf.URI_COMPOSE, params, {
name: 'compose' + new Date().getTime()
});
},
toggleButtons: function(elts, disable)
{
elts.each(function(b) {
[ b.up() ].invoke(disable ? 'addClassName' : 'removeClassName', 'disabled');
});
},
// p = (Element) Parent element
// t = (string) Context menu type
// o = (object) Options:
// - disabled: (boolean) Disabled?
// - insert: (string) Insertion position.
// - no_offset: (boolean) If true, offset from popdown graphic.
// - trigger: (boolean) Trigger popdown on button click?
addPopdown: function(p, t, o)
{
o = o || {};
var elt = new Element('SPAN', { className: 'horde-popdown' }),
ins = {};
p = $(p);
ins[o.insert ? o.insert : 'after'] = elt;
p.insert(ins);
if (o.trigger) {
this.addContextMenu({
disable: o.disabled,
elt: p,
left: true,
offset: p.up(),
type: t
});
}
this.addContextMenu({
disable: o.disabled,
elt: elt,
left: true,
offset: (o.no_offset ? elt : elt.up()),
type: t
});
},
addContextMenu: function(p)
{
if (this.DMenu) {
this.DMenu.addElement(p.elt.identify(), 'ctx_' + p.type, p);
}
},
/* Add dropdown menus to addresses. */
buildAddressLinks: function(alist, elt, limit)
{
var base, tmp,
df = document.createDocumentFragment();
if (alist.raw) {
elt.insert(alist.raw);
return elt;
}
this._buildAddressLinks(alist.addr, df);
if (limit && alist.limit) {
tmp = $('largeaddrspan').clone(true).addClassName('largeaddrspan_active').writeAttribute({ id: null });
elt.insert(tmp);
base = tmp.down('.dispaddrlist');
tmp = tmp.down('.largeaddrlist');
base.down('.largeaddrlistlimit').show();
tmp.setText((tmp.textContent || tmp.innerText).replace('%d', alist.limit));
} else {
base = elt;
}
base.appendChild(df);
if (limit && alist.limit) {
tmp = (base.down('.addrgroup-div') || base).insert('[...]');
}
return elt;
},
_buildAddressLinks: function(alist, df)
{
alist.each(function(o) {
var tmp,
a = new Element('A', { className: 'horde-button' }).store({ email: o });
if (o.g) {
a.insert(o.g.escapeHTML() + ':').addClassName('addrgroup-name');
tmp = new Element('DIV', { className: 'addrgroup-div' });
tmp.insert(a);
df.appendChild(tmp);
this._buildAddressLinks(o.a, tmp);
} else if (o.p) {
a.writeAttribute({ title: o.b }).insert(o.p.escapeHTML());
df.appendChild(a);
} else if (o.b) {
a.insert(o.b.escapeHTML());
df.appendChild(a);
}
this.DMenu.addElement(a.identify(), 'ctx_contacts', { offset: a, left: true });
}, this);
},
/* Add message log info to message view. */
updateMsgLog: function(log)
{
var df = document.createDocumentFragment(),
tmp = $('msgloglist').down('UL');
log.each(function(entry) {
df.appendChild(new Element('LI').insert(new Element('SPAN', { className: 'iconImg imp-' + entry.t })).insert(entry.m));
});
tmp.childElements().invoke('remove');
tmp.appendChild(df);
},
// Abstract: define in any pages that need reloadMessage().
reloadMessage: function(params)
{
},
toggleCheck: function(elt, on)
{
if (on === null) {
if (elt) {
elt.hide();
}
return;
}
var a, r;
if (on) {
a = 'msCheckOn';
r = 'msCheck';
} else {
a = 'msCheck';
r = 'msCheckOn';
}
elt.removeClassName(r).addClassName(a).show();
},
/* Mouse click handler. */
clickHandler: function(e)
{
var elt = e.element(), tmp;
if (elt.hasClassName('unblockImageLink')) {
IMP_JS.unblockImages(e.memo);
} else if (elt.hasClassName('largeaddrspan_active') &&
!e.memo.element().hasClassName('horde-button')) {
if (e.memo.element().hasClassName('largeaddrlistlimit')) {
e.memo.element().hide();
elt.up('TD').fire('DimpCore:updateAddressHeader');
} else {
tmp = elt.down();
[ tmp.down(), tmp.down(1), tmp.next() ].invoke('toggle');
}
} else if (elt.hasClassName('pgpVerifyMsg')) {
elt.replace(DimpCore.text.verify);
DimpCore.reloadMessage({ pgp_verify_msg: 1 });
e.memo.stop();
} else if (elt.hasClassName('smimeVerifyMsg')) {
elt.replace(DimpCore.text.verify);
DimpCore.reloadMessage({ smime_verify_msg: 1 });
e.memo.stop();
}
},
contextOnClick: function(e)
{
var baseelt = e.element();
switch (e.memo.elt.readAttribute('id')) {
case 'ctx_contacts_new':
this.compose('new', {
to_json: Object.toJSON(baseelt.retrieve('email'))
});
break;
case 'ctx_contacts_add':
this.doAction('addContact', {
addr: Object.toJSON(baseelt.retrieve('email'))
});
break;
}
},
contextOnShow: function(e)
{
var tmp;
switch (e.memo) {
case 'ctx_contacts':
tmp = $(e.memo).down('DIV.contactAddr');
if (tmp) {
tmp.next().remove();
tmp.remove();
}
// Add e-mail info to context menu if personal name is shown on
// page.
if ((tmp = e.element().retrieve('email')) && !tmp.g && tmp.p) {
$(e.memo)
.insert({ top: new Element('DIV', { className: 'sep' }) })
.insert({ top: new Element('DIV', { className: 'contactAddr' }).insert(tmp.b.escapeHTML()) });
}
break;
}
},
contextOnTrigger: function(e)
{
if (!DimpCore.context[e.memo]) {
return;
}
var div = new Element('DIV', { className: 'context', id: e.memo }).hide();
if (!Object.isArray(DimpCore.context[e.memo])) {
$H(DimpCore.context[e.memo]).each(function(pair) {
div.insert(this._contextOnTrigger(pair, e.memo));
}, this);
}
$(document.body).insert(div);
},
_contextOnTrigger: function(pair, ctx)
{
var elt;
if (pair.key.startsWith('_sep')) {
return new Element('DIV', { className: 'sep' });
}
if (pair.key.startsWith('_mbox')) {
return new Element('DIV', { className: 'mboxName' }).insert(pair.value.escapeHTML());
}
if (pair.key.startsWith('_sub')) {
elt = new Element('DIV').hide();
$H(pair.value).each(function(v) {
elt.insert(this._contextOnTrigger(v, ctx));
}, this);
return elt;
}
elt = new Element('A');
if (pair.key.startsWith('*')) {
pair.key = pair.key.substring(1);
} else {
elt.insert(new Element('SPAN', { className: 'iconImg' }));
}
elt.writeAttribute('id', ctx + '_' + pair.key);
elt.insert(pair.value.escapeHTML());
return elt;
},
onDomLoad: function()
{
HordeCore.initHandler('click');
if (typeof ContextSensitive != 'undefined') {
this.DMenu = new ContextSensitive();
}
/* Set popup height. */
HordeCore.conf.popup_height = screen.availHeight - 50 -
((window.outerHeight && window.innerHeight) ? (window.outerHeight - window.innerHeight) : 150);
}
};
/* Initialize onload handler. */
document.observe('dom:loaded', DimpCore.onDomLoad.bind(DimpCore));
/* Browser native events. */
document.observe('HordeCore:click', DimpCore.clickHandler.bindAsEventListener(DimpCore));
/* ContextSensitive events. */
document.observe('ContextSensitive:click', DimpCore.contextOnClick.bindAsEventListener(DimpCore));
document.observe('ContextSensitive:show', DimpCore.contextOnShow.bindAsEventListener(DimpCore));
document.observe('ContextSensitive:trigger', DimpCore.contextOnTrigger.bindAsEventListener(DimpCore));
/* Dialog events. Since reloadMessage() can be extended, don't immediately
* bind function call now. */
document.observe('ImpPassphraseDialog:success', function() {
DimpCore.reloadMessage({});
});
/* Notification events. */
document.observe('HordeCore:showNotifications', function(e) {
switch (e.memo.type) {
case 'imp.reply':
case 'imp.forward':
case 'imp.redirect':
HordeBase.Growler.growl(m.message.escapeHTML(), {
className: m.type.replace('.', '-'),
life: 8
});
break;
}
});
/* Disable text selection for everything but compose/message body and FORM
* inputs. */
document.observe(Prototype.Browser.IE ? 'selectstart' : 'mousedown', function(e) {
if (!e.findElement('.allowTextSelection') &&
!e.element().match('SELECT') &&
!e.element().match('TEXTAREA') &&
!e.element().match('INPUT')) {
e.stop();
if (document.activeElement) {
var ae = $(document.activeElement);
if (ae.match('TEXTAREA') || ae.match('INPUT')) {
ae.blur();
}
}
}
});