Skip to content

Commit

Permalink
Update requestsjs - breakdown requestable function to smaller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
christinach committed May 3, 2023
1 parent a609105 commit 7ec5d83
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/orangelight.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//= require 'jquery'

$(document).ready(function() {
$(function() {
//link highlighting of hierarchy
$(".search-subject, .search-name-title, .search-title").hover(
function() {
Expand Down
127 changes: 85 additions & 42 deletions app/assets/javascripts/requests/requests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
$(document).ready(function() {

$(function() {
function isEmail(email) {
const regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
Expand Down Expand Up @@ -58,51 +57,104 @@ $(document).ready(function() {
$('#request-submit-button').prop('disabled', true);
}

deactivateRequestButton();

function requestable(changed) {
const parent = $(changed).closest('[id^="request_"]');
const selected = parent.find('input[type=checkbox][id^="requestable_selected"').is(':checked');
const radios = parent.find('input[type=radio][name^="requestable[][delivery_mode"]');
let delivery_mode = false;
let delivery_location = false;
//deactivateRequestButton();

if (radios.length === 0) {
delivery_mode = true;
function requestablePickups () {
// If there is only one pickup delivery location the length is 0
const requestable_pickups_options = document.querySelectorAll('select[name^="requestable[][pick_up"] option');
let pickup;

if (requestable_pickups_options.length === 0) {
pickup = true;
} else {
radios.each(function() {
if ($(this).is(':checked')) {
if (this.dataset['target'].startsWith('#fields-eed')) {
delivery_mode = true;
delivery_location = true;
} else {
delivery_mode = true;
}
// When there are more than one pickup delivery locations
for (const pickupOption of requestable_pickups_options) {
if (pickupOption.selected == true && pickupOption.value !== '') {
pickup = true;
}
});
}
}
return pickup;
}

const requestable_pickups = parent.find('select[name^="requestable[][pick_up"] option');

// If there is only one pickup delivery location the length is 0
if (requestable_pickups.length === 0) {
delivery_location = true;
} else {
// When there are more than one pickup delivery locations
requestable_pickups.each(function() {
if ($(this).is(':selected') && $(this).val() !== '') {
delivery_location = true;
function radioOfRadioBtns () {
const radios = document.querySelectorAll('input[type=radio][name^="requestable[][delivery_mode"]');
const radio_checked = document.querySelectorAll('input[type=radio][name^="requestable[][delivery_mode"]:checked');

let mode = false;
if (radios && radio_checked.length > 0) {
for (const radio of radio_checked) {
if (radio && radio.dataset['target'].startsWith('#fields-eed')) {
mode = true;
// when it's an edd it should have delivery location true;
} else {
mode = true;
}
});
}
}
return mode;
}

function deliveryModeRadioButtons () {
let mode;
const radios = document.querySelectorAll('input[type=radio][name^="requestable[][delivery_mode"]');

if (radios.length === 0) {
// deactivateRequestButton();
mode = requestablePickups();
} else {
mode = radioOfRadioBtns();
}
return mode;
}

function deliveryMode () {
const radioButtons = deliveryModeRadioButtons();
return radioButtons;
}

if (selected && delivery_mode && delivery_location) {
function deliveryLocation () {
const pickupOptions = requestablePickups();
return pickupOptions;
}

function requestable() {
let selected;
const checkboxes = document.querySelectorAll('input[type=checkbox][id^="requestable_selected"]:checked');
const radio_checked = document.querySelectorAll('input[type=radio][name^="requestable[][delivery_mode"]:checked');

if (checkboxes.length > 0) {
selected = true;
} else {
selected = false;
}
const deMode = deliveryMode();
let deLocation = deliveryLocation();
// special case for edd form. Needs to set delivery location.
if (radio_checked.length === 1 && radio_checked[0].dataset['target'].startsWith('#fields-eed')) {
deLocation = true;
}

if (selected && deMode && deLocation) {
activateRequestButton();
} else {
deactivateRequestButton();
}
}

(function () {
const checkbox_nodelist = document.querySelectorAll('[id^="requestable_selected"]');
// Decision made: If there is only one item
// checkbox is selected
if (checkbox_nodelist.length === 1) {
checkbox_nodelist[0].checked = true;
requestable();
} else {
requestable();
}
})();


// Enhance the Bootstrap collapse utility to toggle hide/show for other options
$('input[type=radio][name^="requestable[][delivery_mode"]').on('change', function() {
// collapse others
Expand All @@ -116,14 +168,6 @@ $(document).ready(function() {
requestable(this);
});

$('input[type=checkbox][id^="requestable_selected"').on('change', function() {
if ($(this).val()) {
activateRequestButton();
} else {
deactivateRequestButton();
}
});

$('input[type=text][id^="requestable__edd_art_title_"').on('input', function() {
if ($(this).val() === "") {
$('#request-submit-button').prop('disabled', true);
Expand All @@ -149,5 +193,4 @@ $(document).ready(function() {
$(this).closest('tr').toggleClass('selected', $(this).is(':checked'));
requestable(this);
});

});

0 comments on commit 7ec5d83

Please sign in to comment.