diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 31ba3f9..052fbc2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -23,169 +23,170 @@ $( document ).ready(function() { - $(function(){ - $('.dropdown-toggle').dropdown() - }); + $(function(){ + $('.dropdown-toggle').dropdown() + }); - $('.accordion-body').on('show', function() { - var plus = $(this).parent().children(".accordion-heading").children(".accordion-toggle").children(".icon-plus").removeClass("icon-plus").addClass("icon-minus"); - }).on('hide', function(){ - var minus = $(this).parent().children(".accordion-heading").children(".accordion-toggle").children(".icon-minus").removeClass("icon-minus").addClass("icon-plus"); - }); + $('.accordion-body').on('show', function() { + var plus = $(this).parent().children(".accordion-heading").children(".accordion-toggle").children(".icon-plus").removeClass("icon-plus").addClass("icon-minus"); + }).on('hide', function(){ + var minus = $(this).parent().children(".accordion-heading").children(".accordion-toggle").children(".icon-minus").removeClass("icon-minus").addClass("icon-plus"); + }); - //accordion home page - $('.accordion-home').on('show', function() { - var plus = $(this).parent().find(".plus-laranja").removeClass("plus-laranja").addClass("minus-laranja"); - }).on('hide', function(){ - var minus = $(this).parent().find(".minus-laranja").removeClass("minus-laranja").addClass("plus-laranja"); - }); + //accordion home page + $('.accordion-home').on('show', function() { + var plus = $(this).parent().find(".plus-laranja").removeClass("plus-laranja").addClass("minus-laranja"); + }).on('hide', function(){ + var minus = $(this).parent().find(".minus-laranja").removeClass("minus-laranja").addClass("plus-laranja"); + }); - //accordion project details page when project has more than 1 plan - $('.accordion-project').on('show', function() { - var plus = $(this).parent().find(".plus-laranja").removeClass("plus-laranja").addClass("minus-laranja"); - }).on('hide', function(){ - var minus = $(this).parent().find(".minus-laranja").removeClass("minus-laranja").addClass("plus-laranja"); - }); + //accordion project details page when project has more than 1 plan + $('.accordion-project').on('show', function() { + var plus = $(this).parent().find(".plus-laranja").removeClass("plus-laranja").addClass("minus-laranja"); + }).on('hide', function(){ + var minus = $(this).parent().find(".minus-laranja").removeClass("minus-laranja").addClass("plus-laranja"); + }); - //$('#3-or-4-splash').modal(); + //$('#3-or-4-splash').modal(); - $('.typeahead').select2({ - width: "element", - allowClear: true - }); + $('.typeahead').select2({ + width: "element", + allowClear: true + }); - $(".help").popover(); + $(".help").popover(); - $('.has-tooltip').tooltip({ + $('.has-tooltip').tooltip({ placement: "right", trigger: "focus" - }); + }); - $(".show-edit-toggle").click(function (e) { - e.preventDefault(); - - $(".edit-project").toggle(); - $(".view-project").toggle(); - }); + $(".show-edit-toggle").click(function (e) { + e.preventDefault(); + + $(".edit-project").toggle(); + $(".view-project").toggle(); + }); - $(".toggle-existing-user-access").change(function(){ - $(this).closest("form").submit(); - }); + $(".toggle-existing-user-access").change(function(){ + $(this).closest("form").submit(); + }); - $("#user_email.text_field.reg-input").blur(function () { - if (validateEmail($(this).val())) { - $(this).parent().children("div").hide(); - } - else { - $(this).parent().children("div").show(); - } - }); + $("#user_email.text_field.reg-input").blur(function () { + if (validateEmail($(this).val())) { + $(this).parent().children("div").hide(); + } + else { + $(this).parent().children("div").show(); + } + }); - $("#user_password.text_field.reg-input").blur(function () { - if ($(this).val().length >= 8) { - $(this).parent().children("div").hide(); - } - else { - $(this).parent().children("div").show(); - } - }); + $("#user_password.text_field.reg-input").blur(function () { + if ($(this).val().length >= 8) { + $(this).parent().children("div").hide(); + } + else { + $(this).parent().children("div").show(); + } + }); - $("#user_password_confirmation.text_field.reg-input").blur(function () { - if ($(this).val() == $("#user_password.text_field.reg-input").val()) { - $(this).parent().children("div").hide(); - } - else { - $(this).parent().children("div").show(); - } - }); + $("#user_password_confirmation.text_field.reg-input").blur(function () { + if ($(this).val() == $("#user_password.text_field.reg-input").val()) { + $(this).parent().children("div").hide(); + } + else { + $(this).parent().children("div").show(); + } + }); - $('#user_organisation_id').on("change", function(e) { - e.preventDefault(); - var selected_org = $(this).select2("val"); - var other_orgs = $("#other-organisation-name").attr("data-orgs").split(","); - var index = $.inArray(selected_org, other_orgs); - if (index > -1) { - $("#other-organisation-name").show(); - $("#user_other_organisation").focus(); - } - else { - $("#other-organisation-name").hide(); - } - }); + $('#user_organisation_id').on("change", function(e) { + e.preventDefault(); + var selected_org = $(this).select2("val"); + var other_orgs = $("#other-organisation-name").attr("data-orgs").split(","); + var index = $.inArray(selected_org, other_orgs); + if (index > -1) { + $("#other-organisation-name").show(); + $("#user_other_organisation").focus(); + } + else { + $("#other-organisation-name").hide(); + } + }); - $("#other-org-link > a").click(function(e){ - e.preventDefault(); - var other_org = $("#other-organisation-name").attr("data-orgs").split(","); - $("#user_organisation_id").select2("val", other_org); - $("#other-org-link").hide(); - $("#user_organisation_id").change(); - }); + $("#other-org-link > a").click(function(e){ + e.preventDefault(); + var other_org = $("#other-organisation-name").attr("data-orgs").split(","); + $("#user_organisation_id").select2("val", other_org); + $("#other-org-link").hide(); + $("#user_organisation_id").change(); + }); //alert dialog for unlink Shibbileth account - $("#unlink-institutional-credentials-dialog").on("show", function(){ - $('.select2-choice').hide(); - }); + $("#unlink-institutional-credentials-dialog").on("show", function(){ + $('.select2-choice').hide(); + }); - $("#unlink-shibboleth-cancelled").click(function (){ - $("#unlink-institutional-credentials-dialog").modal("hide"); - $('.select2-choice').show(); - }); + $("#unlink-shibboleth-cancelled").click(function (){ + $("#unlink-institutional-credentials-dialog").modal("hide"); + $('.select2-choice').show(); + }); - $("#unlink-shibboleth-confirmed").click(function (){ + $("#unlink-shibboleth-confirmed").click(function (){ $("#unlink_flag").val('true'); - $("#edit_user").submit(); - - }); + $("#edit_user").submit(); + + }); - //Question Options - // ------------------------------------------------------------------------------------ - $(".options_table").on("click", ".remove-option", function(e){ - e.preventDefault(); - - // Mark the option for removal - $($(this).siblings()[0]).val(true); - - // Hide the entire table row and the associated hidden field for the item - $(this).parent().parent().addClass('hidden'); - }); - - $(".add-option").click(function(e){ - e.preventDefault(); + //Question Options + // --------------------------------------------------------------------------- + $(".options_table").on("click", ".remove-option", function(e){ + e.preventDefault(); + + // Mark the option for removal + $($(this).siblings()[0]).val(true); + + // Hide the entire table row and the associated hidden field for the item + $(this).parent().parent().addClass('hidden'); + }); + + $(".add-option").click(function(e){ + e.preventDefault(); - var tbl = $(this).parent().find("table.options_table > tbody.options_tbody"), - last = tbl.find("tr:last"), - clone = last.clone(); - nbr = parseInt(last.find(".number_field").val()); - - // Update the input field names and ids - clone.find("input").each(function(index){ - $(this).prop("id", $(this).prop("id").replace(/_\d+_/g, "_" + nbr + "_")); - $(this).prop("name", $(this).prop("name").replace(/\[\d+\]/g, "[" + nbr + "]")); - }); - - // Remove the hidden class and make sure the new row is not marked for removal - clone.removeClass('hidden'); - clone.find("[id$=" + nbr + "__destroy]").val(false); - - // Default the other values - clone.find("[id$=" + nbr + "_number]").val("" + (nbr + 1)); - clone.find("[id$=" + nbr + "_text]").val(""); - clone.find("[id$=" + nbr + "_is_default]").prop("checked", false); - - last.after(clone); - }); + var tbl = $(this).parent().find("table.options_table > tbody.options_tbody"), + last = tbl.find("tr:last"), + clone = last.clone(); + nbr = parseInt(last.find(".number_field").val()); + + // Update the input field names and ids + clone.find("input").each(function(index){ + $(this).prop("id", $(this).prop("id").replace(/_\d+_/g, "_" + nbr + "_")); + $(this).prop("name", $(this).prop("name").replace(/\[\d+\]/g, "[" + nbr + "]")); + }); + + // Remove the hidden class and make sure the new row is not marked for removal + clone.removeClass('hidden'); + clone.find("[id$=" + nbr + "__destroy]").val(false); + + // Default the other values + clone.find("[id$=" + nbr + "_number]").val("" + (nbr + 1)); + clone.find("[id$=" + nbr + "_text]").val(""); + clone.find("[id$=" + nbr + "_is_default]").prop("checked", false); + + last.after(clone); + }); - /*$('#continue-to-new').click(function(e){ - var destination = $(this).attr("href"); - var n = destination.lastIndexOf('='); - destination = decodeURIComponent(destination.substring(n + 1)); - $.post('splash_logs', {destination: destination} ); - $("#3-or-4-splash").modal('hide'); - return false; - });*/ + /*$('#continue-to-new').click(function(e){ + var destination = $(this).attr("href"); + var n = destination.lastIndexOf('='); + destination = decodeURIComponent(destination.substring(n + 1)); + $.post('splash_logs', {destination: destination} ); + $("#3-or-4-splash").modal('hide'); + return false; + });*/ }); +// --------------------------------------------------------------------------- function validateEmail(sEmail) { var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/; if (filter.test(sEmail)) { @@ -196,55 +197,24 @@ } } -/* --------------------------------------------------------------------------- */ +// --------------------------------------------------------------------------- function selectItemsFromJsonArray(array, selector, array_of_values, callback){ - var out = []; - - if(!Array.isArray(array_of_values)){ - array_of_values = [array_of_values]; - } + var out = []; + + if(!Array.isArray(array_of_values)){ + array_of_values = [array_of_values]; + } - for(var i = 0; i < array.length; i++){ - if(array_of_values.indexOf('' + array[i][selector]) >= 0){ - out.push(array[i]); - } - } - - var selectItemsFromJsonArrayInterval = setInterval(function(){ - if(i >= array.length){ - clearInterval(selectItemsFromJsonArrayInterval); - callback(out); - } - }, 50); -} - -/* --------------------------------------------------------------------------- */ -function selectItemFromJsonArray(array, selector, value){ - var out = null; - - for(var i = 0; i < array.length; i++){ - -console.log(i + ') ' + array[i][selector]); - - if(Array.isArray(array[i][selector])){ - -console.log(selector + ' == ' + value + ' found? ' + (array[i][selector].indexOf(value) >= 0)); - - if(array[i][selector].indexOf(value) >= 0){ - out = array[i][selector]; - } - - }else{ - if(array[i][selector].toString() == value.toString()){ - out = array[i][selector]; - } - } - } - - var selectItemFromJsonArrayInterval = setInterval(function(){ - if(i >= array.length || out != null){ - clearInterval(selectItemFromJsonArrayInterval); - callback(out); - } - }, 50); + for(var i = 0; i < array.length; i++){ + if(array_of_values.indexOf('' + array[i][selector]) >= 0){ + out.push(array[i]); + } + } + + var selectItemsFromJsonArrayInterval = setInterval(function(){ + if(i >= array.length){ + clearInterval(selectItemsFromJsonArrayInterval); + callback(out); + } + }, 50); } \ No newline at end of file diff --git a/app/assets/javascripts/projects.js b/app/assets/javascripts/projects.js index 332743c..b2db6ea 100644 --- a/app/assets/javascripts/projects.js +++ b/app/assets/javascripts/projects.js @@ -1,36 +1,36 @@ $( document ).ready(function() { - $(".select2-container").select2(); + $(".select2-container").select2(); - // ---------------------------------------------------------- - $("#project_funder_id").change(function(){ - reloadTemplateData(); - + // ---------------------------------------------------------- + $("#project_funder_id").change(function(){ + reloadTemplateData(); + if($(this).val().length > 0){ $("#other-funder-name").hide(); $("#project_funder_name").val(""); - + }else{ $("#other-funder-name").show(); } - + $("#institution-control-group").show(); $("#create-plan-button").show(); $("#confirm-funder").text($(this).val()); - }); - - // ---------------------------------------------------------- - $("#project_institution_id").change(function(){ - reloadTemplateData(); - - $("#confirm-institution").text($("#project_institution_id").select2('data').text); - }); - - // ---------------------------------------------------------- - $("#project_dmptemplate_id").change(function(){ - reloadGuidanceOptions(); - }); - - // ---------------------------------------------------------- + }); + + // ---------------------------------------------------------- + $("#project_institution_id").change(function(){ + reloadTemplateData(); + + $("#confirm-institution").text($("#project_institution_id").select2('data').text); + }); + + // ---------------------------------------------------------- + $("#project_dmptemplate_id").change(function(){ + reloadGuidanceOptions(); + }); + + // ---------------------------------------------------------- $("#no-funder").click(function(e) { e.preventDefault(); $("#project_funder_id").select2("val", ""); @@ -40,18 +40,18 @@ $("#confirm-funder").text(I18n.t("helpers.none")); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#project_funder_name").change(function(){ $("#confirm-funder").text($(this).val()); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#no-institution").click(function() { $("#project_institution_id").select2("val", ""); $("#confirm-institution").text(I18n.t("helpers.none")); }); - - // ---------------------------------------------------------- + + // ---------------------------------------------------------- $("#project-confirmation-dialog").on("show", function(){ if ($("#confirm-institution").text() == "") { $("#confirm-institution").text(I18n.t("helpers.none")); @@ -72,117 +72,118 @@ $('.select2-choice').hide(); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#new-project-cancelled").click(function (){ $("#project-confirmation-dialog").modal("hide"); $('.select2-choice').show(); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#new-project-confirmed").click(function (){ $("#new_project").submit(); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- //for the default template alert $("#default-template-confirmation-dialog").on("show", function(){ $('.select2-choice').hide(); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#default-template-cancelled").click(function (){ $("#default-template-confirmation-dialog").modal("hide"); $('.select2-choice').show(); }); - // ---------------------------------------------------------- + // ---------------------------------------------------------- $("#default-template-confirmed").click(function (){ $("#default_tag").val('true'); $("#new_project").submit(); }); - - // ---------------------------------------------------------- - function reloadTemplateData(){ - var orgs = [$("#project_funder_id").val(), - $("#project_institution_id").val()]; - - var template = $("#project_dmptemplate_id :selected").val(); - -console.log('reloading templates'); - - selectItemsFromJsonArray(templates, 'organisation', orgs, function(array){ - // Clear and reload the contents of the dropdown - $("#project_dmptemplate_id").html("").select2( {data: array} ).val(); + + + // The following function references a JSON array that is + // constructed in app/view/projects/_dropdown_new_project.html.erb + // ---------------------------------------------------------- + function reloadTemplateData(){ + var orgs = [$("#project_funder_id").val(), + $("#project_institution_id").val()]; + + var template = $("#project_dmptemplate_id :selected").val(); + + selectItemsFromJsonArray(templates, 'organisation', orgs, function(array){ + // Clear and reload the contents of the dropdown + $("#project_dmptemplate_id").html("").select2( {data: array} ).val(); - // If there are no templates, hide the dropdown - if(array.length <= 0){ - $("#template-control-group").hide(); - reloadGuidanceOptions(); - - }else{ - // Select the first item in the list if there was none selected - if(template == undefined){ - $("#project_dmptemplate_id").val(array[0]['id']).trigger('change'); - - }else{ - reloadGuidanceOptions(); - } - - $("#template-control-group").show(); + // If there are no templates, hide the dropdown + if(array.length <= 0){ + $("#template-control-group").hide(); + reloadGuidanceOptions(); + + }else{ + // Select the first item in the list if there was none selected + if(template == undefined){ + $("#project_dmptemplate_id").val(array[0]['id']).trigger('change'); + + }else{ + reloadGuidanceOptions(); + } + + $("#template-control-group").show(); - // if there is only one template disable the dropdown - if(array.length > 1){ - $("#project_dmptemplate_id").prop('disabled', false); - }else{ - $("#project_dmptemplate_id").prop('disabled', true); - } - } - }); - } + // if there is only one template disable the dropdown + if(array.length > 1){ + $("#project_dmptemplate_id").prop('disabled', false); + }else{ + $("#project_dmptemplate_id").prop('disabled', true); + } + } + }); + } - // ---------------------------------------------------------- + // The following function references a JSON array that is + // constructed in app/view/projects/_dropdown_new_project.html.erb + // ---------------------------------------------------------- function reloadGuidanceOptions() { var institution = $("#project_institution_id").select2('val'); var template = $("#project_dmptemplate_id :selected").val(); - var options = null; + var options = null; -console.log('reloading guidance'); - - if(!template){ - template = $("#project_dmptemplate_id :selected").children().first().val(); - } - + if(!template){ + template = $("#project_dmptemplate_id :selected").children().first().val(); + } + options_container = $("#guidance-control-group"); options_container = options_container.find(".choices-group"); options_container.empty(); - var orgs = [$("#project_funder_id").val(), - $("#project_institution_id").val()]; + var orgs = [$("#project_funder_id").val(), + $("#project_institution_id").val()]; - // select all of the guidance groups available to the funder and/or institution - selectItemsFromJsonArray(guidance_for_template_or_organisation, 'organisation', - institution, function(array){ - array = guidance_always_available.concat(array); - - for(var i = 0; i < array.length; i++){ - var selected = false + // select all of the guidance groups available to the funder and/or institution + selectItemsFromJsonArray(guidance_for_template_or_organisation, 'organisation', + institution, function(array){ + array = guidance_always_available.concat(array); + + for(var i = 0; i < array.length; i++){ + var selected = false - options_container.append( - "