summaryrefslogtreecommitdiff
path: root/plugins/managesieve/managesieve.js
blob: 09139d6c5b32f4be822bb4c4bc564c254989c935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Sieve Filters (tab) */

if (window.rcmail) {
  rcmail.addEventListener('init', function(evt) {
    // <span id="settingstabdefault" class="tablink"><roundcube:button command="preferences" type="link" label="preferences" title="editpreferences" /></span>
    var tab = $('<span>').attr('id', 'settingstabpluginmanagesieve').addClass('tablink');
    
    var button = $('<a>').attr('href', rcmail.env.comm_path+'&_action=plugin.managesieve')
	.attr('title', rcmail.gettext('managesieve.managefilters'))
	.html(rcmail.gettext('managesieve.filters'))
	.bind('click', function(e){ return rcmail.command('plugin.managesieve', this) })
	.appendTo(tab);

    // add button and register commands
    rcmail.add_element(tab, 'tabs');
    rcmail.register_command('plugin.managesieve', function() { rcmail.goto_url('plugin.managesieve') }, true);
    rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() }, true);
    rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() }, true);
    rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() }, true);
    rcmail.register_command('plugin.managesieve-up', function() { rcmail.managesieve_up() }, true);
    rcmail.register_command('plugin.managesieve-down', function() { rcmail.managesieve_down() }, true);

    if (rcmail.env.action == 'plugin.managesieve')
      {
	if (rcmail.gui_objects.sieveform)
	  rcmail.enable_command('plugin.managesieve-save', true);
	else {
	  rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false);
          rcmail.enable_command('plugin.managesieve-add', !rcmail.env.sieveconnerror);
	}
	
        if (rcmail.gui_objects.filterslist) {
	    var p = rcmail;
    	    rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist, {multiselect:false, draggable:false, keyboard:false});
    	    rcmail.filters_list.addEventListener('select', function(o){ p.managesieve_select(o); });
    	    rcmail.filters_list.init();
    	    rcmail.filters_list.focus();
	  }
      }
  });

  /*********************************************************/
  /*********     Managesieve filters methods       *********/
  /*********************************************************/

  rcube_webmail.prototype.managesieve_add = function()
    {
      this.load_managesieveframe();
      this.filters_list.clear_selection();
    };

  rcube_webmail.prototype.managesieve_del = function()
    {
    var id = this.filters_list.get_single_selection();

    if (confirm(this.get_label('managesieve.filterconfirmdelete')))
      this.http_request('plugin.managesieve',
	    '_act=delete&_fid='+this.filters_list.rows[id].uid, true);
    };

  rcube_webmail.prototype.managesieve_up = function()
    {
    var id = this.filters_list.get_single_selection();
    this.http_request('plugin.managesieve',
    	    '_act=up&_fid='+this.filters_list.rows[id].uid, true);
    };

  rcube_webmail.prototype.managesieve_down = function()
    {
    var id = this.filters_list.get_single_selection();
    this.http_request('plugin.managesieve',
    	    '_act=down&_fid='+this.filters_list.rows[id].uid, true);
    };

  rcube_webmail.prototype.managesieve_rowid = function(id)
    {
    var rows = this.filters_list.rows;
    
    for (var i=0; i<rows.length; i++)
      if (rows[i] != null && rows[i].uid == id)
	return i;
    }

  rcube_webmail.prototype.managesieve_updatelist = function(action, name, id)
    {
    this.set_busy(true);

    switch (action)
      {
      case 'delete':
        this.filters_list.remove_row(this.managesieve_rowid(id));
	this.filters_list.clear_selection();
	this.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', 'plugin.managesieve-down', false);
	this.show_contentframe(false);

	// re-numbering filters
        var rows = this.filters_list.rows;
        for (var i=0; i<rows.length; i++)
          {
	  if (rows[i] != null && rows[i].uid > id)
	    rows[i].uid = rows[i].uid-1;
	  }
	break;

      case 'down':
        var rows = this.filters_list.rows;
	var from;

	// we need only to replace filter names...
        for (var i=0; i<rows.length; i++)
        {
	  if (rows[i]==null) { // removed row
	    continue;
          } else if (rows[i].uid == id) {
	    from = rows[i].obj.cells[0];
	  } else if (rows[i].uid == id+1){
	    name = rows[i].obj.cells[0].innerHTML;
	    rows[i].obj.cells[0].innerHTML = from.innerHTML;
	    from.innerHTML = name;
	    this.filters_list.highlight_row(i);
	    break;
	  }
	}
	// ... and disable/enable Down button
	this.filters_listbuttons();
        break;

      case 'up':
        var rows = this.filters_list.rows;
	var from;

	// we need only to replace filter names...
        for (var i=0; i<rows.length; i++)
        {
	  if (rows[i]==null) { // removed row
	    continue;
          } else if (rows[i].uid == id-1) {
	    from = rows[i].obj.cells[0];
	    this.filters_list.highlight_row(i);
	  } else if (rows[i].uid == id) {
	    name = rows[i].obj.cells[0].innerHTML;
	    rows[i].obj.cells[0].innerHTML = from.innerHTML;
	    from.innerHTML = name;
	    break;
	  }
	}
	// ... and disable/enable Up button
	this.filters_listbuttons();
        break;
	
      case 'update':
        var rows = parent.rcmail.filters_list.rows;
        for (var i=0; i<rows.length; i++)
	  if (rows[i] && rows[i].uid == id)
	    {
	    rows[i].obj.cells[0].innerHTML = name;
	    break;
	    }
        break;
      
      case 'add':
        var row, new_row, td;
	var list = parent.rcmail.filters_list;

	if (!list)
          break;

	for (var i=0; i<list.rows.length; i++)
	  if (list.rows[i] != null && String(list.rows[i].obj.id).match(/^rcmrow/))
	    row = list.rows[i].obj;

        if (row)
	  {
	    new_row = parent.document.createElement('TR');
	    new_row.id = 'rcmrow'+id;
    	    td = parent.document.createElement('TD');
    	    new_row.appendChild(td);
	    list.insert_row(new_row, false);

    	    if (row.cells[0].className)
    	      td.className = row.cells[0].className;
        
    	    td.innerHTML = name;
	    list.highlight_row(id);

	    parent.rcmail.enable_command('plugin.managesieve-del', 'plugin.managesieve-up', true);
	  }
        else // refresh whole page
	  parent.rcmail.goto_url('plugin.managesieve');
	break;
      }

    this.set_busy(false);

    };

  rcube_webmail.prototype.managesieve_select = function(list)
    {
    var id = list.get_single_selection();
    if (id != null)
      this.load_managesieveframe(list.rows[id].uid);
    };

  rcube_webmail.prototype.managesieve_save = function()
    {
      if (parent.rcmail && parent.rcmail.filters_list)
        {
        var id = parent.rcmail.filters_list.get_single_selection();
	if (id != null)
	  this.gui_objects.sieveform.elements['_fid'].value = parent.rcmail.filters_list.rows[id].uid;
        }
      this.gui_objects.sieveform.submit();
    };

  // load filter frame
  rcube_webmail.prototype.load_managesieveframe = function(id)
    {
    if (typeof(id) != 'undefined' && id != null)
      {
      this.enable_command('plugin.managesieve-del', true);
      this.filters_listbuttons();
      }
    else
      this.enable_command('plugin.managesieve-up', 'plugin.managesieve-down', 'plugin.managesieve-del', false);

    if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
      {
      target = window.frames[this.env.contentframe];
      this.set_busy(true, 'loading');
      target.location.href = this.env.comm_path+'&_action=plugin.managesieve&_framed=1&_fid='+id;
      }
    };

  // enable/disable Up/Down buttons
  rcube_webmail.prototype.filters_listbuttons = function()
    {
    var id = this.filters_list.get_single_selection();
    var rows = this.filters_list.rows;

    for (var i=0; i<rows.length; i++)
      {
      if (rows[i] == null) { // removed row
        } else if (i == id) {
	  this.enable_command('plugin.managesieve-up', false);
	  break;
        } else {
	  this.enable_command('plugin.managesieve-up', true);
	  break;
        }
      }

    for (var i=rows.length-1; i>0; i--)
      {
        if (rows[i] == null) { // removed row
	} else if (i == id) {
	  this.enable_command('plugin.managesieve-down', false);
	  break;
	} else {
	  this.enable_command('plugin.managesieve-down', true);
	  break;
	}
      } 
    };

  // operations on filters form
  rcube_webmail.prototype.managesieve_ruleadd = function(id)
    {
      this.http_post('plugin.managesieve', '_act=ruleadd&_rid='+id);
    };

  rcube_webmail.prototype.managesieve_rulefill = function(content, id, after)
    {
      if (content != '')
        {
	// create new element
	var div = document.getElementById('rules');
	var row = document.createElement('div');

	this.managesieve_insertrow(div, row, after);
	// fill row after inserting (for IE)
	row.setAttribute('id', 'rulerow'+id);
	row.className = 'rulerow';
        row.innerHTML = content;

	this.managesieve_formbuttons(div);
	}
    };

  rcube_webmail.prototype.managesieve_ruledel = function(id)
    {
      if (confirm(this.get_label('managesieve.ruledeleteconfirm')))
	{
	var row = document.getElementById('rulerow'+id);
	row.parentNode.removeChild(row);
	this.managesieve_formbuttons(document.getElementById('rules'));
	}
    };

  rcube_webmail.prototype.managesieve_actionadd = function(id)
    {
      this.http_post('plugin.managesieve', '_act=actionadd&_aid='+id);
    };

  rcube_webmail.prototype.managesieve_actionfill = function(content, id, after)
    {
      if (content != '')
        {
	var div = document.getElementById('actions');
	var row = document.createElement('div');

	this.managesieve_insertrow(div, row, after);
	// fill row after inserting (for IE)
	row.className = 'actionrow';
	row.setAttribute('id', 'actionrow'+id);
        row.innerHTML = content;

        this.managesieve_formbuttons(div);
	}
    };

  rcube_webmail.prototype.managesieve_actiondel = function(id)
    {
      if (confirm(this.get_label('managesieve.actiondeleteconfirm')))
	{
	var row = document.getElementById('actionrow'+id);
	row.parentNode.removeChild(row);
	this.managesieve_formbuttons(document.getElementById('actions'));
	}
    };

  // insert rule/action row in specified place on the list
  rcube_webmail.prototype.managesieve_insertrow = function(div, row, after)
    {
      for (var i=0; i<div.childNodes.length; i++)
	{
        if (div.childNodes[i].id == (div.id == 'rules' ? 'rulerow' : 'actionrow')  + after)
	  break;
	}

      if (div.childNodes[i+1])
        div.insertBefore(row, div.childNodes[i+1]);
      else
        div.appendChild(row);
    }

  // update Delete buttons status
  rcube_webmail.prototype.managesieve_formbuttons = function(div)
    {
      var buttons = new Array();
      var i, j=0;
      // count and get buttons
      for (i=0; i<div.childNodes.length; i++)
	{
	if (div.id == 'rules' && div.childNodes[i].id)
	  {
	  if (/rulerow/.test(div.childNodes[i].id))
	    buttons.push('ruledel' + div.childNodes[i].id.replace(/rulerow/, ''));
	  }
	else if (div.childNodes[i].id)
	  {
	  if (/actionrow/.test(div.childNodes[i].id))
	    buttons.push( 'actiondel' + div.childNodes[i].id.replace(/actionrow/, ''));
	  }
        }

      for (i=0; i<buttons.length; i++)
	{
	var button = document.getElementById(buttons[i]);
	if (i>0 || buttons.length>1)
	  {
	  $(button).removeClass('disabled');
	  button.removeAttribute('disabled');
	  }
	else
	  {
	  $(button).addClass('disabled');
	  button.setAttribute('disabled', true);
	  }
        }
    }
}