Fixes repository items edit action, adds additional event listener for file upload

This commit is contained in:
ZmagoD 2018-06-29 16:59:26 +02:00
parent 7aacc2b4a2
commit 23de3abd61
3 changed files with 38 additions and 41 deletions

View file

@ -69,7 +69,7 @@
$.each(itemData.repository_row.repository_cells, function(i, cell) {
var tableCellId = 'cellId-' + cell.repository_cell_id;
if(cell.type === 'RepositoryAssetValue') {
formBindingsData[tableCellId] = new Blob([cell.value], { type: cell.value.file_content_type });
formBindingsData[tableCellId] = new File(cell.asset_preview);
} else {
formBindingsData[tableCellId] = cell.value;
}
@ -77,11 +77,10 @@
return formBindingsData;
}
RepositoryItemEditForm.prototype.getRowNewData = function() {
return this.formData;
}
/**
* Private methods
/**
* |-----------------|
* | Private methods |
* |-----------------|
*/
/**
@ -100,7 +99,9 @@
}
/**
* Takes object and creates an input file field
* Takes object and creates an input file field, contains a hidden
* input field which is triggered on button click and we get the uploaded
* file from there.
*
* @param {Object} object
* @param {String} name
@ -111,8 +112,9 @@
*/
function changeToInputFileField(object, name, value, id) {
return "<div class='repository-input-file-field'><div class='form-group'>" +
"<input type='file' class='form-control' data-object='" +
object + "' name='" + name + "' value='" + value + "' id='" + id + "'></div>" +
"<input type='file' name='" + name + "' data-id='" + id + "' style='display:none'>" +
"<button class='form-control' data-object='" +
object + "' name='" + name + "' value='" + value + "' id='" + id + "'>Choose File</button></div>" +
"<a onClick='clearFileInput(this)'>" +
"<i class='fas fa-times'></i>" +
"</a></div>";
@ -155,6 +157,7 @@
html += '</select>';
return html;
}
/**
* Takes an object and creates custom html element
*
@ -182,25 +185,46 @@
/**
* Append the change listener to file field
*
* @param {String} type
* @param {Object} input
* @param {Object} formData
*
* @returns {undefined}
*/
function _addSelectedFile(type, input) {
function _addSelectedFile(type, input, formData) {
if (type === 'RepositoryAssetValue') {
$(input).on('change', function(){
this.dataset.changed = 'true';
}).on('click', function(ev) {
ev.prevetDefault();
ev.stopPropagation();
var input = $(this).closest('input[type="file"]');
input.trigger('click');
initFileHandler(input, formData);
});
}
}
/**
* Handle extraction of file from the input field
*
* @param {Object} formData
*
* @returns {undefined}
*/
function initFileHandler(inputField, formData) {
if (inputField.files[0]) {
}
}
function initializeDataBinding(row_node, data) {
var uiBindings = {};
$.each(_.keys(data), function(i, element) {
uiBindings['#' + element] = element;
})
$(row_node).my({ui: uiBindings}, data);
debugger;
}
})(window);

View file

@ -667,35 +667,6 @@ var RepositoryDatatable = (function(global) {
var editForm = new RepositoryItemEditForm(data, node);
editForm.renderForm(TABLE);
/**
// Record name column
var colIndex = getColumnIndex('#row-name');
if (colIndex) {
$(node).children('td').eq(colIndex)
.html(changeToInputField('repository_row', 'name',
data.repository_row.name));
}
// Take care of custom cells
var cells = data.repository_row.repository_cells;
var list_columns = data.repository_row.repository_column_items;
$(node).children('td').each(function(i) {
var td = $(this);
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')] || '';
td.html(changeToFormField('repository_cell',
$(colHeader).attr('id'),
type,
cell,
list_columns));
_addSelectedFile(type, cell, $(this).find('input')[0]);
}
});
*/
_initSelectPicker();
// initialize smart annotation

View file

@ -86,7 +86,8 @@ 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'
cell_value = cell.value.asset
asset_url = cell.value.asset.url(:original)
else
cell_value = escape_input(cell.value.data)
end
@ -94,6 +95,7 @@ class RepositoryRowsController < ApplicationController
json[:repository_row][:repository_cells][cell.repository_column_id] = {
repository_cell_id: cell.id,
value: cell_value,
asset_preview: (asset_url || ''),
type: cell.value_type,
list_items: fetch_list_items(cell)
}