]> git.donarmstrong.com Git - roundcube.git/blob - plugins/managesieve/managesieve.js
Imported Upstream version 0.7
[roundcube.git] / plugins / managesieve / managesieve.js
1 /* (Manage)Sieve Filters */
2
3 if (window.rcmail) {
4   rcmail.addEventListener('init', function(evt) {
5     // add managesieve-create command to message_commands array,
6     // so it's state will be updated on message selection/unselection
7     if (rcmail.env.task == 'mail') {
8       if (rcmail.env.action != 'show')
9         rcmail.env.message_commands.push('managesieve-create');
10       else
11         rcmail.enable_command('managesieve-create', true);
12     }
13     else {
14       var tab = $('<span>').attr('id', 'settingstabpluginmanagesieve').addClass('tablink'),
15         button = $('<a>').attr('href', rcmail.env.comm_path+'&_action=plugin.managesieve')
16           .attr('title', rcmail.gettext('managesieve.managefilters'))
17           .html(rcmail.gettext('managesieve.filters'))
18           .appendTo(tab);
19
20       // add tab
21       rcmail.add_element(tab, 'tabs');
22     }
23
24     if (rcmail.env.task == 'mail' || rcmail.env.action.indexOf('plugin.managesieve') != -1) {
25       // Create layer for form tips
26       if (!rcmail.env.framed) {
27         rcmail.env.ms_tip_layer = $('<div id="managesieve-tip" class="popupmenu"></div>');
28         rcmail.env.ms_tip_layer.appendTo(document.body);
29       }
30     }
31
32     // register commands
33     rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() });
34     rcmail.register_command('plugin.managesieve-act', function() { rcmail.managesieve_act() });
35     rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() });
36     rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() });
37     rcmail.register_command('plugin.managesieve-move', function() { rcmail.managesieve_move() });
38     rcmail.register_command('plugin.managesieve-setadd', function() { rcmail.managesieve_setadd() });
39     rcmail.register_command('plugin.managesieve-setdel', function() { rcmail.managesieve_setdel() });
40     rcmail.register_command('plugin.managesieve-setact', function() { rcmail.managesieve_setact() });
41     rcmail.register_command('plugin.managesieve-setget', function() { rcmail.managesieve_setget() });
42
43     if (rcmail.env.action == 'plugin.managesieve' || rcmail.env.action == 'plugin.managesieve-save') {
44       if (rcmail.gui_objects.sieveform) {
45         rcmail.enable_command('plugin.managesieve-save', true);
46
47         // small resize for header element
48         $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
49           if (this.value == '...') this.style.width = '40px';
50         });
51
52         // resize dialog window
53         if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
54           parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
55         }
56
57         $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
58       }
59       else {
60         rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror);
61       }
62
63       var i, p = rcmail, setcnt, set = rcmail.env.currentset;
64
65       if (rcmail.gui_objects.filterslist) {
66         rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist,
67           {multiselect:false, draggable:true, keyboard:false});
68         rcmail.filters_list.addEventListener('select', function(o){ p.managesieve_select(o); });
69         rcmail.filters_list.addEventListener('dragstart', function(o){ p.managesieve_dragstart(o); });
70         rcmail.filters_list.addEventListener('dragend', function(e){ p.managesieve_dragend(e); });
71         rcmail.filters_list.row_init = function (row) {
72           row.obj.onmouseover = function() { p.managesieve_focus_filter(row); };
73           row.obj.onmouseout = function() { p.managesieve_unfocus_filter(row); };
74         };
75         rcmail.filters_list.init();
76         rcmail.filters_list.focus();
77       }
78
79       if (rcmail.gui_objects.filtersetslist) {
80         rcmail.filtersets_list = new rcube_list_widget(rcmail.gui_objects.filtersetslist, {multiselect:false, draggable:false, keyboard:false});
81         rcmail.filtersets_list.addEventListener('select', function(o){ p.managesieve_setselect(o); });
82         rcmail.filtersets_list.init();
83         rcmail.filtersets_list.focus();
84
85         if (set != null) {
86           set = rcmail.managesieve_setid(set);
87           rcmail.filtersets_list.shift_start = set;
88           rcmail.filtersets_list.highlight_row(set, false);
89         }
90
91         setcnt = rcmail.filtersets_list.rowcount;
92         rcmail.enable_command('plugin.managesieve-set', true);
93         rcmail.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', setcnt);
94         rcmail.enable_command('plugin.managesieve-setdel', setcnt > 1);
95       }
96     }
97     if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled)
98       $('#disabled').attr('checked', true);
99   });
100 };
101
102 /*********************************************************/
103 /*********       Managesieve UI methods          *********/
104 /*********************************************************/
105
106 rcube_webmail.prototype.managesieve_add = function()
107 {
108   this.load_managesieveframe();
109   this.filters_list.clear_selection();
110 };
111
112 rcube_webmail.prototype.managesieve_del = function()
113 {
114   var id = this.filters_list.get_single_selection();
115   if (confirm(this.get_label('managesieve.filterdeleteconfirm'))) {
116     var lock = this.set_busy(true, 'loading');
117     this.http_post('plugin.managesieve',
118       '_act=delete&_fid='+this.filters_list.rows[id].uid, lock);
119   }
120 };
121
122 rcube_webmail.prototype.managesieve_act = function()
123 {
124   var id = this.filters_list.get_single_selection(),
125     lock = this.set_busy(true, 'loading');
126
127   this.http_post('plugin.managesieve',
128     '_act=act&_fid='+this.filters_list.rows[id].uid, lock);
129 };
130
131 // Filter selection
132 rcube_webmail.prototype.managesieve_select = function(list)
133 {
134   var id = list.get_single_selection();
135   if (id != null)
136     this.load_managesieveframe(list.rows[id].uid);
137 };
138
139 // Set selection
140 rcube_webmail.prototype.managesieve_setselect = function(list)
141 {
142   this.show_contentframe(false);
143   this.filters_list.clear(true);
144   this.enable_command('plugin.managesieve-setdel', list.rowcount > 1);
145   this.enable_command( 'plugin.managesieve-setact', 'plugin.managesieve-setget', true);
146
147   var id = list.get_single_selection();
148   if (id != null)
149     this.managesieve_list(this.env.filtersets[id]);
150 };
151
152 rcube_webmail.prototype.managesieve_rowid = function(id)
153 {
154   var i, rows = this.filters_list.rows;
155
156   for (i=0; i<rows.length; i++)
157     if (rows[i] != null && rows[i].uid == id)
158       return i;
159 };
160
161 // Returns set's identifier
162 rcube_webmail.prototype.managesieve_setid = function(name)
163 {
164   for (var i in this.env.filtersets)
165     if (this.env.filtersets[i] == name)
166       return i;
167 };
168
169 // Filters listing request
170 rcube_webmail.prototype.managesieve_list = function(script)
171 {
172   var lock = this.set_busy(true, 'loading');
173
174   this.http_post('plugin.managesieve', '_act=list&_set='+urlencode(script), lock);
175 };
176
177 // Script download request
178 rcube_webmail.prototype.managesieve_setget = function()
179 {
180   var id = this.filtersets_list.get_single_selection(),
181     script = this.env.filtersets[id];
182
183   location.href = this.env.comm_path+'&_action=plugin.managesieve&_act=setget&_set='+urlencode(script);
184 };
185
186 // Set activate/deactivate request
187 rcube_webmail.prototype.managesieve_setact = function()
188 {
189   var id = this.filtersets_list.get_single_selection(),
190    lock = this.set_busy(true, 'loading'),
191     script = this.env.filtersets[id],
192     action = $('#rcmrow'+id).hasClass('disabled') ? 'setact' : 'deact';
193
194   this.http_post('plugin.managesieve', '_act='+action+'&_set='+urlencode(script), lock);
195 };
196
197 // Set delete request
198 rcube_webmail.prototype.managesieve_setdel = function()
199 {
200   if (!confirm(this.get_label('managesieve.setdeleteconfirm')))
201     return false;
202
203   var id = this.filtersets_list.get_single_selection(),
204     lock = this.set_busy(true, 'loading'),
205     script = this.env.filtersets[id];
206
207   this.http_post('plugin.managesieve', '_act=setdel&_set='+urlencode(script), lock);
208 };
209
210 // Set add request
211 rcube_webmail.prototype.managesieve_setadd = function()
212 {
213   this.filters_list.clear_selection();
214   this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', false);
215
216   if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
217     var lock = this.set_busy(true, 'loading');
218     target = window.frames[this.env.contentframe];
219     target.location.href = this.env.comm_path+'&_action=plugin.managesieve&_framed=1&_newset=1&_unlock='+lock;
220   }
221 };
222
223 rcube_webmail.prototype.managesieve_updatelist = function(action, o)
224 {
225   this.set_busy(true);
226
227   switch (action) {
228
229     // Delete filter row
230     case 'del':
231       var i, list = this.filters_list, rows = list.rows;
232
233       list.remove_row(this.managesieve_rowid(o.id));
234       list.clear_selection();
235       this.show_contentframe(false);
236       this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false);
237
238       // re-numbering filters
239       for (i=0; i<rows.length; i++) {
240         if (rows[i] != null && rows[i].uid > o.id)
241           rows[i].uid = rows[i].uid-1;
242       }
243
244       break;
245
246     // Update filter row
247     case 'update':
248       var i, row = $('#rcmrow'+o.id);
249
250       if (o.name)
251         $('td', row).html(o.name);
252       if (o.disabled)
253         row.addClass('disabled');
254       else
255         row.removeClass('disabled');
256
257       $('#disabled', $('iframe').contents()).prop('checked', o.disabled);
258
259       break;
260
261     // Add filter row to the list
262     case 'add':
263       var list = this.filters_list,
264         row = $('<tr><td class="name"></td></tr>');
265
266       $('td', row).html(o.name);
267       row.attr('id', 'rcmrow'+o.id);
268       if (o.disabled)
269         row.addClass('disabled');
270
271       list.insert_row(row.get(0));
272       list.highlight_row(o.id);
273
274       this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', true);
275
276       break;
277
278     // Filling rules list
279     case 'list':
280       var i, tr, td, el, list = this.filters_list;
281
282       if (o.clear)
283         list.clear();
284
285       for (i in o.list) {
286         el = o.list[i];
287         tr = document.createElement('TR');
288         td = document.createElement('TD');
289
290         td.innerHTML = el.name;
291         td.className = 'name';
292         tr.id = 'rcmrow' + el.id;
293         if (el['class'])
294             tr.className = el['class'];
295         tr.appendChild(td);
296
297         list.insert_row(tr);
298       }
299
300       if (o.set)
301         list.highlight_row(o.set);
302       else
303         this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false);
304
305       break;
306
307     // Sactivate/deactivate set
308     case 'setact':
309       var id = this.managesieve_setid(o.name), row = $('#rcmrow' + id);
310       if (o.active) {
311         if (o.all)
312           $('tr', this.gui_objects.filtersetslist).addClass('disabled');
313         row.removeClass('disabled');
314       }
315       else
316         row.addClass('disabled');
317
318       break;
319
320     // Delete set row
321     case 'setdel':
322       var id = this.managesieve_setid(o.name);
323
324       this.filtersets_list.remove_row(id);
325       this.filters_list.clear();
326       this.show_contentframe(false);
327       this.enable_command('plugin.managesieve-setdel', 'plugin.managesieve-setact', 'plugin.managesieve-setget', false);
328
329       delete this.env.filtersets[id];
330
331       break;
332
333     // Create set row
334     case 'setadd':
335       var id = 'S' + new Date().getTime(),
336         list = this.filtersets_list,
337         row = $('<tr class="disabled"><td class="name"></td></tr>');
338
339       $('td', row).html(o.name);
340       row.attr('id', 'rcmrow'+id);
341
342       this.env.filtersets[id] = o.name;
343       list.insert_row(row.get(0));
344
345       // move row into its position on the list
346       if (o.index != list.rowcount-1) {
347         row.detach();
348         var elem = $('tr:visible', list.list).get(o.index);
349         row.insertBefore(elem);
350       }
351
352       list.select(id);
353
354       break;
355   }
356
357   this.set_busy(false);
358 };
359
360 // load filter frame
361 rcube_webmail.prototype.load_managesieveframe = function(id)
362 {
363   var has_id = typeof(id) != 'undefined' && id != null;
364   this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', has_id);
365
366   if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
367     target = window.frames[this.env.contentframe];
368     var msgid = this.set_busy(true, 'loading');
369     target.location.href = this.env.comm_path+'&_action=plugin.managesieve&_framed=1'
370       +(id ? '&_fid='+id : '')+'&_unlock='+msgid;
371   }
372 };
373
374 // load filter frame
375 rcube_webmail.prototype.managesieve_dragstart = function(list)
376 {
377   var id = this.filters_list.get_single_selection();
378
379   this.drag_active = true;
380   this.drag_filter = id;
381 };
382
383 rcube_webmail.prototype.managesieve_dragend = function(e)
384 {
385   if (this.drag_active) {
386     if (this.drag_filter_target) {
387       var lock = this.set_busy(true, 'loading');
388
389       this.show_contentframe(false);
390       this.http_post('plugin.managesieve', '_act=move&_fid='+this.drag_filter
391         +'&_to='+this.drag_filter_target, lock);
392     }
393     this.drag_active = false;
394   }
395 };
396
397 rcube_webmail.prototype.managesieve_focus_filter = function(row)
398 {
399   var id = row.id.replace(/^rcmrow/, '');
400   if (this.drag_active && id != this.drag_filter) {
401     this.drag_filter_target = id;
402     $(row.obj).addClass(id < this.drag_filter ? 'filtermoveup' : 'filtermovedown');
403   }
404 };
405
406 rcube_webmail.prototype.managesieve_unfocus_filter = function(row)
407 {
408   if (this.drag_active) {
409     $(row.obj).removeClass('filtermoveup filtermovedown');
410     this.drag_filter_target = null;
411   }
412 };
413
414 /*********************************************************/
415 /*********          Filter Form methods          *********/
416 /*********************************************************/
417
418 // Form submition
419 rcube_webmail.prototype.managesieve_save = function()
420 {
421   if (parent.rcmail && parent.rcmail.filters_list && this.gui_objects.sieveform.name != 'filtersetform') {
422     var id = parent.rcmail.filters_list.get_single_selection();
423     if (id != null)
424       this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid;
425   }
426   this.gui_objects.sieveform.submit();
427 };
428
429 // Operations on filters form
430 rcube_webmail.prototype.managesieve_ruleadd = function(id)
431 {
432   this.http_post('plugin.managesieve', '_act=ruleadd&_rid='+id);
433 };
434
435 rcube_webmail.prototype.managesieve_rulefill = function(content, id, after)
436 {
437   if (content != '') {
438     // create new element
439     var div = document.getElementById('rules'),
440       row = document.createElement('div');
441
442     this.managesieve_insertrow(div, row, after);
443     // fill row after inserting (for IE)
444     row.setAttribute('id', 'rulerow'+id);
445     row.className = 'rulerow';
446     row.innerHTML = content;
447
448     this.managesieve_formbuttons(div);
449   }
450 };
451
452 rcube_webmail.prototype.managesieve_ruledel = function(id)
453 {
454   if ($('#ruledel'+id).hasClass('disabled'))
455     return;
456
457   if (confirm(this.get_label('managesieve.ruledeleteconfirm'))) {
458     var row = document.getElementById('rulerow'+id);
459     row.parentNode.removeChild(row);
460     this.managesieve_formbuttons(document.getElementById('rules'));
461   }
462 };
463
464 rcube_webmail.prototype.managesieve_actionadd = function(id)
465 {
466   this.http_post('plugin.managesieve', '_act=actionadd&_aid='+id);
467 };
468
469 rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
470 {
471   if (content != '') {
472     var div = document.getElementById('actions'),
473       row = document.createElement('div');
474
475     this.managesieve_insertrow(div, row, after);
476     // fill row after inserting (for IE)
477     row.className = 'actionrow';
478     row.setAttribute('id', 'actionrow'+id);
479     row.innerHTML = content;
480
481     this.managesieve_formbuttons(div);
482   }
483 };
484
485 rcube_webmail.prototype.managesieve_actiondel = function(id)
486 {
487   if ($('#actiondel'+id).hasClass('disabled'))
488     return;
489
490   if (confirm(this.get_label('managesieve.actiondeleteconfirm'))) {
491     var row = document.getElementById('actionrow'+id);
492     row.parentNode.removeChild(row);
493     this.managesieve_formbuttons(document.getElementById('actions'));
494   }
495 };
496
497 // insert rule/action row in specified place on the list
498 rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
499 {
500   for (var i=0; i<div.childNodes.length; i++) {
501     if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow')  + after)
502       break;
503   }
504
505   if (div.childNodes[i+1])
506     div.insertBefore(row, div.childNodes[i+1]);
507   else
508     div.appendChild(row);
509 };
510
511 // update Delete buttons status
512 rcube_webmail.prototype.managesieve_formbuttons = function(div)
513 {
514   var i, button, buttons = [];
515
516   // count and get buttons
517   for (i=0; i<div.childNodes.length; i++) {
518     if (div.id == 'rules' && div.childNodes[i].id) {
519       if (/rulerow/.test(div.childNodes[i].id))
520         buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, ''));
521     }
522     else if (div.childNodes[i].id) {
523       if (/actionrow/.test(div.childNodes[i].id))
524         buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, ''));
525     }
526   }
527
528   for (i=0; i<buttons.length; i++) {
529     button = document.getElementById(buttons[i]);
530     if (i>0 || buttons.length>1) {
531       $(button).removeClass('disabled');
532     }
533     else {
534       $(button).addClass('disabled');
535     }
536   }
537 };
538
539 function rule_header_select(id)
540 {
541   var obj = document.getElementById('header' + id),
542     size = document.getElementById('rule_size' + id),
543     op = document.getElementById('rule_op' + id),
544     target = document.getElementById('rule_target' + id),
545     header = document.getElementById('custom_header' + id),
546     mod = document.getElementById('rule_mod' + id),
547     trans = document.getElementById('rule_trans' + id),
548     comp = document.getElementById('rule_comp' + id);
549
550   if (obj.value == 'size') {
551     size.style.display = 'inline';
552     op.style.display = 'none';
553     target.style.display = 'none';
554     header.style.display = 'none';
555     mod.style.display = 'none';
556     trans.style.display = 'none';
557     comp.style.display = 'none';
558   }
559   else {
560     header.style.display = obj.value != '...' ? 'none' : 'inline';
561     size.style.display = 'none';
562     op.style.display = 'inline';
563     comp.style.display = '';
564     rule_op_select(id);
565     mod.style.display = obj.value == 'body' ? 'none' : 'block';
566     trans.style.display = obj.value == 'body' ? 'block' : 'none';
567   }
568
569   obj.style.width = obj.value == '...' ? '40px' : '';
570 };
571
572 function rule_op_select(id)
573 {
574   var obj = document.getElementById('rule_op' + id),
575     target = document.getElementById('rule_target' + id);
576
577   target.style.display = obj.value == 'exists' || obj.value == 'notexists' ? 'none' : 'inline';
578 };
579
580 function rule_trans_select(id)
581 {
582   var obj = document.getElementById('rule_trans_op' + id),
583     target = document.getElementById('rule_trans_type' + id);
584
585   target.style.display = obj.value != 'content' ? 'none' : 'inline';
586 };
587
588 function rule_mod_select(id)
589 {
590   var obj = document.getElementById('rule_mod_op' + id),
591     target = document.getElementById('rule_mod_type' + id);
592
593   target.style.display = obj.value != 'address' && obj.value != 'envelope' ? 'none' : 'inline';
594 };
595
596 function rule_join_radio(value)
597 {
598   $('#rules').css('display', value == 'any' ? 'none' : 'block');
599 };
600
601 function rule_adv_switch(id, elem)
602 {
603   var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced'+id);
604
605   if (enabled) {
606     adv.hide();
607     elem.removeClass('hide').addClass('show');
608   }
609   else {
610     adv.show();
611     elem.removeClass('show').addClass('hide');
612   }
613 }
614
615 function action_type_select(id)
616 {
617   var obj = document.getElementById('action_type' + id),
618     enabled = {},
619     elems = {
620       mailbox: document.getElementById('action_mailbox' + id),
621       target: document.getElementById('action_target' + id),
622       target_area: document.getElementById('action_target_area' + id),
623       flags: document.getElementById('action_flags' + id),
624       vacation: document.getElementById('action_vacation' + id)
625     };
626
627   if (obj.value == 'fileinto' || obj.value == 'fileinto_copy') {
628     enabled.mailbox = 1;
629   }
630   else if (obj.value == 'redirect' || obj.value == 'redirect_copy') {
631     enabled.target = 1;
632   }
633   else if (obj.value.match(/^reject|ereject$/)) {
634     enabled.target_area = 1;
635   }
636   else if (obj.value.match(/^(add|set|remove)flag$/)) {
637     enabled.flags = 1;
638   }
639   else if (obj.value == 'vacation') {
640     enabled.vacation = 1;
641   }
642
643   for (var x in elems) {
644     elems[x].style.display = !enabled[x] ? 'none' : 'inline';
645   }
646 };
647
648 // Register onmouse(leave/enter) events for tips on specified form element
649 rcube_webmail.prototype.managesieve_tip_register = function(tips)
650 {
651   var n, framed = parent.rcmail,
652     tip = framed ? parent.rcmail.env.ms_tip_layer : rcmail.env.ms_tip_layer;
653
654   for (var n in tips) {
655     $('#'+tips[n][0])
656       .bind('mouseenter', {str: tips[n][1]},
657         function(e) {
658           var offset = $(this).offset(),
659             left = offset.left,
660             top = offset.top - 12;
661
662           if (framed) {
663             offset = $((rcmail.env.task == 'mail'  ? '#sievefilterform > iframe' : '#filter-box'), parent.document).offset();
664             top  += offset.top;
665             left += offset.left;
666           }
667
668           tip.html(e.data.str)
669           top -= tip.height();
670
671           tip.css({left: left, top: top}).show();
672         })
673       .bind('mouseleave', function(e) { tip.hide(); });
674   }
675 };
676
677 /*********************************************************/
678 /*********           Mail UI methods             *********/
679 /*********************************************************/
680
681 rcube_webmail.prototype.managesieve_create = function()
682 {
683   if (!rcmail.env.sieve_headers || !rcmail.env.sieve_headers.length)
684     return;
685
686   var i, html, buttons = {}, dialog = $("#sievefilterform");
687
688   // create dialog window
689   if (!dialog.length) {
690     dialog = $('<div id="sievefilterform"></div>');
691     $('body').append(dialog);
692   }
693
694   // build dialog window content
695   html = '<fieldset><legend>'+this.gettext('managesieve.usedata')+'</legend><ul>';
696   for (i in rcmail.env.sieve_headers)
697     html += '<li><input type="checkbox" name="headers[]" id="sievehdr'+i+'" value="'+i+'" checked="checked" />'
698       +'<label for="sievehdr'+i+'">'+rcmail.env.sieve_headers[i][0]+':</label> '+rcmail.env.sieve_headers[i][1]+'</li>';
699   html += '</ul></fieldset>';
700
701   dialog.html(html);
702
703   // [Next Step] button action
704   buttons[this.gettext('managesieve.nextstep')] = function () {
705     // check if there's at least one checkbox checked
706     var hdrs = $('input[name="headers[]"]:checked', dialog);
707     if (!hdrs.length) {
708       alert(rcmail.gettext('managesieve.nodata'));
709       return;
710     }
711
712     // build frame URL
713     var url = rcmail.get_task_url('mail');
714     url = rcmail.add_url(url, '_action', 'plugin.managesieve');
715     url = rcmail.add_url(url, '_framed', 1);
716
717     hdrs.map(function() {
718       var val = rcmail.env.sieve_headers[this.value];
719       url = rcmail.add_url(url, 'r['+this.value+']', val[0]+':'+val[1]);
720     });
721
722     // load form in the iframe
723     var frame = $('<iframe>').attr({src: url, frameborder: 0})
724     dialog.empty().append(frame).dialog('dialog').resize();
725
726     // Change [Next Step] button with [Save] button
727     buttons = {};
728     buttons[rcmail.gettext('save')] = function() {
729       var win = $('iframe', dialog).get(0).contentWindow;
730       win.rcmail.managesieve_save();
731     };
732     dialog.dialog('option', 'buttons', buttons);
733   };
734
735   // show dialog window
736   dialog.dialog({
737     modal: false,
738     resizable: !bw.ie6,
739     closeOnEscape: (!bw.ie6 && !bw.ie7),  // disable for performance reasons
740     title: this.gettext('managesieve.newfilter'),
741     close: function() { rcmail.managesieve_dialog_close(); },
742     buttons: buttons,
743     minWidth: 600,
744     minHeight: 300,
745     height: 250
746   }).show();
747
748   this.env.managesieve_dialog = dialog;
749 }
750
751 rcube_webmail.prototype.managesieve_dialog_close = function()
752 {
753   var dialog = this.env.managesieve_dialog;
754
755   // BUG(?): if we don't remove the iframe first, it will be reloaded
756   dialog.html('');
757   dialog.dialog('destroy').hide();
758 }
759
760 rcube_webmail.prototype.managesieve_dialog_resize = function(o)
761 {
762   var dialog = this.env.managesieve_dialog,
763     win = $(window), form = $(o);
764     width = form.width(), height = form.height(),
765     w = win.width(), h = win.height();
766
767   dialog.dialog('option', { height: Math.min(h-20, height+120), width: Math.min(w-20, width+65) })
768     .dialog('option', 'position', ['center', 'center']);  // only works in a separate call (!?)
769 }