From 28bac146677f655212f06b5a76737b37c3a7c560 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 3 Jul 2018 12:57:39 +0200 Subject: [PATCH] first working version of the edit form object --- .../forms/repository_item_edit.js | 159 ++++++++++-------- .../repositories/repository_datatable.js.erb | 88 +++++----- app/controllers/repository_rows_controller.rb | 3 +- 3 files changed, 142 insertions(+), 108 deletions(-) diff --git a/app/assets/javascripts/repositories/forms/repository_item_edit.js b/app/assets/javascripts/repositories/forms/repository_item_edit.js index e4d530040..74de4285b 100644 --- a/app/assets/javascripts/repositories/forms/repository_item_edit.js +++ b/app/assets/javascripts/repositories/forms/repository_item_edit.js @@ -3,12 +3,12 @@ (function(global) { 'use strict'; - + /** * RepositoryItemEditForm generates the html inputs for * repository item and returns the form data object - * - * @param {Object} itemData - repository item data fetched from the API + * + * @param {Object} itemData - repository item data fetched from the API * @param {Object} repositoryItemElement - row node in the table */ global.RepositoryItemEditForm = function(itemData, repositoryItemElement) { @@ -19,7 +19,7 @@ /** * Generates the input fields - * + * * @param {Object} table - datatable.js object * @returns {undefinded} */ @@ -41,7 +41,7 @@ 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'); var colHeaderId = $(colHeader).attr('id'); @@ -51,6 +51,7 @@ type, cell, list_columns)); + _addSelectedFile(type, cell, colHeaderId, formData); appendNewElementToFormData(cell, colHeaderId, formData); } }); @@ -59,18 +60,18 @@ /** * Parse received data in to a from object - * + * * @param {Object} itemData - json representations of repository item - * + * * @returns {Object} */ RepositoryItemEditForm.prototype.composeFormData = function(itemData) { var formBindingsData = {}; formBindingsData['rowName'] = itemData.repository_row.name; $.each(itemData.repository_row.repository_cells, function(i, cell) { - var tableCellId = 'cellId-' + cell.repository_cell_id; + var tableCellId = 'colId-' + cell.repository_cell_id; if(cell.type === 'RepositoryAssetValue') { - formBindingsData[tableCellId] = new File(cell.asset_preview); + formBindingsData[tableCellId] = new File([""], cell.value.file_file_name); } else { formBindingsData[tableCellId] = cell.value; } @@ -80,27 +81,45 @@ /** * Handles select picker default value - * + * * @returns {undefinded} */ - RepositoryItemEditForm.prototype.initializeSelectpickerValues = function(selectpicker) { + RepositoryItemEditForm.prototype.initializeSelectpickerValues = function() { $('.bootstrap-select').each(function(_, dropdown) { var selectedValue = $($(dropdown).find('select')[0]).data('selected-value'); + var selectPicker = $($(dropdown).find('select')[0]); var value = '-1' $(dropdown).find('option').each(function(_, option) { - $(option).removeAttr('selected'); + $(option).removeAttr('selected'); if($(option).val() === selectedValue.toString()) { $(option).attr('selected', true); value = $(option).attr('value'); } }); $(dropdown).parent().attr("list_item_id", value); - $(dropdown).val(value); + selectPicker.val(value); + selectPicker.selectpicker('refresh'); }); - selectpicker(); } - /** + RepositoryItemEditForm.prototype.parseToFormObject = function(tableID, selectedRecord) { + var formData = this.formData; + var formDataObj = new FormData(); + formDataObj.append('request_url', $(tableID).data('current-uri')); + formDataObj.append('repository_row_id', $(selectedRecord).attr('id')); + debugger; + $(_.keys(this.formData)).each(function(_, element) { + var value = formData[element]; + if (element === "rowName") { + formDataObj.append('repository_row_name', value); + } else { + var colId = element.replace('colId-', ''); + formDataObj.append('repository_cells[' + colId + ']', value); + } + }); + return formDataObj; + } + /** * |-----------------| * | Private methods | * |-----------------| @@ -108,12 +127,12 @@ /** * Takes object and surrounds it with input - * + * * @param {Object} object * @param {String} name * @param {String} value * @param {String} id - * + * * @returns (String) */ function changeToInputField(object, name, value, id) { @@ -125,19 +144,22 @@ * 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 * @param {String} value * @param {String} id - * + * * @returns (String) */ function changeToInputFileField(object, name, value, id) { + var fileName = (value.file_file_name) ? value.file_file_name : ""; + var buttonLabel = "Choose File:"; return "
" + - "" + - "
" + + "" + + "

" + fileName + "

" + "" + "" + ""; @@ -145,30 +167,30 @@ /** * Returns the colum index - * - * @param {Object} table - * @param {String} id - * + * + * @param {Object} table + * @param {String} id + * * @returns (Boolean | Number) */ function getColumnIndex(table, id) { if(id < 0) - return false; + return false; return table.column(id).index('visible'); } /** * Genrates list items dropdown element - * - * @param {Array} options - * @param {String} current_value + * + * @param {Array} options + * @param {String} current_value * @param {Number} column_id * @param {String} id - * + * * @returns (String) */ function _listItemDropdown(options, current_value, column_id, id) { - var val = undefined; + var val = undefined; var html = '