From e02d780e4434478288aa548875ebf75de813e37f Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 6 Dec 2016 11:54:56 +0100 Subject: [PATCH] Added Enter key handling and fixed error reporting [SCI-712] --- .../javascripts/samples/sample_datatable.js | 123 ++++++++++-------- 1 file changed, 67 insertions(+), 56 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index f4e54b97e..39f04d710 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -768,6 +768,66 @@ function changeToEditMode() { var dropdownList = $('#samples-columns-list'); + function createNewColumn() { + // Make an Ajax request to custom_fields_controller + var url = $('#new-column-form').data('action'); + var columnName = $('#new-column-name').val(); + if (columnName.length > 0) { + $.ajax({ + method: 'POST', + dataType: 'json', + data: {custom_field: {name: columnName}}, + error: function(data) { + var form = $('#new-column-form'); + form.addClass('has-error'); + form.find('.help-block').remove(); + form.append('' + + data.responseJSON.name + + ''); + }, + success: function(data) { + var form = $('#new-column-form'); + form.find('.help-block').remove(); + if (form.hasClass('has-error')) { + form.removeClass('has-error'); + } + $('#new-column-name').val(''); + form.append('' + + I18n.t('samples.js.column_added') + + ''); + $('#samples').data('num-columns', + $('#samples').data('num-columns') + 1); + originalHeader.append( + '' + + data.name + ''); + var colOrder = table.colReorder.order(); + colOrder.push(colOrder.length); + table.colReorder.reset(); + // Remove all event handlers as we re-initialize them later with + // new table + $('#samples').off(); + $('#samples thead').empty(); + $('#samples thead').append(originalHeader); + // Preserve save/delete buttons as we need them after new table + // will be created + $('div.toolbarButtons').appendTo('div.samples-table'); + $('div.toolbarButtons').hide(); + table = dataTableInit(); + table.colReorder.order(colOrder, true); + loadColumnsNames(); + }, + url: url + }); + } else { + var form = $('#new-column-form'); + form.addClass('has-error'); + form.find('.help-block').remove(); + form.append('' + + I18n.t('samples.js.empty_column_name') + + ''); + } + } + function initNewColumnForm() { $('#samples-columns-dropdown').on('show.bs.dropdown', function() { // Clear input and errors when dropdown is opened @@ -781,63 +841,14 @@ function changeToEditMode() { }); $('#add-new-column-button').click(function(e) { - // Make an Ajax request to custom_fields_controller - var url = $('#new-column-form').data('action'); e.stopPropagation(); - var columnName = $('#new-column-name').val(); - if (columnName.length > 0) { - $.ajax({ - method: 'POST', - dataType: 'json', - data: {custom_field: {name: columnName}}, - error: function(data) { - var form = $('#new-column-form'); - form.addClass('has-error'); - form.find('.help-block').remove(); - form.append('' + - data.responseJSON.name + - ''); - }, - success: function(data) { - var form = $('#new-column-form'); - form.find('.help-block').remove(); - if (form.hasClass('has-error')) { - form.removeClass('has-error'); - } - $('#new-column-name').val(''); - form.append('' + - I18n.t('samples.js.column_added') + - ''); - $('#samples').data('num-columns', - $('#samples').data('num-columns') + 1); - originalHeader.append( - '' + - data.name + ''); - var colOrder = table.colReorder.order(); - colOrder.push(colOrder.length); - table.colReorder.reset(); - // Remove all event handlers as we re-initialize them later with - // new table - $('#samples').off(); - $('#samples thead').empty(); - $('#samples thead').append(originalHeader); - // Preserve save/delete buttons as we need them after new table - // will be created - $('div.toolbarButtons').appendTo('div.samples-table'); - $('div.toolbarButtons').hide(); - table = dataTableInit(); - table.colReorder.order(colOrder, true); - loadColumnsNames(); - }, - url: url - }); - } else { - var form = $('#new-column-form'); - form.addClass('has-error'); - form.find('.help-block').remove(); - form.append('' + - I18n.t('samples.js.empty_column_name') + - ''); + createNewColumn(); + }); + + $('#new-column-name').keydown(function(e) { + if (e.keyCode === 13) { + e.preventDefault(); + createNewColumn(); } }); }