1 /* (Manage)Sieve Filters */
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');
11 rcmail.enable_command('managesieve-create', true);
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'))
21 rcmail.add_element(tab, 'tabs');
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);
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() });
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);
47 // small resize for header element
48 $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
49 if (this.value == '...') this.style.width = '40px';
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);
57 $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
60 rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror);
63 var i, p = rcmail, setcnt, set = rcmail.env.currentset;
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); };
75 rcmail.filters_list.init();
76 rcmail.filters_list.focus();
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();
86 set = rcmail.managesieve_setid(set);
87 rcmail.filtersets_list.shift_start = set;
88 rcmail.filtersets_list.highlight_row(set, false);
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);
97 if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled)
98 $('#disabled').attr('checked', true);
102 /*********************************************************/
103 /********* Managesieve UI methods *********/
104 /*********************************************************/
106 rcube_webmail.prototype.managesieve_add = function()
108 this.load_managesieveframe();
109 this.filters_list.clear_selection();
112 rcube_webmail.prototype.managesieve_del = function()
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);
122 rcube_webmail.prototype.managesieve_act = function()
124 var id = this.filters_list.get_single_selection(),
125 lock = this.set_busy(true, 'loading');
127 this.http_post('plugin.managesieve',
128 '_act=act&_fid='+this.filters_list.rows[id].uid, lock);
132 rcube_webmail.prototype.managesieve_select = function(list)
134 var id = list.get_single_selection();
136 this.load_managesieveframe(list.rows[id].uid);
140 rcube_webmail.prototype.managesieve_setselect = function(list)
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);
147 var id = list.get_single_selection();
149 this.managesieve_list(this.env.filtersets[id]);
152 rcube_webmail.prototype.managesieve_rowid = function(id)
154 var i, rows = this.filters_list.rows;
156 for (i=0; i<rows.length; i++)
157 if (rows[i] != null && rows[i].uid == id)
161 // Returns set's identifier
162 rcube_webmail.prototype.managesieve_setid = function(name)
164 for (var i in this.env.filtersets)
165 if (this.env.filtersets[i] == name)
169 // Filters listing request
170 rcube_webmail.prototype.managesieve_list = function(script)
172 var lock = this.set_busy(true, 'loading');
174 this.http_post('plugin.managesieve', '_act=list&_set='+urlencode(script), lock);
177 // Script download request
178 rcube_webmail.prototype.managesieve_setget = function()
180 var id = this.filtersets_list.get_single_selection(),
181 script = this.env.filtersets[id];
183 location.href = this.env.comm_path+'&_action=plugin.managesieve&_act=setget&_set='+urlencode(script);
186 // Set activate/deactivate request
187 rcube_webmail.prototype.managesieve_setact = function()
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';
194 this.http_post('plugin.managesieve', '_act='+action+'&_set='+urlencode(script), lock);
197 // Set delete request
198 rcube_webmail.prototype.managesieve_setdel = function()
200 if (!confirm(this.get_label('managesieve.setdeleteconfirm')))
203 var id = this.filtersets_list.get_single_selection(),
204 lock = this.set_busy(true, 'loading'),
205 script = this.env.filtersets[id];
207 this.http_post('plugin.managesieve', '_act=setdel&_set='+urlencode(script), lock);
211 rcube_webmail.prototype.managesieve_setadd = function()
213 this.filters_list.clear_selection();
214 this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', false);
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;
223 rcube_webmail.prototype.managesieve_updatelist = function(action, o)
231 var i, list = this.filters_list, rows = list.rows;
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);
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;
248 var i, row = $('#rcmrow'+o.id);
251 $('td', row).html(o.name);
253 row.addClass('disabled');
255 row.removeClass('disabled');
257 $('#disabled', $('iframe').contents()).prop('checked', o.disabled);
261 // Add filter row to the list
263 var list = this.filters_list,
264 row = $('<tr><td class="name"></td></tr>');
266 $('td', row).html(o.name);
267 row.attr('id', 'rcmrow'+o.id);
269 row.addClass('disabled');
271 list.insert_row(row.get(0));
272 list.highlight_row(o.id);
274 this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', true);
278 // Filling rules list
280 var i, tr, td, el, list = this.filters_list;
287 tr = document.createElement('TR');
288 td = document.createElement('TD');
290 td.innerHTML = el.name;
291 td.className = 'name';
292 tr.id = 'rcmrow' + el.id;
294 tr.className = el['class'];
301 list.highlight_row(o.set);
303 this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false);
307 // Sactivate/deactivate set
309 var id = this.managesieve_setid(o.name), row = $('#rcmrow' + id);
312 $('tr', this.gui_objects.filtersetslist).addClass('disabled');
313 row.removeClass('disabled');
316 row.addClass('disabled');
322 var id = this.managesieve_setid(o.name);
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);
329 delete this.env.filtersets[id];
335 var id = 'S' + new Date().getTime(),
336 list = this.filtersets_list,
337 row = $('<tr class="disabled"><td class="name"></td></tr>');
339 $('td', row).html(o.name);
340 row.attr('id', 'rcmrow'+id);
342 this.env.filtersets[id] = o.name;
343 list.insert_row(row.get(0));
345 // move row into its position on the list
346 if (o.index != list.rowcount-1) {
348 var elem = $('tr:visible', list.list).get(o.index);
349 row.insertBefore(elem);
357 this.set_busy(false);
361 rcube_webmail.prototype.load_managesieveframe = function(id)
363 var has_id = typeof(id) != 'undefined' && id != null;
364 this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', has_id);
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;
375 rcube_webmail.prototype.managesieve_dragstart = function(list)
377 var id = this.filters_list.get_single_selection();
379 this.drag_active = true;
380 this.drag_filter = id;
383 rcube_webmail.prototype.managesieve_dragend = function(e)
385 if (this.drag_active) {
386 if (this.drag_filter_target) {
387 var lock = this.set_busy(true, 'loading');
389 this.show_contentframe(false);
390 this.http_post('plugin.managesieve', '_act=move&_fid='+this.drag_filter
391 +'&_to='+this.drag_filter_target, lock);
393 this.drag_active = false;
397 rcube_webmail.prototype.managesieve_focus_filter = function(row)
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');
406 rcube_webmail.prototype.managesieve_unfocus_filter = function(row)
408 if (this.drag_active) {
409 $(row.obj).removeClass('filtermoveup filtermovedown');
410 this.drag_filter_target = null;
414 /*********************************************************/
415 /********* Filter Form methods *********/
416 /*********************************************************/
419 rcube_webmail.prototype.managesieve_save = function()
421 if (parent.rcmail && parent.rcmail.filters_list && this.gui_objects.sieveform.name != 'filtersetform') {
422 var id = parent.rcmail.filters_list.get_single_selection();
424 this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid;
426 this.gui_objects.sieveform.submit();
429 // Operations on filters form
430 rcube_webmail.prototype.managesieve_ruleadd = function(id)
432 this.http_post('plugin.managesieve', '_act=ruleadd&_rid='+id);
435 rcube_webmail.prototype.managesieve_rulefill = function(content, id, after)
438 // create new element
439 var div = document.getElementById('rules'),
440 row = document.createElement('div');
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;
448 this.managesieve_formbuttons(div);
452 rcube_webmail.prototype.managesieve_ruledel = function(id)
454 if ($('#ruledel'+id).hasClass('disabled'))
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'));
464 rcube_webmail.prototype.managesieve_actionadd = function(id)
466 this.http_post('plugin.managesieve', '_act=actionadd&_aid='+id);
469 rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
472 var div = document.getElementById('actions'),
473 row = document.createElement('div');
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;
481 this.managesieve_formbuttons(div);
485 rcube_webmail.prototype.managesieve_actiondel = function(id)
487 if ($('#actiondel'+id).hasClass('disabled'))
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'));
497 // insert rule/action row in specified place on the list
498 rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
500 for (var i=0; i<div.childNodes.length; i++) {
501 if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow') + after)
505 if (div.childNodes[i+1])
506 div.insertBefore(row, div.childNodes[i+1]);
508 div.appendChild(row);
511 // update Delete buttons status
512 rcube_webmail.prototype.managesieve_formbuttons = function(div)
514 var i, button, buttons = [];
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/, ''));
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/, ''));
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');
534 $(button).addClass('disabled');
539 function rule_header_select(id)
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);
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';
560 header.style.display = obj.value != '...' ? 'none' : 'inline';
561 size.style.display = 'none';
562 op.style.display = 'inline';
563 comp.style.display = '';
565 mod.style.display = obj.value == 'body' ? 'none' : 'block';
566 trans.style.display = obj.value == 'body' ? 'block' : 'none';
569 obj.style.width = obj.value == '...' ? '40px' : '';
572 function rule_op_select(id)
574 var obj = document.getElementById('rule_op' + id),
575 target = document.getElementById('rule_target' + id);
577 target.style.display = obj.value == 'exists' || obj.value == 'notexists' ? 'none' : 'inline';
580 function rule_trans_select(id)
582 var obj = document.getElementById('rule_trans_op' + id),
583 target = document.getElementById('rule_trans_type' + id);
585 target.style.display = obj.value != 'content' ? 'none' : 'inline';
588 function rule_mod_select(id)
590 var obj = document.getElementById('rule_mod_op' + id),
591 target = document.getElementById('rule_mod_type' + id);
593 target.style.display = obj.value != 'address' && obj.value != 'envelope' ? 'none' : 'inline';
596 function rule_join_radio(value)
598 $('#rules').css('display', value == 'any' ? 'none' : 'block');
601 function rule_adv_switch(id, elem)
603 var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced'+id);
607 elem.removeClass('hide').addClass('show');
611 elem.removeClass('show').addClass('hide');
615 function action_type_select(id)
617 var obj = document.getElementById('action_type' + id),
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)
627 if (obj.value == 'fileinto' || obj.value == 'fileinto_copy') {
630 else if (obj.value == 'redirect' || obj.value == 'redirect_copy') {
633 else if (obj.value.match(/^reject|ereject$/)) {
634 enabled.target_area = 1;
636 else if (obj.value.match(/^(add|set|remove)flag$/)) {
639 else if (obj.value == 'vacation') {
640 enabled.vacation = 1;
643 for (var x in elems) {
644 elems[x].style.display = !enabled[x] ? 'none' : 'inline';
648 // Register onmouse(leave/enter) events for tips on specified form element
649 rcube_webmail.prototype.managesieve_tip_register = function(tips)
651 var n, framed = parent.rcmail,
652 tip = framed ? parent.rcmail.env.ms_tip_layer : rcmail.env.ms_tip_layer;
654 for (var n in tips) {
656 .bind('mouseenter', {str: tips[n][1]},
658 var offset = $(this).offset(),
660 top = offset.top - 12;
663 offset = $((rcmail.env.task == 'mail' ? '#sievefilterform > iframe' : '#filter-box'), parent.document).offset();
671 tip.css({left: left, top: top}).show();
673 .bind('mouseleave', function(e) { tip.hide(); });
677 /*********************************************************/
678 /********* Mail UI methods *********/
679 /*********************************************************/
681 rcube_webmail.prototype.managesieve_create = function()
683 if (!rcmail.env.sieve_headers || !rcmail.env.sieve_headers.length)
686 var i, html, buttons = {}, dialog = $("#sievefilterform");
688 // create dialog window
689 if (!dialog.length) {
690 dialog = $('<div id="sievefilterform"></div>');
691 $('body').append(dialog);
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>';
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);
708 alert(rcmail.gettext('managesieve.nodata'));
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);
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]);
722 // load form in the iframe
723 var frame = $('<iframe>').attr({src: url, frameborder: 0})
724 dialog.empty().append(frame).dialog('dialog').resize();
726 // Change [Next Step] button with [Save] button
728 buttons[rcmail.gettext('save')] = function() {
729 var win = $('iframe', dialog).get(0).contentWindow;
730 win.rcmail.managesieve_save();
732 dialog.dialog('option', 'buttons', buttons);
735 // show dialog window
739 closeOnEscape: (!bw.ie6 && !bw.ie7), // disable for performance reasons
740 title: this.gettext('managesieve.newfilter'),
741 close: function() { rcmail.managesieve_dialog_close(); },
748 this.env.managesieve_dialog = dialog;
751 rcube_webmail.prototype.managesieve_dialog_close = function()
753 var dialog = this.env.managesieve_dialog;
755 // BUG(?): if we don't remove the iframe first, it will be reloaded
757 dialog.dialog('destroy').hide();
760 rcube_webmail.prototype.managesieve_dialog_resize = function(o)
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();
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 (!?)