Selection algorithm simplified.

fixes #322
This commit is contained in:
Antoine Nguyen
2013-12-03 19:02:35 +01:00
parent 4cf0af8bfd
commit 0dfc992bda
2 changed files with 33 additions and 10 deletions
@@ -993,7 +993,9 @@ Webmail.prototype = {
var $tr = $this.parent();
if (!plug.htmltable.is_selected($tr)) {
var $input = $tr.find('#selection');
plug.htmltable.select_row($tr);
$input.prop('checked', true);
}
var nmsgs = plug.htmltable.current_selection().length;
+31 -10
View File
@@ -34,8 +34,10 @@
$(document).on("keyup", "body", $.proxy(this.keyup, this));
},
/**
* Effective change of a table row's state.
*/
_toggle_select: function($tr) {
this.last_selection = $tr;
if ($tr.hasClass(this.options.tr_selected_class)) {
$tr.children('td[name=selection]').children('input').prop('checked', false);
$tr.removeClass(this.options.tr_selected_class);
@@ -51,14 +53,23 @@
}
},
/**
* Change the selection state of a given table row.
*
* Holding the shift key between two selections let users
* select all rows between those two locations.
*
* Holding the ctrl key between two selections let users make
* an additive selection.
*/
toggle_select: function(e) {
var $tr = $(e.target).parents('tr');
if (this.shift_pressed && this.last_selection) {
if ($tr.is(this.last_selection)) {
$tr.children('td[name=selection]').children('input').prop('checked', true);
return;
}
var start = this.last_selection;
this.clear_selection();
this.last_selection = start;
var itfunc = ($tr.index() >= this.last_selection.index())
? "next" : "prev";
var $curtr = null;
@@ -66,7 +77,7 @@
if (this.prev_dir && itfunc != this.prev_dir) {
$curtr = this.last_selection;
} else {
$curtr = this.last_selection[itfunc]("tr");
$curtr = this.last_selection;
}
for (; $curtr.length; $curtr = $curtr[itfunc]("tr")) {
this._toggle_select($curtr);
@@ -79,19 +90,26 @@
} else if (!this.ctrl_pressed && (!this.last_selection || !this.last_selection.is($tr))) {
this.clear_selection();
}
this.last_selection = $tr;
this._toggle_select($tr);
},
/**
* Check if $row is selected or not.
*/
is_selected: function($row) {
return $row.hasClass(this.options.tr_selected_class);
},
/**
* Return the current selection. (jQuery object)
*/
current_selection: function() {
return $("tr[class*=" + this.options.tr_selected_class + "]");
},
/*
* Cancel the current selection
/**
* Cancel the current selection.
*/
clear_selection: function() {
this.last_selection = null;
@@ -102,10 +120,13 @@
}
},
/*
* Manually select a table row
/**
* Manually select a table row.
*/
select_row: function($row) {
if (!this.last_selection) {
this.last_selection = $row;
}
$row.addClass(this.options.tr_selected_class);
if (this.options.tr_selected_event != undefined) {
this.options.tr_selected_event($row);