Aleksander Machniak
2013-12-15 9a0153324eeb1f0e808cb1a063d1f37d49ad48e2
skins/larry/ui.js
@@ -19,7 +19,7 @@
    searchmenu:         { editable:1, callback:searchmenu },
    attachmentmenu:     { },
    listoptions:        { editable:1 },
    dragmessagemenu:    { sticky:1 },
    dragmenu:           { sticky:1 },
    groupmenu:          { above:1 },
    mailboxmenu:        { above:1 },
    spellmenu:          { callback: spellmenu },
@@ -31,6 +31,7 @@
  var me = this;
  var mailviewsplit;
  var compose_headers = {};
  var prefs;
  // export public methods
  this.set = setenv;
@@ -44,13 +45,19 @@
  this.show_header_row = show_header_row;
  this.hide_header_row = hide_header_row;
  this.update_quota = update_quota;
  this.get_pref = get_pref;
  this.save_pref = save_pref;
  // set minimal mode on small screens (don't wait for document.ready)
  if (window.$ && document.body) {
    var minmode = rcmail.get_cookie('minimalmode');
    var minmode = get_pref('minimalmode');
    if (parseInt(minmode) || (minmode === null && $(window).height() < 850)) {
      $(document.body).addClass('minimal');
    }
    if (bw.tablet) {
      $('#viewport').attr('content', "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0");
    }
  }
@@ -61,6 +68,51 @@
  function setenv(key, val)
  {
    env[key] = val;
  }
  /**
   * Get preference stored in browser
   */
  function get_pref(key)
  {
    if (!prefs) {
      prefs = window.localStorage ? rcmail.local_storage_get_item('prefs.larry', {}) : {};
    }
    // fall-back to cookies
    if (prefs[key] == null) {
      var cookie = rcmail.get_cookie(key);
      if (cookie != null) {
        prefs[key] = cookie;
        // copy value to local storage and remove cookie
        if (window.localStorage) {
          rcmail.local_storage_set_item('prefs.larry', prefs);
          rcmail.set_cookie(key, cookie, new Date());  // expire cookie
        }
      }
    }
    return prefs[key];
  }
  /**
   * Saves preference value to browser storage
   */
  function save_pref(key, val)
  {
    prefs[key] = val;
    // write prefs to local storage
    if (window.localStorage) {
      rcmail.local_storage_set_item('prefs.larry', prefs);
    }
    else {
      // store value in cookie
      var exp = new Date();
      exp.setYear(exp.getFullYear() + 1);
      rcmail.set_cookie(key, val, exp);
    }
  }
  /**
@@ -78,7 +130,7 @@
    $('#taskbar .minmodetoggle').click(function(e){
      var ismin = $(document.body).toggleClass('minimal').hasClass('minimal');
      rcmail.set_cookie('minimalmode', ismin?1:0);
      save_pref('minimalmode', ismin?1:0);
      $(window).resize();
    });
@@ -90,8 +142,8 @@
      var dragmenu = $('#dragmessagemenu');
      if (dragmenu.length) {
        rcmail.gui_object('message_dragmenu', 'dragmessagemenu');
        popups.dragmessagemenu = dragmenu;
        rcmail.gui_object('dragmenu', 'dragmessagemenu');
        popups.dragmenu = dragmenu;
      }
      if (rcmail.env.action == 'show' || rcmail.env.action == 'preview') {
@@ -102,14 +154,17 @@
        // add menu link for each attachment
        $('#attachment-list > li').each(function() {
          $(this).append($('<a class="drop">').click(function() { attachmentmenu(this); }));
          $(this).append($('<a class="drop"></a>').click(function() { attachmentmenu(this); }));
        });
      }
      else if (rcmail.env.action == 'compose') {
        rcmail.addEventListener('aftertoggle-editor', function(){ window.setTimeout(function(){ layout_composeview() }, 200); });
        rcmail.addEventListener('aftertoggle-editor', function(e){
          window.setTimeout(function(){ layout_composeview() }, 200);
          if (e && e.mode)
            $("select[name='editorSelector']").val(e.mode);
        });
        rcmail.addEventListener('aftersend-attachment', show_uploadform);
        rcmail.addEventListener('add-recipient', function(p){ show_header_row(p.field, true); });
        layout_composeview();
        // Show input elements with non-empty value
        var f, v, field, fields = ['cc', 'bcc', 'replyto', 'followupto'];
@@ -126,8 +181,17 @@
          $('#composeoptionstoggle').toggleClass('remove');
          $('#composeoptions').toggle();
          layout_composeview();
          save_pref('composeoptions', $('#composeoptions').is(':visible') ? '1' : '0');
          return false;
        }).css('cursor', 'pointer');
        if (get_pref('composeoptions') !== '0') {
          $('#composeoptionstoggle').click();
        }
        // adjust hight when textarea starts to scroll
        $("textarea[name='_to'], textarea[name='_cc'], textarea[name='_bcc']").change(function(e){ adjust_compose_editfields(this); }).change();
        rcmail.addEventListener('autocomplete_insert', function(p){ adjust_compose_editfields(p.field); });
        // toggle compose options if opened in new window and they were visible before
        var opener_rc = rcmail.opener();
@@ -153,6 +217,10 @@
        rcmail.addEventListener('setquota', update_quota);
        rcmail.addEventListener('enable-command', enable_command);
        rcmail.addEventListener('afterimport-messages', show_uploadform);
      }
      else if (rcmail.env.action == 'get') {
        new rcube_splitter({ id:'mailpartsplitterv', p1:'#messagepartheader', p2:'#messagepartcontainer',
          orientation:'v', relative:true, start:226, min:150, size:12}).init();
      }
      if ($('#mailview-left').length) {
@@ -187,10 +255,25 @@
        new rcube_splitter({ id:'prefviewsplitter', p1:'#sectionslist', p2:'#preferences-box',
          orientation:'v', relative:true, start:266, min:180, size:12 }).init();
      }
      else if (rcmail.env.action == 'edit-prefs') {
        $('<a href="#toggle">&#9660;</a>')
            .addClass('advanced-toggle')
            .appendTo('#preferences-details fieldset.advanced legend');
          $('#preferences-details fieldset.advanced legend').click(function(e){
            var collapsed = $(this).hasClass('collapsed'),
              toggle = $('.advanced-toggle', this).html(collapsed ? '&#9650;' : '&#9660;');
            $(this)
              .toggleClass('collapsed')
              .closest('fieldset').children('.propform').toggle()
          }).addClass('collapsed')
      }
    }
    /***  addressbook task  ***/
    else if (rcmail.env.task == 'addressbook') {
      rcmail.addEventListener('afterupload-photo', show_uploadform);
      rcmail.addEventListener('beforepushgroup', push_contactgroup);
      rcmail.addEventListener('beforepopgroup', pop_contactgroup);
      if (rcmail.env.action == '') {
        new rcube_splitter({ id:'addressviewsplitterd', p1:'#addressview-left', p2:'#addressview-right',
@@ -200,12 +283,12 @@
        new rcube_scroller('#directorylist-content', '#directorylist-header', '#directorylist-footer');
      }
    }
    // set min-width to show all toolbar buttons
    var screen = $('.minwidth');
    if (screen.length) {
      screen.css('min-width', $('.toolbar').width() + $('#quicksearchbar').parent().width() + 20);
      var dragmenu = $('#dragcontactmenu');
      if (dragmenu.length) {
        rcmail.gui_object('dragmenu', 'dragcontactmenu');
        popups.dragmenu = dragmenu;
      }
    }
    // turn a group of fieldsets into tabs
@@ -219,6 +302,7 @@
      }
      var select = $(this),
        parent = select.parent(),
        height = Math.max(select.height(), 26) - 2,
        width = select.width() - 22,
        title = $('option', this).first().text();
@@ -233,19 +317,22 @@
      overlay.children().width(width).height(height).css('line-height', (height - 1) + 'px');
      select.change(function() {
          var val = $('option:selected', this).text();
          $(this).next().children().html(val);
        });
      var parent = select.parent();
      if (parent.css('position') != 'absolute')
        parent.css('position', 'relative');
      // re-set original select width to fix click action and options width in some browsers
      if (!bw.mz)
        select.width(overlay.width());
      select.width(overlay.width())
        .change(function() {
          var val = $('option:selected', this).text();
          $(this).next().children().text(val);
        });
    });
    // set min-width to show all toolbar buttons
    var screen = $('body > div.minwidth');
    if (screen.length) {
      screen.css('min-width', $('.toolbar').width() + $('#quicksearchbar').width() + $('#searchfilter').width() + 30);
    }
    $(document.body)
      .bind('mouseup', body_mouseup)
@@ -282,8 +369,10 @@
  function body_mouseup(e)
  {
    var config, obj, target = e.target;
    if (target.className == 'inner')
        target = e.target.parentNode;
    for (var id in popups) {
      obj = popups[id];
      config = popupconfig[id];
@@ -292,9 +381,10 @@
        && !config.toggle
        && (!config.editable || !target_overlaps(target, obj.get(0)))
        && (!config.sticky || !rcube_mouse_is_over(e, obj.get(0)))
        && !$(target).is('.folder-selector-link')
      ) {
        var myid = id+'';
        window.setTimeout(function(){ show_popupmenu(myid, false) }, 10);
        window.setTimeout(function() { show_popupmenu(myid, false); }, 10);
      }
    }
  }
@@ -339,8 +429,15 @@
   */
  function message_displayed(p)
  {
    var siblings = $(p.object).siblings('div');
    if (siblings.length)
      $(p.object).insertBefore(siblings.first());
    // show a popup dialog on errors
    if (p.type == 'error' && rcmail.env.task != 'login') {
      // hide original message object, we don't want both
      rcmail.hide_message(p.object);
      if (me.message_timer) {
        window.clearTimeout(me.message_timer);
      }
@@ -349,8 +446,10 @@
      }
      var msg = p.message,
        pos = $(p.object).offset();
      pos.top -= (rcmail.env.task == 'login' ? 20 : 160);
        dialog_close = function() {
          // check if dialog is still displayed, to prevent from js error
          me.messagedialog.is(':visible') && me.messagedialog.dialog('destroy').hide();
        };
      if (me.messagedialog.is(':visible'))
        msg = me.messagedialog.html() + '<p>' + p.message + '</p>';
@@ -361,16 +460,14 @@
          closeOnEscape: true,
          dialogClass: 'popupmessage ' + p.type,
          title: env.errortitle,
          close: function() {
            me.messagedialog.dialog('destroy').hide();
          },
          position: ['center', pos.top],
          hide: { effect:'drop', direction:'down' },
          close: dialog_close,
          position: ['center', 'center'],
          hide: {effect: 'fadeOut'},
          width: 420,
          minHeight: 90
        }).show();
      me.message_timer = window.setTimeout(function(){ me.messagedialog.dialog('close'); }, Math.max(2000, p.timeout / 2));
      me.message_timer = window.setTimeout(dialog_close, p.timeout);
    }
  }
@@ -401,6 +498,16 @@
    // STUB
  }
  function adjust_compose_editfields(elem)
  {
    if (elem.nodeName == 'TEXTAREA') {
      var $elem = $(elem), line_height = 14,  // hard-coded because some browsers only provide the outer height in elem.clientHeight
        content_height = elem.scrollHeight,
        rows = elem.value.length > 80 && content_height > line_height*1.5 ? 2 : 1;
      $elem.css('height', (line_height*rows) + 'px');
      layout_composeview();
    }
  }
  function layout_composeview()
  {
@@ -446,7 +553,7 @@
  function enable_command(p)
  {
    if (p.command == 'reply-list') {
    if (p.command == 'reply-list' && rcmail.env.reply_all_mode == 1) {
      var label = rcmail.gettext(p.status ? 'replylist' : 'replyall');
      if (rcmail.env.action == 'preview')
        $('a.button.replyall').attr('title', label);
@@ -557,7 +664,7 @@
    var button = $(e.target),
      frame = $('#mailpreviewframe'),
      visible = !frame.is(':visible'),
      splitter = mailviewsplit.pos || parseInt(rcmail.get_cookie('mailviewsplitter') || 320),
      splitter = mailviewsplit.pos || parseInt(get_pref('mailviewsplitter') || 320),
      topstyles, bottomstyles, uid;
    frame.toggle();
@@ -565,7 +672,7 @@
    if (visible) {
      $('#mailview-top').removeClass('fullheight').css({ bottom:'auto' });
      $('#mailview-bottom').css({ height:'auto' });
      $('#mailview-bottom').css({ height:'auto' }).show();
      rcmail.env.contentframe = 'messagecontframe';
      if (uid = rcmail.message_list.get_single_selection())
@@ -583,15 +690,18 @@
      rcmail.env.contentframe = null;
      rcmail.show_contentframe(false);
      $('#mailview-top').addClass('fullheight').css({ height:'auto', bottom:'28px' });
      $('#mailview-bottom').css({ top:'auto', height:'26px' });
      $('#mailview-top').addClass('fullheight').css({ height:'auto', bottom:'0px' });
      $('#mailview-bottom').css({ top:'auto', height:'0px' }).hide();
      if (mailviewsplit.handle)
        mailviewsplit.handle.hide();
    }
    if (visible && uid && rcmail.message_list)
      rcmail.message_list.scrollto(uid);
    if (rcmail.message_list) {
      if (visible && uid)
          rcmail.message_list.scrollto(uid);
      rcmail.message_list.resize();
    }
    rcmail.command('save-pref', { name:'preview_pane', value:(visible?1:0) });
  }
@@ -825,6 +935,35 @@
    });
  }
  function push_contactgroup(p)
  {
    // lets the contacts list swipe to the left, nice!
    var table = $('#contacts-table'),
      scroller = table.parent().css('overflow', 'hidden');
    table.clone()
      .css({ position:'absolute', top:'0', left:'0', width:table.width()+'px', 'z-index':10 })
      .appendTo(scroller)
      .animate({ left: -(table.width()+5) + 'px' }, 300, 'swing', function(){
        $(this).remove();
        scroller.css('overflow', 'auto')
      });
  }
  function pop_contactgroup(p)
  {
    // lets the contacts list swipe to the left, nice!
    var table = $('#contacts-table'),
      scroller = table.parent().css('overflow', 'hidden'),
      clone = table.clone().appendTo(scroller);
      table.css({ position:'absolute', top:'0', left:-(table.width()+5) + 'px', width:table.width()+'px', height:table.height()+'px', 'z-index':10 })
        .animate({ left:'0' }, 300, 'linear', function(){
        clone.remove();
        $(this).css({ position:'relative', left:'0', width:'100%', height:'auto', 'z-index':1 });
        scroller.css('overflow', 'auto')
      });
  }
  function show_uploadform()
  {
@@ -835,7 +974,7 @@
      $dialog.dialog('close');
      return;
    }
    // add icons to clone file input field
    if (rcmail.env.action == 'compose' && !$dialog.data('extended')) {
      $('<a>')
@@ -1098,7 +1237,7 @@
      $(window).resize(onResize);
    // read saved position from cookie
    var cookie = rcmail.get_cookie(this.id);
    var cookie = this.get_cookie();
    if (cookie && !isNaN(cookie)) {
      this.pos = parseFloat(cookie);
      this.resize();
@@ -1187,6 +1326,18 @@
    if (!me.drag_active)
      return false;
    // with timing events dragging action is more responsive
    window.clearTimeout(me.ts);
    me.ts = window.setTimeout(function() { onDragAction(e); }, 1);
    return false;
  };
  /**
   * Dragging action (see onDrag())
   */
  function onDragAction(e)
  {
    var pos = rcube_event.get_mouse_pos(e);
    if (me.relative) {
@@ -1210,7 +1361,6 @@
    me.p1pos = me.relative ? me.p1.position() : me.p1.offset();
    me.p2pos = me.relative ? me.p2.position() : me.p2.offset();
    return false;
  };
  /**
@@ -1253,13 +1403,20 @@
  };
  /**
   * Get saved splitter position from cookie
   */
  this.get_cookie = function()
  {
    return window.UI ? UI.get_pref(this.id) : null;
  };
  /**
   * Saves splitter position in cookie
   */
  this.set_cookie = function()
  {
    var exp = new Date();
    exp.setYear(exp.getFullYear() + 1);
    rcmail.set_cookie(this.id, this.pos, exp);
    if (window.UI)
      UI.save_pref(this.id, this.pos);
  };
} // end class rcube_splitter