Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"helpers.min.js","sources":["../src/helpers.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 * Tiny media plugin helpers for image and embed.\n *\n * @module      tiny_media/helpers\n * @copyright   2024 Stevani Andolo <stevani@hotmail.com.au>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Templates from 'core/templates';\nimport Selectors from './selectors';\nimport Config from 'core/config';\n\n/**\n * Renders and inserts the body template for inserting an media into the modal.\n *\n * @param {object} templateContext - The context for rendering the template.\n * @param {HTMLElement} root - The root element where the template will be inserted.\n * @returns {Promise<void>}\n */\nexport const body = async(templateContext, root) => {\n    return Templates.renderForPromise(templateContext.bodyTemplate, {...templateContext})\n    .then(({html, js}) => {\n        Templates.replaceNodeContents(root.querySelector(Selectors[templateContext.selector].elements.bodyTemplate), html, js);\n        return;\n    })\n    .catch(error => {\n        window.console.log(error);\n    });\n};\n\n/**\n * Renders and inserts the footer template for inserting an media into the modal.\n *\n * @param {object} templateContext - The context for rendering the template.\n * @param {HTMLElement} root - The root element where the template will be inserted.\n * @returns {Promise<void>}\n */\nexport const footer = async(templateContext, root) => {\n    return Templates.renderForPromise(templateContext.footerTemplate, {...templateContext})\n    .then(({html, js}) => {\n        Templates.replaceNodeContents(root.querySelector(Selectors[templateContext.selector].elements.footerTemplate), html, js);\n        return;\n    })\n    .catch(error => {\n        window.console.log(error);\n    });\n};\n\n/**\n * Set extra properties on an instance using incoming data.\n *\n * @param {object} instance\n * @param {object} data\n * @return {object} Modified instance\n */\nexport const setPropertiesFromData = async(instance, data) => {\n    for (const property in data) {\n        if (typeof data[property] !== 'function') {\n            instance[property] = data[property];\n        }\n    }\n    return instance;\n};\n\n/**\n * Check if given string is a valid URL.\n *\n * @param {String} urlString URL the link will point to.\n * @returns {boolean} True is valid, otherwise false.\n */\nexport const isValidUrl = urlString => {\n    const urlPattern = new RegExp('^(https?:\\\\/\\\\/)?' + // Protocol.\n                                '((([a-z\\\\d]([a-z\\\\d-]*[a-z\\\\d])*)\\\\.)+[a-z]{2,}|' + // Domain name.\n                                '((\\\\d{1,3}\\\\.){3}\\\\d{1,3})|localhost)' + // OR ip (v4) address, localhost.\n                                '(\\\\:\\\\d+)?(\\\\/[-a-z\\\\d%_.~+]*)*'); // Port and path.\n    return !!urlPattern.test(urlString);\n};\n\n/**\n * Hide the element(s).\n *\n * @param {string|string[]} elements - The CSS selector for the elements to toggle.\n * @param {object} root - The CSS selector for the elements to toggle.\n */\nexport const hideElements = (elements, root) => {\n    if (elements instanceof Array) {\n        elements.forEach((elementSelector) => {\n            const element = root.querySelector(elementSelector);\n            if (element) {\n                element.classList.add('d-none');\n            }\n        });\n    } else {\n        const element = root.querySelector(elements);\n        if (element) {\n            element.classList.add('d-none');\n        }\n    }\n};\n\n/**\n * Show the element(s).\n *\n * @param {string|string[]} elements - The CSS selector for the elements to toggle.\n * @param {object} root - The CSS selector for the elements to toggle.\n */\nexport const showElements = (elements, root) => {\n    if (elements instanceof Array) {\n        elements.forEach((elementSelector) => {\n            const element = root.querySelector(elementSelector);\n            if (element) {\n                element.classList.remove('d-none');\n            }\n        });\n    } else {\n        const element = root.querySelector(elements);\n        if (element) {\n            element.classList.remove('d-none');\n        }\n    }\n};\n\n/**\n * Displays the upload loader and disables UI elements while loading a file.\n *\n * @param {html} root Modal element\n * @param {string} selector String of type IMAGE/EMBED\n */\nexport const startMediaLoading = (root, selector) => {\n    showElements(Selectors[selector].elements.loaderIcon, root);\n    const elementsToHide = [\n        Selectors[selector].elements.insertMedia,\n        Selectors[selector].elements.urlWarning,\n        Selectors[selector].elements.modalFooter,\n    ];\n    hideElements(elementsToHide, root);\n};\n\n/**\n * Hide the upload loader and enable UI elements when loaded.\n *\n * @param {html} root Modal element\n * @param {string} selector String of type IMAGE/EMBED\n */\nexport const stopMediaLoading = (root, selector) => {\n    hideElements(Selectors[selector].elements.loaderIcon, root);\n    const elementsToShow = [\n        Selectors[selector].elements.insertMedia,\n        Selectors[selector].elements.modalFooter,\n    ];\n    showElements(elementsToShow, root);\n};\n\n/**\n * Return true or false if the url is external.\n *\n * @param {string} url\n * @returns {boolean} True if the URL is external, otherwise false.\n */\nexport const isExternalUrl = (url) => {\n    const regex = new RegExp(`${Config.wwwroot}`);\n\n    // True if the URL is from external, otherwise false.\n    return regex.test(url) === false;\n};\n\n/**\n * Set the string for the URL label element.\n *\n * @param {object} props - The label text to set.\n */\nexport const setFilenameLabel = (props) => {\n    const urlLabelEle = props.root.querySelector(props.fileNameSelector);\n    if (urlLabelEle) {\n        urlLabelEle.innerHTML = props.label;\n        urlLabelEle.setAttribute(\"title\", props.label);\n    }\n};\n\n/**\n * This function checks whether an image URL is local (within the same website's domain) or external (from an external source).\n * Depending on the result, it dynamically updates the visibility and content of HTML elements in a user interface.\n * If the image is local then we only show it's filename.\n * If the image is external then it will show full URL and it can be updated.\n *\n * @param {object} props\n */\nexport const sourceTypeChecked = (props) => {\n    if (props.fetchedTitle) {\n        props.label = props.fetchedTitle;\n    } else {\n        if (!isExternalUrl(props.source)) {\n            // Split the URL by '/' to get an array of segments.\n            const segments = props.source.split('/');\n            // Get the last segment, which should be the filename.\n            const filename = segments.pop().split('?')[0];\n            // Show the file name.\n            props.label = decodeURI(filename);\n        } else {\n            props.label = decodeURI(props.source);\n        }\n    }\n    setFilenameLabel(props);\n};\n\n/**\n * Get filename from the name label.\n *\n * @param {string} fileLabel\n * @returns {string}\n */\nexport const getFileName = (fileLabel) => {\n    if (fileLabel.includes('/')) {\n        const split = fileLabel.split('/');\n        let fileName = split[split.length - 1];\n        fileName = fileName.split('.');\n        if (fileName.length > 1) {\n            return decodeURI(fileName.slice(0, (fileName.length - 1)).join('.'));\n        } else {\n            return decodeURI(fileName[0]);\n        }\n    } else {\n        return decodeURI(fileLabel.split('.')[0]);\n    }\n};\n\n/**\n * Return true or false if % is found.\n *\n * @param {string} value\n * @returns {boolean}\n */\nexport const isPercentageValue = (value) => {\n    return value.match(/\\d+%/);\n};\n"],"names":["async","templateContext","root","Templates","renderForPromise","bodyTemplate","then","_ref","html","js","replaceNodeContents","querySelector","Selectors","selector","elements","catch","error","window","console","log","footerTemplate","_ref2","instance","data","property","urlString","RegExp","test","hideElements","Array","forEach","elementSelector","element","classList","add","showElements","remove","loaderIcon","elementsToHide","insertMedia","urlWarning","modalFooter","elementsToShow","isExternalUrl","url","Config","wwwroot","setFilenameLabel","props","urlLabelEle","fileNameSelector","innerHTML","label","setAttribute","fetchedTitle","source","decodeURI","filename","split","pop","fileLabel","includes","fileName","length","slice","join","value","match"],"mappings":";;;;;;;ogBAkCoBA,MAAMC,gBAAiBC,OAChCC,mBAAUC,iBAAiBH,gBAAgBI,aAAc,IAAIJ,kBACnEK,MAAKC,WAACC,KAACA,KAADC,GAAOA,4BACAC,oBAAoBR,KAAKS,cAAcC,mBAAUX,gBAAgBY,UAAUC,SAAST,cAAeG,KAAMC,OAGtHM,OAAMC,QACHC,OAAOC,QAAQC,IAAIH,0BAWLhB,MAAMC,gBAAiBC,OAClCC,mBAAUC,iBAAiBH,gBAAgBmB,eAAgB,IAAInB,kBACrEK,MAAKe,YAACb,KAACA,KAADC,GAAOA,6BACAC,oBAAoBR,KAAKS,cAAcC,mBAAUX,gBAAgBY,UAAUC,SAASM,gBAAiBZ,KAAMC,OAGxHM,OAAMC,QACHC,OAAOC,QAAQC,IAAIH,yCAWUhB,MAAMsB,SAAUC,YAC5C,MAAMC,YAAYD,KACW,mBAAnBA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