From e94ff445a56ab3b632515e80f7ed50b1a338bdab Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 7 May 2015 18:40:56 +0200 Subject: [PATCH] feature #3748: Integrate selectable data tables --- .../app/tabs/datastores-tab/datatable.js | 10 + .../public/app/utils/tab-datatable.js | 482 +++++++++++++++++- .../public/app/utils/tab-datatable/table.hbs | 28 +- 3 files changed, 509 insertions(+), 11 deletions(-) diff --git a/src/sunstone/public/app/tabs/datastores-tab/datatable.js b/src/sunstone/public/app/tabs/datastores-tab/datatable.js index 013edba642..e4bffb8153 100644 --- a/src/sunstone/public/app/tabs/datastores-tab/datatable.js +++ b/src/sunstone/public/app/tabs/datastores-tab/datatable.js @@ -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); }; diff --git a/src/sunstone/public/app/utils/tab-datatable.js b/src/sunstone/public/app/utils/tab-datatable.js index 3fc0c4a952..4df87617c2 100644 --- a/src/sunstone/public/app/utils/tab-datatable.js +++ b/src/sunstone/public/app/utils/tab-datatable.js @@ -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('' + row_name + ' '); + + 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('' + row_name + ' '); + }); + + $('#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 + }); + } + } }) diff --git a/src/sunstone/public/app/utils/tab-datatable/table.hbs b/src/sunstone/public/app/utils/tab-datatable/table.hbs index 8f1c57edd0..146af7fbb6 100644 --- a/src/sunstone/public/app/utils/tab-datatable/table.hbs +++ b/src/sunstone/public/app/utils/tab-datatable/table.hbs @@ -1,3 +1,14 @@ +
+{{#if conf.select}} +
+
+ +
+
+ +
+
+{{/if}} @@ -5,9 +16,22 @@ {{#each columns}} - + {{/each}} -
{{this}}{{this}}
\ No newline at end of file + +{{#if conf.select}} +
+
+ + + + + + +
+
+{{/if}} +
\ No newline at end of file