alecpl
2012-04-14 9870dac30de1b8d1bdeab57af3eeb7c89b21ef76
commit | author | age
c7dcb3 1 /**
T 2  * Roundcube functions for default skin interface
74d4c7 3  *
T 4  * Copyright (c) 2011, The Roundcube Dev Team
5  *
6  * The contents are subject to the Creative Commons Attribution-ShareAlike
7  * License. It is allowed to copy, distribute, transmit and to adapt the work
8  * by keeping credits to the original autors in the README file.
9  * See http://creativecommons.org/licenses/by-sa/3.0/ for details.
10  *
11  * $Id$
c7dcb3 12  */
T 13
14
15 function rcube_mail_ui()
16 {
68e13c 17   var env = {};
c7dcb3 18   var popups = {};
T 19   var popupconfig = {
20     forwardmenu:        { editable:1 },
21     searchmenu:         { editable:1, callback:searchmenu },
22     listoptions:        { editable:1 },
23     dragmessagemenu:    { sticky:1 },
24     groupmenu:          { above:1 },
25     mailboxmenu:        { above:1 },
26     composeoptionsmenu: { editable:1, overlap:1 },
4be86f 27     spellmenu:          { callback: spellmenu },
c7dcb3 28     // toggle: #1486823, #1486930
T 29     'attachment-form':  { editable:1, above:1, toggle:!bw.ie&&!bw.linux },
30     'upload-form':      { editable:1, toggle:!bw.ie&&!bw.linux }
31   };
32
33   var me = this;
918bb9 34   var mailviewsplit;
74d4c7 35   var compose_headers = {};
c7dcb3 36
T 37   // export public methods
68e13c 38   this.set = setenv;
c7dcb3 39   this.init = init;
bab043 40   this.init_tabs = init_tabs;
68e13c 41   this.show_about = show_about;
c7dcb3 42   this.show_popup = show_popup;
T 43   this.set_searchmod = set_searchmod;
74d4c7 44   this.show_uploadform = show_uploadform;
T 45   this.show_header_row = show_header_row;
46   this.hide_header_row = hide_header_row;
68e13c 47
T 48
49   /**
50    *
51    */
52   function setenv(key, val)
53   {
54     env[key] = val;
55   }
c7dcb3 56
T 57   /**
5ff7ba 58    * Initialize UI
T 59    * Called on document.ready
c7dcb3 60    */
T 61   function init()
62   {
0e530b 63     rcmail.addEventListener('message', message_displayed);
5ff7ba 64
T 65     /***  mail task  ***/
c7dcb3 66     if (rcmail.env.task == 'mail') {
74d4c7 67       rcmail.addEventListener('menu-open', show_listoptions);
c7dcb3 68       rcmail.addEventListener('menu-save', save_listoptions);
836549 69       rcmail.addEventListener('responseafterlist', function(e){ switch_view_mode(rcmail.env.threading ? 'thread' : 'list') });
74d4c7 70
T 71       var dragmenu = $('#dragmessagemenu');
72       if (dragmenu.length) {
73         rcmail.gui_object('message_dragmenu', 'dragmessagemenu');
74         popups.dragmessagemenu = dragmenu;
75       }
918bb9 76
c7dcb3 77       if (rcmail.env.action == 'show' || rcmail.env.action == 'preview') {
28e18c 78         layout_messageview();
543ccb 79         $("#all-headers").resizable({ handles: 's', minHeight: 50 });
f4aaf8 80         $('#previewheaderstoggle').click(function(e){ toggle_preview_headers(this); return false });
74d4c7 81       }
T 82       else if (rcmail.env.action == 'compose') {
071c78 83         rcmail.addEventListener('aftertoggle-editor', function(){ window.setTimeout(function(){ layout_composeview() }, 200); });
68e13c 84         rcmail.addEventListener('aftersend-attachment', show_uploadform);
7961f8 85         rcmail.addEventListener('add-recipient', function(p){ show_header_row(p.field, true); });
74d4c7 86         layout_composeview();
T 87
b4e41f 88         // Show input elements with non-empty value
T 89         var field, fields = ['cc', 'bcc', 'replyto', 'followupto'];
90         for (var f=0; f < fields.length; f++) {
91           if ((field = $('#_'+fields[f])) && field.length && field.val() != '')
92             show_header_row(fields[f], true);
93         }
94
bab043 95         $('#composeoptionstoggle').parent().click(function(){
T 96           $('#composeoptionstoggle').toggleClass('enabled');
74d4c7 97           $('#composeoptions').toggle();
T 98           layout_composeview();
99           return false;
bab043 100         }).css('cursor', 'pointer');
74d4c7 101
T 102         new rcube_splitter({ id:'composesplitterv', p1:'#composeview-left', p2:'#composeview-right',
071c78 103           orientation:'v', relative:true, start:248, min:170, size:12, render:layout_composeview }).init();
918bb9 104       }
T 105       else if (rcmail.env.action == 'list' || !rcmail.env.action) {
5ff7ba 106         var previewframe = $('#mailpreviewframe').is(':visible');
T 107         $('#mailpreviewtoggle').addClass(previewframe ? 'enabled' : 'closed').click(function(e){ toggle_preview_pane(e); return false });
108         $('#maillistmode').addClass(rcmail.env.threading ? '' : 'selected').click(function(e){ switch_view_mode('list'); return false });
109         $('#mailthreadmode').addClass(rcmail.env.threading ? 'selected' : '').click(function(e){ switch_view_mode('thread'); return false });
847d31 110
5ff7ba 111         mailviewsplit = new rcube_splitter({ id:'mailviewsplitter', p1:'#mailview-top', p2:'#mailview-bottom',
T 112           orientation:'h', relative:true, start:310, min:150, size:0, offset:-22 });
113         if (previewframe)
114           mailviewsplit.init();
115
efaf2e 116         new rcube_scroller('#folderlist-content', '#folderlist-header', '#folderlist-footer');
A 117
5ff7ba 118         rcmail.addEventListener('setquota', update_quota);
918bb9 119       }
847d31 120
918bb9 121       if ($('#mailview-left').length) {
T 122         new rcube_splitter({ id:'mailviewsplitterv', p1:'#mailview-left', p2:'#mailview-right',
f45a97 123           orientation:'v', relative:true, start:226, min:150, size:12, callback:render_mailboxlist, render:resize_leftcol }).init();
c7dcb3 124       }
28e18c 125     }
5ff7ba 126     /***  settings task  ***/
28e18c 127     else if (rcmail.env.task == 'settings') {
4f1b7a 128       rcmail.addEventListener('init', function(){
T 129         var tab = '#settingstabpreferences';
130         if (rcmail.env.action)
131           tab = '#settingstab' + (rcmail.env.action.indexOf('identity')>0 ? 'identities' : rcmail.env.action.replace(/\./g, ''));
28e18c 132
4f1b7a 133         $(tab).addClass('selected')
T 134           .children().first().removeAttr('onclick').click(function() { return false; });
135       });
918bb9 136
T 137       if (rcmail.env.action == 'folders') {
138         new rcube_splitter({ id:'folderviewsplitter', p1:'#folderslist', p2:'#folder-details',
f45a97 139           orientation:'v', relative:true, start:266, min:180, size:12 }).init();
fe8374 140
efaf2e 141         new rcube_scroller('#folderslist-content', '#folderslist-header', '#folderslist-footer');
A 142
fe8374 143         rcmail.addEventListener('setquota', update_quota);
918bb9 144       }
7c2a93 145       else if (rcmail.env.action == 'identities') {
918bb9 146         new rcube_splitter({ id:'identviewsplitter', p1:'#identitieslist', p2:'#identity-details',
f45a97 147           orientation:'v', relative:true, start:266, min:180, size:12 }).init();
918bb9 148       }
c7dcb3 149     }
5ff7ba 150     /***  addressbook task  ***/
bab043 151     else if (rcmail.env.task == 'addressbook') {
68e13c 152       rcmail.addEventListener('afterupload-photo', show_uploadform);
T 153
154       if (rcmail.env.action == '') {
155         new rcube_splitter({ id:'addressviewsplitterd', p1:'#addressview-left', p2:'#addressview-right',
156           orientation:'v', relative:true, start:226, min:150, size:12, render:resize_leftcol }).init();
157         new rcube_splitter({ id:'addressviewsplitter', p1:'#addresslist', p2:'#contacts-box',
1ba07f 158           orientation:'v', relative:true, start:286, min:270, size:12 }).init();
efaf2e 159
A 160         new rcube_scroller('#directorylist-content', '#directorylist-header', '#directorylist-footer');
68e13c 161       }
822a1e 162     }
bab043 163
T 164     // turn a group of fieldsets into tabs
165     $('.tabbed').each(function(idx, elem){ init_tabs(elem); })
c7dcb3 166
7a32e9 167     // decorate select elements
85f11f 168     $('select.decorated').each(function(){
A 169       if (bw.opera) {
170         $(this).removeClass('decorated');
171         return;
172       }
7a32e9 173
85f11f 174       var title = $('option', this).first().text();
A 175       if ($('option:selected', this).val() != '')
176         title = $('option:selected', this).text();
7a32e9 177
85f11f 178       var select = $(this)
A 179         .change(function(){
180           var val = $('option:selected', this).text();
181           $(this).next().children().html(val);
182         });
183
184       $('<a class="menuselector dropdownselector"><span class="handle">' + title + '</span></a>')
185         .css('position', 'absolute')
186         .offset(select.position())
187         .insertAfter(select)
188         .children().width(select.outerWidth() - 40);
189
190       select.parent().css('position', 'relative');
191     });
7a32e9 192
T 193     $(document.body)
194       .bind('mouseup', body_mouseup)
195       .bind('keyup', function(e){
196         if (e.keyCode == 27) {
197           for (var id in popups) {
198             if (popups[id].is(':visible'))
199               show_popup(id, false);
200           }
c7dcb3 201         }
7a32e9 202       });
7961f8 203
14a3f2 204     $('iframe').load(function(e){
T 205       // this = iframe
206       var doc = this.contentDocument ? this.contentDocument : this.contentWindow ? this.contentWindow.document : null;
207       $(doc).mouseup(body_mouseup);
208     })
209     .contents().mouseup(body_mouseup);
210
7961f8 211     $(window).resize(function(e) {
T 212       // check target due to bugs in jquery
213       // http://bugs.jqueryui.com/ticket/7514
214       // http://bugs.jquery.com/ticket/9841
215       if (e.target == window) resize();
216     });
28e18c 217   }
T 218
219   /**
14a3f2 220    * Handler for mouse-up events on the document body.
T 221    * This will close all open popup menus
222    */
223   function body_mouseup(e)
224   {
225     var config, obj, target = e.target;
226     for (var id in popups) {
227       obj = popups[id];
228       config = popupconfig[id];
229       if (obj.is(':visible')
230         && target.id != id+'link'
231         && !config.toggle
232         && (!config.editable || !target_overlaps(target, obj.get(0)))
233         && (!config.sticky || !rcube_mouse_is_over(e, obj.get(0)))
234       ) {
235         var myid = id+'';
236         window.setTimeout(function(){ show_popupmenu(myid, false) }, 10);
237       }
238     }
239   }
240
241   /**
28e18c 242    * Update UI on window resize
T 243    */
244   function resize()
245   {
246     if (rcmail.env.task == 'mail' && (rcmail.env.action == 'show' || rcmail.env.action == 'preview')) {
247       layout_messageview();
248     }
74d4c7 249     if (rcmail.env.task == 'mail' && rcmail.env.action == 'compose') {
T 250       layout_composeview();
251     }
c7dcb3 252   }
T 253
254   /**
0e530b 255    * Triggered when a new user message is displayed
T 256    */
257   function message_displayed(p)
258   {
259     // show a popup dialog on errors
260     if (p.type == 'error') {
261       if (!me.messagedialog) {
262         me.messagedialog = $('<div>').addClass('popupdialog');
263       }
264
265       var pos = $(p.object).offset();
266       me.messagedialog.dialog('close');
267       me.messagedialog.html(p.message)
268         .dialog({
269           resizable: false,
270           closeOnEscape: true,
271           dialogClass: 'popupmessage ' + p.type,
71e9ef 272           title: env.errortitle,
0e530b 273           close: function() {
T 274             me.messagedialog.dialog('destroy').hide();
275           },
276           position: ['center', pos.top - 160],
277           hide: { effect:'drop', direction:'down' },
278           width: 420,
279           minHeight: 90
280         }).show();
e493c8 281
T 282       window.setTimeout(function(){ me.messagedialog.dialog('close'); }, Math.max(2000, p.timeout / 2));
0e530b 283     }
T 284   }
285
286
287   /**
c7dcb3 288    * Adjust UI objects of the mail view screen
T 289    */
28e18c 290   function layout_messageview()
c7dcb3 291   {
T 292     $('#messagecontent').css('top', ($('#messageheader').outerHeight() + 10) + 'px');
293     $('#message-objects div a').addClass('button');
294     
295     if (!$('#attachment-list li').length) {
b540ed 296       $('div.rightcol').hide();
T 297       $('div.leftcol').css('margin-right', '0');
c7dcb3 298     }
T 299   }
918bb9 300
T 301
302   function render_mailboxlist(splitter)
303   {
0e530b 304     // TODO: implement smart shortening of long folder names
918bb9 305   }
T 306
307
308   function resize_leftcol(splitter)
309   {
310     if (splitter)
311       $('#quicksearchbar input').css('width', (splitter.pos - 70) + 'px');
74d4c7 312   }
T 313
314
315   function layout_composeview()
316   {
317     var body = $('#composebody'),
071c78 318       form = $('#compose-content'),
74d4c7 319       bottom = $('#composeview-bottom'),
T 320       w, h;
321
071c78 322     bottom.css('height', (form.height() - bottom.position().top) + 'px');
74d4c7 323
4be86f 324     w = body.parent().width() - 5;
A 325     h = body.parent().height() - 16;
74d4c7 326     body.width(w).height(h);
T 327
328     if (window.tinyMCE && tinyMCE.get('composebody')) {
4be86f 329       $('#composebody_tbl').width((w+8)+'px').height('').css('margin-top', '1px');
A 330       $('#composebody_ifr').width((w+8)+'px').height((h-40)+'px');
74d4c7 331     }
T 332     else {
333       $('#googie_edit_layer').height(h+'px');
334     }
fe8374 335
071c78 336     var abooks = $('#directorylist');
T 337     $('#compose-contacts .scroller').css('top', abooks.position().top + abooks.outerHeight());
918bb9 338   }
T 339
c7dcb3 340
847d31 341   function update_quota(p)
T 342   {
343     var y = p.total ? Math.ceil(p.percent / 100 * 20) * 24 : 0;
344     $('#quotadisplay').css('background-position', '0 -'+y+'px');
345   }
346
347
c7dcb3 348   /**
T 349    * Trigger for popup menus
350    */
351   function show_popup(popup, show, config)
352   {
353     // auto-register menu object
354     if (config || !popupconfig[popup])
355       popupconfig[popup] = $.extend(popupconfig[popup] || {}, config);
356
357     var visible = show_popupmenu(popup, show),
358       config = popupconfig[popup];
359     if (typeof config.callback == 'function')
360       config.callback(visible);
361   }
362
363   /**
364    * Show/hide a specific popup menu
365    */
366   function show_popupmenu(popup, show)
367   {
368     var obj = popups[popup],
369       config = popupconfig[popup],
370       ref = $('#'+popup+'link'),
371       above = config.above;
372
373     if (!obj) {
374       obj = popups[popup] = $('#'+popup);
375       obj.appendTo(document.body);  // move them to top for proper absolute positioning
376     }
377
378     if (!obj || !obj.length)
379       return false;
380
381     if (typeof show == 'undefined')
382       show = obj.is(':visible') ? false : true;
383     else if (config.toggle && show && obj.is(':visible'))
384       show = false;
385
386     if (show && ref) {
387       var parent = ref.parent(),
388         win = $(window),
389         pos;
390
391       if (parent.hasClass('dropbutton'))
392         ref = parent;
393
394       pos = ref.offset();
395       ref.offsetHeight = ref.outerHeight();
396       if (!above && pos.top + ref.offsetHeight + obj.height() > win.height())
397         above = true;
398       if (pos.left + obj.width() > win.width())
399         pos.left = win.width() - obj.width() - 12;
400
401       obj.css({ left:pos.left, top:(pos.top + (above ? -obj.height() : ref.offsetHeight)) });
402     }
403
404     obj[show?'show':'hide']();
405
406     // hide drop-down elements on buggy browsers
407     if (bw.ie6 && config.overlap) {
408       $('select').css('visibility', show?'hidden':'inherit');
409       $('select', obj).css('visibility', 'inherit');
410     }
4be86f 411
c7dcb3 412     return show;
T 413   }
414
415   /**
416    *
417    */
418   function target_overlaps(target, elem)
419   {
420     while (target.parentNode) {
421       if (target.parentNode == elem)
422         return true;
423       target = target.parentNode;
424     }
425     return false;
426   }
427
428
918bb9 429   /**
T 430    * Show/hide the preview pane
431    */
c7dcb3 432   function toggle_preview_pane(e)
T 433   {
b540ed 434     var button = $(e.target),
T 435       frame = $('#mailpreviewframe'),
436       visible = !frame.is(':visible'),
918bb9 437       splitter = mailviewsplit.pos || parseInt(bw.get_cookie('mailviewsplitter') || 320),
b540ed 438       topstyles, bottomstyles, uid;
T 439
440     frame.toggle();
c7dcb3 441     button.removeClass().addClass(visible ? 'enabled' : 'closed');
T 442
b540ed 443     if (visible) {
918bb9 444       $('#mailview-top').css({ bottom:'auto' });
T 445       $('#mailview-bottom').css({ height:'auto' });
446
b540ed 447       rcmail.env.contentframe = 'messagecontframe';
T 448       if (uid = rcmail.message_list.get_single_selection())
449         rcmail.show_message(uid, false, true);
918bb9 450
T 451       // let the splitter set the correct size and position
452       if (mailviewsplit.handle) {
453         mailviewsplit.handle.show();
454         mailviewsplit.resize();
455       }
456       else
457         mailviewsplit.init();
b540ed 458     }
T 459     else {
460       rcmail.env.contentframe = null;
461       rcmail.show_contentframe(false);
462
918bb9 463       $('#mailview-top').css({ height:'auto', bottom:'28px' });
T 464       $('#mailview-bottom').css({ top:'auto', height:'26px' });
465
466       if (mailviewsplit.handle)
467         mailviewsplit.handle.hide();
468     }
b540ed 469
T 470     if (visible && uid && rcmail.message_list)
471       rcmail.message_list.scrollto(uid);
472
473     rcmail.command('save-pref', { name:'preview_pane', value:(visible?1:0) });
c7dcb3 474   }
T 475
476
918bb9 477   /**
f4aaf8 478    * Switch between short and full headers display in message preview
T 479    */
480   function toggle_preview_headers(button)
481   {
482     $('#preview-shortheaders').toggle();
9bd97c 483     var full = $('#preview-allheaders').toggle(),
A 484       button = $('a#previewheaderstoggle');
485
f4aaf8 486     // add toggle button to full headers table
9bd97c 487     if (full.is(':visible'))
A 488       button.attr('href', '#hide').removeClass('add').addClass('remove')
489     else
490       button.attr('href', '#details').removeClass('remove').addClass('add')
f4aaf8 491   }
T 492
493
494   /**
918bb9 495    *
T 496    */
a4be51 497   function switch_view_mode(mode)
T 498   {
499     if (rcmail.env.threading != (mode == 'thread'))
500       rcmail.set_list_options(null, undefined, undefined, mode == 'thread' ? 1 : 0);
501
502     $('#maillistmode, #mailthreadmode').removeClass('selected');
503     $('#mail'+mode+'mode').addClass('selected');
504   }
505
506
c7dcb3 507   /**** popup callbacks ****/
T 508
509   function searchmenu(show)
510   {
511     if (show && rcmail.env.search_mods) {
512       var n, all,
513         obj = popups['searchmenu'],
514         list = $('input:checkbox[name="s_mods[]"]', obj),
515         mbox = rcmail.env.mailbox,
516         mods = rcmail.env.search_mods;
517
518       if (rcmail.env.task == 'mail') {
519         mods = mods[mbox] ? mods[mbox] : mods['*'];
520         all = 'text';
521       }
522       else {
523         all = '*';
524       }
525
526       if (mods[all])
527         list.map(function() {
528           this.checked = true;
529           this.disabled = this.value != all;
530         });
531       else {
532         list.prop('disabled', false).prop('checked', false);
533         for (n in mods)
534           $('#s_mod_' + n).prop('checked', true);
535       }
536     }
537   }
538
539
4be86f 540   function spellmenu(show)
A 541   {
542     var link, li,
543       lang = rcmail.spellcheck_lang(),
544       menu = popups.spellmenu,
545       ul = $('ul', menu);
546
547     if (!ul.length) {
548       ul = $('<ul class="toolbarmenu selectable">');
549
550       for (i in rcmail.env.spell_langs) {
551         li = $('<li>');
552         link = $('<a href="#">').text(rcmail.env.spell_langs[i])
553           .addClass('active').data('lang', i)
554           .click(function() {
555             rcmail.spellcheck_lang_set($(this).data('lang'));
556           });
557
558         link.appendTo(li);
559         li.appendTo(ul);
560       }
561
562       ul.appendTo(menu);
563     }
564
565     // select current language
566     $('li', ul).each(function() {
567       var el = $('a', this);
568       if (el.data('lang') == lang)
569         el.addClass('selected');
570       else if (el.hasClass('selected'))
571         el.removeClass('selected');
572     });
573   }
574
575
918bb9 576   /**
T 577    *
578    */
74d4c7 579   function show_listoptions()
T 580   {
581     var $dialog = $('#listoptions');
582
583     // close the dialog
584     if ($dialog.is(':visible')) {
585       $dialog.dialog('close');
586       return;
587     }
588
589     // set form values
590     $('input[name="sort_col"][value="'+rcmail.env.sort_col+'"]').prop('checked', true);
591     $('input[name="sort_ord"][value="DESC"]').prop('checked', rcmail.env.sort_order == 'DESC');
592     $('input[name="sort_ord"][value="ASC"]').prop('checked', rcmail.env.sort_order != 'DESC');
593     $('input[name="view"][value="thread"]').prop('checked', rcmail.env.threading ? true : false);
594     $('input[name="view"][value="list"]').prop('checked', rcmail.env.threading ? false : true);
595
596     // list columns
597     var found, cols = $('input[name="list_col[]"]');
598     for (var i=0; i < cols.length; i++) {
599       if (cols[i].value != 'from') {
600         found = $.inArray(cols[i].value, rcmail.env.coltypes) != -1;
601       }
602       else {
603         found = ($.inArray('from', rcmail.env.coltypes) != -1
604           || $.inArray('to', rcmail.env.coltypes) != -1);
605       }
606       $(cols[i]).prop('checked', found);
607     }
608
609     $dialog.dialog({
610       modal: true,
611       resizable: false,
612       closeOnEscape: true,
613       title: null,
614       close: function() {
615         $dialog.dialog('destroy').hide();
616       },
617       width: 650
618     }).show();
619   }
620
621
622   /**
623    *
624    */
c7dcb3 625   function save_listoptions()
T 626   {
74d4c7 627     $('#listoptions').dialog('close');
c7dcb3 628
T 629     var sort = $('input[name="sort_col"]:checked').val(),
630       ord = $('input[name="sort_ord"]:checked').val(),
631       thread = $('input[name="view"]:checked').val(),
632       cols = $('input[name="list_col[]"]:checked')
633         .map(function(){ return this.value; }).get();
634
635     rcmail.set_list_options(cols, sort, ord, thread == 'thread' ? 1 : 0);
636   }
637
638
918bb9 639   /**
T 640    *
641    */
c7dcb3 642   function set_searchmod(elem)
T 643   {
644     var all, m, task = rcmail.env.task,
645       mods = rcmail.env.search_mods,
646       mbox = rcmail.env.mailbox;
647
648     if (!mods)
649       mods = {};
650
651     if (task == 'mail') {
652       if (!mods[mbox])
653         mods[mbox] = rcube_clone_object(mods['*']);
654       m = mods[mbox];
655       all = 'text';
656     }
657     else { //addressbook
658       m = mods;
659       all = '*';
660     }
661
662     if (!elem.checked)
663       delete(m[elem.value]);
664     else
665       m[elem.value] = 1;
666
667     // mark all fields
668     if (elem.value != all)
669       return;
670
671     $('input:checkbox[name="s_mods[]"]').map(function() {
672       if (this == elem)
673         return;
674
675       this.checked = true;
676       if (elem.checked) {
677         this.disabled = true;
678         delete m[this.value];
679       }
680       else {
681         this.disabled = false;
682         m[this.value] = 1;
683       }
684     });
685   }
74d4c7 686
T 687
688   function show_uploadform()
689   {
690     var $dialog = $('#upload-dialog');
691
692     // close the dialog
693     if ($dialog.is(':visible')) {
694       $dialog.dialog('close');
695       return;
696     }
f5521a 697     
T 698     // add icons to clone file input field
354bf1 699     if (rcmail.env.action == 'compose' && !$dialog.data('extended')) {
f5521a 700       $('<a>')
T 701         .addClass('iconlink add')
702         .attr('href', '#add')
703         .html('Add')
704         .appendTo($('input[type="file"]', $dialog).parent())
705         .click(add_uploadfile);
706       $dialog.data('extended', true);
707     }
74d4c7 708
T 709     $dialog.dialog({
710       modal: true,
711       resizable: false,
712       closeOnEscape: true,
713       title: $dialog.attr('title'),
714       close: function() {
715         try { $('#upload-dialog form').get(0).reset(); }
716         catch(e){ }  // ignore errors
717
718         $dialog.dialog('destroy').hide();
f5521a 719         $('div.addline', $dialog).remove();
74d4c7 720       },
T 721       width: 480
722     }).show();
723
724     if (!document.all)
f5521a 725       $('input[type=file]', $dialog).first().click();
74d4c7 726   }
T 727
f5521a 728   function add_uploadfile(e)
T 729   {
730     var div = $(this).parent();
731     var clone = div.clone().addClass('addline').insertAfter(div);
732     clone.children('.iconlink').click(add_uploadfile);
733     clone.children('input').val('');
734
735     if (!document.all)
736       $('input[type=file]', clone).click();
737   }
738
739
74d4c7 740   /**
T 741    *
742    */
7961f8 743   function show_header_row(which, updated)
74d4c7 744   {
7961f8 745     var row = $('#compose-' + which);
T 746     if (row.is(':visible'))
747       return;  // nothing to be done here
748
749     if (compose_headers[which] && !updated)
74d4c7 750       $('#_' + which).val(compose_headers[which]);
7961f8 751
T 752     row.show();
74d4c7 753     $('#' + which + '-link').hide();
68e13c 754     layout_composeview();
74d4c7 755     return false;
T 756   }
757
758   /**
759    *
760    */
761   function hide_header_row(which)
762   {
763     // copy and clear field value
764     var field = $('#_' + which);
765     compose_headers[which] = field.val();
766     field.val('');
767
768     $('#compose-' + which).hide();
769     $('#' + which + '-link').show();
68e13c 770     layout_composeview();
T 771     return false;
74d4c7 772   }
bab043 773
T 774
775   /**
776    * Fieldsets-to-tabs converter
777    */
778   function init_tabs(elem, current)
779   {
a4e71c 780     var content = $(elem),
T 781       id = content.get(0).id,
bab043 782       fs = content.children('fieldset');
T 783
784     if (!fs.length)
785       return;
786
787     if (!id) {
788       id = 'rcmtabcontainer';
a4e71c 789       content.attr('id', id);
bab043 790     }
T 791
792     // first hide not selected tabs
793     current = current || 0;
794     fs.each(function(idx) { if (idx != current) $(this).hide(); });
795
796     // create tabs container
797     var tabs = $('<div>').addClass('tabsbar').prependTo(content);
798
799     // convert fildsets into tabs
800     fs.each(function(idx) {
801       var tab, a, elm = $(this), legend = elm.children('legend');
802
803       // create a tab
804       a   = $('<a>').text(legend.text()).attr('href', '#');
805       tab = $('<span>').attr({'id': 'tab'+idx, 'class': 'tablink'})
806         .click(function() { show_tab(id, idx); return false })
807
808       // remove legend
809       legend.remove();
810       // style fieldset
811       elm.addClass('tab');
812       // style selected tab
813       if (idx == current)
814         tab.addClass('selected');
815
816       // add the tab to container
817       tab.append(a).appendTo(tabs);
818     });
819   }
820
821   function show_tab(id, index)
822   {
823     var fs = $('#'+id).children('fieldset');
824
825     fs.each(function(idx) {
826       // Show/hide fieldset (tab content)
827       $(this)[index==idx ? 'show' : 'hide']();
828       // Select/unselect tab
829       $('#tab'+idx).toggleClass('selected', idx==index);
830     });
831   }
68e13c 832
T 833   /**
834    * Show about page as jquery UI dialog
835    */
836   function show_about(elem)
837   {
838     var frame = $('<iframe>').attr('id', 'aboutframe')
839       .attr('src', rcmail.url('settings/about'))
19b34b 840       .attr('frameborder', '0')
68e13c 841       .appendTo(document.body);
T 842
843     var h = Math.floor($(window).height() * 0.75);
844     var buttons = {};
845     var supportln = $('#supportlink');
846     if (supportln.length && (env.supporturl = supportln.attr('href')))
847       buttons[supportln.html()] = function(e){ env.supporturl.indexOf('mailto:') < 0 ? window.open(env.supporturl) : location.href = env.supporturl };
848
849     frame.dialog({
850       modal: true,
851       resizable: false,
852       closeOnEscape: true,
853       title: elem ? elem.title || elem.innerHTML : null,
854       close: function() {
855         frame.dialog('destroy').remove();
856       },
857       buttons: buttons,
858       width: 640,
859       height: h
860     }).width(640);
861   }
c7dcb3 862 }
T 863
918bb9 864
efaf2e 865 /**
A 866  * Roundcube Scroller class
867  */
868 function rcube_scroller(list, top, bottom)
869 {
870   var ref = this;
871
872   this.list = $(list);
873   this.top = $(top);
874   this.bottom = $(bottom);
875   this.step_size = 6;
876   this.step_time = 20;
877   this.delay = 500;
878
879   this.top
880     .mouseenter(function() { ref.ts = window.setTimeout(function() { ref.scroll('down'); }, ref.delay); })
881     .mouseout(function() { if (ref.ts) window.clearTimeout(ref.ts); });
882
883   this.bottom
884     .mouseenter(function() { ref.ts = window.setTimeout(function() { ref.scroll('up'); }, ref.delay); })
885     .mouseout(function() { if (ref.ts) window.clearTimeout(ref.ts); });
886
887   this.scroll = function(dir)
888   {
889     var ref = this, size = this.step_size;
890
891     if (!rcmail.drag_active)
892       return;
893
894     if (dir == 'down')
895       size *= -1;
896
897     this.list.get(0).scrollTop += size;
898     this.ts = window.setTimeout(function() { ref.scroll(dir); }, this.step_time);
899   };
900 };
901
918bb9 902
T 903 /**
a4e71c 904  * Roundcube UI splitter class
918bb9 905  *
T 906  * @constructor
907  */
908 function rcube_splitter(p)
909 {
910   this.p = p;
911   this.id = p.id;
912   this.horizontal = (p.orientation == 'horizontal' || p.orientation == 'h');
913   this.halfsize = (p.size !== undefined ? p.size : 10) / 2;
914   this.pos = p.start || 0;
915   this.min = p.min || 20;
916   this.offset = p.offset || 0;
917   this.relative = p.relative ? true : false;
918   this.drag_active = false;
919   this.render = p.render;
920   this.callback = p.callback;
921
922   var me = this;
3a86e2 923   rcube_splitter._instances[this.id] = me;
918bb9 924
T 925   this.init = function()
926   {
927     this.p1 = $(this.p.p1);
928     this.p2 = $(this.p.p2);
929
a4e71c 930     // check if referenced elements exist, otherwise abort
T 931     if (!this.p1.length || !this.p2.length)
932       return;
933
918bb9 934     // create and position the handle for this splitter
T 935     this.p1pos = this.relative ? this.p1.position() : this.p1.offset();
936     this.p2pos = this.relative ? this.p2.position() : this.p2.offset();
937     this.handle = $('<div>')
938       .attr('id', this.id)
939       .attr('unselectable', 'on')
940       .addClass('splitter ' + (this.horizontal ? 'splitter-h' : 'splitter-v'))
941       .appendTo(this.p1.parent())
942       .bind('mousedown', onDragStart);
943
944     if (this.horizontal) {
945       var top = this.p1pos.top + this.p1.outerHeight();
946       this.handle.css({ left:'0px', top:top+'px' });
947     }
948     else {
949       var left = this.p1pos.left + this.p1.outerWidth();
950       this.handle.css({ left:left+'px', top:'0px' });
951     }
952
953     // listen to window resize on IE
954     if (bw.ie)
a4e71c 955       $(window).resize(onResize);
918bb9 956
T 957     // read saved position from cookie
958     var cookie = bw.get_cookie(this.id);
959     if (cookie && !isNaN(cookie)) {
960       this.pos = parseFloat(cookie);
961       this.resize();
962     }
963     else if (this.pos) {
964       this.resize();
965       this.set_cookie();
966     }
967   };
968
969   /**
970    * Set size and position of all DOM objects
971    * according to the saved splitter position
972    */
973   this.resize = function()
974   {
975     if (this.horizontal) {
976       this.p1.css('height', Math.floor(this.pos - this.p1pos.top - this.halfsize) + 'px');
977       this.p2.css('top', Math.ceil(this.pos + this.halfsize + 2) + 'px');
978       this.handle.css('top', Math.round(this.pos - this.halfsize + this.offset)+'px');
979       if (bw.ie) {
980         var new_height = parseInt(this.p2.parent().outerHeight(), 10) - parseInt(this.p2.css('top'), 10) - (bw.ie8 ? 2 : 0);
822a1e 981         this.p2.css('height', (new_height > 0 ? new_height : 0) + 'px');
918bb9 982       }
T 983     }
984     else {
985       this.p1.css('width', Math.floor(this.pos - this.p1pos.left - this.halfsize) + 'px');
986       this.p2.css('left', Math.ceil(this.pos + this.halfsize) + 'px');
987       this.handle.css('left', Math.round(this.pos - this.halfsize + this.offset + 3)+'px');
988       if (bw.ie) {
989         var new_width = parseInt(this.p2.parent().outerWidth(), 10) - parseInt(this.p2.css('left'), 10) ;
990         this.p2.css('width', (new_width > 0 ? new_width : 0) + 'px');
991       }
992     }
993
994     this.p2.resize();
995     this.p1.resize();
996
5fea6b 997     // also resize iframe covers
T 998     if (this.drag_active) {
999       $('iframe').each(function(i, elem) {
1000         var pos = $(this).offset();
1001         $('#iframe-splitter-fix-'+i).css({ top: pos.top+'px', left: pos.left+'px', width:elem.offsetWidth+'px', height: elem.offsetHeight+'px' });
1002       });
1003     }
1004
918bb9 1005     if (typeof this.render == 'function')
T 1006       this.render(this);
1007   };
1008
1009   /**
1010    * Handler for mousedown events
1011    */
1012   function onDragStart(e)
1013   {
1014     // disable text selection while dragging the splitter
1015     if (bw.konq || bw.chrome || bw.safari)
1016       document.body.style.webkitUserSelect = 'none';
1017
1018     me.p1pos = me.relative ? me.p1.position() : me.p1.offset();
1019     me.p2pos = me.relative ? me.p2.position() : me.p2.offset();
1020     me.drag_active = true;
1021
1022     // start listening to mousemove events
1023     $(document).bind('mousemove.'+this.id, onDrag).bind('mouseup.'+this.id, onDragStop);
1024
1025     // enable dragging above iframes
5fea6b 1026     $('iframe').each(function(i, elem) {
T 1027       $('<div>')
1028         .attr('id', 'iframe-splitter-fix-'+i)
1029         .addClass('iframe-splitter-fix')
918bb9 1030         .css({ background: '#fff',
5fea6b 1031           width: elem.offsetWidth+'px', height: elem.offsetHeight+'px',
918bb9 1032           position: 'absolute', opacity: '0.001', zIndex: 1000
T 1033         })
1034         .css($(this).offset())
1035         .appendTo('body');
1036       });
1037   };
1038
1039   /**
1040    * Handler for mousemove events
1041    */
1042   function onDrag(e)
1043   {
1044     if (!me.drag_active)
1045       return false;
1046
1047     var pos = rcube_event.get_mouse_pos(e);
1048
1049     if (me.relative) {
1050       var parent = me.p1.parent().offset();
1051       pos.x -= parent.left;
1052       pos.y -= parent.top;
1053     }
1054
1055     if (me.horizontal) {
1056       if (((pos.y - me.halfsize) > me.p1pos.top) && ((pos.y + me.halfsize) < (me.p2pos.top + me.p2.outerHeight()))) {
1057         me.pos = Math.max(me.min, pos.y - me.offset);
1058         me.resize();
1059       }
1060     }
1061     else {
1062       if (((pos.x - me.halfsize) > me.p1pos.left) && ((pos.x + me.halfsize) < (me.p2pos.left + me.p2.outerWidth()))) {
1063         me.pos = Math.max(me.min, pos.x - me.offset);
1064         me.resize();
1065       }
1066     }
1067
1068     me.p1pos = me.relative ? me.p1.position() : me.p1.offset();
1069     me.p2pos = me.relative ? me.p2.position() : me.p2.offset();
1070     return false;
1071   };
1072
1073   /**
1074    * Handler for mouseup events
1075    */
1076   function onDragStop(e)
1077   {
1078     // resume the ability to highlight text
1079     if (bw.konq || bw.chrome || bw.safari)
1080       document.body.style.webkitUserSelect = 'auto';
1081
1082     // cancel the listening for drag events
1083     $(document).unbind('.'+me.id);
1084     me.drag_active = false;
1085
1086     // remove temp divs
5fea6b 1087     $('div.iframe-splitter-fix').remove();
918bb9 1088
T 1089     me.set_cookie();
1090
1091     if (typeof me.callback == 'function')
1092       me.callback(me);
1093
1094     return bw.safari ? true : rcube_event.cancel(e);
1095   };
1096
1097   /**
1098    * Handler for window resize events
1099    */
1100   function onResize(e)
1101   {
1102     if (me.horizontal) {
1103       var new_height = parseInt(me.p2.parent().outerHeight(), 10) - parseInt(me.p2[0].style.top, 10) - (bw.ie8 ? 2 : 0);
1104       me.p2.css('height', (new_height > 0 ? new_height : 0) +'px');
1105     }
1106     else {
1107       var new_width = parseInt(me.p2.parent().outerWidth(), 10) - parseInt(me.p2[0].style.left, 10);
1108       me.p2.css('width', (new_width > 0 ? new_width : 0) + 'px');
1109     }
1110   };
1111
1112   /**
1113    * Saves splitter position in cookie
1114    */
1115   this.set_cookie = function()
1116   {
1117     var exp = new Date();
1118     exp.setYear(exp.getFullYear() + 1);
1119     bw.set_cookie(this.id, this.pos, exp);
1120   };
1121
1122 } // end class rcube_splitter
1123
1124
3a86e2 1125 // static getter for splitter instances
T 1126 rcube_splitter._instances = {};
1127
1128 rcube_splitter.get_instance = function(id)
1129 {
1130   return rcube_splitter._instances[id];
1131 };