From 3849b1a7a17f05da5f058e72f4532924fb6ec6e5 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 6 Feb 2023 12:55:02 +0100 Subject: [PATCH] Save handson table metadata [SCI-7834] --- app/assets/javascripts/my_modules/results.js | 21 ++++++------------- .../javascripts/results/result_tables.js.erb | 10 +++++++++ app/controllers/result_tables_controller.rb | 3 ++- .../step_elements/tables_controller.rb | 2 +- .../vue/protocol/step_elements/table.vue | 9 ++++++++ app/serializers/table_serializer.rb | 2 +- app/views/result_tables/_edit.html.erb | 1 + app/views/result_tables/_new.html.erb | 1 + app/views/results/_result_table.html.erb | 1 + .../20230206095817_add_metadata_to_table.rb | 7 +++++++ db/structure.sql | 10 ++++++--- 11 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20230206095817_add_metadata_to_table.rb diff --git a/app/assets/javascripts/my_modules/results.js b/app/assets/javascripts/my_modules/results.js index 0fa29d8e7..50137f35c 100644 --- a/app/assets/javascripts/my_modules/results.js +++ b/app/assets/javascripts/my_modules/results.js @@ -18,7 +18,7 @@ root.find('div.hot-table').each(function() { var $container = $(this).find('.step-result-hot-table'); var contents = $(this).find('.hot-contents'); - + var metadata = $(this).find('.hot-metadata'); $container.handsontable({ width: '100%', startRows: 5, @@ -27,23 +27,14 @@ colHeaders: true, fillHandle: false, formulas: true, - cells: function(row, col) { - var cellProperties = {}; - - if (col >= 0) { - cellProperties.readOnly = true; - } else { - cellProperties.readOnly = false; - } - return cellProperties; - } + data: JSON.parse(contents.attr('value')).data, + cell: JSON.parse(metadata.val() || '{}').cells || [], + readOnly: true }); let hot = $container.handsontable('getInstance'); - let data = JSON.parse(contents.attr('value')); - if (Array.isArray(data.data)) hot.loadData(data.data); setTimeout(() => { - hot.render() - }, 0) + hot.render(); + }, 500); }); } diff --git a/app/assets/javascripts/results/result_tables.js.erb b/app/assets/javascripts/results/result_tables.js.erb index f6a9f4f2d..e51954a78 100644 --- a/app/assets/javascripts/results/result_tables.js.erb +++ b/app/assets/javascripts/results/result_tables.js.erb @@ -8,6 +8,7 @@ var $container = $(this).find('.hot'); var data = null; var contents = $(this).find('.hot-contents'); + var metadata = JSON.parse($(this).find('.hot-metadata').val() || '{}'); if (contents.attr('value')) { data = JSON.parse(contents.attr('value')).data; } @@ -21,6 +22,7 @@ rowHeaders: true, colHeaders: true, contextMenu: true, + cell: metadata.cells || [], formulas: true, preventOverflow: 'horizontal', afterChange: function() { @@ -34,8 +36,16 @@ $form.submit(function(){ var hot = $('.hot').handsontable('getInstance'); var contents = $('.hot-contents'); + var metadata = $('.hot-metadata'); var data = JSON.stringify({data: hot.getData()}); contents.attr('value', data); + metadata.attr('value', JSON.stringify({cells: hot.getCellsMeta().map((x) => { + return { + col: x.col, + row: x.row, + className: x.className || '' + } + })})) return true; }); } diff --git a/app/controllers/result_tables_controller.rb b/app/controllers/result_tables_controller.rb index 4c386e680..c85f96864 100644 --- a/app/controllers/result_tables_controller.rb +++ b/app/controllers/result_tables_controller.rb @@ -170,7 +170,8 @@ class ResultTablesController < ApplicationController :name, :archived, table_attributes: [ :id, - :contents + :contents, + :metadata ] ) end diff --git a/app/controllers/step_elements/tables_controller.rb b/app/controllers/step_elements/tables_controller.rb index 8d8f43486..47d57bf5e 100644 --- a/app/controllers/step_elements/tables_controller.rb +++ b/app/controllers/step_elements/tables_controller.rb @@ -60,7 +60,7 @@ module StepElements private def table_params - params.permit(:name, :contents) + params.permit(:name, :contents, metadata: {}) end def load_table diff --git a/app/javascript/vue/protocol/step_elements/table.vue b/app/javascript/vue/protocol/step_elements/table.vue index 4bad4baaa..60f8d9195 100644 --- a/app/javascript/vue/protocol/step_elements/table.vue +++ b/app/javascript/vue/protocol/step_elements/table.vue @@ -158,6 +158,13 @@ let tableData = JSON.stringify({data: this.tableObject.getData()}); this.element.attributes.orderable.contents = tableData; + this.element.attributes.orderable.metadata = {cells: this.tableObject.getCellsMeta().map((x) => { + return { + col: x.col, + row: x.row, + className: x.className || '' + } + })}; this.update(); this.editingTable = false; }, @@ -167,6 +174,7 @@ loadTableData() { let container = this.$refs.hotTable; let data = JSON.parse(this.element.attributes.orderable.contents); + let metadata = this.element.attributes.orderable.metadata || {}; this.tableObject = new Handsontable(container, { data: data.data, width: '100%', @@ -174,6 +182,7 @@ startCols: 5, rowHeaders: true, colHeaders: true, + cell: metadata.cells || [], contextMenu: this.editingTable, formulas: true, preventOverflow: 'horizontal', diff --git a/app/serializers/table_serializer.rb b/app/serializers/table_serializer.rb index b43f4f5e4..9842580ee 100644 --- a/app/serializers/table_serializer.rb +++ b/app/serializers/table_serializer.rb @@ -4,7 +4,7 @@ class TableSerializer < ActiveModel::Serializer include Canaid::Helpers::PermissionsHelper include Rails.application.routes.url_helpers - attributes :name, :contents, :urls, :icon + attributes :name, :contents, :urls, :icon, :metadata def contents object.contents_utf_8 diff --git a/app/views/result_tables/_edit.html.erb b/app/views/result_tables/_edit.html.erb index 083f92775..afc076dfa 100644 --- a/app/views/result_tables/_edit.html.erb +++ b/app/views/result_tables/_edit.html.erb @@ -6,6 +6,7 @@
<%= f.fields_for :table do |ff| %> <%= ff.hidden_field(:contents, value: ff.object.contents_utf_8, class: "hot-contents" ) %> + <%= ff.hidden_field(:metadata, value: ff.object.metadata, class: "hot-metadata" ) %>
<% end %> diff --git a/app/views/result_tables/_new.html.erb b/app/views/result_tables/_new.html.erb index 1a80c4c46..b9b1ea41d 100644 --- a/app/views/result_tables/_new.html.erb +++ b/app/views/result_tables/_new.html.erb @@ -6,6 +6,7 @@
<%= f.fields_for :table do |ff| %> <%= ff.hidden_field(:contents, value: ff.object.contents, class: "hot-contents" ) %> + <%= ff.hidden_field(:metadata, value: ff.object.metadata, class: "hot-metadata" ) %>
<% end %> diff --git a/app/views/results/_result_table.html.erb b/app/views/results/_result_table.html.erb index c933948ac..6d822fec6 100644 --- a/app/views/results/_result_table.html.erb +++ b/app/views/results/_result_table.html.erb @@ -1,5 +1,6 @@
<%= hidden_field(result.table, :contents, value: result.table.contents_utf_8, class: "hot-contents" ) %> + <%= hidden_field(result.table, :metadata, value: result.table.metadata, class: "hot-metadata" ) %>
diff --git a/db/migrate/20230206095817_add_metadata_to_table.rb b/db/migrate/20230206095817_add_metadata_to_table.rb new file mode 100644 index 000000000..2961be060 --- /dev/null +++ b/db/migrate/20230206095817_add_metadata_to_table.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddMetadataToTable < ActiveRecord::Migration[6.1] + def change + add_column :tables, :metadata, :jsonb + end +end diff --git a/db/structure.sql b/db/structure.sql index c58030d80..1b77714f8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1031,7 +1031,8 @@ CREATE TABLE public.my_modules ( my_module_status_id bigint, status_changing boolean DEFAULT false, changing_from_my_module_status_id bigint, - last_transition_error jsonb + last_transition_error jsonb, + provisioning_status integer ); @@ -2681,7 +2682,8 @@ CREATE TABLE public.tables ( last_modified_by_id bigint, data_vector tsvector, name character varying DEFAULT ''::character varying, - team_id integer + team_id integer, + metadata jsonb ); @@ -8595,6 +8597,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220914124900'), ('20221007113010'), ('20221028085051'), -('20221222123021'); +('20221122132857'), +('20221222123021'), +('20230206095817');