/**
* (Manage)Sieve Filters plugin
*
* @licstart The following is the entire license notice for the
* JavaScript code in this file.
*
* Copyright (c) 2012-2014, The Roundcube Dev Team
*
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* @licend The above is the entire license notice
* for the JavaScript code in this file.
*/
window.rcmail&&rcmail.addEventListener("init",function(a){"mail"==rcmail.env.task&&("show"!=rcmail.env.action?rcmail.env.message_commands.push("managesieve-create"):rcmail.enable_command("managesieve-create",!0));"mail"!=rcmail.env.task&&!rcmail.env.action.startsWith("plugin.managesieve")||rcmail.env.framed||(rcmail.env.ms_tip_layer=$('
'),rcmail.env.ms_tip_layer.appendTo(document.body));rcmail.register_command("plugin.managesieve-save",function(){rcmail.managesieve_save()});
rcmail.register_command("plugin.managesieve-act",function(){rcmail.managesieve_act()});rcmail.register_command("plugin.managesieve-add",function(){rcmail.managesieve_add()});rcmail.register_command("plugin.managesieve-del",function(){rcmail.managesieve_del()});rcmail.register_command("plugin.managesieve-move",function(){rcmail.managesieve_move()});rcmail.register_command("plugin.managesieve-setadd",function(){rcmail.managesieve_setadd()});rcmail.register_command("plugin.managesieve-setdel",function(){rcmail.managesieve_setdel()});
rcmail.register_command("plugin.managesieve-setact",function(){rcmail.managesieve_setact()});rcmail.register_command("plugin.managesieve-setget",function(){rcmail.managesieve_setget()});rcmail.env.action.startsWith("plugin.managesieve")&&(rcmail.gui_objects.sieveform?(rcmail.enable_command("plugin.managesieve-save",!0),sieve_form_init()):rcmail.enable_command("plugin.managesieve-add","plugin.managesieve-setadd",!rcmail.env.sieveconnerror),a=rcmail.env.currentset,rcmail.gui_objects.filterslist&&(rcmail.filters_list=
new rcube_list_widget(rcmail.gui_objects.filterslist,{multiselect:!1,draggable:!0,keyboard:!0}),rcmail.filters_list.addEventListener("select",function(a){rcmail.managesieve_select(a)}).addEventListener("dragstart",function(a){rcmail.managesieve_dragstart(a)}).addEventListener("dragend",function(a){rcmail.managesieve_dragend(a)}).addEventListener("initrow",function(a){a.obj.onmouseover=function(){rcmail.managesieve_focus_filter(a)};a.obj.onmouseout=function(){rcmail.managesieve_unfocus_filter(a)}}).init()),
rcmail.gui_objects.filtersetslist&&(rcmail.filtersets_list=new rcube_list_widget(rcmail.gui_objects.filtersetslist,{multiselect:!1,draggable:!1,keyboard:!0}),rcmail.filtersets_list.init().focus(),null!=a&&(a=rcmail.managesieve_setid(a),rcmail.filtersets_list.select(a)),rcmail.filtersets_list.addEventListener("select",function(a){rcmail.managesieve_setselect(a)}),a=rcmail.filtersets_list.rowcount,rcmail.enable_command("plugin.managesieve-set",!0),rcmail.enable_command("plugin.managesieve-setact","plugin.managesieve-setget",
a),rcmail.enable_command("plugin.managesieve-setdel",1c&&(this.uid=a-1,$(this).attr("id","rcmrow"+this.uid))}});d.init();
break;case "update":var e;e=$("#rcmrow"+this.managesieve_rowid(b.id));b.name&&$("td",e).text(b.name);b.disabled?e.addClass("disabled"):e.removeClass("disabled");$("#disabled",$("iframe").contents()).prop("checked",b.disabled);break;case "add":d=this.filters_list;e=$(' ');$("td",e).text(b.name);e.attr("id","rcmrow"+b.id);b.disabled&&e.addClass("disabled");d.insert_row(e.get(0));d.highlight_row(b.id);this.enable_command("plugin.managesieve-del","plugin.managesieve-act",
!0);break;case "list":var f,l,g,d=this.filters_list;b.clear&&d.clear();for(e in b.list)g=b.list[e],f=document.createElement("TR"),l=document.createElement("TD"),$(l).text(g.name),l.className="name",f.id="rcmrow"+g.id,g["class"]&&(f.className=g["class"]),f.appendChild(l),d.insert_row(f);b.set?d.highlight_row(b.set):this.enable_command("plugin.managesieve-del","plugin.managesieve-act",!1);break;case "setact":c=this.managesieve_setid(b.name);e=$("#rcmrow"+c);b.active?(b.all&&$("tr",this.gui_objects.filtersetslist).addClass("disabled"),
e.removeClass("disabled")):e.addClass("disabled");break;case "setdel":c=this.managesieve_setid(b.name);this.filtersets_list.remove_row(c);this.filters_list.clear();this.show_contentframe(!1);this.enable_command("plugin.managesieve-setdel","plugin.managesieve-setact","plugin.managesieve-setget",!1);delete this.env.filtersets[c];break;case "setadd":c="S"+(new Date).getTime(),d=this.filtersets_list,e=$(' '),$("td",e).text(b.name),e.attr("id","rcmrow"+c),
this.env.filtersets[c]=b.name,d.insert_row(e.get(0)),b.index!=d.rowcount-1&&(e.detach(),f=$("tr:visible",d.list).get(b.index),e.insertBefore(f)),d.select(c),this.managesieve_fixdragend(e)}this.set_busy(!1)};
rcube_webmail.prototype.load_managesieveframe=function(a){var b="undefined"!=typeof a&&null!=a;this.enable_command("plugin.managesieve-act","plugin.managesieve-del",b);if(this.env.contentframe&&window.frames&&window.frames[this.env.contentframe]){target=window.frames[this.env.contentframe];var c=this.set_busy(!0,"loading");target.location.href=this.env.comm_path+"&_action=plugin.managesieve-action&_framed=1"+(b?"&_fid="+a:"")+"&_unlock="+c}};
rcube_webmail.prototype.managesieve_dragstart=function(a){a=this.filters_list.get_single_selection();this.drag_active=!0;this.drag_filter=a};rcube_webmail.prototype.managesieve_dragend=function(a){this.drag_active&&(this.drag_filter_target&&(a=this.set_busy(!0,"loading"),this.show_contentframe(!1),this.http_post("plugin.managesieve-action","_act=move&_fid="+this.drag_filter+"&_to="+this.drag_filter_target,a)),this.drag_active=!1)};
rcube_webmail.prototype.managesieve_fixdragend=function(a){var b=this;$(a).on("mouseup"+(bw.iphone||bw.ipad?" touchend":""),function(a){b.drag_active&&b.filters_list.drag_mouse_up(a)})};rcube_webmail.prototype.managesieve_focus_filter=function(a){var b=a.id.replace(/^rcmrow/,"");this.drag_active&&b!=this.drag_filter&&(this.drag_filter_target=b,$(a.obj).addClass(b'),d=a.value?a.value.split("\n"):[""];$("#"+b).length||($.each(d,function(b,d){c.append(smart_field_row(d,a.name,b,$(a).data("size")))}),c.attr("id",b),a=$(a),a.attr("disabled")?c.hide():a.prop("disabled",!0),a.after(c),a.hasClass("error")&&(c.addClass("error"),rcmail.managesieve_tip_register([[b,a.data("tip")]])))}
function smart_field_row(a,b,c,d){c=$(' ');a={value:a,name:b+"[]"};d&&(a.size=d);$("input",c).attr(a).keydown(function(a){var b=$(this);if(13==a.which){a=b.attr("name").replace(/\[\]$/,"");var c=(new Date).getTime();a=smart_field_row("",a,c,d);b.parent().after(a);$("input",a).focus()}else if((8==a.which||46==a.which)&&""==b.val()&&(b=b.parent(),1 iframe":"#filter-box",parent.document).offset(),g+=b.top,l+=b.left);d.html(a.data("tip"));g-=d.height();d.css({left:l,top:g,minWidth:m-2+"px"}).show()})};
function sieve_formattime(a,b){var c,d,e="",f=rcmail.env.time_format||"H:i";for(c=0;ca?"0":"")+a;break;case "G":e+=a;break;case "H":e+=(10>a?"0":"")+a;break;case "i":e+=(10>b?"0":"")+b;break;case "s":e+="00";default:e+=d}return e}
function sieve_form_init(){$('select[name="_header[]"]',rcmail.gui_objects.sieveform).each(function(){"..."==this.value&&(this.style.width="40px")});"plugin.managesieve"==rcmail.env.action&&"mail"==rcmail.env.task&&parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);$('input[type="text"]:first',rcmail.gui_objects.sieveform).focus();$('textarea[data-type="list"]',rcmail.gui_objects.sieveform).each(function(){smart_field_init(this)});$.datepicker&&rcmail.env.date_format&&($.datepicker.setDefaults({dateFormat:rcmail.env.date_format,
changeMonth:!0,showOtherMonths:!0,selectOtherMonths:!0,onSelect:function(a){$(this).focus().val(a)}}),$("input.datepicker").datepicker());$("#vacation_timefrom, #vacation_timeto").attr("autocomplete","off").autocomplete({delay:100,minLength:1,source:function(a,b){var c,d=[];for(c=0;24>c;c++)d.push(sieve_formattime(c,0));d.push(sieve_formattime(23,59));return b(d)},open:function(a,b){var c=$(this),d=c.val(),e=c.autocomplete("widget").css("width","10em"),f=c.data("ui-autocomplete").menu;d&&d.length&&
e.children().each(function(){var a=$(this);0==a.text().indexOf(d)&&f._scrollIntoView(a)})},select:function(a,b){$(this).val(b.item.value);return!1}}).click(function(){$(this).autocomplete("search",$(this).val()||" ")})}
rcube_webmail.prototype.managesieve_create=function(a){if(!a&&"show"!=this.env.action){var b=this.message_list.get_single_selection();a=this.set_busy(!0,"loading");this.http_post("plugin.managesieve-action",{_uid:b},a)}else if(this.env.sieve_headers&&this.env.sieve_headers.length){var c={},d=$("#sievefilterform");d.length||(d=$('
'),$("body").append(d));a=""+this.get_label("managesieve.usedata")+" ");c[this.get_label("managesieve.nextstep")]=function(){var a=$('input[name="headers[]"]:checked',d);if(a.length){var b=rcmail.get_task_url("mail"),b=rcmail.add_url(b,"_action","plugin.managesieve"),b=rcmail.add_url(b,"_framed",1);a.map(function(){var a=rcmail.env.sieve_headers[this.value];b=rcmail.add_url(b,"r["+this.value+"]",
a[0]+":"+a[1])});a=$("