1 /* Sieve Filters (tab) */
4 rcmail.addEventListener('init', function(evt) {
5 // <span id="settingstabdefault" class="tablink"><roundcube:button command="preferences" type="link" label="preferences" title="editpreferences" /></span>
6 var tab = $('<span>').attr('id', 'settingstabpluginmanagesieve').addClass('tablink');
8 var button = $('<a>').attr('href', rcmail.env.comm_path+'&_action=plugin.managesieve')
9 .attr('title', rcmail.gettext('managesieve.managefilters'))
10 .html(rcmail.gettext('managesieve.filters'))
11 .bind('click', function(e){ return rcmail.command('plugin.managesieve', this) })
14 // add button and register commands
15 rcmail.add_element(tab, 'tabs');
16 rcmail.register_command('plugin.managesieve', function() { rcmail.goto_url('plugin.managesieve') }, true);
17 rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() }, true);
18 rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() }, true);
19 rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() }, true);
20 rcmail.register_command('plugin.managesieve-up', function() { rcmail.managesieve_up() }, true);
21 rcmail.register_command('plugin.managesieve-down', function() { rcmail.managesieve_down() }, true);
23 if (rcmail.env.action == 'plugin.managesieve')
25 if (rcmail.gui_objects.sieveform)
26 rcmail.enable_command('plugin.managesieve-save', true);
28 rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false);
29 rcmail.enable_command('plugin.managesieve-add', !rcmail.env.sieveconnerror);
32 if (rcmail.gui_objects.filterslist) {
34 rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist, {multiselect:false, draggable:false, keyboard:false});
35 rcmail.filters_list.addEventListener('select', function(o){ p.managesieve_select(o); });
36 rcmail.filters_list.init();
37 rcmail.filters_list.focus();
42 /*********************************************************/
43 /********* Managesieve filters methods *********/
44 /*********************************************************/
46 rcube_webmail.prototype.managesieve_add = function()
48 this.load_managesieveframe();
49 this.filters_list.clear_selection();
52 rcube_webmail.prototype.managesieve_del = function()
54 var id = this.filters_list.get_single_selection();
56 if (confirm(this.get_label('managesieve.filterconfirmdelete')))
57 this.http_request('plugin.managesieve',
58 '_act=delete&_fid='+this.filters_list.rows[id].uid, true);
61 rcube_webmail.prototype.managesieve_up = function()
63 var id = this.filters_list.get_single_selection();
64 this.http_request('plugin.managesieve',
65 '_act=up&_fid='+this.filters_list.rows[id].uid, true);
68 rcube_webmail.prototype.managesieve_down = function()
70 var id = this.filters_list.get_single_selection();
71 this.http_request('plugin.managesieve',
72 '_act=down&_fid='+this.filters_list.rows[id].uid, true);
75 rcube_webmail.prototype.managesieve_rowid = function(id)
77 var rows = this.filters_list.rows;
79 for (var i=0; i<rows.length; i++)
80 if (rows[i] != null && rows[i].uid == id)
84 rcube_webmail.prototype.managesieve_updatelist = function(action, name, id)
91 this.filters_list.remove_row(this.managesieve_rowid(id));
92 this.filters_list.clear_selection();
93 this.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false);
94 this.show_contentframe(false);
96 // re-numbering filters
97 var rows = this.filters_list.rows;
98 for (var i=0; i<rows.length; i++)
100 if (rows[i] != null && rows[i].uid > id)
101 rows[i].uid = rows[i].uid-1;
106 var rows = this.filters_list.rows;
109 // we need only to replace filter names...
110 for (var i=0; i<rows.length; i++)
112 if (rows[i]==null) { // removed row
114 } else if (rows[i].uid == id) {
115 from = rows[i].obj.cells[0];
116 } else if (rows[i].uid == id+1){
117 name = rows[i].obj.cells[0].innerHTML;
118 rows[i].obj.cells[0].innerHTML = from.innerHTML;
119 from.innerHTML = name;
120 this.filters_list.highlight_row(i);
124 // ... and disable/enable Down button
125 this.filters_listbuttons();
129 var rows = this.filters_list.rows;
132 // we need only to replace filter names...
133 for (var i=0; i<rows.length; i++)
135 if (rows[i]==null) { // removed row
137 } else if (rows[i].uid == id-1) {
138 from = rows[i].obj.cells[0];
139 this.filters_list.highlight_row(i);
140 } else if (rows[i].uid == id) {
141 name = rows[i].obj.cells[0].innerHTML;
142 rows[i].obj.cells[0].innerHTML = from.innerHTML;
143 from.innerHTML = name;
147 // ... and disable/enable Up button
148 this.filters_listbuttons();
152 var rows = parent.rcmail.filters_list.rows;
153 for (var i=0; i<rows.length; i++)
154 if (rows[i] && rows[i].uid == id)
156 rows[i].obj.cells[0].innerHTML = name;
162 var row, new_row, td;
163 var list = parent.rcmail.filters_list;
168 for (var i=0; i<list.rows.length; i++)
169 if (list.rows[i] != null && String(list.rows[i].obj.id).match(/^rcmrow/))
170 row = list.rows[i].obj;
174 new_row = parent.document.createElement('tr');
175 new_row.id = 'rcmrow'+id;
176 td = parent.document.createElement('td');
177 new_row.appendChild(td);
178 list.insert_row(new_row, false);
180 if (row.cells[0].className)
181 td.className = row.cells[0].className;
184 list.highlight_row(id);
186 parent.rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', true);
188 else // refresh whole page
189 parent.rcmail.goto_url('plugin.managesieve');
193 this.set_busy(false);
197 rcube_webmail.prototype.managesieve_select = function(list)
199 var id = list.get_single_selection();
201 this.load_managesieveframe(list.rows[id].uid);
204 rcube_webmail.prototype.managesieve_save = function()
206 if (parent.rcmail && parent.rcmail.filters_list)
208 var id = parent.rcmail.filters_list.get_single_selection();
210 this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid;
212 this.gui_objects.sieveform.submit();
216 rcube_webmail.prototype.load_managesieveframe = function(id)
218 if (typeof(id) != 'undefined' && id != null)
220 this.enable_command('plugin.managesieve-del', true);
221 this.filters_listbuttons();
224 this.enable_command('plugin.managesieve-up', 'plugin.managesieve-down', 'plugin.managesieve-del', false);
226 if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
228 target = window.frames[this.env.contentframe];
229 this.set_busy(true, 'loading');
230 target.location.href = this.env.comm_path+'&_action=plugin.managesieve&_framed=1&_fid='+id;
234 // enable/disable Up/Down buttons
235 rcube_webmail.prototype.filters_listbuttons = function()
237 var id = this.filters_list.get_single_selection();
238 var rows = this.filters_list.rows;
240 for (var i=0; i<rows.length; i++)
242 if (rows[i] == null) { // removed row
243 } else if (i == id) {
244 this.enable_command('plugin.managesieve-up', false);
247 this.enable_command('plugin.managesieve-up', true);
252 for (var i=rows.length-1; i>0; i--)
254 if (rows[i] == null) { // removed row
255 } else if (i == id) {
256 this.enable_command('plugin.managesieve-down', false);
259 this.enable_command('plugin.managesieve-down', true);
265 // operations on filters form
266 rcube_webmail.prototype.managesieve_ruleadd = function(id)
268 this.http_post('plugin.managesieve', '_act=ruleadd&_rid='+id);
271 rcube_webmail.prototype.managesieve_rulefill = function(content, id, after)
275 // create new element
276 var div = document.getElementById('rules');
277 var row = document.createElement('div');
279 this.managesieve_insertrow(div, row, after);
280 // fill row after inserting (for IE)
281 row.setAttribute('id', 'rulerow'+id);
282 row.className = 'rulerow';
283 row.innerHTML = content;
285 this.managesieve_formbuttons(div);
289 rcube_webmail.prototype.managesieve_ruledel = function(id)
291 if (confirm(this.get_label('managesieve.ruledeleteconfirm')))
293 var row = document.getElementById('rulerow'+id);
294 row.parentNode.removeChild(row);
295 this.managesieve_formbuttons(document.getElementById('rules'));
299 rcube_webmail.prototype.managesieve_actionadd = function(id)
301 this.http_post('plugin.managesieve', '_act=actionadd&_aid='+id);
304 rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
308 var div = document.getElementById('actions');
309 var row = document.createElement('div');
311 this.managesieve_insertrow(div, row, after);
312 // fill row after inserting (for IE)
313 row.className = 'actionrow';
314 row.setAttribute('id', 'actionrow'+id);
315 row.innerHTML = content;
317 this.managesieve_formbuttons(div);
321 rcube_webmail.prototype.managesieve_actiondel = function(id)
323 if (confirm(this.get_label('managesieve.actiondeleteconfirm')))
325 var row = document.getElementById('actionrow'+id);
326 row.parentNode.removeChild(row);
327 this.managesieve_formbuttons(document.getElementById('actions'));
331 // insert rule/action row in specified place on the list
332 rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
334 for (var i=0; i<div.childNodes.length; i++)
336 if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow') + after)
340 if (div.childNodes[i+1])
341 div.insertBefore(row, div.childNodes[i+1]);
343 div.appendChild(row);
346 // update Delete buttons status
347 rcube_webmail.prototype.managesieve_formbuttons = function(div)
349 var buttons = new Array();
351 // count and get buttons
352 for (i=0; i<div.childNodes.length; i++)
354 if (div.id == 'rules' && div.childNodes[i].id)
356 if (/rulerow/.test(div.childNodes[i].id))
357 buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, ''));
359 else if (div.childNodes[i].id)
361 if (/actionrow/.test(div.childNodes[i].id))
362 buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, ''));
366 for (i=0; i<buttons.length; i++)
368 var button = document.getElementById(buttons[i]);
369 if (i>0 || buttons.length>1)
371 $(button).removeClass('disabled');
372 button.removeAttribute('disabled');
376 $(button).addClass('disabled');
377 button.setAttribute('disabled', true);