From 6789bf1f4dfd32cd54a0fbe520ff3e1a6f6afb8a Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Thu, 8 May 2014 18:19:20 +0200 Subject: Moved popup-menu functions to Roundcube core for seamless integration of (stackable) skin and core menus --- plugins/zipdownload/zipdownload.js | 25 +-- program/js/app.js | 291 +++++++++++++++++++++++++++++----- program/js/common.js | 6 +- skins/larry/includes/mailtoolbar.html | 4 +- skins/larry/templates/compose.html | 5 +- skins/larry/templates/mail.html | 6 +- skins/larry/ui.js | 249 ++++++++--------------------- 7 files changed, 331 insertions(+), 255 deletions(-) diff --git a/plugins/zipdownload/zipdownload.js b/plugins/zipdownload/zipdownload.js index 644c1e030..af9136c1d 100644 --- a/plugins/zipdownload/zipdownload.js +++ b/plugins/zipdownload/zipdownload.js @@ -43,21 +43,10 @@ window.rcmail && rcmail.addEventListener('init', function(evt) { link.html('').append(span); } - span.addClass('folder-selector-link').text(rcmail.gettext('zipdownload.download')); - + span.text(rcmail.gettext('zipdownload.download')); rcmail.env.download_link = link; }); - - // hide menu on click out of menu element - var fn = function(e) { - var menu = $('#zipdownload-menu'); - if (e.target != menu.get(0)) - menu.hide(); - }; - $(document.body).on('mouseup', fn); - $('iframe').contents().on('mouseup', fn) - .load(function(e) { try { $(this).contents().on('mouseup', fn); } catch(e) {}; }); -}); + }); function rcmail_zipdownload(mode) @@ -100,14 +89,10 @@ function rcmail_zipdownload(mode) } // display download options menu -function rcmail_zipdownload_menu() +function rcmail_zipdownload_menu(e) { - // fix menu style and display menu - var z_index = rcmail.env.download_link.parents('.popupmenu').css('z-index'), - menu = $('#zipdownload-menu').css({'max-height': 'none', 'z-index': z_index + 1}).show(); - - // position menu on the screen - rcmail.element_position(menu, rcmail.env.download_link); + // show (sub)menu for download selection + rcmail.command('menu-open', 'zipdownload-menu', e && e.target ? e.target : rcmail.env.download_link, e); // abort default download action return false; diff --git a/program/js/app.js b/program/js/app.js index 4c9462afa..dd172cd99 100644 --- a/program/js/app.js +++ b/program/js/app.js @@ -46,6 +46,7 @@ function rcube_webmail() this.messages = {}; this.group2expand = {}; this.http_request_jobs = {}; + this.menu_stack = new Array(); // webmail client settings this.dblclick_time = 500; @@ -235,7 +236,6 @@ function rcube_webmail() return ref.command('sort', $(this).attr('rel'), this); }); - document.onmouseup = function(e){ return ref.doc_mouse_up(e); }; this.gui_objects.messagelist.parentNode.onclick = function(e){ return ref.click_on_list(e || window.event); }; this.enable_command('toggle_status', 'toggle_flag', 'sort', true); @@ -279,7 +279,7 @@ function rcube_webmail() this.env.address_group_stack = []; this.env.compose_commands = ['send-attachment', 'remove-attachment', 'send', 'cancel', 'toggle-editor', 'list-adresses', 'pushgroup', 'search', 'reset-search', 'extwin', - 'insert-response', 'save-response']; + 'insert-response', 'save-response', 'menu-open', 'menu-close']; if (this.env.drafts_mailbox) this.env.compose_commands.push('savedraft') @@ -314,8 +314,6 @@ function rcube_webmail() } } - document.onmouseup = function(e){ return ref.doc_mouse_up(e); }; - // init message compose form this.init_messageform(); } @@ -395,7 +393,6 @@ function rcube_webmail() this.contact_list.highlight_row(this.env.cid); this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return ref.click_on_list(e); }; - document.onmouseup = function(e){ return ref.doc_mouse_up(e); }; $(this.gui_objects.qsearchbox).focusin(function() { ref.contact_list.blur(); }); @@ -561,6 +558,18 @@ function rcube_webmail() .get(0).addEventListener('drop', function(e){ return ref.file_dropped(e); }, false); } + // catch document (and iframe) mouse clicks + var body_mouseup = function(e){ return ref.doc_mouse_up(e); }; + $(document.body) + .bind('mouseup', body_mouseup) + .bind('keydown', function(e){ return ref.doc_keypress(e); }); + + $('iframe').load(function(e) { + try { $(this.contentDocument || this.contentWindow).on('mouseup', body_mouseup); } + catch (e) {/* catch possible "Permission denied" error in IE */ } + }) + .contents().on('mouseup', body_mouseup); + // trigger init event hook this.triggerEvent('init', { task:this.task, action:this.env.action }); @@ -636,8 +645,8 @@ function rcube_webmail() } // trigger plugin hooks - this.triggerEvent('actionbefore', {props:props, action:command}); - ret = this.triggerEvent('before'+command, props); + this.triggerEvent('actionbefore', {props:props, action:command, originalEvent:event}); + ret = this.triggerEvent('before'+command, props || event); if (ret !== undefined) { // abort if one of the handlers returned false if (ret === false) @@ -712,9 +721,14 @@ function rcube_webmail() var mimetype = this.env.attachments[props.id]; this.enable_command('open-attachment', mimetype && this.env.mimetypes && $.inArray(mimetype, this.env.mimetypes) >= 0); } + this.show_menu(props, props.show || undefined, event); + break; - case 'menu-save': case 'menu-close': + this.hide_menu(props, event); + break; + + case 'menu-save': this.triggerEvent(command, {props:props, originalEvent:event}); return false; @@ -900,14 +914,14 @@ function rcube_webmail() case 'move': case 'moveto': // deprecated if (this.task == 'mail') - this.move_messages(props, obj); + this.move_messages(props, event); else if (this.task == 'addressbook') this.move_contacts(props); break; case 'copy': if (this.task == 'mail') - this.copy_messages(props, obj); + this.copy_messages(props, event); else if (this.task == 'addressbook') this.copy_contacts(props); break; @@ -1469,7 +1483,8 @@ function rcube_webmail() if (menu && modkey == SHIFT_KEY && this.commands['copy']) { var pos = rcube_event.get_mouse_pos(e); this.env.drag_target = target; - $(menu).css({top: (pos.y-10)+'px', left: (pos.x-10)+'px'}).show(); + this.show_menu(this.gui_objects.dragmenu.id, true, e); + $(menu).css({top: (pos.y-10)+'px', left: (pos.x-10)+'px'}); return true; } @@ -1585,12 +1600,13 @@ function rcube_webmail() } }; + // global mouse-click handler to cleanup some UI elements this.doc_mouse_up = function(e) { - var list, id; + var list, id, target = rcube_event.get_target(e); // ignore event if jquery UI dialog is open - if ($(rcube_event.get_target(e)).closest('.ui-dialog, .ui-widget-overlay').length) + if ($(target).closest('.ui-dialog, .ui-widget-overlay').length) return; list = this.message_list || this.contact_list; @@ -1604,8 +1620,74 @@ function rcube_webmail() this.button_out(this.buttons_sel[id], id); this.buttons_sel = {}; } + + // reset popup menus; delayed to have updated menu_stack data + window.setTimeout(function(e){ + var obj, skip, config, id, i; + for (i = ref.menu_stack.length - 1; i >= 0; i--) { + id = ref.menu_stack[i]; + obj = $('#' + id); + + if (obj.is(':visible') + && target != obj.data('opener') + && target != obj.get(0) // check if scroll bar was clicked (#1489832) + && id != skip + && (obj.attr('data-editable') != 'true' || !$(target).parents('#' + id).length) + && (obj.attr('data-sticky') != 'true' || !rcube_mouse_is_over(e, obj.get(0))) + ) { + ref.hide_menu(id, e); + } + skip = obj.data('parent'); + } + }, 10); }; + // global keypress event handler + this.doc_keypress = function(e) + { + // Helper method to move focus to the next/prev active menu item + var focus_menu_item = function(dir) { + var obj, mod = dir < 0 ? 'prevAll' : 'nextAll', limit = dir < 0 ? 'last' : 'first'; + if (ref.focused_menu && (obj = $('#'+ref.focused_menu))) { + return obj.find(':focus').closest('li')[mod](':has(:not([aria-disabled=true]))').find('a,input')[limit]().focus().length; + } + + return 0; + }; + + var target = e.target || {}, + keyCode = rcube_event.get_keycode(e); + + if (e.keyCode != 27 && (!this.menu_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 + focus_menu_item(mod = keyCode == 38 || keyCode == 63232 ? -1 : 1); + break; + + case 9: // tab + if (this.focused_menu) { + var mod = rcube_event.get_modifier(e); + if (!focus_menu_item(mod == SHIFT_KEY ? -1 : 1)) { + this.hide_menu(this.focused_menu, e); + } + } + return rcube_event.cancel(e); + + case 27: // esc + if (this.menu_stack.length) + this.hide_menu(this.menu_stack[this.menu_stack.length-1], e); + break; + } + + return true; + } + this.click_on_list = function(e) { if (this.gui_objects.qsearchbox) @@ -2707,12 +2789,12 @@ function rcube_webmail() }; // copy selected messages to the specified mailbox - this.copy_messages = function(mbox, obj) + this.copy_messages = function(mbox, event) { if (mbox && typeof mbox === 'object') mbox = mbox.id; else if (!mbox) - return this.folder_selector(obj, function(folder) { ref.command('copy', folder); }); + return this.folder_selector(event, function(folder) { ref.command('copy', folder); }); // exit if current or no mailbox specified if (!mbox || mbox == this.env.mailbox) @@ -2729,12 +2811,12 @@ function rcube_webmail() }; // move selected messages to the specified mailbox - this.move_messages = function(mbox, obj) + this.move_messages = function(mbox, event) { if (mbox && typeof mbox === 'object') mbox = mbox.id; else if (!mbox) - return this.folder_selector(obj, function(folder) { ref.command('move', folder); }); + return this.folder_selector(event, function(folder) { ref.command('move', folder); }); // exit if current or no mailbox specified if (!mbox || (mbox == this.env.mailbox && !this.is_multifolder_listing())) @@ -3938,7 +4020,7 @@ function rcube_webmail() // cleanup rx = new RegExp(rx_delim + '\\s*' + rx_delim, 'g'); - input_val = input_val.replace(rx, delim); + input_val = String(input_val).replace(rx, delim); rx = new RegExp('^[\\s' + rx_delim + ']+'); input_val = input_val.replace(rx, ''); @@ -6736,17 +6818,15 @@ function rcube_webmail() }; // create folder selector popup, position and display it - this.folder_selector = function(obj, callback) + this.folder_selector = function(event, callback) { var container = this.folder_selector_element; if (!container) { var rows = [], delim = this.env.delimiter, - ul = $('