I am currently a student immersing myself in the world of coding, particularly focusing on learning javascript/jquery. In my recent project, I have developed a chess game using RoR and now I am tackling the promotion move. The idea is that when a Pawn piece reaches the opposite end of the board, a modal should pop up, allowing the player to choose a new piece to replace the pawn. I already have a modal set up on the front end that should trigger automatically once the conditions are met. However, despite creating a function called is_pawn_promotion
to handle this, the modal fails to open on its own. I suspect that the is_pawn_promotion
function is not called correctly. I've attempted to rearrange the code for calling the openModal function, but to no avail. Any guidance or assistance would be highly appreciated, and I hope my description of the issue is clear enough.
Below is the JS file containing the is_pawn_promotion
function. The file also includes the openModal
function that I'm trying to execute 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>