Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
{"version":3,"file":"pagehelpers.min.js","sources":["../src/pagehelpers.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Page utility helpers.\n *\n * @module core/pagehelpers\n * @copyright  2023 Ferran Recio <ferran@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n/**\n * Maximum sizes for breakpoints. This needs to correspond with Bootstrap\n * Breakpoints\n *\n * @private\n */\nconst Sizes = {\n    small: 576,\n    medium: 991,\n    large: 1400\n};\n\nconst Selectors = {\n    focusable: 'a, button, input, select, textarea, [tabindex]:not([tabindex=\"-1\"])',\n};\n\nconst Classes = {\n    behatSite: 'behat-site',\n};\n\n/**\n * Check fi the current page is a Behat site.\n * @returns {boolean} true if the current page is a Behat site.\n */\nexport const isBehatSite = () => {\n    return document.body.classList.contains(Classes.behatSite);\n};\n\n/**\n * Get the current body width.\n * @returns {number} the current body width.\n */\nexport const getCurrentWidth = () => {\n    const DomRect = document.body.getBoundingClientRect();\n    return DomRect.x + DomRect.width;\n};\n\n/**\n * Check if the user uses an extra small size browser.\n *\n * @returns {boolean} true if the body is smaller than sizes.small max size.\n */\nexport const isExtraSmall = () => {\n    const browserWidth = getCurrentWidth();\n    return browserWidth < Sizes.small;\n};\n\n/**\n * Check if the user uses a small size browser.\n *\n * @returns {boolean} true if the body is smaller than sizes.medium max size.\n */\nexport const isSmall = () => {\n    const browserWidth = getCurrentWidth();\n    return browserWidth < Sizes.medium;\n};\n\n/**\n * Check if the user uses a large size browser.\n *\n * @returns {boolean} true if the body is smaller than sizes.large max size.\n */\nexport const isLarge = () => {\n    const browserWidth = getCurrentWidth();\n    return browserWidth >= Sizes.large;\n};\n\n/**\n * Get the first focusable element inside a container.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @returns {HTMLElement|null}\n */\nexport const firstFocusableElement = (container) => {\n    const containerElement = container || document;\n    return containerElement.querySelector(Selectors.focusable);\n};\n\n/**\n * Get the last focusable element inside a container.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @returns {HTMLElement|null}\n */\nexport const lastFocusableElement = (container) => {\n    const containerElement = container || document;\n    const focusableElements = containerElement.querySelectorAll(Selectors.focusable);\n    return focusableElements[focusableElements.length - 1] ?? null;\n};\n\n/**\n * Get all focusable elements inside a container.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @returns {HTMLElement[]}\n */\nexport const focusableElements = (container) => {\n    const containerElement = container || document;\n    return containerElement.querySelectorAll(Selectors.focusable);\n};\n\n/**\n * Get the previous focusable element in a container.\n * It uses the current focused element to know where to start the search.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @param {Boolean} [loopSelection] Whether to loop selection or not. Default to false.\n * @returns {HTMLElement|null}\n */\nexport const previousFocusableElement = (container, loopSelection) => {\n    return getRelativeFocusableElement(container, loopSelection, -1);\n};\n\n/**\n * Get the next focusable element in a container.\n * It uses the current focused element to know where to start the search.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @param {Boolean} [loopSelection] Whether to loop selection or not. Default to false.\n * @returns {HTMLElement|null}\n */\nexport const nextFocusableElement = (container, loopSelection) => {\n    return getRelativeFocusableElement(container, loopSelection, 1);\n};\n\n/**\n * Internal function to get the next or previous focusable element.\n * @param {HTMLElement} [container] Container to search in. Defaults to document.\n * @param {Boolean} [loopSelection] Whether to loop selection or not.\n * @param {Number} [direction] Direction to search in. 1 for next, -1 for previous.\n * @returns {HTMLElement|null}\n * @private\n */\nconst getRelativeFocusableElement = (container, loopSelection, direction) => {\n    const focusedElement = document.activeElement;\n    const focusables = [...focusableElements(container)];\n    const focusedIndex = focusables.indexOf(focusedElement);\n\n    if (focusedIndex === -1) {\n        return null;\n    }\n\n    const newIndex = focusedIndex + direction;\n\n    if (focusables[newIndex] !== undefined) {\n        return focusables[newIndex];\n    }\n    if (loopSelection != true) {\n        return null;\n    }\n    if (direction > 0) {\n        return focusables[0] ?? null;\n    }\n    return focusables[focusables.length - 1] ?? null;\n};\n"],"names":["Sizes","Selectors","Classes","document","body","classList","contains","getCurrentWidth","DomRect","getBoundingClientRect","x","width","container","querySelector","focusableElements","querySelectorAll","length","loopSelection","getRelativeFocusableElement","direction","focusedElement","activeElement","focusables","focusedIndex","indexOf","newIndex","undefined"],"mappings":";;;;;;;;MA6BMA,YACK,IADLA,aAEM,IAFNA,YAGK,KAGLC,oBACS,sEAGTC,kBACS,kCAOY,IAChBC,SAASC,KAAKC,UAAUC,SAASJ,yBAO/BK,gBAAkB,WACrBC,QAAUL,SAASC,KAAKK,+BACvBD,QAAQE,EAAIF,QAAQG,sEAQH,IACHJ,kBACCP,6BAQH,IACEO,kBACCP,8BAQH,IACEO,mBACEP,2CAQWY,YACTA,WAAaT,UACdU,cAAcZ,mDAQLW,yCAE3BE,mBADmBF,WAAaT,UACKY,iBAAiBd,uDACrDa,kBAAkBA,kBAAkBE,OAAS,oDAAM,YAQjDF,kBAAqBF,YACLA,WAAaT,UACdY,iBAAiBd,oGAUL,CAACW,UAAWK,gBACzCC,4BAA4BN,UAAWK,eAAgB,iCAU9B,CAACL,UAAWK,gBACrCC,4BAA4BN,UAAWK,cAAe,SAW3DC,4BAA8B,CAACN,UAAWK,cAAeE,mCACrDC,eAAiBjB,SAASkB,cAC1BC,WAAa,IAAIR,kBAAkBF,YACnCW,aAAeD,WAAWE,QAAQJ,oBAElB,IAAlBG,oBACO,WAGLE,SAAWF,aAAeJ,sBAEHO,IAAzBJ,WAAWG,UACJH,WAAWG,UAED,GAAjBR,cACO,KAEPE,UAAY,uBACLG,WAAW,wCAAM,yBAErBA,WAAWA,WAAWN,OAAS,sCAAM"}