I am a student learning to code and have just started to learn javascript/jquery. I have created a RoR chess game and am currently working on the promotion move. Basically, once a Pawn piece has reached the opposite end of the board, a modal should open and give the player a selection of pieces to choose from to replace the pawn with. On the front end I have this modal that should automatically be triggered when a pawn meets the necessary conditions. I created a function is_pawn_promotion that should do so, but the modal still does not open on it's own, and now I am unsure what to do next. I am wondering if the is_pawn_promotion function is not being called properly (obviously it is not). I have tried to reorganize the location of the openModal function but to no avail. Any help would be greatly appreciated and hopefully I have provided a clear enough picture of what is going on.
Here is the JS file containing the is_pawn_promotion. I have included the whole file which contains the openModal function I am trying to call in order to open the modal.
$( function() {
$( ".piece" ).draggable({
snap: ".piece-square",
grid: [60, 60],
containment: ".game-board",
});
$( ".piece-square" ).droppable({
accept: ".piece",
drop: function( event, ui ) {
var x = $(event.target).data('x');
var y = $(event.target).data('y');
var urlUpdatePath = $('.ui-draggable-dragging').data('url');
var is_pawn_promotion = function() {
return $(".piece") === 'Pawn' &&
(y === 0 || y === 7);
};
var sendAJAXRequest = function(x, y, type) {
$.ajax({
type: 'PUT',
url: urlUpdatePath,
data: { piece: { x_position: x, y_position: y, piece_type: type} },
success: function(response) {
if(response == 'OK') {
console.log(response);
} else {
alert(response);
}
}
});
};
if (is_pawn_promotion()) {
openModal();
var promoSubmitButton = $(".promo-selection-submit");
promoSubmitButton.on('click', function() {
var type = $('.promo-selection.input[selected]').val();
sendAJAXRequest(x, y, type);
});
} else {
sendAJAXRequest(x, y);
}
}
});
});
var openModal = function() {
// Change modal-state checkbox to checked
$("#promo-modal").prop("checked", true);
if ($("#promo-modal").is(":checked")) {
$("body").addClass("modal-open");
} else {
$("body").removeClass("modal-open");
}
$(".modal-fade-screen, .modal-close").on("click", function() {
$(".modal-state:checked").prop("checked", false).change();
});
$(".modal-inner").on("click", function(e) {
e.stopPropagation();
});
};
the modal
<div class="modal">
<input class="modal-state" id="promo-modal" type="checkbox" />
<div class="modal-fade-screen">
<div class="modal-inner">
<div class="modal-close" for="promo-modal"></div>
<div class="promo-modal-text">
<h1>Pawn Promotion!</h1>
<h1>Make your selection: </h1>
</div>
<form action="#" class="pawn-promotion">
<div class="promo-selection-container">
<% [Queen, Knight, Bishop, Rook].each do |piece_type| %>
<div class="promo-selection">
<label>
<%= image_tag(piece_type.new(color: current_color).piece_image) %>
<%= piece_type.name %>
<input type="radio" name="promo-piece" value="<%= piece_type.name %>">
</label>
</div>
<% end %>
</div>
<br/>
<div class="promo-selection-submit">
<input type="submit">
</div>
</form>
</div>
</div>
</div>