').attr({'class': 'rulerow', id: 'rulerow'+id})
.html(content);
this.managesieve_insertrow(div, row, after);
// initialize smart list inputs
$('textarea[data-type="list"]', row).each(function() {
smart_field_init(this);
});
this.managesieve_formbuttons(div);
}
};
rcube_webmail.prototype.managesieve_ruledel = function(id)
{
if ($('#ruledel'+id).hasClass('disabled'))
return;
this.confirm_dialog(this.get_label('managesieve.ruledeleteconfirm'), 'delete', function(e, ref) {
var row = document.getElementById('rulerow'+id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('rules'));
});
};
rcube_webmail.prototype.managesieve_actionadd = function(id)
{
this.http_post('plugin.managesieve-action', '_act=actionadd&_aid='+id);
};
rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
{
if (content != '') {
var div = $('#actions')[0],
row = $('
').attr({'class': 'actionrow', id: 'actionrow'+id})
.html(content);
this.managesieve_insertrow(div, row, after);
// initialize smart list inputs
$('textarea[data-type="list"]', row).each(function() {
smart_field_init(this);
});
this.managesieve_formbuttons(div);
}
};
rcube_webmail.prototype.managesieve_actiondel = function(id)
{
if ($('#actiondel'+id).hasClass('disabled'))
return;
this.confirm_dialog(this.get_label('managesieve.actiondeleteconfirm'), 'delete', function(e, ref) {
var row = document.getElementById('actionrow'+id);
row.parentNode.removeChild(row);
ref.managesieve_formbuttons(document.getElementById('actions'));
});
};
// insert rule/action row in specified place on the list
rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
{
var node = $('#' + ($(div).attr('id') == 'rules' ? 'rulerow' : 'actionrow') + after)[0];
if (node)
$(row).insertAfter(node);
else
$(div).append(row);
this.triggerEvent('managesieve.insertrow', {obj: row});
};
// update Delete buttons status
rcube_webmail.prototype.managesieve_formbuttons = function(div)
{
var buttons = $('a.delete', div);
buttons.removeClass('disabled');
if (buttons.length == 1)
buttons.addClass('disabled');
};
// update vacation addresses field with user identities
rcube_webmail.prototype.managesieve_vacation_addresses = function(id)
{
var lock = this.set_busy(true, 'loading');
this.http_post('plugin.managesieve-action', {_act: 'addresses', _aid: id}, lock);
};
// update vacation addresses field with user identities
rcube_webmail.prototype.managesieve_vacation_addresses_update = function(id, addresses)
{
var field = $('#vacation_addresses,#action_addresses' + (id || ''));
smart_field_reset(field.get(0), addresses);
};
function rule_header_select(id)
{
var is_header,
obj = document.getElementById('header' + id),
size = document.getElementById('rule_size' + id),
spamtest = document.getElementById('rule_spamtest' + id),
msg = document.getElementById('rule_message' + id),
op = document.getElementById('rule_op' + id),
header = document.getElementById('custom_header' + id + '_list'),
custstr = document.getElementById('custom_var' + id + '_list'),
mod = document.getElementById('rule_mod' + id),
trans = document.getElementById('rule_trans' + id),
comp = document.getElementById('rule_comp' + id),
mime = document.getElementById('rule_mime' + id),
mime_part = document.getElementById('rule_mime_part' + id),
datepart = document.getElementById('rule_date_part' + id),
dateheader = document.getElementById('rule_date_header_div' + id),
rule = $('#rule_op' + id),
h = obj.value,
set = [op, header, custstr, mod, trans, comp, size, mime, mime_part];
if (h == 'size') {
if (msg) set.push(msg);
$.each(set, function() { if (this != window) this.style.display = 'none'; });
if (spamtest)
spamtest.style.display = 'none';
size.style.display = '';
}
else if (h == 'spamtest') {
if (msg) set.push(msg);
$.each(set, function() { if (this != window) this.style.display = 'none'; });
if (spamtest)
spamtest.style.display = '';
size.style.display = 'none';
}
else if (h == 'message' && msg) {
$.each(set, function() { if (this != window) this.style.display = 'none'; });
msg.style.display = '';
}
else {
is_header = h != 'body' && h != 'currentdate' && h != 'date' && h != 'string';
header.style.display = h != '...' ? 'none' : '';
custstr.style.display = h != 'string' ? 'none' : '';
size.style.display = 'none';
op.style.display = '';
comp.style.display = '';
mod.style.display = is_header ? '' : 'none';
trans.style.display = h == 'body' ? '' : 'none';
if (spamtest)
spamtest.style.display = 'none';
if (mime)
mime.style.display = is_header ? '' : 'none';
if (mime_part)
mime_part.style.display = is_header ? '' : 'none';
if (msg)
msg.style.display = h == 'message' ? '' : 'none';
}
if (datepart)
datepart.style.display = h == 'currentdate' || h == 'date' ? 'inline' : 'none';
if (dateheader)
dateheader.style.display = h == 'date' ? '' : 'none';
$('[value="exists"],[value="notexists"]', rule).prop('disabled', h == 'string');
if (!rule.val())
rule.val('contains');
rule_op_select(op, id, h);
rule_mod_select(id, h, !is_header);
rule_mime_select(id);
if (spamtest)
rule_spamtest_select(id);
obj.style.width = h == '...' ? '40px' : '';
};
function rule_op_select(obj, id, header)
{
var target = document.getElementById('rule_target' + id + '_list');
if (!header)
header = document.getElementById('header' + id).value;
target.style.display = obj.value.match(/^(exists|notexists)$/) || header.match(/^(size|spamtest|message)$/) ? 'none' : '';
};
function rule_trans_select(id)
{
var obj = document.getElementById('rule_trans_op' + id),
target = document.getElementById('rule_trans_type' + id);
target.style.display = obj.value != 'content' ? 'none' : 'inline';
};
function rule_mod_select(id, header, reset)
{
var obj = document.getElementById('rule_mod_op' + id),
target = document.getElementById('rule_mod_type' + id),
duplicate = document.getElementById('rule_duplicate_div' + id),
index = document.getElementById('rule_index_div' + id);
if (reset)
obj.value = '';
if (!header)
header = document.getElementById('header' + id).value;
target.style.display = obj.value != 'address' && obj.value != 'envelope' ? 'none' : '';
if (index)
index.style.display = !header.match(/^(body|currentdate|size|spamtest|message|string)$/) && obj.value != 'envelope' ? '' : 'none';
if (duplicate)
duplicate.style.display = header == 'message' ? '' : 'none';
};
function rule_spamtest_select(id)
{
var obj = document.getElementById('rule_spamtest_op' + id),
target = document.getElementById('rule_spamtest_target' + id);
target.style.display = obj.value ? '' : 'none';
$(obj)[obj.value ? 'removeClass' : 'addClass']('rounded-right');
};
function rule_join_radio(value)
{
$('#rules').css('display', value == 'any' ? 'none' : 'block');
};
function rule_adv_switch(id, elem)
{
var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced'+id);
if (enabled) {
adv.get(0).style.display = 'none';
elem.removeClass('hide').addClass('show');
}
else {
adv.get(0).style.display = '';
elem.removeClass('show').addClass('hide');
}
};
function rule_mime_select(id)
{
var elem = $('#rule_mime_type' + id),
param_elem = $('#rule_mime_param' + id + '_list');
if (param_elem.length)
param_elem[0].style.display = elem.val() == 'param' ? '' : 'none';
};
function action_type_select(id)
{
var obj = document.getElementById('action_type' + id),
v = obj.value, enabled = {},
elems = {
mailbox: document.getElementById('action_mailbox' + id),
target: document.getElementById('redirect_target' + id),
target_area: document.getElementById('action_target_area' + id),
flags: document.getElementById('action_flags' + id),
vacation: document.getElementById('action_vacation' + id),
forward: document.getElementById('action_forward' + id),
set: document.getElementById('action_set' + id),
notify: document.getElementById('action_notify' + id),
addheader: document.getElementById('action_addheader' + id),
deleteheader: document.getElementById('action_deleteheader' + id)
};
if (v == 'fileinto' || v == 'fileinto_copy') {
enabled.mailbox = 1;
}
else if (v == 'redirect' || v == 'redirect_copy') {
enabled.target = 1;
}
else if (v.match(/^reject|ereject$/)) {
enabled.target_area = 1;
}
else if (v.match(/^(add|set|remove)flag$/)) {
enabled.flags = 1;
}
else if (v.match(/^(vacation|forward|set|notify|addheader|deleteheader)$/)) {
enabled[v] = 1;
}
for (var x in elems) {
if (elems[x])
elems[x].style.display = !enabled[x] ? 'none' : '';
}
};
function vacation_action_select()
{
var selected = $('#vacation_action').val();
$('#action_target_span')[selected == 'discard' || selected == 'keep' ? 'hide' : 'show']();
};
// Initializes smart list input
function smart_field_init(field)
{
if (window.UI && UI.smart_field_init)
return UI.smart_field_init(field);
var id = field.id + '_list',
area = $('
'),
list = field.value ? field.value.split("\n") : [''];
if ($('#'+id).length)
return;
// add input rows
$.each(list, function(i, v) {
area.append(smart_field_row(v, i, field));
});
area.attr('id', id);
field = $(field);
if (field.attr('disabled'))
area.hide();
// disable the original field anyway, we don't want it in POST
else
field.prop('disabled', true);
if (field.data('hidden'))
area.hide();
field.after(area);
if (field.hasClass('error')) {
area.addClass('error');
rcmail.managesieve_tip_register([[id, field.data('tip-class'), field.data('tip-msg')]]);
}
};
function smart_field_row(value, idx, field)
{
// build row element content
var input, content = '
'
+ '',
elem = $(content),
attrs = {
value: value,
name: field.name + '[]',
size: $(field).data('size'),
title: field.title,
placeholder: $(field).attr('placeholder')
};
input = elem.find('input').attr(attrs).keydown(function(e) {
var input = $(this);
// element creation event (on Enter)
if (e.which == 13) {
var elem = smart_field_row('', (new Date()).getTime(), field);
input.parent().after(elem);
$('input', elem).focus();
}
// backspace or delete: remove input, focus previous one
else if ((e.which == 8 || e.which == 46) && input.val() == '') {
var parent = input.parent(), siblings = parent.parent().children();
if (siblings.length > 1) {
if (parent.prev().length)
parent.prev().children('input').focus();
else
parent.next().children('input').focus();
parent.remove();
return false;
}
}
});
// element deletion event
$('span[class="reset"]', elem).click(function() {
var span = $(this.parentNode);
if (span.parent().children().length > 1)
span.remove();
else
$('input', span).val('').focus();
});
return elem;
}
// Reset and fill the smart list input with new data
function smart_field_reset(field, data)
{
if (window.UI && UI.smart_field_reset)
return UI.smart_field_reset(field, data);
var id = field.id + '_list',
list = data.length ? data : [''];
area = $('#' + id);
area.empty();
// add input rows
$.each(list, function(i, v) {
area.append(smart_field_row(v, i, field));
});
}
// Register onmouse(leave/enter) events for tips on specified form element
rcube_webmail.prototype.managesieve_tip_register = function(tips)
{
if (window.UI && UI.form_errors)
return UI.form_errors(tips);
var n, framed = parent.rcmail,
tip = framed ? parent.rcmail.env.ms_tip_layer : rcmail.env.ms_tip_layer;
for (n in tips) {
$('#'+tips[n][0])
.data('tip-class', tips[n][1])
.data('tip-msg', tips[n][2])
.mouseleave(function(e) { tip.hide(); })
.mouseenter(function(e) {
var elem = $(this),
offset = elem.offset(),
left = offset.left,
top = offset.top - 12,
minwidth = elem.width(),
span = $('
').addClass(elem.data('tip-class')).text(elem.data('tip-msg'));
if (framed) {
offset = $((rcmail.env.task == 'mail' ? '#sievefilterform > iframe' : '#filter-box'), parent.document).offset();
top += offset.top;
left += offset.left;
}
tip.html('').append(span);
top -= tip.height();
tip.css({left: left, top: top, minWidth: (minwidth-2) + 'px'}).show();
});
}
};
// format time string
function sieve_formattime(hour, minutes)
{
var i, c, h, time = '', format = rcmail.env.time_format || 'H:i';
for (i=0; i= 12 ? 'pm' : 'am'; break;
case 'A': time += hour >= 12 ? 'PM' : 'AM'; break;
case 'g':
case 'h':
h = hour == 0 ? 12 : hour > 12 ? hour - 12 : hour;
time += (c == 'h' && hour < 10 ? '0' : '') + hour;
break;
case 'G': time += hour; break;
case 'H': time += (hour < 10 ? '0' : '') + hour; break;
case 'i': time += (minutes < 10 ? '0' : '') + minutes; break;
case 's': time += '00';
default: time += c;
}
}
return time;
}
function sieve_form_init()
{
var form = rcmail.gui_objects.sieveform;
// resize dialog window
if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
parent.rcmail.managesieve_dialog_resize(form);
}
$('input[type="text"]', form).first().focus();
// initialize smart list inputs
$('textarea[data-type="list"]', form).each(function() {
smart_field_init(this);
});
// initialize rules form(s)
$('[name^="_header"]', form).each(function() {
if (/([0-9]+)$/.test(this.id)) {
rule_header_select(RegExp.$1);
}
});
// enable date pickers on date fields
if ($.datepicker && rcmail.env.date_format) {
$.datepicker.setDefaults({
dateFormat: rcmail.env.date_format,
changeMonth: true,
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function(dateText) { $(this).focus().val(dateText); }
});
$('input.datepicker').datepicker();
}
// configure drop-down menu on time input fields based on jquery UI autocomplete
$('#vacation_timefrom, #vacation_timeto')
.attr('autocomplete', "off")
.autocomplete({
delay: 100,
minLength: 1,
source: function(p, callback) {
var h, result = [];
for (h = 0; h < 24; h++)
result.push(sieve_formattime(h, 0));
result.push(sieve_formattime(23, 59));
return callback(result);
},
open: function(event, ui) {
// scroll to current time
var $this = $(this), val = $this.val(),
widget = $this.autocomplete('widget').css('width', '10em'),
menu = $this.data('ui-autocomplete').menu;
if (val && val.length)
widget.children().each(function() {
var li = $(this);
if (li.text().indexOf(val) == 0)
menu._scrollIntoView(li);
});
},
select: function(event, ui) {
$(this).val(ui.item.value);
return false;
}
})
.click(function() { // show drop-down upon clicks
$(this).autocomplete('search', $(this).val() || ' ');
})
// display advanced controls when contain errors
$('input.error').each(function() {
if (String(this.id).match(/([0-9]+)$/)) {
$('#ruleadv' + RegExp.$1 + '.show').click();
}
});
}
/*********************************************************/
/********* RAW editor methods *********/
/*********************************************************/
var cmeditor;
function cmCreateErrorElem(msg)
{
var marker = document.createElement("div");
marker.style.color = "#822";
marker.innerHTML = "●";
marker.title = msg;
return marker;
}
function cmScrollToError()
{
var line = $('.CodeMirror-lines .line-error'),
scroll = $('.CodeMirror-scroll'),
h = line.parent();
scroll.scrollTop(line.offset().top - scroll.offset().top - Math.round(scroll.height()/2));
}
function sieve_raw_editor_init()
{
var textArea = document.getElementById('rawfiltersettxt');
if (textArea && !cmeditor) {
cmeditor = CodeMirror.fromTextArea(textArea, {
mode: 'sieve',
lineNumbers: true,
gutters: ["CodeMirror-linenumbers", "errorGutter"],
styleActiveLine: true
});
// fetching errors from environment and setting the line background
// and a gutter element with the error message accordingly
$.each(rcmail.env.sieve_errors || [], function(i, err) {
var lineNo = Number(err.line) - 1;
cmeditor.addLineClass(lineNo, 'background', 'line-error');
cmeditor.setGutterMarker(lineNo, 'errorGutter', cmCreateErrorElem(err.msg));
if (!i) cmScrollToError();
});
}
}
/*********************************************************/
/********* Mail UI methods *********/
/*********************************************************/
rcube_webmail.prototype.managesieve_create = function(force)
{
if (!force && this.env.action != 'show') {
var uid = this.message_list.get_single_selection(),
lock = this.set_busy(true, 'loading');
this.http_post('plugin.managesieve-action', {_uid: uid}, lock);
return;
}
if (!this.env.sieve_headers || !this.env.sieve_headers.length)
return;
var i, buttons = {},
title = this.get_label('managesieve.newfilter'),
dialog = $(''),
props = {minWidth: 600, minHeight: 250, height: 300};
// build dialog window content
dialog.append($('