From aea679ec11edbdc86b360e29c9a1c26c63260eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Thu, 8 Dec 2016 15:02:49 +0100 Subject: [PATCH] Added links for creating new sample type and group when creating new sample. [fixes SCI-701] --- .../javascripts/samples/sample_datatable.js | 19 +++++++++++++---- app/assets/javascripts/sitewide/utils.js | 21 +++++++++++++++++++ config/locales/en.yml | 2 ++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index fe7725be7..5a50c8a4f 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -664,7 +664,7 @@ function onClickAddSample() { else if ($(th).attr("id") == "sample-type") { var colIndex = getColumnIndex("#sample-type") if (colIndex) { - var selectType = createSampleTypeSelect(data["sample_types"], -1); + var selectType = createSampleTypeSelect(data["sample_types"]); var td = createTdElement(""); td.appendChild(selectType[0]); tr.appendChild(td); @@ -673,7 +673,7 @@ function onClickAddSample() { else if ($(th).attr("id") == "sample-group") { var colIndex = getColumnIndex("#sample-group") if (colIndex) { - var selectGroup = createSampleGroupSelect(data["sample_groups"], -1); + var selectGroup = createSampleGroupSelect(data["sample_groups"]); var td = createTdElement(""); td.appendChild(selectGroup[0]); tr.appendChild(td); @@ -740,9 +740,15 @@ function createTdElement(content) { * @param selected Selected sample type id */ function createSampleTypeSelect(data, selected) { + selected = _.isUndefined(selected) ? 1 : selected + 1; + var $selectType = $('') .attr('name', 'sample_type_id').addClass('show-tick'); + var $option = $("") + .attr('value', -2) + .text(I18n.t('samples.table.add_sample_type')); + $selectType.append($option); var $option = $('') .attr('value', -1).text(I18n.t('samples.table.no_type')) $selectType.append($option); @@ -752,7 +758,7 @@ function createSampleTypeSelect(data, selected) { .attr('value', val.id).text(val.name); $selectType.append($option); }); - $selectType.val(selected); + $selectType.makeDropdownOptionsLinks(selected); return $selectType; } @@ -762,9 +768,14 @@ function createSampleTypeSelect(data, selected) { * @param selected Selected sample group id */ function createSampleGroupSelect(data, selected) { + selected = _.isUndefined(selected) ? 1 : selected + 1; + var $selectGroup = $('') .attr('name', 'sample_group_id').addClass('show-tick'); + var $option = $("") + .text(I18n.t('samples.table.add_sample_group')); + $selectGroup.append($option); var $span = $("").addClass('glyphicon glyphicon-asterisk'); var $option = $('') .attr('value', -1).text(I18n.t('samples.table.no_group')) @@ -780,7 +791,7 @@ function createSampleGroupSelect(data, selected) { $selectGroup.append($option); }); - $selectGroup.val(selected); + $selectGroup.makeDropdownOptionsLinks(selected); return $selectGroup; } diff --git a/app/assets/javascripts/sitewide/utils.js b/app/assets/javascripts/sitewide/utils.js index f42f515c2..621762258 100644 --- a/app/assets/javascripts/sitewide/utils.js +++ b/app/assets/javascripts/sitewide/utils.js @@ -215,3 +215,24 @@ function initPageTutorialSteps(pageFirstStepN, pageLastStepN, nextPagePath, }); } } + +/** + * Add redirection links on dropdown elements. + * @param {[type]} selectedIdx Index of element to be selected. + * @return {Object} This + */ +$.fn.makeDropdownOptionsLinks = function(selectedIdx) { + selectedIdx = _.isUndefined(selectedIdx) ? 1 : selectedIdx; + $(this).change(function() { + location = $(this).find('option:selected').attr('href'); + }); + $(this).find('option') + .each(function(i) { + if (!$(this).is("[href]")) { + $(this).attr('href', '#'); + } + }) + .eq(selectedIdx).attr('selected', true); + + return this; +} diff --git a/config/locales/en.yml b/config/locales/en.yml index 7289a9ae4..4186c603d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -847,6 +847,8 @@ en: added_by: "Added by" no_group: "No sample group" no_type: "No sample type" + add_sample_type: "Add new sample type" + add_sample_group: "Add new sample group" new: head_title: "%{organization} | Add new sample" title: "Add new sample to team %{organization}"