$('td.editable').on("keyup", function(e){
  if(e.which == 13) {
    var caller = $(e.delegateTarget);
    var targetModal = $(caller.data("target"));
    if (caller.is(":focus")){
      openEditModal (caller, targetModal);
    }
  }
});

$('td.editable').on( "click", function(e) {
  var caller = $(e.delegateTarget);
  var targetModal = $(caller.data("target"));
  openEditModal (caller, targetModal)
});


//open and position modal
function openEditModal (caller, targetModal) {
  var callerPosition = caller.offset();
  var targetInput = targetModal.find('.form-control').eq('0');
  var targetSubmit = targetModal.find('.btn-submit').eq('0');

  // show and position modal
  targetModal.modal('show');
  targetModal.find('.modal-dialog').css({top: callerPosition.top, left: callerPosition.left});

  // focus on first input
  targetInput.focus();


  if (targetModal.is(".modal-text")){
      //jump to next modal on return
      console.log("return");
      targetModal.on("keyup", function(e) {
        if(e.which == 13) {
            targetModal.modal('hide');
            caller.next().trigger( "click" );
            event.stopPropagation();
        }
      });
  }else {
    //on hide get back focus to modal caller
    targetModal.one('hide.bs.modal', function (e) {
      setTimeout(function(){  caller.focus(); }, 100);
    });

  }
  if (targetModal.is(".modal-date")){

    // get focus to submit
    targetModal.on("keyup", function(e) {

        if(e.which == 13) {
          $("#input-modal-edit-date").val("13 septembre 2002");
          targetSubmit.focus();
        }
         //event.stopPropagation();
      })
  }
}

// INIT modals
// simple text modal
$('#modal-edit-text', '#modal-edit-text-modified').on('shown.bs.modal', function (e) {

  // place cursor at the end of the string
  let modalInput = $(this).find('.form-control').eq('0');
  let temp = modalInput.val();
  modalInput.val('');
  modalInput.val(temp);
});


$('.modal-date').on('shown.bs.modal', function (e) {


});

// select
$('#modal-edit-select, #modal-edit-select-multiple').on('shown.bs.modal', function (e) {
  setTimeout(function(){$('.chosen-select').trigger("chosen:open"); }, 100);
  $('.chosen-search input').focus();
});

// synchro chosen -> modal header
$('#modal-edit-select').on('change', function(e, params) {
    $('#input-modal-edit-text-chosen').val(params.selected);
});


// targetModal.one("keyup", function(e) {
//   if(e.which == 13) {
//       targetModal.modal('hide');
//       targetModal.off("keyup");
//       caller.next().trigger( "click" );
//       // setTimeout(function(){
//       //   caller.next().trigger( "click" );
//       // }, 100);
//   }
//    event.stopPropagation();
// });


// slider config
var slider = document.getElementById('editSlider');
var sliderFeedback = document.getElementById('input-modal-edit-range-text');

noUiSlider.create(slider, {
 start: [0, 1],
 connect: true,
 behaviour: 'tap',
 step: 1,
 range: {
   'min': 0,
   'max': 10
   },
  pips: {
    mode: 'steps',
    density: 10,
    },
  format: {
    to: function ( value ) {
     return value;
    },
   from: function ( value ) {
     return value;
   }
  }
});
$('sliderFeedback').val("De 0 à 1");
slider.noUiSlider.on('update', function( values, handle ) {
 sliderFeedback.value = 'De ' + values[0] +' à ' + values[1];
});
