Files
edx-platform/lms/static/js/dashboard/dropdown.js
Syed Ali Abbas Zaidi 8480dbc228 chore: apply amnesty on existing not fixable issues (#32215)
* fix: eslint operator-linebreak issue

* fix: eslint quotes issue

* fix: react jsx indent and props issues

* fix: eslint trailing spaces issues

* fix: eslint line around directives issue

* fix: eslint semi rule

* fix: eslint newline per chain rule

* fix: eslint space infix ops rule

* fix: eslint space-in-parens issue

* fix: eslint space before function paren issue

* fix: eslint space before blocks issue

* fix: eslint arrow body style issue

* fix: eslint dot-location issue

* fix: eslint quotes issue

* fix: eslint quote props issue

* fix: eslint operator assignment issue

* fix: eslint new line after import issue

* fix: indent issues

* fix: operator assignment issue

* fix: all autofixable eslint issues

* fix: all react related fixable issues

* fix: autofixable eslint issues

* chore: remove all template literals

* fix: remaining autofixable issues

* chore: apply amnesty on all existing issues

* fix: failing xss-lint issues

* refactor: apply amnesty on remaining issues

* refactor: apply amnesty on new issues

* fix: remove file level suppressions

* refactor: apply amnesty on new issues
2023-08-07 19:13:19 +05:00

94 lines
3.6 KiB
JavaScript

var edx = edx || {};
(function($) {
'use strict';
edx.dashboard = edx.dashboard || {};
edx.dashboard.dropdown = {};
// Generate the properties object to be passed along with business intelligence events.
edx.dashboard.dropdown.toggleCourseActionsDropdownMenu = function(event) {
var $target = $(event.currentTarget),
dashboardIndex = $target.data().dashboardIndex,
$dropdown = $($target.data('dropdownSelector') || '#actions-dropdown-' + dashboardIndex),
$dropdownButton = $($target.data('dropdownButtonSelector') || '#actions-dropdown-link-' + dashboardIndex),
ariaExpandedState = ($dropdownButton.attr('aria-expanded') === 'true'),
menuItems = $dropdown.find('a');
// eslint-disable-next-line no-shadow
var catchKeyPress = function(object, event) {
// get currently focused item
var $focusedItem = $(':focus');
// get the index of the currently focused item
var focusedItemIndex = menuItems.index($focusedItem);
// var to store next focused item index
var itemToFocusIndex;
// if space or escape key pressed
if (event.which === 32 || event.which === 27) {
$dropdownButton.click();
event.preventDefault();
// eslint-disable-next-line brace-style
}
// if up arrow key pressed or shift+tab
else if (event.which === 38 || (event.which === 9 && event.shiftKey)) {
// if first item go to last
if (focusedItemIndex === 0 || focusedItemIndex === -1) {
menuItems.last().focus();
} else {
itemToFocusIndex = focusedItemIndex - 1;
menuItems.get(itemToFocusIndex).focus();
}
event.preventDefault();
// eslint-disable-next-line brace-style
}
// if down arrow key pressed or tab key
else if (event.which === 40 || event.which === 9) {
// if last item go to first
if (focusedItemIndex === menuItems.length - 1 || focusedItemIndex === -1) {
menuItems.first().focus();
} else {
itemToFocusIndex = focusedItemIndex + 1;
menuItems.get(itemToFocusIndex).focus();
}
event.preventDefault();
}
};
// Toggle the visibility control for the selected element and set the focus
$dropdown.toggleClass('is-visible');
if ($dropdown.hasClass('is-visible')) {
$dropdown.attr('tabindex', -1);
$dropdown.focus();
} else {
$dropdown.removeAttr('tabindex');
$dropdownButton.focus();
}
// Inform the ARIA framework that the dropdown has been expanded
$dropdownButton.attr('aria-expanded', !ariaExpandedState);
// catch keypresses when inside dropdownMenu (we want to catch spacebar;
// escape; up arrow or shift+tab; and down arrow or tab)
$dropdown.on('keydown', function(e) {
catchKeyPress($(this), e);
});
};
edx.dashboard.dropdown.bindToggleButtons = function(selector) {
$(selector).bind(
'click',
edx.dashboard.dropdown.toggleCourseActionsDropdownMenu
);
};
$(document).ready(function() {
edx.dashboard.dropdown.bindToggleButtons('.action-more');
edx.dashboard.dropdown.bindToggleButtons('.js-entitlement-action-more');
});
}(jQuery));