refactor js

dev_contacts
PhilW 7 years ago
parent 294a864732
commit ea8d7a3ffd

@ -15,7 +15,8 @@
* for the JavaScript code in this file.
*/
rcube_webmail.prototype.swipe_position_target = function(obj, pos, vertical) {
rcube_webmail.prototype.swipe = {
position_target: function(obj, pos, vertical) {
var translate = '';
if (pos)
@ -25,84 +26,56 @@ rcube_webmail.prototype.swipe_position_target = function(obj, pos, vertical) {
$(obj).children('td').css('transform', translate);
else
$(obj).css('transform', translate);
};
rcube_webmail.prototype.swipe_list_selection = function(uid, show, prev_sel) {
// make the system think no preview pane exists while we do some fake message selects
// to enable/disable relevant commands for current selection
var prev_contentframe = rcmail.env.contentframe, i;
this.env.contentframe = null;
if (show) {
if (this.message_list.selection.length == 0 || !this.message_list.in_selection(uid)) {
prev_sel = prev_sel ? prev_sel : this.message_list.get_selection();
this.message_list.clear_selection();
this.message_list.highlight_row(uid, true);
}
}
else if (prev_sel) {
this.message_list.clear_selection();
for (i in prev_sel)
rcmail[this.list_object].highlight_row(prev_sel[i], true);
}
else {
this.message_list.clear_selection();
}
this.env.contentframe = prev_contentframe;
return prev_sel;
};
},
rcube_webmail.prototype.swipe_action_callback = function(command, type, props) {
action_callback: function(command, type, props) {
if (!props.uid)
return;
var prev_uid = rcmail.env.uid;
rcmail.env.uid = props.uid;
if (type == 'mark') {
this.mark_message(command, props.uid);
rcmail.mark_message(command);
}
else if (type == 'compose') {
this.enable_command(command, true);
this.env.uid = props.uid;
this.command(command, '', props.obj, props.originalEvent);
rcmail.enable_command(command, true);
rcmail.command(command, '', props.obj, props.originalEvent);
}
else if (type == 'select') {
this.message_list.highlight_row(props.uid, true);
rcmail.message_list.highlight_row(props.uid, true);
var select_class = '';
if (select_class = $('#swipeoptions-menu').data('listselection-class')) {
if (command == 'deselect' && this.message_list.get_selection().length == 0)
$(this.gui_objects.messagelist).removeClass(select_class);
if (command == 'deselect' && rcmail.message_list.get_selection().length == 0)
$(rcmail.gui_objects.messagelist).removeClass(select_class);
else
$(this.gui_objects.messagelist).addClass(select_class);
$(rcmail.gui_objects.messagelist).addClass(select_class);
}
}
else {
var prev_sel = this.swipe_list_selection(props.uid, true);
// enable command
var prev_command = this.commands[command];
this.enable_command(command, true);
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
$('#' + this.buttons[command][0].id).trigger('click');
$('#' + rcmail.buttons[command][0].id).trigger('click');
// restore original state
this.enable_command(command, prev_command);
this.swipe_list_selection(props.uid, false, prev_sel);
rcmail.enable_command(command, prev_command);
}
};
rcube_webmail.prototype.swipe_select_action = function(direction, obj) {
rcmail.env.uid = prev_uid;
},
select_action: function(direction, obj) {
var action = {
'class': '',
'text': '',
'callback': null
};
ret = this.triggerEvent('swipe-action', {'direction': direction, 'obj': obj});
ret = rcmail.triggerEvent('swipe-action', {'direction': direction, 'obj': obj});
if (ret !== undefined) {
// abort if one of the handlers returned false
if (ret === false)
@ -111,87 +84,87 @@ rcube_webmail.prototype.swipe_select_action = function(direction, obj) {
return ret;
}
if (this.env.swipe_actions[direction] == 'archive' && this.env.archive_folder) {
if (rcmail.env.swipe_actions[direction] == 'archive' && rcmail.env.archive_folder) {
action.class = 'archive';
action.text = 'archive.buttontext';
action.callback = function(p) { rcmail.swipe_action_callback('plugin.archive', null, p); };
action.callback = function(p) { rcmail.swipe.action_callback('plugin.archive', null, p); };
}
else if (this.env.swipe_actions[direction] == 'checkmail') {
else if (rcmail.env.swipe_actions[direction] == 'checkmail') {
action.class = 'checkmail';
action.text = 'refresh';
action.callback = function(p) { rcmail.command('checkmail'); };
}
else if (this.env.swipe_actions[direction] == 'delete') {
else if (rcmail.env.swipe_actions[direction] == 'delete') {
action.class = 'delete';
action.text = 'delete';
action.callback = function(p) { rcmail.swipe_action_callback('delete', null, p); };
action.callback = function(p) { rcmail.swipe.action_callback('delete', null, p); };
}
else if (this.env.swipe_actions[direction] == 'forward') {
else if (rcmail.env.swipe_actions[direction] == 'forward') {
action.class = 'forward';
action.text = 'forward';
action.callback = function(p) { rcmail.swipe_action_callback('forward', 'compose', p); };
action.callback = function(p) { rcmail.swipe.action_callback('forward', 'compose', p); };
}
else if (this.env.swipe_actions[direction] == 'markasjunk') {
else if (rcmail.env.swipe_actions[direction] == 'markasjunk') {
action.class = 'junk';
action.text = 'markasjunk.buttontitle';
action.callback = function(p) { rcmail.swipe_action_callback('plugin.markasjunk', null, p); };
action.callback = function(p) { rcmail.swipe.action_callback('plugin.markasjunk', null, p); };
}
else if (this.env.swipe_actions[direction] == 'move') {
else if (rcmail.env.swipe_actions[direction] == 'move') {
action.class = 'move';
action.text = 'moveto';
action.callback = function(p) { rcmail.swipe_action_callback('move', null, p); };
action.callback = function(p) { rcmail.swipe.action_callback('move', null, p); };
}
else if (this.env.swipe_actions[direction] == 'reply') {
else if (rcmail.env.swipe_actions[direction] == 'reply') {
action.class = 'reply';
action.text = 'reply';
action.callback = function(p) { rcmail.swipe_action_callback('reply', 'compose', p); };
action.callback = function(p) { rcmail.swipe.action_callback('reply', 'compose', p); };
}
else if (this.env.swipe_actions[direction] == 'reply-all') {
else if (rcmail.env.swipe_actions[direction] == 'reply-all') {
action.class = 'replyall';
action.text = 'replyall';
action.callback = function(p) { rcmail.swipe_action_callback('reply-all', 'compose', p); };
action.callback = function(p) { rcmail.swipe.action_callback('reply-all', 'compose', p); };
}
else if (this.env.swipe_actions[direction] == 'swipe-read') {
else if (rcmail.env.swipe_actions[direction] == 'swipe-read') {
if (obj.hasClass('unread')) {
action.class = 'read';
action.text = 'swipe.markasread';
action.callback = function(p) { rcmail.swipe_action_callback('read', 'mark', p); };
action.callback = function(p) { rcmail.swipe.action_callback('read', 'mark', p); };
}
else {
action.class = 'unread';
action.text = 'swipe.markasunread';
action.callback = function(p) { rcmail.swipe_action_callback('unread', 'mark', p); };
action.callback = function(p) { rcmail.swipe.action_callback('unread', 'mark', p); };
}
}
else if (this.env.swipe_actions[direction] == 'swipe-flagged') {
else if (rcmail.env.swipe_actions[direction] == 'swipe-flagged') {
if (obj.hasClass('flagged')) {
action.class = 'unflagged';
action.text = 'swipe.markasunflagged';
action.callback = function(p) { rcmail.swipe_action_callback('unflagged', 'mark', p); };
action.callback = function(p) { rcmail.swipe.action_callback('unflagged', 'mark', p); };
}
else {
action.class = 'flagged';
action.text = 'swipe.markasflagged';
action.callback = function(p) { rcmail.swipe_action_callback('flagged', 'mark', p); };
action.callback = function(p) { rcmail.swipe.action_callback('flagged', 'mark', p); };
}
}
else if (this.env.swipe_actions[direction] == 'swipe-select') {
else if (rcmail.env.swipe_actions[direction] == 'swipe-select') {
if (obj.hasClass('selected')) {
action.class = 'deselect';
action.text = 'swipe.deselect';
action.callback = function(p) { rcmail.swipe_action_callback('deselect', 'select', p); };
action.callback = function(p) { rcmail.swipe.action_callback('deselect', 'select', p); };
}
else {
action.class = 'select';
action.text = 'select';
action.callback = function(p) { rcmail.swipe_action_callback('select', 'select', p); };
action.callback = function(p) { rcmail.swipe.action_callback('select', 'select', p); };
}
}
return action;
};
},
rcube_webmail.prototype.swipe_event = function(opts) {
init: function(opts) {
var swipeevents = {
'startevent': 'pointerdown',
'moveevent': 'pointermove',
@ -201,12 +174,12 @@ rcube_webmail.prototype.swipe_event = function(opts) {
'type': function(e) { return e.pointerType; },
'pos': function(e, x) { return e.originalEvent[ x ? 'pageX' : 'pageY']; },
'clearswipe': function(e) {
rcmail.swipe_position_target(opts[swipedata.axis].target_obj, 0, swipedata.axis == 'vertical');
rcmail.swipe.position_target(opts[swipedata.axis].target_obj, 0, swipedata.axis == 'vertical');
$('#swipe-action').removeClass().hide();
opts[swipedata.axis].target_obj.removeClass('swipe-active');
$(rcmail.gui_objects.messagelist).removeClass('swipe-block');
swipedata = {};
rcmail.env.swipe_active = null;
rcmail.swipe.active = null;
if (opts.parent_obj)
opts.parent_obj.off(swipeevents.moveevent, rcube_event.cancel);
@ -228,11 +201,11 @@ rcube_webmail.prototype.swipe_event = function(opts) {
// swipe down on message list container
opts.source_obj
.on(swipeevents.startevent, function(e) {
if (!rcmail.env.swipe_active && swipeevents.type(e) == 'touch') {
if (!rcmail.swipe.active && swipeevents.type(e) == 'touch') {
swipedata.x = swipeevents.pos(e, true);
swipedata.y = swipeevents.pos(e, false);
swipedata.id = swipeevents.id(e);
swipedata.scrollable = rcmail.env.swipe_parent[0].offsetHeight < rcmail.env.swipe_parent[0].scrollHeight;
swipedata.scrollable = rcmail.swipe.parent[0].offsetHeight < rcmail.swipe.parent[0].scrollHeight;
if (opts.parent_obj)
opts.parent_obj.off(swipeevents.moveevent, rcube_event.cancel);
@ -266,13 +239,13 @@ rcube_webmail.prototype.swipe_event = function(opts) {
}
// make sure no other swipes are active
if (!temp_axis || !opts[temp_axis] || (rcmail.env.swipe_active && rcmail.env.swipe_active != temp_axis))
if (!temp_axis || !opts[temp_axis] || (rcmail.swipe.active && rcmail.swipe.active != temp_axis))
return;
// do not interfere with normal message list scrolling
if (temp_axis == 'vertical' && (changeY < 0 || opts.vertical.target_obj.parent().scrollTop() != 0)) {
if (bw.pointer && swipedata.scrollable)
rcmail.env.swipe_parent.css('touch-action', 'pan-y');
rcmail.swipe.parent.css('touch-action', 'pan-y');
if (swipedata.axis)
swipeevents.clearswipe(e);
@ -283,7 +256,7 @@ rcube_webmail.prototype.swipe_event = function(opts) {
// save the axis info
swipedata.axis = temp_axis;
var direction = (swipedata.axis == 'vertical' ? 'down' : (changeX < 0 ? 'left' : 'right'));
var action = rcmail.swipe_select_action(direction, opts.source_obj);
var action = rcmail.swipe.select_action(direction, opts.source_obj);
// skip if there is no event
if (!action.callback)
@ -308,7 +281,7 @@ rcube_webmail.prototype.swipe_event = function(opts) {
'height': action_style.height
}).show();
opts[swipedata.axis].target_obj.addClass('swipe-active');
rcmail.env.swipe_active = swipedata.axis; // set the active swipe
rcmail.swipe.active = swipedata.axis; // set the active swipe
if (opts.parent_obj)
opts.parent_obj.on(swipeevents.moveevent, rcube_event.cancel);
@ -325,11 +298,11 @@ rcube_webmail.prototype.swipe_event = function(opts) {
$('#swipe-action').data('callback', null);
}
rcmail.swipe_position_target(opts[swipedata.axis].target_obj, swipedata.axis == 'vertical' ? changeY : changeX, swipedata.axis == 'vertical');
rcmail.swipe.position_target(opts[swipedata.axis].target_obj, swipedata.axis == 'vertical' ? changeY : changeX, swipedata.axis == 'vertical');
})
.on(swipeevents.endevent, function(e) {
if (swipeevents.type(e) == 'touch' && swipedata.id == swipeevents.id(e) && rcmail.env.swipe_active &&
rcmail.env.swipe_active == swipedata.axis && opts[swipedata.axis].target_obj.hasClass('swipe-active')) {
if (swipeevents.type(e) == 'touch' && swipedata.id == swipeevents.id(e) && rcmail.swipe.active &&
rcmail.swipe.active == swipedata.axis && opts[swipedata.axis].target_obj.hasClass('swipe-active')) {
var callback = null;
if (callback = $('#swipe-action').data('callback'))
callback({'uid': opts[swipedata.axis].uid, 'obj': opts[swipedata.axis].target_obj, 'originalEvent': e});
@ -342,6 +315,7 @@ rcube_webmail.prototype.swipe_event = function(opts) {
swipeevents.clearswipe(e);
});
}
};
$(document).ready(function() {
if (window.rcmail && ((bw.touch && !bw.ie) || bw.pointer)) {
@ -350,12 +324,12 @@ $(document).ready(function() {
if (rcmail.message_list.draggable || !messagelist_container[0].addEventListener)
return;
rcmail.env.swipe_parent = messagelist_container;
rcmail.env.swipe_parent.prepend($('<div>').attr('id', 'swipe-action').html($('<div>').append($('<span>'))).hide());
rcmail.swipe.parent = messagelist_container;
rcmail.swipe.parent.prepend($('<div>').attr('id', 'swipe-action').html($('<div>').append($('<span>'))).hide());
// down swipe on message list container
var swipe_config = {
'source_obj': rcmail.env.swipe_parent,
'source_obj': rcmail.swipe.parent,
'parent_obj': null,
'vertical': {
'minmove': $(window).height() * 0.1,
@ -364,7 +338,7 @@ $(document).ready(function() {
return {
'top': 0,
'left': 0,
'width': rcmail.env.swipe_parent.width() + 'px',
'width': rcmail.swipe.parent.width() + 'px',
'height': $(window).height() * 0.2 + 'px'
};
},
@ -373,19 +347,19 @@ $(document).ready(function() {
}
};
rcmail.swipe_event(swipe_config);
rcmail.swipe.init(swipe_config);
// prevent accidental message list scroll when swipe active
rcmail.env.swipe_parent.on('scroll', function() {
rcmail.swipe.parent.on('scroll', function() {
if (!bw.pointer) {
if (rcmail.env.swipe_active)
if (rcmail.swipe.active)
return false;
}
else if ($(this).scrollTop() == 0) {
// allow vertical pointer events to fire (if one is configured)
var action = rcmail.swipe_select_action('down');
var action = rcmail.swipe.select_action('down');
// Edge does not support pan-down, only pan-y
rcmail.env.swipe_parent.css('touch-action', action.callback ? (bw.edge ? 'none' : 'pan-down') : 'pan-y');
rcmail.swipe.parent.css('touch-action', action.callback ? (bw.edge ? 'none' : 'pan-down') : 'pan-y');
}
}).trigger('scroll');
});
@ -397,7 +371,7 @@ $(document).ready(function() {
var swipe_config = {
'source_obj': $('#' + props.row.id),
'parent_obj': rcmail.env.swipe_parent,
'parent_obj': rcmail.swipe.parent,
'horizontal': {
'minmove': $('#' + props.row.id).width() * 0.25,
'maxmove': $('#' + props.row.id).width() * 0.6,
@ -428,7 +402,7 @@ $(document).ready(function() {
}
};
rcmail.swipe_event(swipe_config);
rcmail.swipe.init(swipe_config);
});
// add swipe options to list options menu
@ -450,8 +424,7 @@ $(document).ready(function() {
// save swipe options
rcmail.set_list_options_core = rcmail.set_list_options;
rcmail.set_list_options = function(cols, sort_col, sort_order, threads, layout)
{
rcmail.set_list_options = function(cols, sort_col, sort_order, threads, layout) {
var post = {};
$.each(['left', 'right', 'down'], function() {
if ($('select[name="swipe_' + this + '"]:visible').val() != rcmail.env.swipe_actions[this]) {
@ -464,7 +437,7 @@ $(document).ready(function() {
rcmail.http_post('plugin.swipe.save_settings', post);
rcmail.set_list_options_core(cols, sort_col, sort_order, threads, layout);
}
};
if ($('#swipeoptions-menu > fieldset').find('select').length > 0)
$('#swipeoptions-menu > fieldset').appendTo('#' + $('#swipeoptions-menu').data('options-menuid'));

Loading…
Cancel
Save