JS improvements, remove duplicate code

dev_contacts
PhilW 7 years ago
parent 58970bffbf
commit 742c66370e

@ -1,4 +1,4 @@
<div id="swipeoptions-menu" class="popupmenu propform" role="dialog" aria-labelledby="aria-label-listoptions" data-options-menuname="messagelistmenu" data-options-menuid="listoptions-menu"> <div id="swipeoptions-menu" class="popupmenu propform" role="dialog" aria-labelledby="aria-label-listoptions" data-options-menuname="messagelistmenu" data-options-menuid="listoptions-menu" data-listselection-class="withselection">
<fieldset class="swipe"> <fieldset class="swipe">
<legend><roundcube:label name="swipe.swipeactions" /></legend> <legend><roundcube:label name="swipe.swipeactions" /></legend>
<div class="form-group row"> <div class="form-group row">

@ -56,6 +56,51 @@ rcube_webmail.prototype.swipe_list_selection = function(uid, show, prev_sel) {
return prev_sel; return prev_sel;
}; };
rcube_webmail.prototype.swipe_action_callback = function(command, type, props) {
if (!props.uid)
return;
if (type == 'mark') {
rcmail.mark_message(command, props.uid);
}
else if (type == 'compose') {
rcmail.enable_command(command, true);
rcmail.env.uid = props.uid;
rcmail.command(command, '', props.obj, props.originalEvent);
}
else if (type == 'select') {
rcmail.message_list.highlight_row(props.uid, true);
var select_class = '';
if (select_class = $('#swipeoptions-menu').data('listselection-class')) {
if (command == 'deselect' && rcmail.message_list.get_selection().length == 0)
$(rcmail.gui_objects.messagelist).removeClass(select_class);
else
$(rcmail.gui_objects.messagelist).addClass(select_class);
}
}
else {
var prev_sel = rcmail.swipe_list_selection(props.uid, true);
// enable command
var prev_command = rcmail.commands[command];
rcmail.enable_command(command, true);
// some actions require a button click in the UI to trigger things like popovers
// rather than a direct command call
$('#' + rcmail.buttons[command][0].id).trigger('click');
if (props.delay_disable) {
return {'prev_command': prev_command, 'prev_sel': prev_sel};
}
else {
// restore original state
rcmail.enable_command(command, prev_command);
rcmail.swipe_list_selection(props.uid, false, prev_sel);
}
}
};
rcube_webmail.prototype.swipe_select_action = function(direction, obj) { rcube_webmail.prototype.swipe_select_action = function(direction, obj) {
var action = { var action = {
'class': '', 'class': '',
@ -66,77 +111,45 @@ rcube_webmail.prototype.swipe_select_action = function(direction, obj) {
if (rcmail.env.swipe_actions[direction] == 'checkmail') { if (rcmail.env.swipe_actions[direction] == 'checkmail') {
action.class = 'checkmail'; action.class = 'checkmail';
action.text = 'refresh'; action.text = 'refresh';
action.callback = function(uid, obj, e) { rcmail.command('checkmail'); }; action.callback = function(p) { rcmail.command('checkmail'); };
} }
else if (rcmail.env.swipe_actions[direction] == 'delete') { else if (rcmail.env.swipe_actions[direction] == 'delete') {
action.class = 'delete'; action.class = 'delete';
action.text = 'delete'; action.text = 'delete';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('delete', null, p); };
if (!uid)
return;
var prev_sel = rcmail.swipe_list_selection(uid, true);
// enable command
var prev_command = rcmail.commands['delete'];
rcmail.enable_command('delete', true);
var result = rcmail.command('delete', '', obj, e);
rcmail.enable_command('delete', prev_command);
rcmail.swipe_list_selection(uid, false, prev_sel);
};
} }
else if (rcmail.env.swipe_actions[direction] == 'flagged') { else if (rcmail.env.swipe_actions[direction] == 'flagged') {
if (obj.hasClass('flagged')) { if (obj.hasClass('flagged')) {
action.class = 'unflagged'; action.class = 'unflagged';
action.text = 'swipe.markasunflagged'; action.text = 'swipe.markasunflagged';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('unflagged', 'mark', p); };
if (!uid)
return;
rcmail.mark_message('unflagged', uid);
};
} }
else { else {
action.class = 'flagged'; action.class = 'flagged';
action.text = 'swipe.markasflagged'; action.text = 'swipe.markasflagged';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('flagged', 'mark', p); };
if (!uid)
return;
rcmail.mark_message('flagged', uid);
};
} }
} }
else if (rcmail.env.swipe_actions[direction] == 'forward') { else if (rcmail.env.swipe_actions[direction] == 'forward') {
action.class = 'forward'; action.class = 'forward';
action.text = 'forward'; action.text = 'forward';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('forward', 'compose', p); };
if (!uid)
return;
rcmail.enable_command('forward', true);
rcmail.env.uid = uid;
rcmail.command('forward', '', obj, e);
};
} }
else if (rcmail.env.swipe_actions[direction] == 'move') { else if (rcmail.env.swipe_actions[direction] == 'move') {
action.class = 'move'; action.class = 'move';
action.text = 'moveto'; action.text = 'moveto';
action.callback = function(uid, obj, e) { action.callback = function(p) {
if (!uid) p.delay_disable = true;
return;
var prev_sel = rcmail.swipe_list_selection(uid, true); var ret = rcmail.swipe_action_callback('move', null, p);
var prev_command = rcmail.commands['move'];
rcmail.enable_command('move', true);
$('#' + rcmail.buttons['move'][0].id).trigger('click');
// delay disabling the action until the next click // delay disabling the action until the next click
rcmail.env.swipe_delayed_action = function(e) { rcmail.env.swipe_delayed_action = function(e) {
rcmail.enable_command('move', prev_command); if ($(e.target).parents('.folderlist').length == 0) {
rcmail.swipe_list_selection(uid, false, prev_sel); rcmail.enable_command('move', ret.prev_command);
rcmail.env.swipe_delayed_action = null; rcmail.swipe_list_selection(p.uid, false, ret.prev_sel);
rcmail.env.swipe_delayed_action = null;
}
}; };
}; };
} }
@ -144,91 +157,40 @@ rcube_webmail.prototype.swipe_select_action = function(direction, obj) {
if (obj.hasClass('unread')) { if (obj.hasClass('unread')) {
action.class = 'read'; action.class = 'read';
action.text = 'swipe.markasread'; action.text = 'swipe.markasread';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('read', 'mark', p); };
if (!uid)
return;
rcmail.mark_message('read', uid);
};
} }
else { else {
action.class = 'unread'; action.class = 'unread';
action.text = 'swipe.markasunread'; action.text = 'swipe.markasunread';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('unread', 'mark', p); };
if (!uid)
return;
rcmail.mark_message('unread', uid);
};
} }
} }
else if (rcmail.env.swipe_actions[direction] == 'reply') { else if (rcmail.env.swipe_actions[direction] == 'reply') {
action.class = 'reply'; action.class = 'reply';
action.text = 'reply'; action.text = 'reply';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('reply', 'compose', p); };
if (!uid)
return;
rcmail.enable_command('reply', true);
rcmail.env.uid = uid;
rcmail.command('reply', '', obj, e);
};
} }
else if (rcmail.env.swipe_actions[direction] == 'replyall') { else if (rcmail.env.swipe_actions[direction] == 'replyall') {
action.class = 'replyall'; action.class = 'replyall';
action.text = 'replyall'; action.text = 'replyall';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('reply-all', 'compose', p); };
if (!uid)
return;
rcmail.enable_command('reply-all', true);
rcmail.env.uid = uid;
rcmail.command('reply-all', '', obj, e);
};
} }
else if (rcmail.env.swipe_actions[direction] == 'select') { else if (rcmail.env.swipe_actions[direction] == 'select') {
if (obj.hasClass('selected')) { if (obj.hasClass('selected')) {
action.class = 'deselect'; action.class = 'deselect';
action.text = 'swipe.deselect'; action.text = 'swipe.deselect';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('deselect', 'select', p); };
if (!uid)
return;
rcmail.message_list.highlight_row(uid, true);
if (rcmail.message_list.get_selection().length == 0)
$(rcmail.gui_objects.messagelist).removeClass('withselection');
};
} }
else { else {
action.class = 'select'; action.class = 'select';
action.text = 'select'; action.text = 'select';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('select', 'select', p); };
if (!uid)
return;
$(rcmail.gui_objects.messagelist).addClass('withselection');
rcmail.message_list.highlight_row(uid, true);
};
} }
} }
else if (rcmail.env.swipe_actions[direction] == 'archive' && rcmail.env.archive_folder) { else if (rcmail.env.swipe_actions[direction] == 'archive' && rcmail.env.archive_folder) {
action.class = 'archive'; action.class = 'archive';
action.text = 'archive.buttontext'; action.text = 'archive.buttontext';
action.callback = function(uid, obj, e) { action.callback = function(p) { rcmail.swipe_action_callback('plugin.archive', null, p); };
if (!uid)
return;
var prev_sel = rcmail.swipe_list_selection(uid, true);
// enable command
var prev_command = rcmail.commands['plugin.archive'];
rcmail.enable_command('plugin.archive', true);
var result = rcmail.command('plugin.archive', '', obj, e);
rcmail.enable_command('plugin.archive', prev_command);
rcmail.swipe_list_selection(uid, false, prev_sel);
};
} }
return action; return action;
@ -318,7 +280,7 @@ rcube_webmail.prototype.swipe_event = function(opts) {
var callback = null; var callback = null;
if (callback = $('#swipe-action').data('callback')) if (callback = $('#swipe-action').data('callback'))
callback(opts.uid, opts.target_obj, e); callback({'uid': opts.uid, 'obj': opts.target_obj, 'originalEvent': e});
$('#swipe-action').removeClass().hide(); $('#swipe-action').removeClass().hide();
opts.target_obj.removeClass('swipe-active'); opts.target_obj.removeClass('swipe-active');

Loading…
Cancel
Save