From 42d3e7de14837b70c35ab0cae2982785e4ee5724 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 8 Feb 2023 10:31:51 +0800 Subject: [PATCH] MDL-77174 core: Add getElement to core/normalise --- lib/amd/build/normalise.min.js | 2 +- lib/amd/build/normalise.min.js.map | 2 +- lib/amd/src/normalise.js | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/amd/build/normalise.min.js b/lib/amd/build/normalise.min.js index 7e9da8c647f..dd2d8ecf922 100644 --- a/lib/amd/build/normalise.min.js +++ b/lib/amd/build/normalise.min.js @@ -5,6 +5,6 @@ define("core/normalise",["exports","jquery"],(function(_exports,_jquery){var obj * @module core/normalise * @copyright 2020 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.getList=_exports.getFirst=void 0,_jquery=(obj=_jquery)&&obj.__esModule?obj:{default:obj};const getList=nodes=>nodes instanceof HTMLElement?[nodes]:nodes instanceof Array?nodes:nodes instanceof NodeList?Array.from(nodes):nodes instanceof _jquery.default?nodes.get():Array.from(nodes);_exports.getList=getList;_exports.getFirst=nodes=>getList(nodes)[0]})); + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.getList=_exports.getFirst=_exports.getElement=void 0,_jquery=(obj=_jquery)&&obj.__esModule?obj:{default:obj};const getList=nodes=>nodes instanceof HTMLElement?[nodes]:nodes instanceof Array?nodes:nodes instanceof NodeList?Array.from(nodes):nodes instanceof _jquery.default?nodes.get():Array.from(nodes);_exports.getList=getList;_exports.getFirst=nodes=>getList(nodes)[0];_exports.getElement=node=>node instanceof HTMLElement?node:null!=node&&node._node?node._node:node instanceof _jquery.default&&node.length>0?node.get(0):null})); //# sourceMappingURL=normalise.min.js.map \ No newline at end of file diff --git a/lib/amd/build/normalise.min.js.map b/lib/amd/build/normalise.min.js.map index 1009d925686..394905d2839 100644 --- a/lib/amd/build/normalise.min.js.map +++ b/lib/amd/build/normalise.min.js.map @@ -1 +1 @@ -{"version":3,"file":"normalise.min.js","sources":["../src/normalise.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 .\n\n/**\n * Normalisation helpers.\n *\n * @module core/normalise\n * @copyright 2020 Andrew Nicols \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport jQuery from 'jquery';\n\n/**\n * Normalise a list of Nodes into an Array of Nodes.\n *\n * @method getList\n * @param {(Array|jQuery|NodeList|HTMLElement)} nodes\n * @returns {HTMLElement[]}\n */\nexport const getList = nodes => {\n if (nodes instanceof HTMLElement) {\n // A single record to conver to a NodeList.\n return [nodes];\n }\n\n if (nodes instanceof Array) {\n // A single record to conver to a NodeList.\n return nodes;\n }\n\n if (nodes instanceof NodeList) {\n // Already a NodeList.\n return Array.from(nodes);\n }\n\n if (nodes instanceof jQuery) {\n // A jQuery object to a NodeList.\n return nodes.get();\n }\n\n // Fallback to just having a go.\n return Array.from(nodes);\n};\n\n/**\n * Return the first element in a list of normalised Nodes.\n *\n * @param {Array|jQuery|NodeList|HTMLElement} nodes the unmormalised list of nodes\n * @returns {HTMLElement|undefined} the first list element\n */\nexport const getFirst = nodes => {\n const list = getList(nodes);\n return list[0];\n};\n"],"names":["getList","nodes","HTMLElement","Array","NodeList","from","jQuery","get"],"mappings":";;;;;;;qKAgCaA,QAAUC,OACfA,iBAAiBC,YAEV,CAACD,OAGRA,iBAAiBE,MAEVF,MAGPA,iBAAiBG,SAEVD,MAAME,KAAKJ,OAGlBA,iBAAiBK,gBAEVL,MAAMM,MAIVJ,MAAME,KAAKJ,kDASEA,OACPD,QAAQC,OACT"} \ No newline at end of file +{"version":3,"file":"normalise.min.js","sources":["../src/normalise.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 .\n\n/**\n * Normalisation helpers.\n *\n * @module core/normalise\n * @copyright 2020 Andrew Nicols \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport jQuery from 'jquery';\n\n/**\n * Normalise a list of Nodes into an Array of Nodes.\n *\n * @method getList\n * @param {(Array|jQuery|NodeList|HTMLElement)} nodes\n * @returns {HTMLElement[]}\n */\nexport const getList = nodes => {\n if (nodes instanceof HTMLElement) {\n // A single record to conver to a NodeList.\n return [nodes];\n }\n\n if (nodes instanceof Array) {\n // A single record to conver to a NodeList.\n return nodes;\n }\n\n if (nodes instanceof NodeList) {\n // Already a NodeList.\n return Array.from(nodes);\n }\n\n if (nodes instanceof jQuery) {\n // A jQuery object to a NodeList.\n return nodes.get();\n }\n\n // Fallback to just having a go.\n return Array.from(nodes);\n};\n\n/**\n * Return the first element in a list of normalised Nodes.\n *\n * @param {Array|jQuery|NodeList|HTMLElement} nodes the unmormalised list of nodes\n * @returns {HTMLElement|undefined} the first list element\n */\nexport const getFirst = nodes => {\n const list = getList(nodes);\n return list[0];\n};\n\n/**\n * Normalise a single node into an HTMLElement.\n *\n * @param {jQuery|Y.Ndoe|HTMLELement} node The node to normalise\n * @returns {HTMLElement}\n */\nexport const getElement = (node) => {\n if (node instanceof HTMLElement) {\n return node;\n }\n\n if (node?._node) {\n // This is likely a YUI Node.\n // We can use (node instanceof Y.Node) but we would have to load YUI to do some.\n return node._node;\n }\n\n if (node instanceof jQuery && node.length > 0) {\n return node.get(0);\n }\n\n return null;\n};\n"],"names":["getList","nodes","HTMLElement","Array","NodeList","from","jQuery","get","node","_node","length"],"mappings":";;;;;;;yLAgCaA,QAAUC,OACfA,iBAAiBC,YAEV,CAACD,OAGRA,iBAAiBE,MAEVF,MAGPA,iBAAiBG,SAEVD,MAAME,KAAKJ,OAGlBA,iBAAiBK,gBAEVL,MAAMM,MAIVJ,MAAME,KAAKJ,kDASEA,OACPD,QAAQC,OACT,uBASWO,MACnBA,gBAAgBN,YACTM,KAGPA,MAAAA,MAAAA,KAAMC,MAGCD,KAAKC,MAGZD,gBAAgBF,iBAAUE,KAAKE,OAAS,EACjCF,KAAKD,IAAI,GAGb"} \ No newline at end of file diff --git a/lib/amd/src/normalise.js b/lib/amd/src/normalise.js index 98e8c285946..befe9c18612 100644 --- a/lib/amd/src/normalise.js +++ b/lib/amd/src/normalise.js @@ -65,3 +65,27 @@ export const getFirst = nodes => { const list = getList(nodes); return list[0]; }; + +/** + * Normalise a single node into an HTMLElement. + * + * @param {jQuery|Y.Ndoe|HTMLELement} node The node to normalise + * @returns {HTMLElement} + */ +export const getElement = (node) => { + if (node instanceof HTMLElement) { + return node; + } + + if (node?._node) { + // This is likely a YUI Node. + // We can use (node instanceof Y.Node) but we would have to load YUI to do some. + return node._node; + } + + if (node instanceof jQuery && node.length > 0) { + return node.get(0); + } + + return null; +};