feature #3748: Integrate selectable data tables

This commit is contained in:
Daniel Molina committed 2015-05-07 18:40:56 +02:00
1 parent d7ad90b6a6
commit e94ff445a5
3 files changed
+509 -11

No files matched your search

@@ -52,6 +52,16 @@ define(function(require) {
Locale.tr("Status"),
]
this.selectOptions = {
"id_index": 1,
"name_index": 4,
"uname_index": 2,
"select_resource": Locale.tr("Please select a datastore from the list"),
"you_selected": Locale.tr("You selected the following datastore:"),
"select_resource_multiple": Locale.tr("Please select one or more datastores from the list"),
"you_selected_multiple": Locale.tr("You selected the following datastores:")
}
TabDataTable.call(this);
};
+473 -9
View File
@@ -7,6 +7,8 @@ define(function(require) {
var TemplateEmptyTable = require('hbs!./tab-datatable/empty-table');
var Sunstone = require('sunstone');
var SunstoneConfig = require('sunstone-config');
var Locale = require('utils/locale');
var Tips = require('utils/tips');
/*
TEMPLATES
@@ -45,8 +47,43 @@ define(function(require) {
// resource
// dataTableOptions
// columns
// conf {info: true, actions: true, select: true}
//
this.dataTableHTML = TemplateDataTableHTML({'dataTableId': this.dataTableId, 'columns': this.columns});
var that = this;
if (this.conf.select) {
if (!this.selectOptions.select_resource) {
this.selectOptions.select_resource = Locale.tr("Please select a resource from the list");
}
if (!this.selectOptions.you_selected) {
this.selectOptions.you_selected = Locale.tr("You selected the following resource:");
}
if (this.selectOptions.id_index == undefined) {
this.selectOptions.id_index = 0;
}
$.extend(that.selectOptions, conf.selectOptions);
that.selectOptions.fixed_ids_map_orig = {};
if (that.selectOptions.fixed_ids != undefined) {
$.each(that.selectOptions.fixed_ids, function() {
that.selectOptions.fixed_ids_map_orig[this] = true;
});
}
if (that.selectOptions.multiple_choice == undefined) {
that.selectOptions.multiple_choice = false;
}
}
this.dataTableHTML = TemplateDataTableHTML({
'dataTableId': this.dataTableId,
'columns': this.columns,
'conf': this.conf,
'selectOptions': this.selectOptions});
this.searchInputHTML = TemplateSearchInputHTML({'dataTableSearchId': this.dataTableId + 'Search'});
return this;
@@ -65,7 +102,12 @@ define(function(require) {
'getElementData': _getElementData,
'waitingNodes': _waitingNodes,
'recountCheckboxes': _recountCheckboxes,
'filter': _filter
'filter': _filter,
'resetResourceTableSelect': _resetResourceTableSelect,
'retrieveResourceTableSelect': _retrieveResourceTableSelect,
'refreshResourceTableSelect': _refreshResourceTableSelect,
'selectResourceTableSelect': _selectResourceTableSelect,
'updateFn': _updateFn
}
return TabDatatable;
@@ -86,7 +128,6 @@ define(function(require) {
that.recountCheckboxes(that.dataTable);
})
this.dataTable.fnSetColumnVis(0, false);
this.dataTable.fnSort([[1, SunstoneConfig.tableOrder]]);
@@ -99,6 +140,10 @@ define(function(require) {
if (this.conf.info) {
this.infoListener(this.resource + ".show", this.tabId);
}
if (this.conf.select) {
_initSelectResourceTableSelect();
}
}
//Shows run a custom action when clicking on rows.
@@ -254,7 +299,8 @@ define(function(require) {
// Updates a data_table, with a 2D array containing the new values
// Does a partial redraw, so the filter and pagination are kept
function _updateView(request, list) {
// fromArray if true do not process the list since it is already an array of elements
function _updateView(request, list, fromArray) {
var selected_row_id = null;
var checked_row_ids = new Array();
var that = this;
@@ -291,11 +337,15 @@ define(function(require) {
this.dataTable.fnClearTable(false);
var item_list = [];
$.each(list, function() {
item_list.push(that.elementArray(this));
});
var item_list;
if (fromArray) {
item_list = list;
} else {
item_list = [];
$.each(list, function() {
item_list.push(that.elementArray(this));
});
}
var that = this;
if (item_list.length > 0) {
@@ -393,4 +443,418 @@ define(function(require) {
function _filter(value, columnId) {
this.dataTable.fnFilter(value, columnId);
}
/*
SELECT RESOURCE FUNCTION DEFINITIONS
*/
function _initSelectResourceTableSelect() {
var that = this;
var section = $('#' + that.dataTableId + 'Container');
if (that.selectOptions.id_index == undefined) {
that.selectOptions.id_index = 0;
}
if (that.selectOptions.name_index == undefined) {
that.selectOptions.name_index = 1;
}
if (that.selectOptions.dataTable_options == undefined) {
that.selectOptions.dataTable_options = {};
}
if (that.selectOptions.select_callback == undefined) {
that.selectOptions.select_callback = function() {};
}
if (that.selectOptions.multiple_choice) {
that.selectOptions.dataTable_options.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
var row_id = aData[that.selectOptions.id_index];
var ids = $('#selected_ids_row_' + that.dataTableId, section).data("ids");
if (ids[row_id]) {
$("td", nRow).addClass('markrowchecked');
$('input.check_item', this).attr('checked', 'checked');
} else {
$("td", nRow).removeClass('markrowchecked');
$('input.check_item', this).removeAttr('checked');
}
};
} else {
that.selectOptions.dataTable_options.fnRowCallback = function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
var row_id = aData[that.selectOptions.id_index];
var selected_id = $('#selected_resource_id_' + that.dataTableId, section).val();
if (row_id == selected_id) {
$("td", nRow).addClass('markrow');
$('input.check_item', this).attr('checked', 'checked');
} else {
$("td", nRow).removeClass('markrow');
$('input.check_item', this).removeAttr('checked');
}
};
}
$('#refresh_button_' + that.dataTableId, section).off("click");
section.on('click', '#refresh_button_' + that.dataTableId, function() {
that.updateFn($('table[id=datatable_' + that.dataTableId + ']', section).dataTable());
return false;
});
$('#' + that.dataTableId + '_search', section).keyup(function() {
that.dataTable.fnFilter($(this).val());
})
that.dataTable.fnSort([[that.selectOptions.id_index, config['user_config']['table_order']]]);
if (that.selectOptions.read_only) {
$('#selected_ids_row_' + that.dataTableId, section).hide();
} else if (that.selectOptions.multiple_choice) {
$('#selected_resource_' + that.dataTableId, section).hide();
$('#select_resource_' + that.dataTableId, section).hide();
$('#selected_resource_multiple_' + that.dataTableId, section).hide();
$('#select_resource_multiple_' + that.dataTableId, section).show();
}
$('#selected_resource_id_' + that.dataTableId, section).hide();
$('#selected_resource_name_' + that.dataTableId, section).hide();
$('#selected_ids_row_' + that.dataTableId, section).data("options", that.selectOptions);
if (that.selectOptions.read_only) {
} else if (that.selectOptions.multiple_choice) {
$('#selected_ids_row_' + that.dataTableId, section).data("ids", {});
function row_click(row, aData) {
that.dataTable.unbind("draw");
var row_id = aData[that.selectOptions.id_index];
var row_name = aData[that.selectOptions.name_index];
var ids = $('#selected_ids_row_' + that.dataTableId, section).data("ids");
if (ids[row_id]) {
delete ids[row_id];
// Happens if row is not yet rendered (i.e. higher unvisited page)
if (row != undefined) {
$("td", row).removeClass('markrowchecked');
$('input.check_item', row).removeAttr('checked');
}
$('#selected_ids_row_' + that.dataTableId + ' span[row_id="' + row_id + '"]', section).remove();
} else {
ids[row_id] = true;
// Happens if row is not yet rendered (i.e. higher unvisited page)
if (row != undefined) {
$("td", row).addClass('markrowchecked');
$('input.check_item', row).attr('checked', 'checked');
}
$('#selected_ids_row_' + that.dataTableId, section).append('<span row_id="' + row_id + '" class="radius label">' + row_name + ' <span class="fa fa-times blue"></span></span> ');
that.selectOptions.select_callback(aData, that.selectOptions);
}
if ($.isEmptyObject(ids)) {
$('#selected_resource_multiple_' + that.dataTableId, section).hide();
$('#select_resource_multiple_' + that.dataTableId, section).show();
} else {
$('#selected_resource_multiple_' + that.dataTableId, section).show();
$('#select_resource_multiple_' + that.dataTableId, section).hide();
}
$('.alert-box', section).hide();
return true;
};
$('#' + that.dataTableId + ' tbody', section).on("click", "tr", function(e) {
var aData = that.dataTable.fnGetData(this);
row_click(this, aData);
});
$(section).on("click", '#selected_ids_row_' + that.dataTableId + ' span.fa.fa-times', function() {
var row_id = $(this).parent("span").attr('row_id');
var found = false;
// TODO: improve preformance, linear search
$.each(that.dataTable.fnGetData(), function(index, row) {
if (row[that.selectOptions.id_index] == row_id) {
found = true;
row_click(that.dataTable.fnGetNodes(index), row);
return false;
}
});
if (!found) {
var ids = $('#selected_ids_row_' + that.dataTableId, section).data("ids");
delete ids[row_id];
$('#selected_ids_row_' + that.dataTableId + ' span[row_id="' + row_id + '"]', section).remove();
if ($.isEmptyObject(ids)) {
$('#selected_resource_multiple_' + that.dataTableId, section).hide();
$('#select_resource_multiple_' + that.dataTableId, section).show();
} else {
$('#selected_resource_multiple_' + that.dataTableId, section).show();
$('#select_resource_multiple_' + that.dataTableId, section).hide();
}
}
});
} else {
$('#' + that.dataTableId + ' tbody', section).delegate("tr", "click", function(e) {
that.dataTable.unbind("draw");
var aData = that.dataTable.fnGetData(this);
$("td.markrow", that.dataTable).removeClass('markrow');
$('tbody input.check_item', that.dataTable).removeAttr('checked');
$('#selected_resource_' + that.dataTableId, section).show();
$('#select_resource_' + that.dataTableId, section).hide();
$('.alert-box', section).hide();
$("td", this).addClass('markrow');
$('input.check_item', this).attr('checked', 'checked');
$('#selected_resource_id_' + that.dataTableId, section).val(aData[that.selectOptions.id_index]).change();
$('#selected_resource_id_' + that.dataTableId, section).hide();
$('#selected_resource_name_' + that.dataTableId, section).text(aData[that.selectOptions.name_index]).change();
$('#selected_resource_name_' + that.dataTableId, section).show();
that.selectOptions.select_callback(aData, that.selectOptions);
return true;
});
}
Tips.setup(section);
}
function _resetResourceTableSelect() {
var that = this;
var section = $('#' + that.dataTableId + 'Container');
// TODO: do for multiple_choice
// TODO: works for more than one page?
$("td.markrow", that.dataTable).removeClass('markrow');
$('tbody input.check_item', that.dataTable).removeAttr('checked');
$('#' + that.dataTableId + '_search', section).val("").trigger("keyup");
$('#refresh_button_' + that.dataTableId).click();
$('#selected_resource_id_' + that.dataTableId, section).val("").hide();
$('#selected_resource_name_' + that.dataTableId, section).text("").hide();
$('#selected_resource_' + that.dataTableId, section).hide();
$('#select_resource_' + that.dataTableId, section).show();
}
// Returns an ID, or an array of IDs for opts.multiple_choice
function _retrieveResourceTableSelect() {
var that = this;
var section = $('#' + that.dataTableId + 'Container');
if (that.selectOptions.multiple_choice) {
var ids = $('#selected_ids_row_' + that.dataTableId, section).data("ids");
var arr = [];
$.each(ids, function(key, val) {
arr.push(key);
});
return arr;
} else {
return $('#selected_resource_id_' + that.dataTableId, section).val();
}
}
// Clicks the refresh button
function _refreshResourceTableSelect() {
var that = this;
var section = $('#' + that.dataTableId + 'Container');
$('#refresh_button_' + that.dataTableId, section).click();
}
function _selectResourceTableSelect(selectedResources) {
var that = this;
var section = $('#' + that.dataTableId + 'Container');
if (that.selectOptions.multiple_choice) {
_refreshResourceTableSelect(section, that.dataTableId);
var data_ids = $('#selected_ids_row_' + that.dataTableId, section).data("ids");
data_ids = {};
$('#selected_ids_row_' + that.dataTableId + ' span[row_id]', section).remove();
if (selectedResources.ids == undefined) {
selectedResources.ids = [];
}
// TODO: {name, uname} support for multiple_choice
$.each(selectedResources.ids, function(index, row_id) {
if (isNaN(row_id)) {
return true;
}
data_ids[row_id] = true;
var row_name = "" + row_id;
// TODO: improve preformance, linear search. Needed to get the
// name of the resource in the label. If function getName() was
// indexed in the cache, it could be used here
$.each(that.dataTable.fnGetData(), function(index, row) {
if (row[that.selectOptions.id_index] == row_id) {
row_name = row[that.selectOptions.name_index];
return false;
}
});
$('#selected_ids_row_' + that.dataTableId, section).append('<span row_id="' + row_id + '" class="radius label">' + row_name + ' <span class="fa fa-times blue"></span></span> ');
});
$('#selected_ids_row_' + that.dataTableId, section).data("ids", data_ids);
if ($.isEmptyObject(data_ids)) {
$('#selected_resource_multiple_' + that.dataTableId, section).hide();
$('#select_resource_multiple_' + that.dataTableId, section).show();
} else {
$('#selected_resource_multiple_' + that.dataTableId, section).show();
$('#select_resource_multiple_' + that.dataTableId, section).hide();
}
$('.alert-box', section).hide();
that.dataTable.fnDraw();
} else {
$("td.markrow", that.dataTable).removeClass('markrow');
$('tbody input.check_item', that.dataTable).removeAttr('checked');
$('#selected_resource_' + that.dataTableId, section).show();
$('#select_resource_' + that.dataTableId, section).hide();
$('.alert-box', section).hide();
var row_id = undefined;
var row_name = "";
if (that.selectOptions.ids != undefined) {
row_id = that.selectOptions.ids;
row_name = "" + row_id;
// TODO: improve preformance, linear search. Needed to get the
// name of the resource in the label. If function getName() was
// indexed in the cache, it could be used here
$.each(that.dataTable.fnGetData(), function(index, row) {
if (row[that.selectOptions.id_index] == row_id) {
row_name = row[that.selectOptions.name_index];
return false;
}
});
} else if (selectedResources.names != undefined) {
row_name = selectedResources.names.name;
var row_uname = selectedResources.names.uname;
$.each(that.dataTable.fnGetData(), function(index, row) {
if (row[options.name_index] == row_name &&
row[options.uname_index] == row_uname) {
row_id = row[that.selectOptions.id_index];
return false;
}
});
}
// $("td", this).addClass('markrow');
// $('input.check_item', this).attr('checked','checked');
$('#selected_resource_id_' + that.dataTableId, section).val(row_id).change();
$('#selected_resource_id_' + that.dataTableId, section).hide();
$('#selected_resource_name_' + that.dataTableId, section).text(row_name).change();
$('#selected_resource_name_' + that.dataTableId, section).show();
that.refreshResourceTableSelect(section, that.dataTableId);
}
}
function _updateFn() {
var that = this;
var success_func = function (request, resource_list) {
var list_array = [];
var fixed_ids_map = $.extend({}, fixed_ids_map_orig);
$.each(resource_list, function() {
var add = true;
if (opts.filter_fn) {
add = opts.filter_fn(this.DATASTORE);
}
if (opts.fixed_ids != undefined) {
add = (add && fixed_ids_map[this.DATASTORE.ID]);
}
if (add) {
list_array.push(that.elementArray(this));
delete fixed_ids_map[this.DATASTORE.ID];
}
});
var n_columns = that.columns.length() + 1; // SET FOR EACH RESOURCE
$.each(fixed_ids_map, function(id, v) {
var empty = [];
for (var i = 0; i <= n_columns; i++) {
empty.push("");
}
empty[that.selectOptions.id_index] = id; // SET FOR EACH RESOURCE, id_index
list_array.push(empty);
});
that.updateView(null, list_array, true);
}
var error_func = function(request, error_json, container) {
success_func(request, []);
onError(request, error_json, container);
}
if (opts.zone_id == undefined) {
OpenNebula[that.resource].list({
timeout: true,
success: success_func,
error: error_func
});
} else {
OpenNebula[that.resource].list_in_zone({
data: {zone_id: opts.zone_id},
timeout: true,
success: success_func,
error: error_func
});
}
}
})
@@ -1,3 +1,14 @@
<div id="{{dataTableId}}Container">
{{#if conf.select}}
<div class="row">
<div class="large-8 small-2 columns">
<a id="refresh_button_{{dataTableId}}" href="#" class="button small radius secondary"> <i class="fa fa-refresh" /></a>
</div>
<div class="large-4 small-10 columns">
<input id="{{dataTableId}}_search" class="search" type="search" placeholder="{{tr "Search"}}"/>
</div>
</div>
{{/if}}
<table id="{{dataTableId}}" class="datatable twelve">
<thead>
<tr>
@@ -5,9 +16,22 @@
<input type="checkbox" class="check_all" value=""/>
</th>
{{#each columns}}
<th>{{this}}</th>
<th>{{this}}</th>
{{/each}}
</tr>
</thead>
<tbody></tbody>
</table>
</table>
{{#if conf.select}}
<div class="row">
<div class="large-12 columns" id="selected_ids_row_{{dataTableId}}">
<label for="selected_resource_id_{{dataTableId}}" id="select_resource_{{dataTableId}}" class="radius secondary label">{{optionsSelect.select_resource}}</label>
<span id="selected_resource_{{dataTableId}}" class="radius secondary label" style="display: none;">{{optionsSelect.you_selected}}</span>
<span id="select_resource_multiple_{{dataTableId}}" class="radius secondary label" style="display: none;">{{optionsSelect.select_resource_multiple}}</span>
<span id="selected_resource_multiple_{{dataTableId}}" class="radius secondary label" style="display: none;">{{optionsSelect.you_selected_multiple}}</span>
<input id="selected_resource_id_{{dataTableId}}" type="text"/>
<span id="selected_resource_name_{{dataTableId}}" class="radius label" type="text"></span>
</div>
</div>
{{/if}}
</div>