Thomas Bruederli
2014-05-07 b2992dd2283c3d0ac95f3293497dfaed0493f607
skins/larry/ui.js
@@ -33,6 +33,8 @@
  var mailviewsplit;
  var compose_headers = {};
  var prefs;
  var focused_popup;
  var popup_keyboard_active = false;
  // export public methods
  this.set = setenv;
@@ -40,6 +42,7 @@
  this.init_tabs = init_tabs;
  this.show_about = show_about;
  this.show_popup = show_popup;
  this.toggle_popup = toggle_popup;
  this.add_popup = add_popup;
  this.set_searchmod = set_searchmod;
  this.set_searchscope = set_searchscope;
@@ -138,8 +141,9 @@
    /***  mail task  ***/
    if (rcmail.env.task == 'mail') {
      rcmail.addEventListener('menu-open', menu_open)
        .addEventListener('menu-save', menu_save)
      rcmail.addEventListener('menu-open', menu_toggle)
        .addEventListener('menu-close', menu_toggle)
        .addEventListener('menu-save', save_listoptions)
        .addEventListener('responseafterlist', function(e){ switch_view_mode(rcmail.env.threading ? 'thread' : 'list', true) })
        .addEventListener('responseaftersearch', function(e){ switch_view_mode(rcmail.env.threading ? 'thread' : 'list', true) });
@@ -157,7 +161,14 @@
        // add menu link for each attachment
        $('#attachment-list > li').each(function() {
          $(this).append($('<a class="drop"></a>').click(function() { attachmentmenu(this); }));
          $(this).append($('<a class="drop" tabindex="0" aria-haspopup="true">Show options</a>')
              .bind('click keypress', function(e) {
                  if (e.type != 'keypress' || rcube_event.get_keycode(e) == 13) {
                      attachmentmenu(this, e);
                      return false;
                  }
              })
          );
        });
        if (get_pref('previewheaders') == '1') {
@@ -256,6 +267,10 @@
        new rcube_splitter({ id:'identviewsplitter', p1:'#identitieslist', p2:'#identity-details',
          orientation:'v', relative:true, start:266, min:180, size:12 }).init();
      }
      else if (rcmail.env.action == 'responses') {
        new rcube_splitter({ id:'responseviewsplitter', p1:'#identitieslist', p2:'#identity-details',
          orientation:'v', relative:true, start:266, min:180, size:12 }).init();
      }
      else if (rcmail.env.action == 'preferences' || !rcmail.env.action) {
        new rcube_splitter({ id:'prefviewsplitter', p1:'#sectionslist', p2:'#preferences-box',
          orientation:'v', relative:true, start:266, min:180, size:12 }).init();
@@ -329,6 +344,10 @@
          var val = $('option:selected', this).text();
          $(this).next().children().text(val);
        });
      select
        .on('focus', function(e){ overlay.addClass('focus'); })
        .on('blur', function(e){ overlay.removeClass('focus'); });
    });
    // set min-width to show all toolbar buttons
@@ -339,14 +358,7 @@
    $(document.body)
      .bind('mouseup', body_mouseup)
      .bind('keyup', function(e){
        if (e.keyCode == 27) {
          for (var id in popups) {
            if (popups[id].is(':visible'))
              show_popup(id, false);
          }
        }
      });
      .bind('keydown', popup_keypress);
    $('iframe').load(function(e){
      // this = iframe
@@ -471,6 +483,8 @@
          minHeight: 90
        }).show();
      me.messagedialog.closest('div[role=dialog]').attr('role', 'alertdialog');
      me.message_timer = window.setTimeout(dialog_close, p.timeout);
    }
  }
@@ -485,7 +499,7 @@
    $('#message-objects div a').addClass('button');
    if (!$('#attachment-list li').length) {
      $('div.rightcol').hide();
      $('div.rightcol').hide().attr('aria-hidden', 'true');
      $('div.leftcol').css('margin-right', '0');
    }
  }
@@ -582,13 +596,21 @@
  /**
   * Trigger for popup menus
   */
  function show_popup(popup, show, config)
  function toggle_popup(popup, e, config)
  {
    show_popup(popup, undefined, config, rcube_event.is_keyboard(e));
  }
  /**
   * (Deprecated) trigger for popup menus
   */
  function show_popup(popup, show, config, keyboard)
  {
    // auto-register menu object
    if (config || !popupconfig[popup])
      add_popup(popup, config);
    var visible = show_popupmenu(popup, show),
    var visible = show_popupmenu(popup, show, keyboard),
      config = popupconfig[popup];
    if (typeof config.callback == 'function')
      config.callback(visible);
@@ -597,7 +619,7 @@
  /**
   * Show/hide a specific popup menu
   */
  function show_popupmenu(popup, show)
  function show_popupmenu(popup, show, keyboard)
  {
    var obj = popups[popup],
      config = popupconfig[popup],
@@ -634,10 +656,74 @@
      obj.css({ left:pos.left, top:(pos.top + (above ? -obj.height() : ref.offsetHeight)) });
    }
    else if (!show && keyboard && ref.length) {
      ref.focus();
    }
    obj[show?'show':'hide']();
    obj[show?'show':'hide']().attr('aria-hidden', show?'false':'true');
    popup_keyboard_active = show && keyboard;
    if (popup_keyboard_active) {
      focused_popup = popup;
      obj.find('a,input:not(:disabled)').not('[aria-disabled=true]').first().focus();
    }
    else {
      focused_popup = null;
    }
    return show;
  }
  /**
   * Handler for keyboard events on active popups
   */
  function popup_keypress(e)
  {
    var target = e.target || {},
      keyCode = rcube_event.get_keycode(e);
    if (e.keyCode != 27 && (!popup_keyboard_active || target.nodeName == 'TEXTAREA' || target.nodeName == 'SELECT'))
      return true;
    switch (keyCode) {
      case 38:
      case 40:
      case 63232: // "up", in safari keypress
      case 63233: // "down", in safari keypress
        popup_focus_item(mod = keyCode == 38 || keyCode == 63232 ? -1 : 1);
        break;
      case 9:   // tab
        if (focused_popup) {
          var mod = rcube_event.get_modifier(e);
          if (!popup_focus_item(mod == SHIFT_KEY ? -1 : 1)) {
            show_popup(focused_popup, false, undefined, true);
          }
        }
        return rcube_event.cancel(e);
      case 27:  // esc
        for (var id in popups) {
          if (popups[id].is(':visible'))
            show_popup(id, false, undefined, true);
        }
        break;
    }
    return true;
  }
  /**
   * Helper method to move focus to the next/prev popup menu item
   */
  function popup_focus_item(dir)
  {
    var obj, mod = dir < 0 ? 'prevAll' : 'nextAll', limit = dir < 0 ? 'last' : 'first';
    if (focused_popup && (obj = popups[focused_popup])) {
      return obj.find(':focus').closest('li')[mod](':has(:not([aria-disabled=true]))').find('a,input')[limit]().focus().length;
    }
    return 0;
  }
  /**
@@ -730,25 +816,20 @@
  function switch_view_mode(mode, force)
  {
    if (force || !$('#mail'+mode+'mode').hasClass('disabled')) {
      $('#maillistmode, #mailthreadmode').removeClass('selected');
      $('#mail'+mode+'mode').addClass('selected');
      $('#maillistmode, #mailthreadmode').removeClass('selected').attr('tabindex', '0').attr('aria-disabled', 'false');
      $('#mail'+mode+'mode').addClass('selected').attr('tabindex', '-1').attr('aria-disabled', 'true');
    }
  }
  /**** popup callbacks ****/
  function menu_open(p)
  function menu_toggle(p)
  {
    if (p && p.props && p.props.menu == 'attachmentmenu')
      show_popupmenu('attachmentmenu');
      show_popupmenu('attachmentmenu', undefined, rcube_event.is_keyboard(p.originalEvent));
    else
      show_listoptions();
  }
  function menu_save(prop)
  {
    save_listoptions();
      show_listoptions(p);
  }
  function searchmenu(show)
@@ -785,7 +866,7 @@
    }
  }
  function attachmentmenu(elem)
  function attachmentmenu(elem, event)
  {
    var id = elem.parentNode.id.replace(/^attach/, '');
@@ -798,25 +879,29 @@
    });
    popupconfig.attachmentmenu.link = elem;
    rcmail.command('menu-open', {menu: 'attachmentmenu', id: id});
    rcmail.command('menu-open', {menu: 'attachmentmenu', id: id}, elem, event);
  }
  function spellmenu(show)
  {
    var link, li,
    var k, link, li,
      lang = rcmail.spellcheck_lang(),
      menu = popups.spellmenu,
      ul = $('ul', menu);
    if (!ul.length) {
      ul = $('<ul class="toolbarmenu selectable">');
      ul = $('<ul class="toolbarmenu selectable" role="menu">');
      for (i in rcmail.env.spell_langs) {
        li = $('<li>');
        link = $('<a href="#"></a>').text(rcmail.env.spell_langs[i])
          .addClass('active').data('lang', i)
          .click(function() {
            rcmail.spellcheck_lang_set($(this).data('lang'));
      for (k in rcmail.env.spell_langs) {
        li = $('<li role="menuitem">');
        link = $('<a href="#'+k+'" tabindex="0"></a>').text(rcmail.env.spell_langs[k])
          .addClass('active').data('lang', k)
          .bind('click keypress', function(e) {
              if (e.type != 'keypress' || rcube_event.get_keycode(e) == 13) {
                  rcmail.spellcheck_lang_set($(this).data('lang'));
                  show_popupmenu('spellmenu', false, rcube_event.is_keyboard(e))
                  return false;
              }
          });
        link.appendTo(li);
@@ -830,9 +915,9 @@
    $('li', ul).each(function() {
      var el = $('a', this);
      if (el.data('lang') == lang)
        el.addClass('selected');
        el.addClass('selected').attr('aria-selected', 'true');
      else if (el.hasClass('selected'))
        el.removeClass('selected');
        el.removeClass('selected').removeAttr('aria-selected');
    });
  }
@@ -840,13 +925,13 @@
  /**
   *
   */
  function show_listoptions()
  function show_listoptions(p)
  {
    var $dialog = $('#listoptions');
    // close the dialog
    if ($dialog.is(':visible')) {
      $dialog.dialog('close');
      $dialog.dialog('close', p.originalEvent);
      return;
    }
@@ -865,8 +950,13 @@
      resizable: false,
      closeOnEscape: true,
      title: null,
      close: function() {
      open: function(e) {
        setTimeout(function(){ $dialog.find('a, input:not(:disabled)').not('[aria-disabled=true]').first().focus(); }, 100);
      },
      close: function(e) {
        $dialog.dialog('destroy').hide();
        if (e.originalEvent && rcube_event.is_keyboard(e.originalEvent))
          $('#listmenulink').focus();
      },
      minWidth: 500,
      width: $dialog.width()+25
@@ -877,10 +967,13 @@
  /**
   *
   */
  function save_listoptions()
  function save_listoptions(p)
  {
    $('#listoptions').dialog('close');
    if (rcube_event.is_keyboard(p.originalEvent))
      $('#listmenulink').focus();
    var sort = $('input[name="sort_col"]:checked').val(),
      ord = $('input[name="sort_ord"]:checked').val(),
      cols = $('input[name="list_col[]"]:checked')