simpler select function

This commit is contained in:
Mathieu 2015-03-05 17:05:02 +01:00
parent 95f0f520c7
commit eaeff19314

View File

@ -122,14 +122,14 @@ define([
$('.delete-button').click($.proxy(this.delete_selected, this)); $('.delete-button').click($.proxy(this.delete_selected, this));
// Bind events for selection menu buttons. // Bind events for selection menu buttons.
$('#selector-menu').click(function(event){that.select($(event.target).attr('id'),true)}); $('#selector-menu').click(function(event){that.select($(event.target).attr('id'))});
$('.tree-selector').change(function(){that.select($(this).attr('id'),$(this).is(':checked'))}); $('#select-all').change(function(){that.select($(this).is(':checked') ? 'select-all' : 'select-none')});
$('#button-select-all').click(function(e) { $('#button-select-all').click(function(e) {
// toggle checkbox if the click doesn't come from the checkbox already // toggle checkbox if the click doesn't come from the checkbox already
if (!$(e.target).is('input[type=checkbox]')) { if (!$(e.target).is('input[type=checkbox]')) {
var checkbox = $('#select-all'); var checkbox = $('#select-all');
checkbox.prop('checked', !checkbox.prop('checked')); checkbox.prop('checked', !checkbox.prop('checked'));
that.select('select-all',checkbox.prop('checked')); that.select(checkbox.prop('checked') ? 'select-all' : 'select-none');
} }
}); });
} }
@ -387,21 +387,20 @@ define([
/** /**
* Select all items in the tree of specified type. * Select all items in the tree of specified type.
* selection_type : string among "select-all, "select-folders", "select-notebooks", "select-running-notebooks", "select-files" * selection_type : string among "select-all", "select-folders", "select-notebooks", "select-running-notebooks", "select-files"
* state : boolean, true to select and false to deselect * any other string (like "select-none") deselects all items
*/ */
NotebookList.prototype.select = function(selection_type,state) { NotebookList.prototype.select = function(selection_type) {
var that = this; var that = this;
$('.list_item').each(function(index, item) { $('.list_item').each(function(index, item) {
// For each item, determine if the state should be set, depending on the selection_type that triggered select var item_type = $(item).data('type');
var set_state = (selection_type === "select-all"); var state = false;
set_state = set_state || (selection_type === "select-folders" && $(item).data('type') === 'directory'); state = state || (selection_type === "select-all");
set_state = set_state || (selection_type === "select-notebooks" && $(item).data('type') === 'notebook'); state = state || (selection_type === "select-folders" && item_type === 'directory');
set_state = set_state || (selection_type === "select-running-notebooks" && $(item).data('type') === 'notebook' && that.sessions[$(item).data('path')] !== undefined); state = state || (selection_type === "select-notebooks" && item_type === 'notebook');
set_state = set_state || (selection_type === "select-files" && $(item).data('type') === 'file'); state = state || (selection_type === "select-running-notebooks" && item_type === 'notebook' && that.sessions[$(item).data('path')] !== undefined);
if (set_state) { state = state || (selection_type === "select-files" && item_type === 'file');
$(item).find('input[type=checkbox]').prop('checked', state); $(item).find('input[type=checkbox]').prop('checked', state);
}
}); });
this._selection_changed(); this._selection_changed();
}; };