MDL-84071 gradereport_singleview: Rewrite singleview to native JS
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
issueNumber: MDL-84071
|
||||
notes:
|
||||
gradereport_singleview:
|
||||
- message: >
|
||||
The `grade/report/singleview/js/singleview.js` file has been removed. And the
|
||||
`grade/report/singleview/amd/src/singleview.js` file has been added. The new file
|
||||
is converted from YUI to native JS.
|
||||
|
||||
type: improved
|
||||
@@ -0,0 +1,11 @@
|
||||
define("gradereport_singleview/singleview",["exports"],(function(_exports){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=function(){if(initialized)return;initialized=!0,document.addEventListener("keydown",keydownHandler,!0),document.querySelectorAll(selectors_override).forEach((input=>{input.addEventListener("change",(()=>{!function(input){const checked=input.checked,[,itemid,userid]=input.getAttribute("name").split("_"),interest="_".concat(itemid,"_").concat(userid);document.querySelectorAll(selectors_input(interest)).forEach((text=>{text.disabled=!checked})),document.querySelectorAll(selectors_select(interest)).forEach((select=>{select.disabled=!checked}))}(input)}))}))};
|
||||
/**
|
||||
* Allow navigation through table cells using Ctrl + arrow keys and handle override toggles.
|
||||
*
|
||||
* @module gradereport_singleview/singleview
|
||||
* @copyright The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
const selectors_cell="td.cell, th.cell",selectors_col="td, th",selectors_keyboardHandled="table input, table select, table a",selectors_navigableCell='input:not([type="hidden"]):not([disabled]), select, a',selectors_override="input[name^=override_]",selectors_row="tr",selectors_input=interest=>"input[name$='".concat(interest,"'][data-uielement='text']"),selectors_select=interest=>"select[name$='".concat(interest,"']");let initialized=!1;function keydownHandler(event){if(!event.ctrlKey)return;if(!["ArrowLeft","ArrowUp","ArrowRight","ArrowDown"].includes(event.key))return;const activeElement=document.activeElement;if(!activeElement.matches(selectors_keyboardHandled))return;let next=null;switch(event.key){case"ArrowLeft":next=getPrevCell(activeElement.closest(selectors_col));break;case"ArrowUp":next=getAboveCell(activeElement.closest(selectors_col));break;case"ArrowRight":next=getNextCell(activeElement.closest(selectors_col));break;case"ArrowDown":next=getBelowCell(activeElement.closest(selectors_col))}var _next$querySelector;(event.preventDefault(),event.stopImmediatePropagation(),next)&&(null===(_next$querySelector=next.querySelector(selectors_navigableCell))||void 0===_next$querySelector||_next$querySelector.focus())}function getNextCell(cell){var _checkElement$nextEle;const checkElement=cell||document.activeElement,next=null!==(_checkElement$nextEle=checkElement.nextElementSibling)&&void 0!==_checkElement$nextEle&&_checkElement$nextEle.matches(selectors_cell)?checkElement.nextElementSibling:null;return next?next.querySelector(selectors_navigableCell)?next:getNextCell(next):null}function getPrevCell(cell){var _checkElement$previou;const checkElement=cell||document.activeElement,prev=null!==(_checkElement$previou=checkElement.previousElementSibling)&&void 0!==_checkElement$previou&&_checkElement$previou.matches(selectors_cell)?checkElement.previousElementSibling:null;return prev?prev.querySelector(selectors_navigableCell)?prev:getPrevCell(prev):null}function getAboveCell(cell){const checkElement=cell||document.activeElement,tr=checkElement.closest(selectors_row).previousElementSibling,columnIndex=getColumnIndex(checkElement);if(!tr)return null;const next=tr.querySelectorAll(selectors_col)[columnIndex];return null!=next&&next.querySelector(selectors_navigableCell)?next:getAboveCell(next)}function getBelowCell(cell){const checkElement=cell||document.activeElement,tr=checkElement.closest("tr").nextElementSibling,columnIndex=getColumnIndex(checkElement);if(!tr)return null;const next=tr.querySelectorAll("td, th")[columnIndex];return null!=next&&next.querySelector(selectors_navigableCell)?next:getBelowCell(next)}function getColumnIndex(cell){const rowNode=cell.closest(selectors_row);if(!rowNode||!cell)return-1;return Array.from(rowNode.querySelectorAll(selectors_col)).indexOf(cell)}}));
|
||||
|
||||
//# sourceMappingURL=singleview.min.js.map
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,232 @@
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Allow navigation through table cells using Ctrl + arrow keys and handle override toggles.
|
||||
*
|
||||
* @module gradereport_singleview/singleview
|
||||
* @copyright The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
const selectors = {
|
||||
cell: 'td.cell, th.cell',
|
||||
col: 'td, th',
|
||||
keyboardHandled: 'table input, table select, table a',
|
||||
navigableCell: 'input:not([type="hidden"]):not([disabled]), select, a',
|
||||
override: 'input[name^=override_]',
|
||||
row: 'tr',
|
||||
// Dyanmic selectors.
|
||||
input: (interest) => `input[name$='${interest}'][data-uielement='text']`,
|
||||
select: (interest) => `select[name$='${interest}']`,
|
||||
};
|
||||
|
||||
let initialized = false;
|
||||
|
||||
/**
|
||||
* Initializes the module, setting up event listeners for table cell navigation and override toggles.
|
||||
*/
|
||||
export function init() {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
initialized = true;
|
||||
|
||||
// Add ctrl+arrow controls for navigation.
|
||||
// Use capturing phase to intercept events before they reach anchor elements.
|
||||
document.addEventListener('keydown', keydownHandler, true);
|
||||
|
||||
// Handle override toggles.
|
||||
document.querySelectorAll(selectors.override).forEach(input => {
|
||||
input.addEventListener('change', () => {
|
||||
updateOverrideToggle(input);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles control+arrow table navigation.
|
||||
*
|
||||
* @private
|
||||
* @param {KeyboardEvent} event The keydown event.
|
||||
*/
|
||||
function keydownHandler(event) {
|
||||
if (!event.ctrlKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if it's an arrow key.
|
||||
if (!['ArrowLeft', 'ArrowUp', 'ArrowRight', 'ArrowDown'].includes(event.key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeElement = document.activeElement;
|
||||
if (!activeElement.matches(selectors.keyboardHandled)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let next = null;
|
||||
switch (event.key) {
|
||||
case 'ArrowLeft':
|
||||
next = getPrevCell(activeElement.closest(selectors.col));
|
||||
break;
|
||||
case 'ArrowUp':
|
||||
next = getAboveCell(activeElement.closest(selectors.col));
|
||||
break;
|
||||
case 'ArrowRight':
|
||||
next = getNextCell(activeElement.closest(selectors.col));
|
||||
break;
|
||||
case 'ArrowDown':
|
||||
next = getBelowCell(activeElement.closest(selectors.col));
|
||||
break;
|
||||
}
|
||||
|
||||
// Immediately prevent default behavior and stop propagation.
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
|
||||
if (next) {
|
||||
next.querySelector(selectors.navigableCell)?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles changes to override toggles.
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLInputElement} input The override toggle input element.
|
||||
*/
|
||||
function updateOverrideToggle(input) {
|
||||
const checked = input.checked;
|
||||
const [, itemid, userid] = input.getAttribute('name').split('_');
|
||||
const interest = `_${itemid}_${userid}`;
|
||||
|
||||
// Handle text inputs.
|
||||
document.querySelectorAll(selectors.input(interest)).forEach(
|
||||
text => {
|
||||
text.disabled = !checked;
|
||||
}
|
||||
);
|
||||
|
||||
// Handle select elements.
|
||||
document.querySelectorAll(selectors.select(interest)).forEach(
|
||||
select => {
|
||||
select.disabled = !checked;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the next cell in the table.
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} cell The cell of the table.
|
||||
* @returns {HTMLElement|null} The next navigable cell or null if none found.
|
||||
*/
|
||||
function getNextCell(cell) {
|
||||
const checkElement = cell || document.activeElement;
|
||||
const next = checkElement.nextElementSibling?.matches(selectors.cell) ? checkElement.nextElementSibling : null;
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
// Continue until we find a navigable cell.
|
||||
if (!next.querySelector(selectors.navigableCell)) {
|
||||
return getNextCell(next);
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the previous cell in the table.
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} cell The cell of the table.
|
||||
*/
|
||||
function getPrevCell(cell) {
|
||||
const checkElement = cell || document.activeElement;
|
||||
const prev = checkElement.previousElementSibling?.matches(selectors.cell) ? checkElement.previousElementSibling : null;
|
||||
if (!prev) {
|
||||
return null;
|
||||
}
|
||||
// Continue until we find a navigable cell.
|
||||
if (!prev.querySelector(selectors.navigableCell)) {
|
||||
return getPrevCell(prev);
|
||||
}
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the cell above the current cell in the table.
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} cell The current table cell element.
|
||||
* @returns {HTMLElement|null} The cell above or null if none found.
|
||||
*/
|
||||
function getAboveCell(cell) {
|
||||
const checkElement = cell || document.activeElement;
|
||||
const tr = checkElement.closest(selectors.row).previousElementSibling;
|
||||
const columnIndex = getColumnIndex(checkElement);
|
||||
if (!tr) {
|
||||
return null;
|
||||
}
|
||||
const next = tr.querySelectorAll(selectors.col)[columnIndex];
|
||||
// Continue until we find a navigable cell.
|
||||
if (!next?.querySelector(selectors.navigableCell)) {
|
||||
return getAboveCell(next);
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the cell below the current cell in the table.
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} cell The current table cell element.
|
||||
* @returns {HTMLElement|null} The cell below or null if none found.
|
||||
*/
|
||||
function getBelowCell(cell) {
|
||||
const checkElement = cell || document.activeElement;
|
||||
const tr = checkElement.closest('tr').nextElementSibling;
|
||||
const columnIndex = getColumnIndex(checkElement);
|
||||
if (!tr) {
|
||||
return null;
|
||||
}
|
||||
const next = tr.querySelectorAll('td, th')[columnIndex];
|
||||
// Continue until we find a navigable cell.
|
||||
if (!next?.querySelector(selectors.navigableCell)) {
|
||||
return getBelowCell(next);
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the column index of a cell.
|
||||
*
|
||||
* @param {HTMLElement} cell The cell of the table.
|
||||
* @returns {number} The index of the cell within its row.
|
||||
*/
|
||||
function getColumnIndex(cell) {
|
||||
const rowNode = cell.closest(selectors.row);
|
||||
if (!rowNode || !cell) {
|
||||
return -1;
|
||||
}
|
||||
const cells = Array.from(rowNode.querySelectorAll(selectors.col));
|
||||
|
||||
return cells.indexOf(cell);
|
||||
}
|
||||
@@ -259,15 +259,9 @@ abstract class screen {
|
||||
public function js() {
|
||||
global $PAGE;
|
||||
|
||||
$module = [
|
||||
'name' => 'gradereport_singleview',
|
||||
'fullpath' => '/grade/report/singleview/js/singleview.js',
|
||||
'requires' => ['base', 'dom', 'event', 'event-simulate', 'io-base']
|
||||
];
|
||||
|
||||
$PAGE->requires->strings_for_js(['overridenoneconfirm', 'removeoverride', 'removeoverridesave'],
|
||||
'gradereport_singleview');
|
||||
$PAGE->requires->js_init_call('M.gradereport_singleview.init', [], false, $module);
|
||||
$PAGE->requires->js_call_amd('gradereport_singleview/singleview', 'init');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
M.gradereport_singleview = {};
|
||||
|
||||
M.gradereport_singleview.init = function(Y) {
|
||||
if (this.initialised) {
|
||||
return;
|
||||
}
|
||||
this.initialised = true;
|
||||
|
||||
var getColumnIndex = function(cell) {
|
||||
var rowNode = cell.ancestor('tr');
|
||||
if (!rowNode || !cell) {
|
||||
return;
|
||||
}
|
||||
var cells = rowNode.all('td, th');
|
||||
return cells.indexOf(cell);
|
||||
},
|
||||
getNextCell = function(cell) {
|
||||
var n = cell || document.activeElement,
|
||||
next = n.next('td.cell, th.cell');
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
// Continue on until we find a navigable cell
|
||||
if (!next || !Y.one(next).one('input:not([type="hidden"]):not([disabled="DISABLED"]), select, a')) {
|
||||
return getNextCell(next);
|
||||
}
|
||||
return next;
|
||||
},
|
||||
getPrevCell = function(cell) {
|
||||
var n = cell || document.activeElement,
|
||||
next = n.previous('td.cell, th.cell');
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
// Continue on until we find a navigable cell
|
||||
if (!Y.one(next).one('input:not([type="hidden"]):not([disabled="DISABLED"]), select, a')) {
|
||||
return getPrevCell(next);
|
||||
}
|
||||
return next;
|
||||
},
|
||||
getAboveCell = function(cell) {
|
||||
var n = cell || document.activeElement,
|
||||
tr = n.ancestor('tr').previous('tr'),
|
||||
columnIndex = getColumnIndex(n),
|
||||
next = null;
|
||||
if (tr) {
|
||||
next = tr.all('td, th').item(columnIndex);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
// Continue on until we find a navigable cell
|
||||
if (!Y.one(next).one('input:not([type="hidden"]):not([disabled="DISABLED"]), select, a')) {
|
||||
return getAboveCell(next);
|
||||
}
|
||||
return next;
|
||||
},
|
||||
getBelowCell = function(cell) {
|
||||
var n = cell || document.activeElement,
|
||||
tr = n.ancestor('tr').next('tr'),
|
||||
columnIndex = getColumnIndex(n),
|
||||
next = null;
|
||||
if (tr) {
|
||||
next = tr.all('td, th').item(columnIndex);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
// Continue on until we find a navigable cell
|
||||
if (!Y.one(next).one('input:not([type="hidden"]):not([disabled="DISABLED"]), select, a')) {
|
||||
return getBelowCell(next);
|
||||
}
|
||||
return next;
|
||||
};
|
||||
|
||||
// Add ctrl+arrow controls for navigation
|
||||
Y.one(Y.config.doc.body).delegate('key', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
var next = null;
|
||||
switch (e.keyCode) {
|
||||
case 37:
|
||||
next = getPrevCell(this.ancestor('td, th'));
|
||||
break;
|
||||
case 38:
|
||||
next = getAboveCell(this.ancestor('td, th'));
|
||||
break;
|
||||
case 39:
|
||||
next = getNextCell(this.ancestor('td, th'));
|
||||
break;
|
||||
case 40:
|
||||
next = getBelowCell(this.ancestor('td, th'));
|
||||
break;
|
||||
}
|
||||
if (next) {
|
||||
Y.one(next).one('input:not([type="hidden"]):not([disabled="DISABLED"]), select, a').focus();
|
||||
}
|
||||
return;
|
||||
}, 'down:37,38,39,40+ctrl', 'table input, table select, table a');
|
||||
|
||||
// Override Toggle.
|
||||
Y.all('input[name^=override_]').each(function(input) {
|
||||
input.on('change', function() {
|
||||
var checked = input.getDOMNode().checked;
|
||||
var names = input.getAttribute('name').split("_");
|
||||
|
||||
var itemid = names[1];
|
||||
var userid = names[2];
|
||||
|
||||
var interest = '_' + itemid + '_' + userid;
|
||||
|
||||
Y.all('input[name$=' + interest + ']').filter('input[data-uielement=text]').each(function(text) {
|
||||
text.getDOMNode().disabled = !checked;
|
||||
});
|
||||
// deal with scales that are not text... UCSB
|
||||
Y.all('select[name$=' + interest + ']').each(function(select) {
|
||||
select.getDOMNode().disabled = !checked;
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user