Aleksander Machniak
2014-09-29 34d179a69a8c64e1dca5bb96c11c7a90cbf3bd5a
commit | author | age
48e9c1 1 /* (Manage)Sieve Filters */
T 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
27f0c2 14     if (rcmail.env.task == 'mail' || rcmail.env.action.startsWith('plugin.managesieve')) {
48e9c1 15       // Create layer for form tips
T 16       if (!rcmail.env.framed) {
17         rcmail.env.ms_tip_layer = $('<div id="managesieve-tip" class="popupmenu"></div>');
18         rcmail.env.ms_tip_layer.appendTo(document.body);
19       }
20     }
21
22     // register commands
23     rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() });
24     rcmail.register_command('plugin.managesieve-act', function() { rcmail.managesieve_act() });
25     rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() });
26     rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() });
27     rcmail.register_command('plugin.managesieve-move', function() { rcmail.managesieve_move() });
28     rcmail.register_command('plugin.managesieve-setadd', function() { rcmail.managesieve_setadd() });
29     rcmail.register_command('plugin.managesieve-setdel', function() { rcmail.managesieve_setdel() });
30     rcmail.register_command('plugin.managesieve-setact', function() { rcmail.managesieve_setact() });
31     rcmail.register_command('plugin.managesieve-setget', function() { rcmail.managesieve_setget() });
32
27f0c2 33     if (rcmail.env.action.startsWith('plugin.managesieve')) {
48e9c1 34       if (rcmail.gui_objects.sieveform) {
T 35         rcmail.enable_command('plugin.managesieve-save', true);
36
37         // small resize for header element
38         $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
39           if (this.value == '...') this.style.width = '40px';
40         });
41
42         // resize dialog window
43         if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
44           parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
45         }
46
47         $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
c6f075 48
AM 49         // initialize smart list inputs
50         $('textarea[data-type="list"]', rcmail.gui_objects.sieveform).each(function() {
51           smart_field_init(this);
52         });
390eac 53
AM 54         // enable date pickers on date fields
55         if ($.datepicker && rcmail.env.date_format) {
56           $.datepicker.setDefaults({
57             dateFormat: rcmail.env.date_format,
58             changeMonth: true,
59             showOtherMonths: true,
60             selectOtherMonths: true,
61             onSelect: function(dateText) { $(this).focus().val(dateText) }
62           });
63           $('input.datepicker').datepicker();
64         }
48e9c1 65       }
T 66       else {
67         rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror);
68       }
69
70       var i, p = rcmail, setcnt, set = rcmail.env.currentset;
71
72       if (rcmail.gui_objects.filterslist) {
73         rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist,
74           {multiselect:false, draggable:true, keyboard:false});
75         rcmail.filters_list.addEventListener('select', function(e) { p.managesieve_select(e); });
76         rcmail.filters_list.addEventListener('dragstart', function(e) { p.managesieve_dragstart(e); });
77         rcmail.filters_list.addEventListener('dragend', function(e) { p.managesieve_dragend(e); });
78         rcmail.filters_list.row_init = function (row) {
79           row.obj.onmouseover = function() { p.managesieve_focus_filter(row); };
80           row.obj.onmouseout = function() { p.managesieve_unfocus_filter(row); };
81         };
82         rcmail.filters_list.init();
83         rcmail.filters_list.focus();
84       }
85
86       if (rcmail.gui_objects.filtersetslist) {
87         rcmail.filtersets_list = new rcube_list_widget(rcmail.gui_objects.filtersetslist, {multiselect:false, draggable:false, keyboard:false});
88         rcmail.filtersets_list.addEventListener('select', function(e) { p.managesieve_setselect(e); });
89         rcmail.filtersets_list.init();
90         rcmail.filtersets_list.focus();
91
92         if (set != null) {
93           set = rcmail.managesieve_setid(set);
94           rcmail.filtersets_list.shift_start = set;
95           rcmail.filtersets_list.highlight_row(set, false);
96         }
97
98         setcnt = rcmail.filtersets_list.rowcount;
99         rcmail.enable_command('plugin.managesieve-set', true);
100         rcmail.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', setcnt);
101         rcmail.enable_command('plugin.managesieve-setdel', setcnt > 1);
102
103         // Fix dragging filters over sets list
104         $('tr', rcmail.gui_objects.filtersetslist).each(function (i, e) { p.managesieve_fixdragend(e); });
105       }
106     }
107     if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled)
108       $('#disabled').attr('checked', true);
109   });
110 };
111
112 /*********************************************************/
113 /*********       Managesieve UI methods          *********/
114 /*********************************************************/
115
116 rcube_webmail.prototype.managesieve_add = function()
117 {
118   this.load_managesieveframe();
119   this.filters_list.clear_selection();
120 };
121
122 rcube_webmail.prototype.managesieve_del = function()
123 {
124   var id = this.filters_list.get_single_selection();
125   if (confirm(this.get_label('managesieve.filterdeleteconfirm'))) {
126     var lock = this.set_busy(true, 'loading');
27f0c2 127     this.http_post('plugin.managesieve-action',
48e9c1 128       '_act=delete&_fid='+this.filters_list.rows[id].uid, lock);
T 129   }
130 };
131
132 rcube_webmail.prototype.managesieve_act = function()
133 {
134   var id = this.filters_list.get_single_selection(),
135     lock = this.set_busy(true, 'loading');
136
27f0c2 137   this.http_post('plugin.managesieve-action',
48e9c1 138     '_act=act&_fid='+this.filters_list.rows[id].uid, lock);
T 139 };
140
141 // Filter selection
142 rcube_webmail.prototype.managesieve_select = function(list)
143 {
144   var id = list.get_single_selection();
145   if (id != null)
146     this.load_managesieveframe(list.rows[id].uid);
147 };
148
149 // Set selection
150 rcube_webmail.prototype.managesieve_setselect = function(list)
151 {
152   this.show_contentframe(false);
153   this.filters_list.clear(true);
154   this.enable_command('plugin.managesieve-setdel', list.rowcount > 1);
27f0c2 155   this.enable_command('plugin.managesieve-setact', 'plugin.managesieve-setget', true);
48e9c1 156
T 157   var id = list.get_single_selection();
158   if (id != null)
159     this.managesieve_list(this.env.filtersets[id]);
160 };
161
162 rcube_webmail.prototype.managesieve_rowid = function(id)
163 {
164   var i, rows = this.filters_list.rows;
165
78ef00 166   for (i in rows)
48e9c1 167     if (rows[i] != null && rows[i].uid == id)
T 168       return i;
169 };
170
171 // Returns set's identifier
172 rcube_webmail.prototype.managesieve_setid = function(name)
173 {
174   for (var i in this.env.filtersets)
175     if (this.env.filtersets[i] == name)
176       return i;
177 };
178
179 // Filters listing request
180 rcube_webmail.prototype.managesieve_list = function(script)
181 {
182   var lock = this.set_busy(true, 'loading');
183
27f0c2 184   this.http_post('plugin.managesieve-action', '_act=list&_set='+urlencode(script), lock);
48e9c1 185 };
T 186
187 // Script download request
188 rcube_webmail.prototype.managesieve_setget = function()
189 {
190   var id = this.filtersets_list.get_single_selection(),
191     script = this.env.filtersets[id];
192
27f0c2 193   location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_act=setget&_set='+urlencode(script);
48e9c1 194 };
T 195
196 // Set activate/deactivate request
197 rcube_webmail.prototype.managesieve_setact = function()
198 {
199   var id = this.filtersets_list.get_single_selection(),
200    lock = this.set_busy(true, 'loading'),
201     script = this.env.filtersets[id],
202     action = $('#rcmrow'+id).hasClass('disabled') ? 'setact' : 'deact';
203
27f0c2 204   this.http_post('plugin.managesieve-action', '_act='+action+'&_set='+urlencode(script), lock);
48e9c1 205 };
T 206
207 // Set delete request
208 rcube_webmail.prototype.managesieve_setdel = function()
209 {
210   if (!confirm(this.get_label('managesieve.setdeleteconfirm')))
211     return false;
212
213   var id = this.filtersets_list.get_single_selection(),
214     lock = this.set_busy(true, 'loading'),
215     script = this.env.filtersets[id];
216
27f0c2 217   this.http_post('plugin.managesieve-action', '_act=setdel&_set='+urlencode(script), lock);
48e9c1 218 };
T 219
220 // Set add request
221 rcube_webmail.prototype.managesieve_setadd = function()
222 {
223   this.filters_list.clear_selection();
224   this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', false);
225
226   if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
227     var lock = this.set_busy(true, 'loading');
228     target = window.frames[this.env.contentframe];
27f0c2 229     target.location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_framed=1&_newset=1&_unlock='+lock;
48e9c1 230   }
T 231 };
232
233 rcube_webmail.prototype.managesieve_updatelist = function(action, o)
234 {
235   this.set_busy(true);
236
237   switch (action) {
238
239     // Delete filter row
240     case 'del':
ce57ca 241       var id = o.id, list = this.filters_list;
48e9c1 242
T 243       list.remove_row(this.managesieve_rowid(o.id));
244       list.clear_selection();
245       this.show_contentframe(false);
246       this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false);
247
da7905 248       // filter identifiers changed, fix the list
AM 249       $('tr', this.filters_list.list).each(function() {
250         // remove hidden (deleted) rows
251         if (this.style.display == 'none') {
252           $(this).detach();
253           return;
254         }
255
ce57ca 256         var rowid = this.id.substr(6);
AM 257
258         // remove all attached events
259         $(this).unbind();
260
261         // update row id
262         if (rowid > id)
263           $(this).attr('id', 'rcmrow' + (rowid-1));
da7905 264       });
AM 265       list.init();
48e9c1 266
T 267       break;
268
269     // Update filter row
270     case 'update':
da7905 271       var i, row = $('#rcmrow'+this.managesieve_rowid(o.id));
48e9c1 272
T 273       if (o.name)
d6b592 274         $('td', row).text(o.name);
48e9c1 275       if (o.disabled)
T 276         row.addClass('disabled');
277       else
278         row.removeClass('disabled');
279
280       $('#disabled', $('iframe').contents()).prop('checked', o.disabled);
281
282       break;
283
284     // Add filter row to the list
285     case 'add':
286       var list = this.filters_list,
287         row = $('<tr><td class="name"></td></tr>');
288
d6b592 289       $('td', row).text(o.name);
48e9c1 290       row.attr('id', 'rcmrow'+o.id);
T 291       if (o.disabled)
292         row.addClass('disabled');
293
294       list.insert_row(row.get(0));
295       list.highlight_row(o.id);
296
297       this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', true);
298
299       break;
300
301     // Filling rules list
302     case 'list':
303       var i, tr, td, el, list = this.filters_list;
304
305       if (o.clear)
306         list.clear();
307
308       for (i in o.list) {
309         el = o.list[i];
310         tr = document.createElement('TR');
311         td = document.createElement('TD');
312
d6b592 313         $(td).text(el.name);
48e9c1 314         td.className = 'name';
T 315         tr.id = 'rcmrow' + el.id;
316         if (el['class'])
317             tr.className = el['class'];
318         tr.appendChild(td);
319
320         list.insert_row(tr);
321       }
322
323       if (o.set)
324         list.highlight_row(o.set);
325       else
326         this.enable_command('plugin.managesieve-del', 'plugin.managesieve-act', false);
327
328       break;
329
330     // Sactivate/deactivate set
331     case 'setact':
332       var id = this.managesieve_setid(o.name), row = $('#rcmrow' + id);
333       if (o.active) {
334         if (o.all)
335           $('tr', this.gui_objects.filtersetslist).addClass('disabled');
336         row.removeClass('disabled');
337       }
338       else
339         row.addClass('disabled');
340
341       break;
342
343     // Delete set row
344     case 'setdel':
345       var id = this.managesieve_setid(o.name);
346
347       this.filtersets_list.remove_row(id);
348       this.filters_list.clear();
349       this.show_contentframe(false);
350       this.enable_command('plugin.managesieve-setdel', 'plugin.managesieve-setact', 'plugin.managesieve-setget', false);
351
352       delete this.env.filtersets[id];
353
354       break;
355
356     // Create set row
357     case 'setadd':
358       var id = 'S' + new Date().getTime(),
359         list = this.filtersets_list,
360         row = $('<tr class="disabled"><td class="name"></td></tr>');
361
d6b592 362       $('td', row).text(o.name);
48e9c1 363       row.attr('id', 'rcmrow'+id);
T 364
365       this.env.filtersets[id] = o.name;
366       list.insert_row(row.get(0));
367
368       // move row into its position on the list
369       if (o.index != list.rowcount-1) {
370         row.detach();
371         var elem = $('tr:visible', list.list).get(o.index);
372         row.insertBefore(elem);
373       }
374
375       list.select(id);
376
377       // Fix dragging filters over sets list
378       this.managesieve_fixdragend(row);
379
380       break;
381   }
382
383   this.set_busy(false);
384 };
385
386 // load filter frame
387 rcube_webmail.prototype.load_managesieveframe = function(id)
388 {
389   var has_id = typeof(id) != 'undefined' && id != null;
390   this.enable_command('plugin.managesieve-act', 'plugin.managesieve-del', has_id);
391
392   if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
393     target = window.frames[this.env.contentframe];
394     var msgid = this.set_busy(true, 'loading');
27f0c2 395     target.location.href = this.env.comm_path+'&_action=plugin.managesieve-action&_framed=1'
a648ba 396       +(has_id ? '&_fid='+id : '')+'&_unlock='+msgid;
48e9c1 397   }
T 398 };
399
400 // load filter frame
401 rcube_webmail.prototype.managesieve_dragstart = function(list)
402 {
403   var id = this.filters_list.get_single_selection();
404
405   this.drag_active = true;
406   this.drag_filter = id;
407 };
408
409 rcube_webmail.prototype.managesieve_dragend = function(e)
410 {
411   if (this.drag_active) {
412     if (this.drag_filter_target) {
413       var lock = this.set_busy(true, 'loading');
414
415       this.show_contentframe(false);
27f0c2 416       this.http_post('plugin.managesieve-action', '_act=move&_fid='+this.drag_filter
48e9c1 417         +'&_to='+this.drag_filter_target, lock);
T 418     }
419     this.drag_active = false;
420   }
421 };
422
423 // Fixes filters dragging over sets list
424 // @TODO: to be removed after implementing copying filters
425 rcube_webmail.prototype.managesieve_fixdragend = function(elem)
426 {
427   var p = this;
428   $(elem).bind('mouseup' + ((bw.iphone || bw.ipad) ? ' touchend' : ''), function(e) {
429     if (p.drag_active)
430       p.filters_list.drag_mouse_up(e);
431   });
432 };
433
434 rcube_webmail.prototype.managesieve_focus_filter = function(row)
435 {
436   var id = row.id.replace(/^rcmrow/, '');
437   if (this.drag_active && id != this.drag_filter) {
438     this.drag_filter_target = id;
439     $(row.obj).addClass(id < this.drag_filter ? 'filtermoveup' : 'filtermovedown');
440   }
441 };
442
443 rcube_webmail.prototype.managesieve_unfocus_filter = function(row)
444 {
445   if (this.drag_active) {
446     $(row.obj).removeClass('filtermoveup filtermovedown');
447     this.drag_filter_target = null;
448   }
449 };
450
451 /*********************************************************/
452 /*********          Filter Form methods          *********/
453 /*********************************************************/
454
455 // Form submition
456 rcube_webmail.prototype.managesieve_save = function()
457 {
390eac 458   if (this.env.action == 'plugin.managesieve-vacation') {
AM 459     var data = $(this.gui_objects.sieveform).serialize();
460     this.http_post('plugin.managesieve-vacation', data, this.display_message(this.get_label('managesieve.vacation.saving'), 'loading'));
461     return;
462   }
463
48e9c1 464   if (parent.rcmail && parent.rcmail.filters_list && this.gui_objects.sieveform.name != 'filtersetform') {
T 465     var id = parent.rcmail.filters_list.get_single_selection();
466     if (id != null)
467       this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid;
468   }
469   this.gui_objects.sieveform.submit();
470 };
471
472 // Operations on filters form
473 rcube_webmail.prototype.managesieve_ruleadd = function(id)
474 {
27f0c2 475   this.http_post('plugin.managesieve-action', '_act=ruleadd&_rid='+id);
48e9c1 476 };
T 477
478 rcube_webmail.prototype.managesieve_rulefill = function(content, id, after)
479 {
480   if (content != '') {
481     // create new element
482     var div = document.getElementById('rules'),
483       row = document.createElement('div');
484
485     this.managesieve_insertrow(div, row, after);
486     // fill row after inserting (for IE)
487     row.setAttribute('id', 'rulerow'+id);
488     row.className = 'rulerow';
489     row.innerHTML = content;
490
c6f075 491     // initialize smart list inputs
AM 492     $('textarea[data-type="list"]', row).each(function() {
493       smart_field_init(this);
494     });
495
48e9c1 496     this.managesieve_formbuttons(div);
T 497   }
498 };
499
500 rcube_webmail.prototype.managesieve_ruledel = function(id)
501 {
502   if ($('#ruledel'+id).hasClass('disabled'))
503     return;
504
505   if (confirm(this.get_label('managesieve.ruledeleteconfirm'))) {
506     var row = document.getElementById('rulerow'+id);
507     row.parentNode.removeChild(row);
508     this.managesieve_formbuttons(document.getElementById('rules'));
509   }
510 };
511
512 rcube_webmail.prototype.managesieve_actionadd = function(id)
513 {
27f0c2 514   this.http_post('plugin.managesieve-action', '_act=actionadd&_aid='+id);
48e9c1 515 };
T 516
517 rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
518 {
519   if (content != '') {
520     var div = document.getElementById('actions'),
521       row = document.createElement('div');
522
523     this.managesieve_insertrow(div, row, after);
524     // fill row after inserting (for IE)
525     row.className = 'actionrow';
526     row.setAttribute('id', 'actionrow'+id);
527     row.innerHTML = content;
528
6f0790 529     // initialize smart list inputs
AM 530     $('textarea[data-type="list"]', row).each(function() {
531       smart_field_init(this);
532     });
533
48e9c1 534     this.managesieve_formbuttons(div);
T 535   }
536 };
537
538 rcube_webmail.prototype.managesieve_actiondel = function(id)
539 {
540   if ($('#actiondel'+id).hasClass('disabled'))
541     return;
542
543   if (confirm(this.get_label('managesieve.actiondeleteconfirm'))) {
544     var row = document.getElementById('actionrow'+id);
545     row.parentNode.removeChild(row);
546     this.managesieve_formbuttons(document.getElementById('actions'));
547   }
548 };
549
550 // insert rule/action row in specified place on the list
551 rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
552 {
553   for (var i=0; i<div.childNodes.length; i++) {
554     if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow')  + after)
555       break;
556   }
557
558   if (div.childNodes[i+1])
559     div.insertBefore(row, div.childNodes[i+1]);
560   else
561     div.appendChild(row);
562 };
563
564 // update Delete buttons status
565 rcube_webmail.prototype.managesieve_formbuttons = function(div)
566 {
567   var i, button, buttons = [];
568
569   // count and get buttons
570   for (i=0; i<div.childNodes.length; i++) {
571     if (div.id == 'rules' && div.childNodes[i].id) {
572       if (/rulerow/.test(div.childNodes[i].id))
573         buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, ''));
574     }
575     else if (div.childNodes[i].id) {
576       if (/actionrow/.test(div.childNodes[i].id))
577         buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, ''));
578     }
579   }
580
581   for (i=0; i<buttons.length; i++) {
582     button = document.getElementById(buttons[i]);
583     if (i>0 || buttons.length>1) {
584       $(button).removeClass('disabled');
585     }
586     else {
587       $(button).addClass('disabled');
588     }
589   }
590 };
591
592 function rule_header_select(id)
593 {
594   var obj = document.getElementById('header' + id),
595     size = document.getElementById('rule_size' + id),
596     op = document.getElementById('rule_op' + id),
a1679c 597     header = document.getElementById('custom_header' + id + '_list'),
48e9c1 598     mod = document.getElementById('rule_mod' + id),
T 599     trans = document.getElementById('rule_trans' + id),
0185a2 600     comp = document.getElementById('rule_comp' + id),
AM 601     datepart = document.getElementById('rule_date_part' + id),
602     dateheader = document.getElementById('rule_date_header_div' + id),
603     h = obj.value;
48e9c1 604
0185a2 605   if (h == 'size') {
48e9c1 606     size.style.display = 'inline';
0185a2 607     $.each([op, header, mod, trans, comp], function() { this.style.display = 'none'; });
48e9c1 608   }
T 609   else {
0185a2 610     header.style.display = h != '...' ? 'none' : 'inline-block';
48e9c1 611     size.style.display = 'none';
T 612     op.style.display = 'inline';
613     comp.style.display = '';
0185a2 614     mod.style.display = h == 'body' || h == 'currentdate' || h == 'date' ? 'none' : 'block';
AM 615     trans.style.display = h == 'body' ? 'block' : 'none';
48e9c1 616   }
T 617
0185a2 618   if (datepart)
AM 619     datepart.style.display = h == 'currentdate' || h == 'date' ? 'inline' : 'none';
620   if (dateheader)
621     dateheader.style.display = h == 'date' ? '' : 'none';
622
623   rule_op_select(op, id, h);
624   rule_mod_select(id, h);
625   obj.style.width = h == '...' ? '40px' : '';
48e9c1 626 };
T 627
c6f075 628 function rule_op_select(obj, id, header)
48e9c1 629 {
5c6e74 630   var target = document.getElementById('rule_target' + id + '_list');
48e9c1 631
5c6e74 632   if (!header)
AM 633     header = document.getElementById('header' + id).value;
634
635   target.style.display = obj.value == 'exists' || obj.value == 'notexists' || header == 'size' ? 'none' : 'inline-block';
48e9c1 636 };
T 637
638 function rule_trans_select(id)
639 {
640   var obj = document.getElementById('rule_trans_op' + id),
641     target = document.getElementById('rule_trans_type' + id);
642
643   target.style.display = obj.value != 'content' ? 'none' : 'inline';
644 };
645
0185a2 646 function rule_mod_select(id, header)
48e9c1 647 {
T 648   var obj = document.getElementById('rule_mod_op' + id),
0185a2 649     target = document.getElementById('rule_mod_type' + id),
AM 650     index = document.getElementById('rule_index_div' + id);
651
652   if (!header)
653     header = document.getElementById('header' + id).value;
48e9c1 654
T 655   target.style.display = obj.value != 'address' && obj.value != 'envelope' ? 'none' : 'inline';
0185a2 656
AM 657   if (index)
658     index.style.display = header != 'body' && header != 'currentdate' && header != 'size' && obj.value != 'envelope'  ? '' : 'none';
48e9c1 659 };
T 660
661 function rule_join_radio(value)
662 {
663   $('#rules').css('display', value == 'any' ? 'none' : 'block');
664 };
665
666 function rule_adv_switch(id, elem)
667 {
668   var elem = $(elem), enabled = elem.hasClass('hide'), adv = $('#rule_advanced'+id);
669
670   if (enabled) {
671     adv.hide();
672     elem.removeClass('hide').addClass('show');
673   }
674   else {
675     adv.show();
676     elem.removeClass('show').addClass('hide');
677   }
678 }
679
680 function action_type_select(id)
681 {
682   var obj = document.getElementById('action_type' + id),
f001af 683     v = obj.value, enabled = {},
48e9c1 684     elems = {
T 685       mailbox: document.getElementById('action_mailbox' + id),
0f4806 686       target: document.getElementById('redirect_target' + id),
48e9c1 687       target_area: document.getElementById('action_target_area' + id),
T 688       flags: document.getElementById('action_flags' + id),
ebb204 689       vacation: document.getElementById('action_vacation' + id),
2e7bd6 690       set: document.getElementById('action_set' + id),
PS 691       notify: document.getElementById('action_notify' + id)
48e9c1 692     };
T 693
0185a2 694   if (v == 'fileinto' || v == 'fileinto_copy') {
48e9c1 695     enabled.mailbox = 1;
T 696   }
0185a2 697   else if (v == 'redirect' || v == 'redirect_copy') {
48e9c1 698     enabled.target = 1;
T 699   }
0185a2 700   else if (v.match(/^reject|ereject$/)) {
48e9c1 701     enabled.target_area = 1;
T 702   }
0185a2 703   else if (v.match(/^(add|set|remove)flag$/)) {
48e9c1 704     enabled.flags = 1;
T 705   }
0185a2 706   else if (v == 'vacation') {
48e9c1 707     enabled.vacation = 1;
T 708   }
0185a2 709   else if (v == 'set') {
ebb204 710     enabled.set = 1;
AM 711   }
0185a2 712   else if (v == 'notify') {
2e7bd6 713     enabled.notify = 1;
PS 714   }
48e9c1 715
T 716   for (var x in elems) {
717     elems[x].style.display = !enabled[x] ? 'none' : 'inline';
718   }
719 };
720
c6f075 721 // Inititalizes smart list input
AM 722 function smart_field_init(field)
723 {
724   var id = field.id + '_list',
725     area = $('<span class="listarea"></span>'),
726     list = field.value ? field.value.split("\n") : [''];
727
728   if ($('#'+id).length)
729     return;
730
731   // add input rows
732   $.each(list, function(i, v) {
733     area.append(smart_field_row(v, field.name, i, $(field).data('size')));
734   });
735
736   area.attr('id', id);
737   field = $(field);
738
739   if (field.attr('disabled'))
740     area.hide();
34d179 741   // disable the original field anyway, we don't want it in POST
AM 742   else
743     field.prop('disabled', true);
c6f075 744
AM 745   field.after(area);
746
747   if (field.hasClass('error')) {
748     area.addClass('error');
749     rcmail.managesieve_tip_register([[id, field.data('tip')]]);
750   }
751 };
752
753 function smart_field_row(value, name, idx, size)
754 {
755   // build row element content
756   var input, content = '<span class="listelement">'
bba781 757       + '<span class="reset"></span><input type="text"></span>',
c6f075 758     elem = $(content),
AM 759     attrs = {value: value, name: name + '[]'};
760
761   if (size)
762     attrs.size = size;
763
bba781 764   input = $('input', elem).attr(attrs).keydown(function(e) {
AM 765     var input = $(this);
766     // element creation event (on Enter)
767     if (e.which == 13) {
768       var name = input.attr('name').replace(/\[\]$/, ''),
769         dt = (new Date()).getTime(),
770         elem = smart_field_row('', name, dt, size);
c6f075 771
bba781 772       input.parent().after(elem);
AM 773       $('input', elem).focus();
774     }
c6f075 775   });
AM 776
777   // element deletion event
778   $('span[class="reset"]', elem).click(function() {
bba781 779     var span = $(this.parentNode);
c6f075 780
AM 781     if (span.parent().children().length > 1)
782       span.remove();
783     else
784       $('input', span).val('').focus();
785   });
786
787   return elem;
788 }
789
48e9c1 790 // Register onmouse(leave/enter) events for tips on specified form element
T 791 rcube_webmail.prototype.managesieve_tip_register = function(tips)
792 {
793   var n, framed = parent.rcmail,
794     tip = framed ? parent.rcmail.env.ms_tip_layer : rcmail.env.ms_tip_layer;
795
796   for (var n in tips) {
797     $('#'+tips[n][0])
c6f075 798       .data('tip', tips[n][1])
AM 799       .bind('mouseenter', function(e) {
800         var elem = $(this),
801           offset = elem.offset(),
802           left = offset.left,
803           top = offset.top - 12,
804           minwidth = elem.width();
48e9c1 805
c6f075 806         if (framed) {
AM 807           offset = $((rcmail.env.task == 'mail'  ? '#sievefilterform > iframe' : '#filter-box'), parent.document).offset();
808           top  += offset.top;
809           left += offset.left;
810         }
48e9c1 811
c6f075 812         tip.html(elem.data('tip'));
AM 813         top -= tip.height();
48e9c1 814
c6f075 815         tip.css({left: left, top: top, minWidth: (minwidth-2) + 'px'}).show();
AM 816       })
817     .bind('mouseleave', function(e) { tip.hide(); });
48e9c1 818   }
T 819 };
820
821 /*********************************************************/
822 /*********           Mail UI methods             *********/
823 /*********************************************************/
824
1edb6b 825 rcube_webmail.prototype.managesieve_create = function(force)
48e9c1 826 {
1edb6b 827   if (!force && this.env.action != 'show' && !$('#'+this.env.contentframe).is(':visible')) {
AM 828     var uid = this.message_list.get_single_selection(),
829       lock = this.set_busy(true, 'loading');
830
831     this.http_post('plugin.managesieve-action', {_uid: uid}, lock);
832     return;
833   }
834
835   if (!this.env.sieve_headers || !this.env.sieve_headers.length)
48e9c1 836     return;
T 837
838   var i, html, buttons = {}, dialog = $("#sievefilterform");
839
840   // create dialog window
841   if (!dialog.length) {
842     dialog = $('<div id="sievefilterform"></div>');
843     $('body').append(dialog);
844   }
845
846   // build dialog window content
847   html = '<fieldset><legend>'+this.gettext('managesieve.usedata')+'</legend><ul>';
1edb6b 848   for (i in this.env.sieve_headers)
48e9c1 849     html += '<li><input type="checkbox" name="headers[]" id="sievehdr'+i+'" value="'+i+'" checked="checked" />'
1edb6b 850       +'<label for="sievehdr'+i+'">'+this.env.sieve_headers[i][0]+':</label> '+this.env.sieve_headers[i][1]+'</li>';
48e9c1 851   html += '</ul></fieldset>';
T 852
853   dialog.html(html);
854
855   // [Next Step] button action
856   buttons[this.gettext('managesieve.nextstep')] = function () {
857     // check if there's at least one checkbox checked
858     var hdrs = $('input[name="headers[]"]:checked', dialog);
859     if (!hdrs.length) {
860       alert(rcmail.gettext('managesieve.nodata'));
861       return;
862     }
863
864     // build frame URL
865     var url = rcmail.get_task_url('mail');
866     url = rcmail.add_url(url, '_action', 'plugin.managesieve');
867     url = rcmail.add_url(url, '_framed', 1);
868
869     hdrs.map(function() {
870       var val = rcmail.env.sieve_headers[this.value];
871       url = rcmail.add_url(url, 'r['+this.value+']', val[0]+':'+val[1]);
872     });
873
874     // load form in the iframe
875     var frame = $('<iframe>').attr({src: url, frameborder: 0})
46f06c 876     dialog.empty().append(frame).dialog('widget').resize();
48e9c1 877
T 878     // Change [Next Step] button with [Save] button
879     buttons = {};
880     buttons[rcmail.gettext('save')] = function() {
881       var win = $('iframe', dialog).get(0).contentWindow;
882       win.rcmail.managesieve_save();
883     };
884     dialog.dialog('option', 'buttons', buttons);
885   };
886
887   // show dialog window
888   dialog.dialog({
889     modal: false,
890     resizable: !bw.ie6,
891     closeOnEscape: (!bw.ie6 && !bw.ie7),  // disable for performance reasons
892     title: this.gettext('managesieve.newfilter'),
893     close: function() { rcmail.managesieve_dialog_close(); },
894     buttons: buttons,
895     minWidth: 600,
896     minHeight: 300,
897     height: 250
898   }).show();
899
900   this.env.managesieve_dialog = dialog;
901 }
902
903 rcube_webmail.prototype.managesieve_dialog_close = function()
904 {
905   var dialog = this.env.managesieve_dialog;
906
907   // BUG(?): if we don't remove the iframe first, it will be reloaded
908   dialog.html('');
909   dialog.dialog('destroy').hide();
910 }
911
912 rcube_webmail.prototype.managesieve_dialog_resize = function(o)
913 {
914   var dialog = this.env.managesieve_dialog,
915     win = $(window), form = $(o);
916     width = $('fieldset:first', o).width(), // fieldset width is more appropriate here
917     height = form.height(),
918     w = win.width(), h = win.height();
919
920   dialog.dialog('option', { height: Math.min(h-20, height+120), width: Math.min(w-20, width+65) })
921     .dialog('option', 'position', ['center', 'center']);  // works in a separate call only (!?)
922 }