Merge pull request #1094 from mlorb/ml-sci-2075

implement file column type when editing & creating new rows... [SCI-2075]
This commit is contained in:
mlorb 2018-04-16 14:21:29 +02:00 committed by GitHub
commit aa2a347f1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 133 additions and 109 deletions

View file

@ -454,6 +454,10 @@ var RepositoryDatatable = (function(global) {
$(th).attr('data-type') === 'RepositoryListValue') {
input = initialListItemsRequest($(th).attr('id'));
tr.appendChild(createTdElement(input));
} else if ($(th).hasClass('repository-column') &&
$(th).attr('data-type') === 'RepositoryAssetValue') {
input = changeToInputFileField('repository_cell_file', th.attr('id'), '');
tr.appendChild(createTdElement(input));
} else {
// Column we don't care for, just add empty td
tr.appendChild(createTdElement(''));
@ -612,19 +616,15 @@ var RepositoryDatatable = (function(global) {
var rawIndex = TABLE.column.index('fromVisible', i);
var colHeader = TABLE.column(rawIndex).header();
if ($(colHeader).hasClass('repository-column')) {
var type = $(colHeader).attr('data-type');
// Check if cell on this record exists
var cell = cells[$(colHeader).attr('id')];
if (cell) {
var cell = cells[$(colHeader).attr('id')] || '';
td.html(changeToFormField('repository_cell',
$(colHeader).attr('id'),
type,
cell,
list_columns));
} else {
td.html(changeToFormField('repository_cell',
$(colHeader).attr('id'),
'',
list_columns));
}
_addSelectedFile(type, cell, $(this).find('input')[0]);
_initSelectPicker();
}
});
@ -663,28 +663,32 @@ var RepositoryDatatable = (function(global) {
node = selectedRecord;
}
// First fetch all the data in input fields
var data = {
request_url: $(TABLE_ID).data('current-uri'),
repository_row_id: $(selectedRecord).attr('id'),
repository_row: {},
repository_cells: {}
};
var formData = new FormData();
formData.append('request_url', $(TABLE_ID).data('current-uri'));
formData.append('repository_row_id', $(selectedRecord).attr('id'));
// Direct record attributes
// Record name
data.repository_row.name = $('td input[data-object = repository_row]').val();
formData.append('repository_row_name', $('td input[data-object = repository_row]').val());
// Custom cells text type
$(node).find('td input[data-object = repository_cell]').each(function() {
// Send data only and only if cell is not empty
if ($(this).val().trim()) {
data.repository_cells[$(this).attr('name')] = $(this).val();
formData.append('repository_cells[' + $(this).attr('name') + ']', $(this).val());
}
});
// Custom cells file type
$(node).find('td input[data-object = repository_cell_file]').each(function() {
// Send data only and only if cell is not empty
if ($(this).context.files.length == 1) {
formData.append('repository_cells[' + $(this).attr('name') + ']', $(this).context.files[0]);
}
});
// Custom cells list type
$(node).find('td[column_id]').each(function(index, el) {
var value = $(el).attr('list_item_id');
data.repository_cells[$(el).attr('column_id')] = value;
formData.append('repository_cells[' + $(el).attr('column_id') + ']', value);
});
var url;
@ -700,7 +704,9 @@ var RepositoryDatatable = (function(global) {
url: url,
type: type,
dataType: 'json',
data: data,
data: formData,
processData: false,
contentType: false,
success: function(data) {
HelperModule.flashAlertMsg(data.flash, 'success');
SmartAnnotation.closePopup();
@ -910,6 +916,15 @@ var RepositoryDatatable = (function(global) {
return _listItemDropdown(massage_response, '-1', column_id);
}
function _addSelectedFile(type, cell, input) {
if (type === 'RepositoryAssetValue' && cell.value != null) {
const dT = new ClipboardEvent('').clipboardData || // Firefox workaround exploiting https://bugzilla.mozilla.org/show_bug.cgi?id=1422655
new DataTransfer(); // specs compliant (as of March 2018 only Chrome)
dT.items.add(new File([_], cell.value.file_file_name));
input.files = dT.files;
}
}
function _initSelectPicker() {
$('.selectpicker')
.selectpicker({liveSearch: true})
@ -972,21 +987,23 @@ var RepositoryDatatable = (function(global) {
object + "' name='" + name + "' value='" + value + "'></input></div>";
}
// Takes object and surrounds it with input
function changeToInputFileField(object, name, value) {
return "<div class='form-group'><input type='file' class='form-control' data-object='" +
object + "' name='" + name + "' value='" + value + "'></input></div>";
}
// Takes an object and creates custom html element
function changeToFormField(object, name, cell, list_columns) {
if (cell === '') {
function changeToFormField(object, name, column_type, cell, list_columns) {
var value = cell.value || '';
if (column_type === 'RepositoryListValue') {
var column = _.findWhere(list_columns, { column_id: parseInt(name) });
if (column) {
return _listItemDropdown(column.list_items, '', parseInt(name));
var list_items = column.list_items || cell.list_items;
return _listItemDropdown(list_items, value, parseInt(name));
} else if (column_type === 'RepositoryAssetValue') {
return changeToInputFileField('repository_cell_file', name, value);
} else {
return changeToInputField(object, name, '');
}
} else {
if (cell.type === 'RepositoryListValue') {
return _listItemDropdown(cell.list_items, cell.value, parseInt(name));
} else {
return changeToInputField(object, name, cell.value);
}
return changeToInputField(object, name, value);
}
}

View file

@ -30,45 +30,11 @@ class RepositoryRowsController < ApplicationController
repository_cells: [] }
record.transaction do
record.name = record_params[:name] unless record_params[:name].blank?
record.name = record_params[:repository_row_name] unless record_params[:repository_row_name].blank?
errors[:default_fields] = record.errors.messages unless record.save
if cell_params
cell_params.each do |key, value|
column = @repository.repository_columns.detect do |c|
c.id == key.to_i
end
if column.data_type == 'RepositoryListValue'
next if value == '-1'
# check if item existx else revert the transaction
list_item = RepositoryListItem.where(repository_column: column)
.find(value)
cell_value = RepositoryListValue.new(
repository_list_item_id: list_item.id,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: record,
repository_column: column
}
)
else
cell_value = RepositoryTextValue.new(
data: value,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: record,
repository_column: column
}
)
end
if cell_value.save
record_annotation_notification(record, cell_value.repository_cell)
else
errors[:repository_cells] << {
"#{column.id}": cell_value.errors.messages
}
end
next if create_cell_value(record, key, value, errors).nil?
end
end
raise ActiveRecord::Rollback if errors[:repository_cells].any?
@ -113,9 +79,15 @@ class RepositoryRowsController < ApplicationController
# Add custom cells ids as key (easier lookup on js side)
@record.repository_cells.each do |cell|
if cell.value_type == 'RepositoryAssetValue'
cell_value = cell.value.asset if cell.value_type == 'RepositoryAssetValue'
else
cell_value = escape_input(cell.value.data)
end
json[:repository_row][:repository_cells][cell.repository_column_id] = {
repository_cell_id: cell.id,
value: escape_input(cell.value.data),
value: cell_value,
type: cell.value_type,
list_items: fetch_list_items(cell)
}
@ -134,7 +106,7 @@ class RepositoryRowsController < ApplicationController
}
@record.transaction do
@record.name = record_params[:name].blank? ? nil : record_params[:name]
@record.name = record_params[:repository_row_name].blank? ? nil : record_params[:repository_row_name]
errors[:default_fields] = @record.errors.messages unless @record.save
if cell_params
cell_params.each do |key, value|
@ -154,6 +126,16 @@ class RepositoryRowsController < ApplicationController
else
existing.delete
end
elsif existing.value_type == 'RepositoryAssetValue'
if existing.value.asset.update(file: value)
existing.value.asset.created_by = current_user
existing.value.asset.last_modified_by = current_user
existing.value.asset.post_process_file(current_team)
else
errors[:repository_cells] << {
"#{existing.repository_column_id}": { data: existing.value.asset.errors.messages[:file].first }
}
end
else
existing.value.data = value
if existing.value.save
@ -168,42 +150,7 @@ class RepositoryRowsController < ApplicationController
else
# Looks like it is a new cell, so we need to create new value, cell
# will be created automatically
column = @repository.repository_columns.detect do |c|
c.id == key.to_i
end
if column.data_type == 'RepositoryListValue'
next if value == '-1'
# check if item existx else revert the transaction
list_item = RepositoryListItem.where(repository_column: column)
.find(value)
cell_value = RepositoryListValue.new(
repository_list_item_id: list_item.id,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: @record,
repository_column: column
}
)
else
cell_value = RepositoryTextValue.new(
data: value,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: @record,
repository_column: column
}
)
end
if cell_value.save
record_annotation_notification(@record,
cell_value.repository_cell)
else
errors[:repository_cells] << {
"#{column.id}": cell_value.errors.messages
}
end
next if create_cell_value(@record, key, value, errors).nil?
end
end
# Clean up empty cells, not present in updated record
@ -240,6 +187,66 @@ class RepositoryRowsController < ApplicationController
end
end
def create_cell_value(record, key, value, errors)
column = @repository.repository_columns.detect do |c|
c.id == key.to_i
end
if column.data_type == 'RepositoryListValue'
return if value == '-1'
# check if item existx else revert the transaction
list_item = RepositoryListItem.where(repository_column: column)
.find(value)
cell_value = RepositoryListValue.new(
repository_list_item_id: list_item.id,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: record,
repository_column: column
}
)
elsif column.data_type == 'RepositoryAssetValue'
asset = Asset.new(file: value,
created_by: current_user,
last_modified_by: current_user,
team: current_team)
if asset.save
asset.post_process_file(current_team)
else
errors[:repository_cells] << {
"#{column.id}": { data: asset.errors.messages[:file].first }
}
end
cell_value = RepositoryAssetValue.new(
asset: asset,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: record,
repository_column: column
}
)
else
cell_value = RepositoryTextValue.new(
data: value,
created_by: current_user,
last_modified_by: current_user,
repository_cell_attributes: {
repository_row: record,
repository_column: column
}
)
end
if cell_value.save
record_annotation_notification(record,
cell_value.repository_cell)
else
errors[:repository_cells] << {
"#{column.id}": cell_value.errors.messages
}
end
end
def delete_records
deleted_count = 0
if selected_params
@ -313,7 +320,7 @@ class RepositoryRowsController < ApplicationController
end
def record_params
params.require(:repository_row).permit(:name).to_h
params.permit(:repository_row_name).to_h
end
def cell_params