Newer
Older
dmpopidor / lib / assets / javascripts / application.js
@briley briley on 9 Jun 2017 6 KB resolved cherry-pick conflicts
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require v1.js
//= require select2.min.js
//= require jquery.placeholder.js
//= require tinymce-jquery
//= require i18n
//= require i18n/translations
//= require_tree ./locale
//= require gettext/all
//= require jquery-accessible-autocomplet-list-aria.js
//= require roadmap.js

$( document ).ready(function() {

  $(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 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().children(".accordion-heading").find(".plus-laranja").removeClass("plus-laranja").addClass("minus-laranja");
  }).on('hide', function(){
    var minus = $(this).parent().children(".accordion-heading").find(".minus-laranja").removeClass("minus-laranja").addClass("plus-laranja");
  });

	$('.export-format-selection').click(function(e){
		e.preventDefault();
		if($(this).val() == 'pdf'){
			$('#pdf-format-options').show();
		}else{
			$('#pdf-format-options').hide();
		}
	});

  //$('#3-or-4-splash').modal();

  $('.typeahead').select2({
    width: "element",
    allowClear: true
  });

  $(".help").popover();

  $('.has-tooltip').tooltip({
        placement: "right",
        trigger: "focus"
  });

  $(".show-edit-toggle").click(function (e) {
    e.preventDefault();
    
    $(".edit-plan-details").toggle();
    $(".show-plan-details").toggle();
  });

  $(".toggle-existing-user-access").change(function(){
    $(this).closest("form").submit();
  });

  $('#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();
  });
    
    //alert dialog for unlink Shibbileth account 
     $("#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-confirmed").click(function (){
        $("#unlink_flag").val('true');
    $("#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();

    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;
  });*/

});

// ---------------------------------------------------------------------------
function selectItemsFromJsonArray(array, selector, array_of_values, callback){
  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);
}