From af8ab3e08105d47d109c63b500bc33f3abe56e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Mon, 12 Sep 2016 16:52:51 +0200 Subject: [PATCH] Fixed step's checklist items editing (items not multiplying and new item insertion always at bottom) [fixes SCI-427]. Improved step/result comment GUI [SCI-446]. --- app/assets/javascripts/my_modules/results.js | 7 +- app/assets/javascripts/protocols/steps.js | 99 ++++++++++++-------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/my_modules/results.js b/app/assets/javascripts/my_modules/results.js index 3bb7b013a..8853fc3ce 100644 --- a/app/assets/javascripts/my_modules/results.js +++ b/app/assets/javascripts/my_modules/results.js @@ -97,6 +97,7 @@ function initResultCommentsLink($el) { moreBtn.remove(); } else { moreBtn.attr("href", data.more_url); + moreBtn.trigger("blur"); } // Reposition dropdown comment options @@ -113,9 +114,6 @@ function initResultCommentTabAjax() { var targetId = $this.attr("aria-controls"); if (parentNode.hasClass("active")) { - // TODO move to fn - parentNode.removeClass("active"); - $("#" + targetId).removeClass("active"); return false; } }) @@ -137,9 +135,6 @@ function initResultCommentTabAjax() { }) .on("ajax:error", function(e, xhr, status, error) { // TODO - }) - .on("ajax:complete", function () { - $(this).tab("show"); }); } diff --git a/app/assets/javascripts/protocols/steps.js b/app/assets/javascripts/protocols/steps.js index 5b6ee01bc..2158edf50 100644 --- a/app/assets/javascripts/protocols/steps.js +++ b/app/assets/javascripts/protocols/steps.js @@ -422,6 +422,7 @@ function initStepCommentsLink($el) { moreBtn.remove(); } else { moreBtn.attr("href", data.more_url); + moreBtn.trigger("blur"); } // Reposition dropdown comment options @@ -439,9 +440,6 @@ function initStepCommentTabAjax() { var targetId = $this.attr("aria-controls"); if (parentNode.hasClass("active")) { - // TODO move to fn - parentNode.removeClass("active"); - $("#" + targetId).removeClass("active"); return false; } }) @@ -463,9 +461,6 @@ function initStepCommentTabAjax() { }) .on("ajax:error", function(e, xhr, status, error) { // TODO - }) - .on("ajax:complete", function () { - $(this).tab("show"); }); } @@ -495,49 +490,75 @@ function initCallBacks() { initDeleteStep(); } -function reorderCheckboxData(el) { - var itemIds = []; - var checkboxes = $(el).find(".nested_fields:not(:hidden) .form-group"); +/* + * Correction for sorting with "Sortable.min" JS library to work correctly with + * "nested_form_fields" gem. + */ +function reorderCheckboxData(checkboxUl) { + // Make sure checkbox item insertion script is always at the bottom of "ul" + // tag, otherwise item will not be inserted at bottom + if(!$(checkboxUl).children().last().is('script')) { + $(checkboxUl).find("script").appendTo(checkboxUl); + } - checkboxes.each(function () { - var itemId = $(this).find("label").attr("for").match(/(\d+)_text/)[1]; - itemIds.push(itemId); - }); - - itemIds.sort(); - - checkboxes.each(function (i) { + var $checkboxes = $(checkboxUl).find(".nested_fields"); + $checkboxes.each(function (itemPos) { var $this = $(this); - var label = $this.find(".control-label"); - var input = $this.find(".form-control"); - var posInput = $this.parent().find(".checklist-item-pos"); - var itemId = itemIds[i]; - var forAttr = label.attr("for"); - var idAttr = input.attr("id"); - var nameAttr = input.attr("name"); - var posIdAttr = posInput.attr("id"); - var posNameAttr = posInput.attr("name"); - forAttr = forAttr.replace(/\d+_text/, itemId + "_text"); - nameAttr = nameAttr.replace(/\[\d+\]\[text\]/, "[" + itemId + "][text]"); - posIdAttr = posIdAttr.replace(/\d+_position/, itemId + "_text"); - posNameAttr = posNameAttr.replace(/\[\d+\]\[position\]/, "[" + itemId + "][position]"); + var $formGroup = $this.find(".form-group"); + var $label = $formGroup.find(".control-label"); + var $textInput = $formGroup.find(".checklist-item-text"); + var $posInput = $formGroup.parent().find(".checklist-item-pos"); + var $destroyLink = $this.find(".remove_nested_fields_link"); - label.attr("for", forAttr); - input.attr("name", nameAttr); - input.attr("id", forAttr); - posInput.attr("name", posNameAttr); - posInput.attr("id", posIdAttr); - posInput.val(itemId); + var labelFor = $label.attr("for"); + var textName = $textInput.attr("name"); + var textId = $textInput.attr("id"); + var posName = $posInput.attr("name"); + var posId = $posInput.attr("id"); + var destroyLink = $destroyLink.attr("data-delete-association-field-name"); + + labelFor = labelFor.replace(/\d+_text/, itemPos + "_text"); + textName = textName.replace(/\[\d+\]\[text\]/, "[" + itemPos + "][text]"); + textId = textId.replace(/\d+_text/, itemPos + "_text"); + posName = posName.replace(/\[\d+\]\[position\]/, "[" + itemPos + "][position]"); + posId = posId.replace(/\d+_position/, itemPos + "_position"); + destroyLink = destroyLink.replace(/\[\d+\]\[_destroy\]/, "[" + itemPos + "][_destroy]"); + + $label.attr("for", labelFor); + $textInput.attr("name", textName); // Actually needed for sorting to work + $textInput.attr("id", textId); + $posInput.attr("name", posName); + $posInput.attr("id", posId); + $posInput.val(itemPos); + $destroyLink.attr("data-delete-association-field-name", destroyLink); + + var $idInput = $this.find("> input"); + if ($idInput.length) { + var idName = $idInput.attr("name"); + var idId = $idInput.attr("id"); + + idName = idName.replace(/\[\d+\]\[id\]/, "[" + itemPos + "][id]"); + idId = idId.replace(/\d+_id/, itemPos + "_id"); + + $idInput.attr("name", idName); + $idInput.attr("id", idId); + } + + if ($this.css('display') == 'none') { + // Actually needed for deleting to work + var $destroyInput = $this.prev(); + var destroyName = $destroyInput.attr("name"); + destroyName = destroyName.replace(/\[\d+\]\[_destroy\]/, "[" + itemPos + "][_destroy]"); + $destroyInput.attr("name", destroyName); + } }); } function enableCheckboxSorting(el) { Sortable.create(el, { draggable: 'fieldset', - filter: 'script', handle: '.glyphicon-chevron-right', - onUpdate: function () { reorderCheckboxData(el); } @@ -548,7 +569,7 @@ function initializeCheckboxSorting() { var el = $("#new-step-checklists a[data-association-path=step_checklists]"); el.click(function () { - // calling code below must be defered because at this step HTML in not + // calling code below must be defered because at this step HTML is not // inserted into DOM. setTimeout(function () { var list = el.parent().find("fieldset.nested_step_checklists:last ul");