Aleksander Machniak
2015-04-18 0f797eddd81762db824c3dd835914ada460b622b
plugins/managesieve/managesieve.js
@@ -1,4 +1,19 @@
/* (Manage)Sieve Filters */
/**
 * (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.
 */
if (window.rcmail) {
  rcmail.addEventListener('init', function(evt) {
@@ -33,70 +48,42 @@
    if (rcmail.env.action.startsWith('plugin.managesieve')) {
      if (rcmail.gui_objects.sieveform) {
        rcmail.enable_command('plugin.managesieve-save', true);
        // small resize for header element
        $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
          if (this.value == '...') this.style.width = '40px';
        });
        // resize dialog window
        if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
          parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
        }
        $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
        // initialize smart list inputs
        $('textarea[data-type="list"]', rcmail.gui_objects.sieveform).each(function() {
          smart_field_init(this);
        });
        // 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();
        }
        sieve_form_init();
      }
      else {
        rcmail.enable_command('plugin.managesieve-add', 'plugin.managesieve-setadd', !rcmail.env.sieveconnerror);
      }
      var i, p = rcmail, setcnt, set = rcmail.env.currentset;
      var setcnt, set = rcmail.env.currentset;
      if (rcmail.gui_objects.filterslist) {
        rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist,
          {multiselect:false, draggable:true, keyboard:false});
          {multiselect:false, draggable:true, keyboard:true});
        rcmail.filters_list
          .addEventListener('select', function(e) { p.managesieve_select(e); })
          .addEventListener('dragstart', function(e) { p.managesieve_dragstart(e); })
          .addEventListener('dragend', function(e) { p.managesieve_dragend(e); })
          .addEventListener('select', function(e) { rcmail.managesieve_select(e); })
          .addEventListener('dragstart', function(e) { rcmail.managesieve_dragstart(e); })
          .addEventListener('dragend', function(e) { rcmail.managesieve_dragend(e); })
          .addEventListener('initrow', function(row) {
            row.obj.onmouseover = function() { p.managesieve_focus_filter(row); };
            row.obj.onmouseout = function() { p.managesieve_unfocus_filter(row); };
            row.obj.onmouseover = function() { rcmail.managesieve_focus_filter(row); };
            row.obj.onmouseout = function() { rcmail.managesieve_unfocus_filter(row); };
          })
          .init().focus();
          .init();
      }
      if (rcmail.gui_objects.filtersetslist) {
        rcmail.filtersets_list = new rcube_list_widget(rcmail.gui_objects.filtersetslist,
          {multiselect:false, draggable:false, keyboard:false});
          {multiselect:false, draggable:false, keyboard:true});
        rcmail.filtersets_list
          .addEventListener('select', function(e) { p.managesieve_setselect(e); })
          .init().focus();
        rcmail.filtersets_list.init().focus();
        if (set != null) {
          set = rcmail.managesieve_setid(set);
          rcmail.filtersets_list.shift_start = set;
          rcmail.filtersets_list.highlight_row(set, false);
          rcmail.filtersets_list.select(set);
        }
        // attach select event after initial record was selected
        rcmail.filtersets_list.addEventListener('select', function(e) { rcmail.managesieve_setselect(e); });
        setcnt = rcmail.filtersets_list.rowcount;
        rcmail.enable_command('plugin.managesieve-set', true);
@@ -104,9 +91,10 @@
        rcmail.enable_command('plugin.managesieve-setdel', setcnt > 1);
        // Fix dragging filters over sets list
        $('tr', rcmail.gui_objects.filtersetslist).each(function (i, e) { p.managesieve_fixdragend(e); });
        $('tr', rcmail.gui_objects.filtersetslist).each(function (i, e) { rcmail.managesieve_fixdragend(e); });
      }
    }
    if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled)
      $('#disabled').attr('checked', true);
  });
@@ -238,7 +226,6 @@
  this.set_busy(true);
  switch (action) {
    // Delete filter row
    case 'del':
      var id = o.id, list = this.filters_list;
@@ -262,8 +249,10 @@
        $(this).unbind();
        // update row id
        if (rowid > id)
          $(this).attr('id', 'rcmrow' + (rowid-1));
        if (rowid > id) {
          this.uid = rowid - 1;
          $(this).attr('id', 'rcmrow' + this.uid);
        }
      });
      list.init();
@@ -721,6 +710,13 @@
  }
};
function vacation_action_select()
{
  var selected = $('#vacation_action').val();
  $('#action_target_span')[selected == 'discard' || selected == 'keep' ? 'hide' : 'show']();
};
// Inititalizes smart list input
function smart_field_init(field)
{
@@ -741,6 +737,9 @@
  if (field.attr('disabled'))
    area.hide();
  // disable the original field anyway, we don't want it in POST
  else
    field.prop('disabled', true);
  field.after(area);
@@ -763,6 +762,7 @@
  input = $('input', elem).attr(attrs).keydown(function(e) {
    var input = $(this);
    // element creation event (on Enter)
    if (e.which == 13) {
      var name = input.attr('name').replace(/\[\]$/, ''),
@@ -771,6 +771,21 @@
      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;
      }
    }
  });
@@ -818,13 +833,108 @@
  }
};
// format time string
function sieve_formattime(hour, minutes)
{
  var i, c, h, time = '', format = rcmail.env.time_format || 'H:i';
  for (i=0; i<format.length; i++) {
    c = format.charAt(i);
    switch (c) {
      case 'a': time += hour > 12 ? 'am' : 'pm'; break;
      case 'A': time += hour > 12 ? 'AM' : 'PM'; 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()
{
  // small resize for header element
  $('select[name="_header[]"]', rcmail.gui_objects.sieveform).each(function() {
    if (this.value == '...') this.style.width = '40px';
  });
  // resize dialog window
  if (rcmail.env.action == 'plugin.managesieve' && rcmail.env.task == 'mail') {
    parent.rcmail.managesieve_dialog_resize(rcmail.gui_objects.sieveform);
  }
  $('input[type="text"]:first', rcmail.gui_objects.sieveform).focus();
  // initialize smart list inputs
  $('textarea[data-type="list"]', rcmail.gui_objects.sieveform).each(function() {
    smart_field_init(this);
  });
  // 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() || ' ');
    })
}
/*********************************************************/
/*********           Mail UI methods             *********/
/*********************************************************/
rcube_webmail.prototype.managesieve_create = function(force)
{
  if (!force && this.env.action != 'show' && !$('#'+this.env.contentframe).is(':visible')) {
  if (!force && this.env.action != 'show') {
    var uid = this.message_list.get_single_selection(),
      lock = this.set_busy(true, 'loading');