SpeciesFileGroup/taxonworks

View on GitHub
app/assets/javascripts/views/loans/_form.js.erb

Summary

Maintainability
Test Coverage
/*

   Code behind adding/remove loan items on /loan/:id/edit 

 */
var TW = TW || {};
TW.views = TW.views || {};
TW.views.loans = TW.views.loans || {};
TW.views.loans.form = TW.views.loans.form || {};

Object.assign(TW.views.loans.form, { 

  base_name: 'loan[loan_items_attributes]',

  add_loan_item: function(form, gid, label) {
    var random_index = new Date().getTime(); 
    var indexed_base_name = this.base_name + '[' +  random_index + ']';
    var loan_item_list = form.find('.loan_items_list');

    var type_regex = /gid:\/\/taxon-works\/(.*)\/\d+/;
    var match = type_regex.exec(gid);
    var item_type = match[1];

    var total_field;
    if (gid.match(/Otu/)) {
      total_field = '<td><input type="text" size="4" name="' + indexed_base_name + '[total]" /></td>';
    } else {
      total_field = '<td></td>';
    }

    form.append( $('<input hidden name="' + indexed_base_name + '[global_entity]" value="' + gid + '" >'));

    loan_item_list.append( $('<tr class="contextMenuCells btn btn-neutral loan_item new_loan_item" data-loan-item-index="' + random_index + '">')
        .append('<td>' + label + '</td>')
        .append('<td><input class="datepicker_recurring_start" type="text" size="9" name="' + indexed_base_name + '[date_returned_jquery]" /></td>')
        .append('<td><select name="' + indexed_base_name + '[disposition]"><option value=""></option><%= LoanItem::STATUS.collect{|a| "<option>#{a}</option>"}.join %></select></td>') 
        .append(total_field)
        .append('<td>' + item_type + '</td>')
        .append( this.remove_link() )
        );
  },

  remove_link: function() {
    var link = $('<td><a href="#" data-turbolinks="false" class="remove_loan_item">remove</a></td>');
    this.bind_remove_links(link);
    return link;
  },

  bind_remove_links: function(links) {
    links.click(function () {
      var list_item = $(this).closest('tr');
      var form = list_item.closest('form');
      var loan_item_id = list_item.data('loan-item-gid');
      var loan_item_index = list_item.data('loan-item-index');

      if (loan_item_id != undefined) {
        var loan_item_list = list_item.closest('.loan_items_list');

        // if there is an ID from an existing item add the necessary (hidden) _destroy input
        form.append($('<input hidden name="' + this.base_name + '[' +  loan_item_index + '][global_entity]" value="' + loan_item_id + '" >') );
        form.append($('<input hidden name="' + this.base_name + '[' +  loan_item_index + '][_destroy]" value="1" >') );

        TW.views.loans.form.warn_for_save(form.find('#loan_item_selector_message'));
      }
      list_item.remove();
    });
  },

  warn_for_save: function(msg_div) {
    msg_div.addClass('warning');
    msg_div.html('Update Loan click required to confirm removal/reorder of loan item.');
  },

  make_list_sortable: function(form) {
    var list_items = form.find('.loan_items_list');
    list_items.sortable({
      change: function() {
        if ($('form[id^="new_"]').length == 0) {
          TW.views.loans.form.warn_for_save(form.find('#loan_item_selector_message')); 
        }
      }  
    });
  },

  bind_position_handling_to_submit_button: function(form) {
    form.closest('form').find('input[name="commit"]').click(function () {
      var i = 1;
      var loan_item_index;
      form.find('.loan_item').each( function() {
        console.log($(this));
        loan_item_index = $(this).data('loan-item-index');
        $(this).append(
            $('<input hidden name="' +  this.base_name + '[' +  loan_item_index + '][position]" value="' + i + '" >')
            );
        i = i + 1; 
      });
    });
  },

  bind_autocomplete_select_event: function() {
    var input = $("#loan_item_autocomplete"); 
    input.on( "autocompleteselect", function( event, ui ) {
      TW.views.loans.form.add_loan_item( $('#loan_item_selector'), ui.item.gid, ui.item.label); 
      input.val(""); 
      return false;
    } );
  },

  initialize_selector: function(form) {
    // Bind injected datepicker elements
    $('body').on('focus',".datepicker_recurring_start", function() {
      $(this).datepicker();
    });

    TW.views.loan_items.select_loan_item.initialize_select_loan_items();
    this.bind_autocomplete_select_event();
    
    this.bind_remove_links(form.find('.remove_loan_item')); 
    this.make_list_sortable(form);
    this.bind_position_handling_to_submit_button(form);
  } 
}
);


$(document).on("turbolinks:load", function() {
  if ( $('#loan_item_selector').length ) {
    TW.views.loans.form.initialize_selector($('#loan_item_selector'));
  }
})